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>

int fun(int i)
{
    i++;
    return i;
}

int main()
{
    int fun(int);
    int i=3;
    fun(i=fun(fun(i)));
    printf("%d\n", i);
    return 0;
}

A.
5
B.
4
C.
Error
D.
Garbage value

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Step 1: int fun(int); This is prototype of function fun(). It tells the compiler that the function fun() accept one integer parameter and returns an integer value.

Step 2: int i=3; The variable i is declared as an integer type and initialized to value 3.

Step 3: fun(i=fun(fun(i)));. The function fun(i) increements the value of i by 1(one) and return it.

Lets go step by step,

=> fun(i) becomes fun(3) is called and it returns 4.

=> i = fun(fun(i)) becomes i = fun(4) is called and it returns 5 and stored in variable i.(i=5)

=> fun(i=fun(fun(i))); becomes fun(5); is called and it return 6 and nowhere the return value is stored.

Step 4: printf("%d\n", i); It prints the value of variable i.(5)

Hence the output is '5'.

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


2.

A pointer to a block of memory is effectively same as an array

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Yes, It is possible to allocate a block of memory (of arbitrary size) at run-time, using the standard library's malloc function, and treat it as an array.

Learn more problems on : Arrays

Discuss about this problem : Discuss in Forum


3.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a u;
    u.ch[0]=3;
    u.ch[1]=2;
    printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The system will allocate 2 bytes for the union.

The statements u.ch[0]=3; u.ch[1]=2; store data in memory as given below.

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


4.

What will be the output of the program?

#include<stdio.h>
int main()
{
    static int a[20];
    int i = 0;
    a[i] = i  ;
    printf("%d, %d, %d\n", a[0], a[1], i);
    return 0;
}

A.
1, 0, 1
B.
1, 1, 1
C.
0, 0, 0
D.
0, 1, 0

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Step 1: static int a[20]; here variable a is declared as an integer type and static. If a variable is declared as static and it will be automatically initialized to value '0'(zero).

Step 2: int i = 0; here vaiable i is declared as an integer type and initialized to '0'(zero).
Step 3: a[i] = i ; becomes a[0] = 0;
Step 4: printf("%d, %d, %d\n", a[0], a[1], i);
Here a[0] = 0, a[1] = 0(because all staic variables are initialized to '0') and i = 0.
Step 4: Hence the output is "0, 0, 0".

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum


5.

Bitwise & can be used to check if more than one bit in a number is on.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Bitwise Operators

Discuss about this problem : Discuss in Forum


6.

What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three

/* myprog.c */
#include<stdio.h>

int main(int argc, char **argv)
{
    printf("%c\n", **++argv);
    return 0;
}

A.
myprog one two three
B.
myprog one
C.
o
D.
two

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum


7.

What will be the output of the program?

#include<stdio.h>
int main()
{
    unsigned int i = 65536; /* Assume 2 byte integer*/
    while(i != 0)
        printf("%d",++i);
    printf("\n");
    return 0;
}

A.
Infinite loop
B.
0 1 2 ... 65535
C.
0 1 2 ... 32767 - 32766 -32765 -1 0
D.
No output

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.

Step 1:unsigned int i = 65536; here variable i becomes '0'(zero). because unsigned int varies from 0 to 65535.

Step 2: while(i != 0) this statement becomes while(0 != 0). Hence the while(FALSE) condition is not satisfied. So, the inside the statements of while loop will not get executed.

Hence there is no output.

Note: Don't forget that the size of int should be 2 bytes. If you run the above program in GCC it may run infinite loop, because in Linux platform the size of the integer is 4 bytes.

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


8.

What will be the output of the program?

#include<stdio.h>
int main()
{
    int a = 300, b, c;
    if(a >= 400)
        b = 300;
    c = 200;
    printf("%d, %d, %d\n", a, b, c);
    return 0;
}

A.
300, 300, 200
B.
Garbage, 300, 200
C.
300, Garbage, 200
D.
300, 300, Garbage

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Step 1: int a = 300, b, c; here variable a is initialized to '300', variable b and c are declared, but not initialized.
Step 2: if(a >= 400) means if(300 >= 400). Hence this condition will be failed.
Step 3: c = 200; here variable c is initialized to '200'.
Step 4: printf("%d, %d, %d\n", a, b, c); It prints "300, garbage value, 200". because variable b is not initialized.

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()
{
    unsigned int i = 65535; /* Assume 2 byte integer*/
    while(i++ != 0)
        printf("%d",++i);
    printf("\n");
    return 0;
}

A.
Infinite loop
B.
0 1 2 ... 65535
C.
0 1 2 ... 32767 - 32766 -32765 -1 0
D.
No output

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.

Step 1:unsigned int i = 65535;

Step 2:
Loop 1: while(i++ != 0) this statement becomes while(65535 != 0). Hence the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '1'(variable 'i' is already incremented by '1' in while statement and now incremented by '1' in printf statement) Loop 2: while(i++ != 0) this statement becomes while(1 != 0). Hence the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '3'(variable 'i' is already incremented by '1' in while statement and now incremented by '1' in printf statement)
....
....

The while loop will never stops executing, because variable i will never become '0'(zero). Hence it is an 'Infinite loop'.

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


10.

Point out the error, if any in the program.

#include<stdio.h>
int main()
{
    int P = 10;
    switch(P)
    {
       case 10:
       printf("Case 1");

       case 20:
       printf("Case 2");
       break;

       case P:
       printf("Case 2");
       break;
    }
    return 0;
}

A.
Error: No default value is specified
B.
Error: Constant expression required at line case P:
C.
Error: There is no break statement in each case.
D.
No error will be reported.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The compiler will report the error "Constant expression required" in the line case P: . Because, variable names cannot be used with case statements.

The case statements will accept only constant expression.

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


11.

What is stderr ?

A.
standard error
B.
standard error types
C.
standard error streams
D.
standard error definitions

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The standard error(stderr) stream is the default destination for error messages and other diagnostic warnings. Like stdout, it is usually also directed to the output device of the standard console (generally, the screen).

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


12.

Union elements can be of different sizes.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


13.

A structure can be nested inside another structure.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


14.

scanf() or atoi() function can be used to convert a string like "436" in to integer.

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

scanf is a function that reads data with specified format from a given string stream source.
scanf("%d",&number);

atoi() convert string to integer.
var number;
number = atoi("string");

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


15.

In the following program add a statement in the function fact() such that the factorial gets stored in j.

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

int main()
{
    int i=5;
    fact(&i);
    printf("%d\n", i);
    return 0;
}
void fact(int *j)
{
    static int s=1;
    if(*j!=0)
    {
        s = s**j;
        *j = *j-1;
        fact(j);
        /* Add a statement here */
    }
}

A.
j=s;
B.
*j=s;
C.
*j=&s;
D.
&j=s;

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


16.

The expression of the right hand side of || operators doesn't get evaluated if the left hand side determines the outcome.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Because, if a is non-zero then b will not be evaluated in the expression (a || b)

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum


17.

What will be the output of the program?

#include<stdio.h>
typedef void v;
typedef int i;

int main()
{
    v fun(i, i);
    fun(2, 3);
    return 0;
}
v fun(i a, i b)
{
    i s=2;
    float i;
    printf("%d,", sizeof(i));
    printf(" %d", a*b*s);
}

A.
2, 8
B.
4, 8
C.
2, 4
D.
4, 12

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum


18.

What will be the output of the program?

#include<stdio.h>

int main()
{
    int y=128;
    const int x=y;
    printf("%d\n", x);
    return 0;
}

A.
128
B.
Garbage value
C.
Error
D.
0

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Step 1: int y=128; The variable 'y' is declared as an integer type and initialized to value "128".

Step 2: const int x=y; The constant variable 'x' is declared as an integer and it is initialized with the variable 'y' value.

Step 3: printf("%d\n", x); It prints the value of variable 'x'.

Hence the output of the program is "128"

Learn more problems on : Const

Discuss about this problem : Discuss in Forum


19.

Will the program compile successfully?

#include<stdio.h>

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

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Yes, It prints "IndiaBIX"

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum


20.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    void *vp;
    char ch=74, *cp="JACK";
    int j=65;
    vp=&ch;
    printf("%c", *(char*)vp);
    vp=&j;
    printf("%c", *(int*)vp);
    vp=cp;
    printf("%s", (char*)vp+2);
    return 0;
}

A.
JCK
B.
J65K
C.
JAK
D.
JACK

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


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