Online C Programming Test - C Programming Test - Random



Loading...  Loading Test...

Instruction:

  • Total number of questions : 20.
  • Time alloted : 30 minutes.
  • Each question carry 1 mark, no negative marks.
  • DO NOT refresh the page.
  • All the best :-).

1.

What will be the output of the program ?

#include<stdio.h>
power(int**);
int main()
{
    int a=5, *aa; /* Address of 'a' is 1000 */
    aa = &a;
    a = power(&aa);
    printf("%d\n", a);
    return 0;
}
power(int **ptr)
{
    int b;
    b = **ptr***ptr;
    return (b);
}

A.
5
B.
25
C.
125
D.
Garbage value

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum

2.

Will the following program give any warning on compilation in TurboC (under DOS)?

#include<stdio.h>

int main()
{
    int *p1, i=25;
    void *p2;
    p1=&i;
    p2=&i;
    p1=p2;
    p2=p1;
    return 0;
}

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum

3.

Will the program compile in Turbo C?

#include<stdio.h>
int main()
{
    int a=10, *j;
    void *k;
    j=k=&a;
    j++;
    k++;
    printf("%u %u\n", j, k);
    return 0;
}

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Error in statement k++. We cannot perform arithmetic on void pointers.

The following error will be displayed while compiling above program in TurboC.

Compiling PROGRAM.C:
Error PROGRAM.C 8: Size of the type is unknown or zero.

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum

4.

Which of the following special symbol allowed in a variable name?

A.
* (asterisk)
B.
| (pipeline)
C.
- (hyphen)
D.
_ (underscore)

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Variable names in C are made up of letters (upper and lower case) and digits. The underscore character ("_") is also permitted. Names must not begin with a digit.

Examples of valid (but not very descriptive) C variable names:
=> foo
=> Bar
=> BAZ
=> foo_bar
=> _foo42
=> _
=> QuUx

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum

5.

What will be the output of the program?

#include<stdio.h>
#define PRINT(i) printf("%d,",i)

int main()
{
    int x=2, y=3, z=4;
    PRINT(x);
    PRINT(y);
    PRINT(z);
    return 0;
}

A.
2, 3, 4,
B.
2, 2, 2,
C.
3, 3, 3,
D.
4, 4, 4,

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The macro PRINT(i) print("%d,", i); prints the given variable value in an integer format.

Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type and initialized to 2, 3, 4 respectively.

Step 2: PRINT(x); becomes printf("%d,",x). Hence it prints '2'.

Step 3: PRINT(y); becomes printf("%d,",y). Hence it prints '3'.

Step 4: PRINT(z); becomes printf("%d,",z). Hence it prints '4'.

Hence the output of the program is 2, 3, 4.

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum

6.

Can we specify a variable filed width in a scanf() format string?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

In scanf() a * in a format string after a % sign is used for the suppression of assignment. That is, the current input field is scanned but not stored.

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum

7.

What will be the output of the program?

#include<stdio.h>

int addmult(int ii, int jj)
{
    int kk, ll;
    kk = ii + jj;
    ll = ii * jj;
    return (kk, ll);
}

int main()
{
    int i=3, j=4, k, l;
    k = addmult(i, j);
    l = addmult(i, j);
    printf("%d %d\n", k, l);
    return 0;
}

A.
12 12
B.
No error, No output
C.
Error: Compile error
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum

8.

Which of the following statements are correct about the below program?

#include<stdio.h>
int main()
{
    int n = 0, y = 1;
    y == 1 ? n=0 : n=1;
    if(n)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}

A.
Error: Declaration terminated incorrectly
B.
Error: Syntax error
C.
Error: Lvalue required
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum

9.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    printf("%%%%\n");
    return 0;
}

A.
%%%%%
B.
%%
C.
No output
D.
Error

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum

10.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    int i=32, j=0x20, k, l, m;
    k=i|j;
    l=i&j;
    m=k^l;
    printf("%d, %d, %d, %d, %d\n", i, j, k, l, m);
    return 0;
}

A.
0, 0, 0, 0, 0
B.
0, 32, 32, 32, 32
C.
32, 32, 32, 32, 0
D.
32, 32, 32, 32, 32

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Bitwise Operators

Discuss about this problem : Discuss in Forum

11.

We want to round off x, a float, to an int value, The correct way to do is

A.
y = (int)(x + 0.5)
B.
y = int(x + 0.5)
C.
y = (int)x + 0.5
D.
y = (int)((int)x + 0.5)

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Rounding off a value means replacing it by a nearest value that is approximately equal or smaller or greater to the given number.

y = (int)(x + 0.5); here x is any float value. To roundoff, we have to typecast the value of x by using (int)

Example:


#include <stdio.h>

int main ()
{
  float x = 3.6;
  int y = (int)(x + 0.5);
  printf ("Result = %d\n", y );
  return 0;
}

Output:
Result = 4.

Learn more problems on : Floating Point Issues

Discuss about this problem : Discuss in Forum

12.

Does there any function exist to convert the int or float to a string?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

1. itoa() converts an integer to a string.
2. ltoa() converts a long to a string.
3. ultoa() converts an unsigned long to a string.
4. sprintf() sends formatted output to a string, so it can be used to convert any type of values to string type.

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
   int   num1 = 12345;
   float num2 = 5.12;
   char str1[20];
   char str2[20];

   itoa(num1, str1, 10); /* 10 radix value */
   printf("integer = %d string = %s \n", num1, str1);

   sprintf(str2, "%f", num2);
   printf("float = %f string = %s", num2, str2);

   return 0;
}

// Output:
// integer = 12345 string = 12345
// float = 5.120000 string = 5.120000

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum

13.

What will the function rewind() do?

A.
Reposition the file pointer to a character reverse.
B.
Reposition the file pointer stream to end of file.
C.
Reposition the file pointer to begining of that line.
D.
Reposition the file pointer to begining of file.

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

rewind() takes the file pointer to the beginning of the file. so that the next I/O operation will take place at the beginning of the file.
Example: rewind(FilePointer);

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum

14.

Which of the following statements correct about k used in the below statement?
char ****k;

A.
k is a pointer to a pointer to a pointer to a char
B.
k is a pointer to a pointer to a pointer to a pointer to a char
C.
k is a pointer to a char pointer
D.
k is a pointer to a pointer to a char

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum

15.

Which statement will you add to the following program to ensure that the program outputs "IndiaBIX" on execution?

#include<stdio.h>

int main()
{
    char s[] = "IndiaBIX";
    char t[25];
    char *ps, *pt;
    ps = s;
    pt = t;
    while(*ps)
        *pt++ = *ps++;

    /* Add a statement here */
    printf("%s\n", t);
    return 0;
}

A.
*pt='';
B.
pt='\0';
C.
pt='\n';
D.
*pt='\0';

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum

16.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    struct value
    {
        int bit1:1;
        int bit3:4;
        int bit4:4;
    }bit={1, 2, 13};

    printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);
    return 0;
}

A.
1, 2, 13
B.
1, 4, 4
C.
-1, 2, -3
D.
-1, -2, -13

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Note the below statement inside the struct:

int bit1:1; --> 'int' indicates that it is a SIGNED integer.

For signed integers the leftmost bit will be taken for +/- sign.

If you store 1 in 1-bit field:

The left most bit is 1, so the system will treat the value as negative number.

The 2's complement method is used by the system to handle the negative values.

Therefore, the data stored is 1. The 2's complement of 1 is also 1 (negative).

Therefore -1 is printed.


If you store 2 in 4-bits field:

Binary 2: 0010 (left most bit is 0, so system will treat it as positive value)

0010 is 2

Therefore 2 is printed.


If you store 13 in 4-bits field:

Binary 13: 1101 (left most bit is 1, so system will treat it as negative value)

Find 2's complement of 1101:

1's complement of 1101 : 0010
2's complement of 1101 : 0011 (Add 1 to the result of 1's complement)

0011 is 3 (but negative value)

Therefore -3 is printed.

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum

17.

Point out the error in the following program.

#include<stdio.h>
void display(int (*ff)());

int main()
{
    int show();
    int (*f)();
    f = show;
    display(f);
    return 0;
}
void display(int (*ff)())
{
    (*ff)();
}
int show()
{
    printf("IndiaBIX");
}

A.
Error: invalid parameter in function display()
B.
Error: invalid function call f=show;
C.
No error and prints "IndiaBIX"
D.
No error and prints nothing.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum

18.

We want to test whether a value lies in the range 2 to 4 or 5 to 7. Can we do this using a switch?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

We can do this in following switch statement

switch(a)
{
    case 2:
    case 3:
    case 4:
       /* some statements */
       break;
    case 5:
    case 6:
    case 7:
       /* some statements */
       break;
}

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum

19.

What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile?

#include<stdio.h>
#define SWAP(a, b, c)(c t; t=a, a=b, b=t)
int main()
{
    int x=10, y=20;
    SWAP(x, y, int);
    printf("%d %d\n", x, y);
    return 0;
}

A.
It compiles
B.
Compiles with an warning
C.
Not compile
D.
Compiles and print nothing

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The code won't compile since declaration of t cannot occur within parenthesis.

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum

20.

Is there any difference int the following declarations?
int fun(int arr[]);
int fun(int arr[2]);

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

No, both the statements are same. It is the prototype for the function fun() that accepts one integer array as an parameter and returns an integer value.

Learn more problems on : Arrays

Discuss about this problem : Discuss in Forum

Submit your test now to view the Results and Statistics with answer explanation.


Have a question?

Ask your question now !