C & C++ Interview Questions and Answers

C & C++ Interview Questions and Answers

(1) What will be output if you will compile and execute the following c code?

struct marks
{
int p:3;
int c:3;
};
void main(){
struct marks s={2,-6};
printf(“%d %d”,s.p,s.c);
}

(a) 2 -6
(b) 2 1
(c) 2 2
(d) Compiler error

Answer: (c)

Explanation: Binary value of 2: 00000010 (Select three two bit)
Binary value of 6: 00000110
Binary value of -6: 11;]111001+1=11111010
(Select last three bit)

(2) What will be output if you will compile and execute the following c code?

#include
void main(){
int i;
float a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=3;i++)
printf(“%d “,*ptr++);
}

(a)0 0 0 0
(b)Garbage Garbage Garbage Garbage
(c)102 56 -80 32
(d)102 102 -90 64

Answer: (b)

Explanation: *ptr++ will increment the address every time so we cant guess the value present in that address

(3) What will be output if you will compile and execute the following c code?

#include
Void main(){
printf(“%s”,”c” “question” “bank”);
}

(a) c question bank
(b) c
(c) bank
(d) cquestionbank

Answer : (d)

Explanation: In c string constant “xy” is same as “x” “y”

(4) What will be output if you will compile and execute the following c code?

#include
int main(){
char *str=”c-pointer”;
printf(“%*.*s”,10,7,str);
return 0;
}

(a) c-pointer
(b) cpointer
(c) cpointer null null
(d) c-point

Answer: (d)

Explanation: Meaning of %*.*s in the printf function:
First * indicates the width i.e. how many spaces will take to print the string and second * indicates how many characters will print of any string.

(5) What will be output if you will compile and execute the following c code?

#include
int main(){
int a=-12;
a=a>>3;
printf(“%d”,a);
return 0;
}

(a) -4 (b) -2 (c) -3 (d) -96

Answer :(b)

Explanation:
Binary value of 12 is: 00000000 00001100
-12 is 2’s com of 12 =>11111111 11110100
While performing 3 left shift value becomes => 111111 111110 => -2

(6) What will be output if you will compile and execute the following c code?

#include
#include
int main(){
printf(“%d %d”,sizeof(“string”),strlen(“string”));
return 0;
}

(a) 7 6
(b) 7 7
(c) 6 7
(d) 6 6

Answer: (a)

Explanation: char has memory of 1 byte so sizeof(“string”)=>7 including null char; strlen(“string”)=>6 which is the length.

(7) What will be output if you will compile and execute the following c code?

#include
int main(){
int a=1,2;
int b=(1,2);
printf(“%d %d”,a,b);
return 0;
}

(a)1,1
(b)1,2
(c)2,1
(d)2,2

Answer: (b)

Explanation: 1,2 will think as array and a[0]which is same as a will be assigned as 1
(1,2) comma operator will lead to right most one so it returns 2

(8) What will be output if you will compile and execute the following c code?

#include
int main(){
int i=0;
if(i==0){
i=((5,(i=3)),i=1);
printf(“%d”,i);
}
else
printf(“equal”);
}

(a) 5
(b) 3
(c) 1
(d) equal

Answer : (c)

Explanation: refer previous Explanation

(9) What will be output if you will compile and execute the following c code?

#include
#define message “union is\
power of c”
int main(){
printf(“%s”,message);
return 0;
}

(a) union is power of c
(b) union ispower of c
(c) union is
Power of c
(d) Compiler error

Answer: (b)

Explanation: If you want to write macro constant in new line the end with the character \.

(10) What will be output if you will compile and execute the following c code?

#include
#define call(x) #x
int main(){
printf(“%s”,call(c/c++));
return 0;
}

(a)c
(b)c++
(c)#c/c++
(d)c/c++

Answer: (d)

Explanation: the macro call() will return the string as it is

(11) What will be output if you will compile and execute the following c code?

#include
int main(){
if(printf(“cquestionbank”))
printf(“I know c”);
else
printf(“I know c++”);
return 0;
}

(a) I know c
(b) I know c++
(c) cquestionbankI know c
(d) cquestionbankI know c++

Answer: (c)

Explanation: printf() always returns true if it prints somethink

(12) What will be output if you will compile and execute the following c code?

int main(){
int i=10;
static int x=i;
if(x==i)
printf(“Equal”);
else if(x>i)
printf(“Greater than”);
else
printf(“Less than”);
return 0;
}

(a) Equal
(b) Greater than
(c) Less than
(d) Compiler error

Answer: (d)

Explanation: we cant allocate value for a static variable dynamically

(13) What will be output if you will compile and execute the following c code?

#include
int main(){printf(“%s”,__DATE__);
return 0;
}

(a) Current system date
(b) Current system date with time
(c) null
(d) Compiler error

Answer: (a)

(14) What will be output if you will compile and execute the following c code?

#include
#define var 3
int main(){
char *cricket[var+~0]={“clarke”,”kallis”};
char *ptr=cricket;
printf(“%c”,*++ptr);
return 0;
}

Choose all that apply:
(A) a
(B) r
(C) l
(D) Compilation error

Answer: (C)

Explanation: in the above code cricket[0] will be “clarke” and cricket[1] will be “kallis”
so *ptr=cricket
this will Asian cricket[0] value to *ptr
and *++ptr denotes the second character in the string that is ‘l’

(15) What will be output if you will compile and execute the following c code?

#include
int main(){
int i=5,j;
j=++i+++i+++i;
printf(“%d %d”,i,j);
return 0;
}

(A) 7 21
(B) 8 21
(C) 7 24
(D) 8 24

Answer: (D)

Explanation: where j=++i + ++i + ++i;
++ operator have high priority than + so all ++ operator will perform first after that it will like
j=8+8+8;
so j=24

(16)What will be output of the following c program?

#include
int main(){
int class=150;
int public=25;
int private=30;
class = class >> private – public;
printf(“%d”,class);
return 0;
}

(A) 1
(B) 2
(C) 4
(D) Compilation error

Answer: (C)

Explanation: Keyword of c++ can be used in c

(17) What will be output if you will compile and execute the following c code?

#include
int main(){
int i=2,j=2;
while(i+1?–i:j++)
printf(“%d”,i);
return 0;
}

(A)1
(B)2
(C)3
(D)4

Answer: (A)

Explanation: where i+1 will return 3 but still i is 2
rule: any number other that 0 will be considered as TRUE so the return of 2 will be considered as true and the left Expression to ‘:’ will execute to bring it as i=1

(18) What will be output if you will compile and execute the following c code?

#include
int main(){
int i,j;
i=j=2;
while(–i&&j++)
printf(“%d %d”,i,j);
return 0;
}

(A) 2 3
(B) 0 3
(C) 1 3
(D)Infinite loop

Answer: (c)

Explanation: in while loop
first time (1 && 2)
so it accepts the first time and print 1 3
second time
in the while loop(0 && 3)
0 will be considered as false
so it will stops

(19) The size of generic pointer in c is 2?

a)true b)false

Answer: (a)

(20) Int aaaaaaaaa; is a legal variable name?

a)true b)false

Answer: (a)