10 Objective type interview questions with answers on C/C++
Before you start:-
- You can check your answer by filling a,b,c,d or e in the text box.
- You can get the answer by clicking Give Up Button.
Try yourself before giving up 🙂
Q1)
#include main() { unsigned char i; int sum=0; for(i=0;i<300;i++) sum=sum+i; printf("%d",sum); }
Options: a) 44850 b) 45150 c) 300 d) Infinite Loop
Q2)
#include fun(int i) { int j=0; while (i & i-1) { j++; } printf("%d",j); } main(){ f(100); }
Options: a. 100 b. 99 c. 2 d. 3
Q3)
Code 1 : for(i=0; i<1000; i++) for(j=0; j<100; j++) x = y; Code 2 : for(i=0; i<100; i++) for(j=0; j<1000; j++) x = y;
Which code will execute faster ?
Options:
a) Code 1 and Code 2 are of same speed,
b) Code 1,
c) Code 2,
d) Can’t Say
Q4)
main() { int a[10] = {1, 2, 3, ...., 10}, i, x=10, temp; for(i=0; i < x; i++){ temp = a[i]; a[i] = a[x-i-1]; a[x-i-1] = temp; } }
What happens to the elements of the array a ?
a) Reversed
b) Only some portions are altered
c) Remains same
d) None
Q5)
#include # define MAX(a, b) a>b ? a:b main() { int m, n; m = 3 + MAX(2, 3); n = 2 * MAX(3, 2); printf("%d, %d\n", m, n); }
Options: a) 6, 6 b) 5, 6 c) 2, 3 d) None
Q6)
#include main(){ int i=10; printf("%d %d %d",i,++i,i++); }
a).10 11 12
b).12 11 10
c).10 11 11
d).12 12 10
Q7)
What is the size of ‘q’ in the following program?
union{ int x; char y; struct { char x; char y; int xy; }p; }q;
Options:
a)11
b)6
c)4
d)5
e)none
Q8)
Result of the following program is ?
main() { int i=0; for(i=0;i<20;i++) { switch(i) { case 0:i+=5; case 1:i+=2; case 5:i+=5; default i+=4; break; } printf("%d,",i); } }
Options:
a)0,5,9,13,17,
b)5,9,13,17,
c)12,17,22,
d)16,21,
e)syntax error
Q9)
#include main() { char *p1="name"; char *p2; p2=(char*)malloc(20); while(*p2++ = *p1++); printf("%s\n",p2); }
Options: a) name b) n c) ame d) Empty String
Q10)
#include main() { int x=20,y=35; x=y++ + x++; y= ++y + ++x; printf("%d %d\n",x,y); }
Options: a) 55 94 b) 56 94 c) 57 94 d) None
Facebook Comments
Div Ya says
Explain 10th question
Ausaf jafri says
how……….1st quest have infinite loop,???????????????
puzzlersworld says
@bc42db8149b30a88b769066d3152364c:disqus its a tricky one :), i is declared as unsigned char, it can hold maximum value of 256, increasing it further will reduce the value back to 0, it will never reach 300 and for loop condition will always be satisfied.
k says
Explain 5th question
admin says
In C/C++ MACRO’s are replaced at compile time as it is.
so m = 3 + MAX(2, 3);
will become
m = 3 + 2>3 ? 2:3
this will evaluate to 5 >3 ? 2:3, thus m will be 2
Simillarly
n = 2 * MAX(3, 2); will become
n = 2 * 3>2 : 3:2 => 6>2 ? 3:2 thus n will be 3