C INTERVIEW QUESTIONS


PREVIOUS                                                                                                            NEXT

C INTERVIEW QUESTIONS WITH ANSWERS

1
#include<stdio.h>

int main()
{
    enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
    printf("%d, %d, %d, %d, %d, %d\n", MON, TUE, WED, THU, FRI, SAT);
    return 0;
}
[A].
-1, 0, 1, 2, 3, 4
[B].
-1, 2, 6, 3, 4, 5
[C].
-1, 0, 6, 2, 3, 4
[D].
-1, 0, 6, 7, 8, 9
@
Answer: Option D
  
enum days={MON=-1,TUE,WED=6,THU,FRI,SAT}
so the value for monday = -1;
usually enum ill take the values sequentially, dats if enum month={a=0,b};
her the value for b=1(dats next of 1)
like wise the value for TUE=0;
den wed=6(ITS ALREADY GIVEN),therefore next value s 7 hence THU=7,DEN FRI=8,SAT=9

2
#include<stdio.h>

int main()
{
    enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
    printf("%d, %d, %d, %d, %d, %d\n", ++MON, TUE, WED, THU, FRI, SAT);
    return 0;
}
[A].
-1, 0, 1, 2, 3, 4
[B].
Error
@
[C].
0, 1, 6, 3, 4, 5
[D].
0, 0, 6, 7, 8, 9
Answer: Option B
Explanation:
Because ++ or -- cannot be done on enum value.




3.

#include<stdio.h>

int main()
{
    union var
    {
        int a, b;
    };
    union var v;
    v.a=10;
    v.b=20;
    printf("%d\n", v.a);
    return 0;
}
 


[A].
10
[B].
20
@
[C].
30
[D].
0
Answer: Option B

Because union can initialize only one variable at a time. It overwrites the memory with binary value of 20 where it was initialized with binary value of 10 before.

It takes the last initialized value of its member variables.

4. Which of the ollowing statement is correct prototype of the malloc() function in c ?
[A].
int* malloc(int);
[B].
char* malloc(char);
[C].
unsigned int* malloc(unsigned int);
[D].
void* malloc(size_t);
Answer: Option D
By defalut for malloc() function return type is void.



4.
Which bitwise operator is suitable for checking whether a particular bit is on or off?
[A].
&& operator
[B].
& operator
@
[C].
|| operator
[D].
! operator
Answer: Option B
As we know that truth table of AND is if 2 number is true then only output becomes true. For that reason & is used to find bits on or off.

5. What will be the output of the program ?
#include<stdio.h>

int main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
    return 0;
}
A.
2, 1, 15

B.
1, 2, 5

C.
3, 2, 15

D.
2, 3, 20

Answer: Option C
Explanation:
Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .
Step 2: int i, j, m; The variable i,j,m are declared as an integer type.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15

6. What will be the output of the program If the integer is 4bytes long?
#include<stdio.h>

int main()
{
    int ***r, **q, *p, i=8;
    p = &i;
    q = &p;
    r = &q;
    printf("%d, %d, %d\n", *p, **q, ***r);
    return 0;
}
[A].
8, 8, 8

[B].
4000, 4002, 4004
[C].
4000, 4004, 4008
[D].
4000, 4008, 4016
Answer: Option A

Here each * means address holding it i.e. 'p' holds address of 'i', *p value at i = 8.

'q' holds address of p, p holds address of 'i'. So with two ** we are back to value at i = 8.

Similarly, three times *** means jump from r to q to p that holds i = 8.

7. What will be the output of the program?
#include<stdio.h>
#include<math.h>
int main()
{
    printf("%d, %d, %d\n", sizeof(3.14f), sizeof(3.14), sizeof(3.14l));
    return 0;
}
A.
4, 4, 4

B.
4, 8, 8

C.
4, 8, 10

D.
4, 8, 12

Answer: Option C
Explanation:
sizeof(3.14f) here '3.14f' specifies the float data type. Hence size of float is 4 bytes.
sizeof(3.14) here '3.14' specifies the double data type. Hence size of float is 8 bytes.
sizeof(3.14l) here '3.14l' specifies the long double data type. Hence size of float is 10 bytes.
Note: If you run the above program in Linux platform (GCC Compiler) it will give 4, 8, 12 as output. If you run in Windows platform (TurboC Compiler) it will give 4, 8, 10 as output. Because, C is a machine dependent language



8. What will be the output of the program ?
#include<stdio.h>

int main()
{
    printf("%%%%\n");
    return 0;
}
[A].
%%%%%
[B].
%%

[C].
No output
[D].
Error
Answer: Option B

Because we can't print % in C.

To print % we need %% in C for printing single %.

Hence to print %% we need %%%%


9.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    printf("%c\n", ~('C'*-1));
    return 0;
}
[A].
A
[B].
B
@
[C].
C
[D].
D
Answer: Option B

ASCII Value of 'C' is 67.

67*-1=-67

applying bitwise not(~) to -67 will result in 66, which is the ASCII value of 'B'.




PREVIOUS                                                                                                                   NEXT

No comments:

Post a Comment