1.) main()
{
int
=10;
i=!i>14;
printf
("i=%d",i);
}
Answer:
i=0
Explanation:
In
the expression !i>14 , NOT (!) operator has more precedence than ‘ >’
symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is
false). 0>14 is false (zero).
2.)
#include<stdio.h>
main()
{
int
i=1,j=2;
switch(i)
{
case
1: printf("GOOD");
break;
case
j: printf("BAD");
break;
}
}
Answer:
Compiler
Error: Constant expression required in function main.
Explanation:
The
case statement can have only constant expressions (this implies that we cannot
use variable names directly so an error).
//Note:
Enumerated types and int and char type declared with qualifier const can be used in case statements.
3.)
main()
{
int
i;
printf("%d",scanf("%d",&i));
// value 10 is given as input here
}
Answer:
1
Explanation:
Scanf
returns number of items successfully read and not 1/0. Here 10 is given
as input which should have been scanned successfully. So number of items read
is 1.
4.) main()
{
int
i=0;
for(;i++;printf("%d",i))
;
printf("%d",i);
}
Answer:
1
Explanation:
before
entering into the for loop the checking condition is "evaluated".
Here it evaluates to 0 (false) and comes out of the loop, and i is incremented
(note the semicolon after the for loop).
5.) main()
{
int i=-1;
+i;
printf("i = %d, +i = %d \n",i,+i);
}
Answer:
i
= -1, +i = -1
Explanation:
Unary + is the only dummy operator in C. Where-ever it comes you
can just ignore it just because it has no effect in the expressions (hence the
name dummy operator).
6.) main()
{
char not;
not=!2;
printf("%d",not);
}
Answer:
0
Explanation:
!
is a logical operator. In C the value 0 is considered to be the boolean value
FALSE, and any non-zero value is considered to be the boolean value TRUE. Here
2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints 0.
7.) main()
{
int k=1;
printf("%d==1 is
""%s",k,k==1?"TRUE":"FALSE");
}
Answer:
1==1
is TRUE
Explanation:
When
two strings are placed together (or separated by white-space) they are
concatenated (this is called as "stringization" operation). So the
string is as if it is given as "%d==1 is %s". The conditional
operator( ?: ) evaluates to "TRUE".
8.) main()
{
int y;
scanf("%d",&y); // input given is 2000
if( (y%4==0 && y%100 != 0) || y%100 == 0 )
printf("%d is a leap year");
else
printf("%d is not a leap year");
}
Answer:
2000
is a leap year
Explanation:
An
ordinary program to check if leap year or not.
9.) main()
{
int i=-1;
-i;
printf("i = %d, -i = %d \n",i,-i);
}
Answer:
i
= -1, -i = 1
Explanation:
-i
is executed and this execution doesn't affect the value of i. In printf first
you just print the value of i. After that the value of the expression -i =
-(-1) is printed.
10.) int i;
main(){
int t;
for (
t=4;scanf("%d",&i)-t;printf("%d\n",i))
printf("%d--",t--);
}
// If the inputs are 0,1,2,3 find the o/p
Answer:
4--0
3--1
2--2
Explanation:
Let
us assume some x= scanf("%d",&i)-t the values during execution will be
t
i x
4
0 -4
3
1 -2
2
2 0
11.) main(){
int a= 0;int b = 20;char x =1;char y =10;
if(a,b,x,y)
printf("hello");
}
Answer:
hello
Explanation:
The
comma operator has associativity from left to right. Only the rightmost value
is returned and the other values are evaluated and ignored. Thus the value of
last variable y is returned to check in if. Since it is a non zero value if
becomes true so, "hello" will be printed.
12.) main(){
unsigned
int i;
for(i=1;i>-2;i--)
printf("I Love You");
}
Answer:
No
output
Explanation:
i
is an unsigned integer. It is compared with a signed value. Since the both
types doesn't match, signed is promoted to unsigned value. The unsigned
equivalent of -2 is a huge value so condition becomes false and control comes
out of the loop.
13. void main()
{
while(1){
if(printf("%d",printf("%d")))
break;
else
continue;
}
}
Answer:
Garbage values
Explanation:
The
inner printf executes first to print some garbage value. The printf returns no
of characters printed and this value also cannot be predicted. Still the outer
printf prints something and so returns a non-zero value. So it encounters
the break statement and comes out of the while statement.
14.) #include<conio.h>
main()
{
int x,y=2,z,a;
if(x=y%2) z=2;
a=2;
printf("%d %d ",z,x);
}
Answer:
Garbage-value 0
Explanation:
The
value of y%2 is 0. This value is assigned to x. The condition reduces to if (x)
or in other words if(0) and so z goes uninitialized.
Thumb Rule: Check all control paths to write bug free code.
15.) main()
{
unsigned char i=0;
for(;i>=0;i++) ;
printf("%d\n",i);
}
Answer:
infinite loop
Explanation:
The difference between the previous question and this one is
that the char is declared to be unsigned. So the i++ can never yield negative
value and i>=0 never becomes false so that it can come out of the for loop.
16.) main()
{
char i=0;
for(;i>=0;i++) ;
printf("%d\n",i);
}
Answer:
128
Explanation:
The detail if the char
is signed/unsigned by default is implementation dependent. If the
implementation treats the char to be signed by default the program will print
–128 and terminate. On the other hand if it considers char to be unsigned by
default, it goes to infinite loop.
17.) main()
{
char p[ ]="%d\n";
p[1] = 'c';
printf(p,65);
}
Answer:
A
Explanation:
Due to the assignment
p[1] = ‘c’ the string becomes, “%c\n”. Since this string becomes the format
string for printf and ASCII value of 65 is ‘A’, the same gets printed.
18.) main()
{
int i=10,j=20;
j = i, j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
Answer:
10 10
Explanation:
The Ternary operator (
? : ) is equivalent for if-then-else statement. So the question can be written
as:
if(i,j)
{
if(i,j)
j = i;
else
j =
j;
}
else
j = j;
19.) Which version do you prefer of the following two,
1) printf(“%s”,str);
// or the more curt one
2) printf(str);
Answer :
Prefer the first one.
Explanation:
If the str contains
any format characters like %d then it will result in a subtle bug.
20.) void main()
{
char
ch;
for(ch=0;ch<=127;ch++)
printf(“%c
%d \n“, ch, ch);
}
Answer:
Infinite
Loop
Implementaion
dependent
Explanation:
The
char type may be unsigned by default. If it is signed then ch++ is executed
after ch reaches 127 and rotates back to -128. Thus ch is always smaller than
127.
No comments:
Post a Comment