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 main()
{
    int x=30, *y, *z;
    y=&x; /* Assume address of x is 500 and integer is 4 byte size */
    z=y;
    *y++=*z++;
    x++;
    printf("x=%d, y=%d, z=%d\n", x, y, z);
    return 0;
}

A.
x=31, y=502, z=502
B.
x=31, y=500, z=500
C.
x=31, y=498, z=498
D.
x=31, y=504, z=504

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


2.

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


3.

How many times the while loop will get executed if a short int is 2 byte wide?

#include<stdio.h>
int main()
{
    int j=1;
    while(j <= 255)
    {
        printf("%c %d\n", j, j);
        j++;
    }
    return 0;
}

A.
Infinite times
B.
255 times
C.
256 times
D.
254 times

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The while(j <= 255) loop will get executed 255 times. The size short int(2 byte wide) does not affect the while() loop.

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


4.

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

#include<stdio.h>
int main()
{
    int i = 10, j = 15;
    if(i % 2 = j % 3)
        printf("IndiaBIX\n");
    return 0;
}

A.
Error: Expression syntax
B.
Error: Lvalue required
C.
Error: Rvalue required
D.
The Code runs successfully

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

if(i % 2 = j % 3) This statement generates "LValue required error". There is no variable on the left side of the expression to assign (j % 3).

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


5.

What will be the output of the program?

#include<stdio.h>
int main()
{
    int i=-3, j=2, k=0, m;
    m = ++i && ++j || ++k;
    printf("%d, %d, %d, %d\n", i, j, k, m);
    return 0;
}

A.
1, 2, 0, 1
B.
-3, 2, 0, 1
C.
-2, 3, 0, 1
D.
2, 3, 1, 1

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.

Step 2: m = ++i && ++j || ++k;
becomes m = (-2 && 3) || ++k;
becomes m = TRUE || ++k;.
(++k) is not executed because (-2 && 3) alone return TRUE.
Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.

Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of i,j are increemented by '1'(one).

Hence the output is "-2, 3, 0, 1".

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum


6.

Which header file should you include, if you are going to develop a function, which can accept variable number of arguments?

A.
varagrg.h
B.
stdlib.h
C.
stdio.h
D.
stdarg.h

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum


7.

In the following code snippet can we declare a new typedef named ptr even though struct employee has not been completely declared while using typedef?

typedef struct employee *ptr;
struct employee
{
    char name[20];
    int age;
    ptr next;
}

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Typedef

Discuss about this problem : Discuss in Forum


8.

Every function must return a value

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

No, If a function return type is declared as void it cannot return any value.

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


9.

Usually recursion works slower than loops.

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

When a recursive call is made, the function/process clones itself and then process that funtion. This leads to time and space constrains.

In a loop, there is no recursive call involved that saves a lot of time and space too.

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


10.

What will be the output of the program, if a short int is 2 bytes wide?

#include<stdio.h>
int main()
{
    short int i = 0;
    for(i<=5 && i>=-1; ++i; i>0)
        printf("%u,", i);
    return 0;
}

A.
1 ... 65535
B.
Expression syntax error
C.
No output
D.
0, 1, 2, 3, 4, 5

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

for(i<=5 && i>=-1; ++i; i>0) so expression i<=5 && i>=-1 initializes for loop. expression ++i is the loop condition. expression i>0 is the increment expression.

In for( i <= 5 && i >= -1; ++i; i>0) expression i<=5 && i>=-1 evaluates to one.

Loop condition always get evaluated to true. Also at this point it increases i by one.

An increment_expression i>0 has no effect on value of i.so for loop get executed till the limit of integer (ie. 65535)

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


11.

Point out the error in the following program.

#include<stdio.h>

int main()
{
    char str[] = "IndiaBIX";
    printf("%.#s %2s", str, str);
    return 0;
}

A.
Error: in Array declaration
B.
Error: printf statement
C.
Error: unspecified character in printf
D.
No error

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


12.

malloc() returns a float pointer if memory is allocated for storing float's and a double pointer if memory is allocated for storing double's.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Memory Allocation

Discuss about this problem : Discuss in Forum


13.

stderr, stdin, stdout are FILE pointers

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Yes, these will be declared like

The corresponding stdio.h variable is FILE* stdin;

The corresponding stdio.h variable is FILE* stdout;

The corresponding stdio.h variable is FILE* stderr;

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum


14.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    enum status {pass, fail, absent};
    enum status stud1, stud2, stud3;
    stud1 = pass;
    stud2 = absent;
    stud3 = fail;
    printf("%d %d %d\n", stud1, stud2, stud3);
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


15.

Point out the error in the program?

#include<stdio.h>

int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a z1 = {512};
    union a z2 = {0, 2};
    return 0;
}

A.
Error: invalid union declaration
B.
Error: in Initializing z2
C.
No error
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


16.

What will be the output of the program?

#include<stdio.h>

int main()
{
    int i;
    char c;
    for(i=1; i<=5; i++)
    {
        scanf("%c", &c); /* given input is 'a' */
        printf("%c", c);
        ungetc(c, stdin);
    }
    return 0;
}

A.
aaaa
B.
aaaaa
C.
Garbage value.
D.
Error in ungetc statement.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

for(i=1; i<=5; i++) Here the for loop runs 5 times.

Loop 1:
scanf("%c", &c); Here we give 'a' as input.
printf("%c", c); prints the character 'a' which is given in the previous "scanf()" statement.
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

Loop 2:
Here the scanf("%c", &c); get the input from "stdin" because of "ungetc" function.
printf("%c", c); Now variable c = 'a'. So it prints the character 'a'.
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

This above process will be repeated in Loop 3, Loop 4, Loop 5.

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


17.

What will be the output of the program?

#include<stdio.h>
#define CUBE(x) (x*x*x)

int main()
{
    int a, b=3;
    a = CUBE(b++);
    printf("%d, %d\n", a, b);
    return 0;
}

A.
9, 4
B.
27, 4
C.
27, 6
D.
Error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The macro function CUBE(x) (x*x*x) calculates the cubic value of given number(Eg: 103.)

Step 1: int a, b=3; The variable a and b are declared as an integer type and varaible b id initialized to 3.

Step 2: a = CUBE(b++); becomes

=> a = b++ * b++ * b++;

=> a = 3 * 3 * 3; Here we are using post-increement operator, so the 3 is not incremented in this statement.

=> a = 27; Here, 27 is store in the variable a. By the way, the value of variable b is incremented by 3. (ie: b=6)

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

Hence the output of the program is 27, 6.

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum


18.

What will be the output of the program?

#include<stdio.h>
#define MAN(x, y) ((x)>(y)) ? (x):(y);

int main()
{
    int i=10, j=5, k=0;
    k = MAN(++i, j++);
    printf("%d, %d, %d\n", i, j, k);
    return 0;
}

A.
12, 6, 12
B.
11, 5, 11
C.
11, 5, Garbage
D.
12, 6, Garbage

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The macro MAN(x, y) ((x)>(y)) ? (x):(y); returns the biggest number of given two numbers.

Step 1: int i=10, j=5, k=0; The variable i, j, k are declared as an integer type and initialized to value 10, 5, 0 respectively.

Step 2: k = MAN(++i, j++); becomes,

=> k = ((++i)>(j++)) ? (++i):(j++);

=> k = ((11)>(5)) ? (12):(6);

=> k = 12

Step 3: printf("%d, %d, %d\n", i, j, k); It prints the variable i, j, k.

In the above macro step 2 the variable i value is increemented by 2 and variable j value is increemented by 1.

Hence the output of the program is 12, 6, 12

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum


19.

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


20.

How many bytes are occupied by near, far and huge pointers (DOS)?

A.
near=2 far=4 huge=4
B.
near=4 far=8 huge=8
C.
near=2 far=4 huge=8
D.
near=4 far=4 huge=8

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every pointers is 4 bytes long.

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.