C Interview Questions 2
Questions by Rejinpaul.com
1. #include
int main()
{
int a=0;
#if (a==0)
printf("Equal");
#else if
printf("Not equal");
#endif
return 0;
}
a)Equal
b)Null
c)Garbage
d)Compilation error
The Correct Answer is:
d)Compilation error
Explanation:
Syntax of conditional preprocessor directive (if) is:
#if
#else
#endif
In this code (a==0) is not constant expression. A
constant expression mean expression doesn’t contain
any variables.Note: const int a; Here a is also variable
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
2. #include
int main()
{
for(;NULL;)
printf("cquestionbank");
return 0;
}
a)c
b) bank
c) cquestionbank
d) Infinite loop
The Correct Answer is:
c) cquestionbank
Explanation:
Here NULL is micro constantan. Value of this
symbolic constant is 0 or 0L as defined stdio.h:
#ifndef NULL#
if defined(__TINY__) ||
defined(__SMALL__) || defined(__MEDIUM__)
# define NULL 0
# else# define NULL 0L
# endif
#endif
So corresponding intermediate file of above code
will be:
int main()
{
for(;0;)
printf("cquestionbank");
return 0;
}
As you know in c :0: Means falseNon- zero: True So for loop should not execute any time because
intitial condtion is false. But it is bug of turbo c
compiler. Loop will
execute one time and it will print : cquestionbank
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
3. #include
int main()
{
int x=25;
if(!!x)
printf("%d",!x);
else
printf("%d",x);
return 0;
}
a) 0
b) 25
c)1
d)-1
The Correct Answer is:
a) 0
Explanation:
! is negation operator.
!x = 0 if x is non-zero number.!
x = 1 if x is equal to zero.
So,
!!x
=! (! x)
=! (! 25)
=! (0)
=1
Zero: It represents false.
Non-zero: It represents false.
if (1) means condition is true hence if part will
execute.
!x =! 1 = 0
Hence printf statement will print zero in
the console window.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
4. #include
int main()
{
float a=0.5, b=0.9;
if(a&&b>0.9)
printf("Sachin");
else
printf("Rahul");
return 0;
}
a)Sachin
b)Rahul
c)null
d)Run time error
The Correct Answer is:
b)Rahul
Explanation:
Consider on the expression:
a && b > 0.9
As we know > operator enjoy higher precedence than
&& operator.
So expression will be
a && (b > 0.9)
=0.5 && (0.9 > 0.9)
Properties of > (Greater than operator)
a > b returns 1 if a is greater than b.
a > b return 0 if a is less than or equal to b.
So our expression became:= 0.5 && 0
In c zero represents false and non-zero represents true.
= true && false= false= 0
So, if (0) means condition is false. Hence else part will execute.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
5. #include
int main()
{
int x=5, y=10;
if(!(!x) && x)
printf("%d",x);
else
printf("%d",y);
return 0 ;
}
a)1
b)0
c)Compilation error
d)5
The Correct Answer is:
d)5
Explanation:
Consider on expression:
! (! x) && x
=! (! 5) && 5
=! 0 && 5=1 && 5
=1
So, if condition is true.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
6. #include
int main()
{
char ch=321;
printf("%d %c",ch,ch);
return 0 ;
}
a)321 #
b)65 A
c)321 !
d)66 B
The Correct Answer is:
b)65 A
Explanation:
Number 321 is beyond the range of char variable ch.
So ch will store corresponding cyclic value as shown
in the figure:
Number 321 is beyond the range of char variable ch. So ch will store corresponding cyclic value as shown in the figure:Number 321 is beyond the range of char variable ch. So ch will store corresponding cyclic value Equivalent cyclic value of 321 = 321 % 128 = 65
65 is ASCII Value of character 'A'
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
7. #include
int main()
{
int a,b;
a = -3- -3;
b = -3 - - (-3 );
printf("%d %d",a,b);
return 0 ;
}
a)0 0
b)0 -3
c)-3 0
d)0 -6
The Correct Answer is:
d)0 -6
Explanation:
- Operator in c has two forms:- : Unary minus operator
(It requires one operand)- : Binary minus
operator (It requires two operands)Note: Unary minus
operator has higher precedence than binary minus operator.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
8. #include
int main()
{
int x;
x= -2 + 11 - 7 * 9 % 6 / 12;
printf("%d",x);
return 0 ;
}
a)9
b)6
c)7
d)8
The Correct Answer is:
a)9
Explanation:
Consider on the expressionx = -2 + 11 - 7 * 9 % 6 / 12
First of all unary minus will perform the operation x = (-2) + 11 - 7 * 9 % 6 / 12 Now
multiplication operator will perform theoperation x = (-2) + 11 - (7 * 9) % 6 / 12 x = (-2) + 11 - 63 % 6 / 12
After this modular division operator will performoperation:
x = (-2) + 11 - (63 % 6) / 12 x = (-2) + 11 - 3 / 12
After this division will perform the operation:
x = (-2) + 11 - (3 / 12) x = (-2) + 11 - 0
Now binary plus operator will perform the operation:
x = ((-2) + 11) - 0 x = 9 - 0Now binary minus operator will perform theoperation:
x = 9At the end assignment operator will perform the operation nd it will assign 9 to variable x.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
9. #include
int main(){
int x=3, y=4, z=4;
printf("%d", (z>=y>=x?100:200));
return 0 ;
}
a)100
b)200
c)0
d)1
The Correct Answer is:
b)200
Explanation:
Consider on arithmetical expression:
z>=y>=x? 100:200
= (z >= y >= x? 100: 200)
= (4 >= 4 >= 3? 100: 200)
= ((4 >= 4) >= 3? 100: 200)
= (1 >= 3? 100: 200)
= (0? 100: 200)= 200
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
10. include
int main(){
int a=30, b=40, x;
x=(a!=10) && (b=50);
printf("%d",x);
return 0 ;
}
a)1
b)2
c)3
d)4
The Correct Answer is:
a)1
Explanation:
(a!= 10) && (b = 50)
= (30! = 10) && (50)
= 1 && 50
= 1
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
11. #include
int main(){
float x=12.25, y=13.65;
if(x=y)
printf("x and y are equal");
else
printf("x and y are not equal");
return 0 ;
}
a)x and y are not equal
b)It will print nothing
c)x and y are equal
d)Run time error
The Correct Answer is:
c)x and y are equal
Explanation:
= is assignment operator.
So, x=y value of variable y will assign to variable x.
x= 13.65As we know in c:
Zero represents false.
Any non-zero number represents true.H
ence if (13.65) is true condition.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
12. #include
int main(){
int i=1, j=1;
for(;j;printf("%d%d\t",i,j))
j=i++ <= 5;
return 0 ; }
a)Infinite loop
b)1 2 3 4 5
c)21 31 41 51 61 70
d)Compilation error
The Correct Answer is:
c)21 31 41 51 61 70
Explanation:
for (exp1;exp2;exp3){
}
Note: all Exp1, Exp2, Exp3 and
are optional.
Order of evaluation is:
In first cycle
1. Exp12.
Exp23.
4. Exp2
In rest cycles
1. Exp2
In rest cycles
1. Exp2
2.
3. Exp3
Now we come to our questionIn first cycle1.
It has not exp12.
Exp2 is j which is equal to 1
i.e. condition is true3.
is j=i++ <= 5
j = 1 <= 5
j = 1i = 2 (Due to postfix increment operator)
4. Exp3 is printf statement which will print i as 2and j as 1In second cycle1.
Exp2 is j which is equal to 1
i.e. condition is true2.
is j=i++ <= 5j = 2 <= 5j = 1i = 3 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 3and j as 1In third cycle1.
Exp2 is j which is equal to 1
i.e. condition is true2.
is j=i++ <= 5j = 4 <= 5j = 1i = 5 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 4 and j as 1In fifth cycle
1. Exp2 is j which is equal to 1
i.e. condition is true
2.
is j=i++ <= 5j = 5 <= 5j = 1i = 6 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 6 and j as 1In sixth cycle1.
Exp2 is j which is equal to 1 i.e. condition is true
2.
is j=i++ <= 5j = 6 <= 5j = 0i = 7 (Due to postfix increment operator)3. Exp3 is printf statement which will print i as 7 and j as 0In seventh cycle1.
Exp2 is j which is equal to 0 i.e.
condition is false. So loop will terminate.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
13. What function is used to release the allocated memory space?
a)deallocate()
b)release ()
c)drop()
d)free ()
The Correct Answer is:
d)free ()
Explanation:
**
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
14. #include
auto int a=5;
int main(){
int x;
x=~a+a&a+a<
printf("%d",x);
return 0;
}
a)0
b)155
c)154
d)Compilation error
The Correct Answer is:
d)Compilation error
Explanation:
**
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
15. #include
void main(){
int a=5;
{
int b=10;
++b;
++a;
{
int a=20;
++a;
a=++b;
}
++a;
++b;
printf("%d %d",a,b);
}
printf(" %d",a);
}
a)7 13 7
b)13 13 13
c)13 13 5
d)Compilation error
The Correct Answer is:
a)7 13 7
Explanation:
**
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
16. #include
#include
void main(){
int a[]={0,1,2,3,4,5,6,7,8,9,10};
int i=0,num;
num=a[++i+a[++i]]+a[++i];
printf("%d",num);
}
a)6
b)7
c)8
d)9
The Correct Answer is:
d)9
Explanation:
a[++i+a[++i]]+a[++i]
As we know in any expression
pre increment operator first increment the value of variable then final
value assign to all variable.
Variable i has incremented three times so final
value of I will be 3. Now 3 will be assigned to all
i. So expression will be:
= a [3+a [3] +a [3]
= a [3 + 3] + 3
=a [6] + 3= 6 + 3=9
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
17. #include
#include
void main(){
int i=3,val;
val=sizeof f(i)+ +f(i=1)+ +f(i-1);
printf("%d %d",val,i);
}
int f(int num){
return num*5;
}
a)2 0
b)7 1
c)17 0
d)
The Correct Answer is:
b)7 1
Explanation:
Consider on the expression:
sizeof f(i)+ +f(i=1)+ +f(i-1) //i=3
= sizeof f(3)+ +f(i=1)+ +f(i-1)
= sizeof 15 + +f(i=1)+ +f(i-1)
= 2 + +f(i=1)+ +f(i-1)
= 2 + f(1) + +f(0) //i=1
= 2 + 5 + f(0)
= 7 + 0= 7
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
Pages:
1
2
3
4
Newer Post
Older Post
Home
Ad Inside Post
Comments system
Disqus Shortname