Thursday, March 1, 2012

if-else set 2

1.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=10;
if(printf("%d",a>=10)-10)
for(;;)
break;
else;
}
Choose all that apply:
(A) It will print nothing
(B) 0
(C) 1
(D) Compilation error: Misplaced else
(E) Infinite loop
Explanation:C
Return type of printf function is int. This function return a integral value which is equal to number of charcters printf function will print on console.
Operator >= will return 1 if both operands are either equal or first operand is grater than second operand. So a>=10 will return 1 since a is equal to 10.Thus printf function will print 1. Since this function is printing only one character so it will also return 1. So, printf("%d",a>=10) - 10
= 1 - 10
= -9
Since -9 is non-zero number so if(-9) is true condition and if clause will execute.
 
2.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=5,b=10;
if(++a||++b)
printf("%d %d",a,b);
else
printf("John Terry");
}
Choose all that apply:
(A) 5 10
(B) 6 11
(C) 6 10
(D) 5 11
(E) John Terry
Explanation:c
Consider the following expression:
++a || ++b
In this expression || is Logical OR operator. Two important properties of this operator are:
Property 1:
(Expression1) || (Expression2)
|| operator returns 0 if and only if both expressions return a zero otherwise it || operator returns 1.
Property 2:
To optimize the execution time there is rule, Expression2 will only evaluate if and only if Expression1 return zero.
In this program initial value of a is 5. So ++a will be 6. Since ++a is returning a non-zero so ++b will not execute and if condition will be true and if clause will be executed.
 
3.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
static int i;
for(;;)
if(i+++"The Matrix")
printf("Memento");
else
break;
}
Choose all that apply:
(A) It will print Memento at one time
(B) It will print Memento at three times
(C) It will print Memento at 65535 times
(D) It will print Memento at infinite times
(E) Compilation error: Unknown operator +++
Explanation:C
think urself
 
4.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int x=1;
if(x--)
printf("The Godfather");
--x;
else
printf("%d",x);
}
Choose all that apply:
(A) The Godfather
(B) 1
(C) 0
(D) Compilation error
(E) None of the above
Explanation:d
If you are not using { and } in if clause then you can write only one statement. Otherwise it will cause of compilation error: Misplace else
 
5.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
if('\0');
else if(NULL)
printf("cquestionbank");
else;
}
Choose all that apply:
(A) cquestionbank
(B) It will print nothing
(C) Warning: Condition is always true
(D) Warning: Unreachable code
(E) Compilation error: if statement without any body
Explanation:b,d
‘\0’ is null character constant. Its ASCII value is zero. if(0) means false so program control will check it else if clause.
NULL is macro constant which has been defined in stdio.h which also returns zero.
 
6.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=5,b=10;
clrscr();
if(a<++a||b<++b)
printf("%d %d",a,b);
else
printf("John Terry");
}
Choose all that apply:
(A) 5 10
(B) 6 11
(C) 6 10
(D) Compilation error
(E) John Terry
Explanation:e
Consider the following expression:
a<++a||b<++b
In the above expression || is logical OR operator. It divides any expression in the sub expressions. In this way we have two sub expressions:
(1) a<++a
(2) b<++b
In the expression: a< ++a
There are two operators. There precedence and associate are:
Precedence
Operator
Associate
1
++
Right to left
2
<
Left to right
From table it is clear first ++ operator will perform the operation then < operator.
One important property of pre-increment (++) operator is: In any expression first pre-increment increments the value of variable then it assigns same final value of the variable to all that variables. So in the expression: a < ++a
Initial value of variable a is 5.
Step 1: Increment the value of variable a in whole expression. Final value of a is 6.
Step 2: Now start assigning value to all a in the expression. After assigning 6 expression will be:
6 < 6
Since condition is false .So second expression i.e. b<++b will be evaluated. Again 11 < 11 is false. So || will operator will return zero and else clause will execute.
 
7.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int x=1,y=2;
if(--x && --y)
printf("x=%d y=%d",x,y);
else
printf("%d %d",x,y);
}
Choose all that apply:
(A) 1 2
(B) x=1 y=2
(C) 0 2
(D) x=0 y=1
(E) 0 1
Explanation:c
Consider the following expression:
--x && --y
In this expression && is Logical AND operator. Two important properties of this operator are:
Property 1:
(Expression1) && (Expression2)
&& operator returns 1 if and only if both expressions return a non-zero value other wise it && operator returns 0.
Property 2:
To optimize the execution time there is rule, Expression2 will only evaluate if and only if Expression1 return a non-zero value.
In this program initial value of x is 1. So –x will be zero. Since -–x is returning zero so -–y will not execute and if condition will be false. Hence else part will be executed.
 
8.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
signed int a=-1;
unsigned int b=-1u;
if(a==b)
printf("The Lord of the Rings");
else
printf("American Beauty");
}
Choose all that apply:
(A) The Lord of the Rings
(B) American Beauty
(C) Compilation error: Cannot compare signed number with unsigned number
(D) Compilation error: Undefined symbol -1u
(E) Warning: Illegal operation
Explanation:a
 
9.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
char c=256;
char *ptr="Leon";
if(c==0)
while(!c)
if(*ptr++)
printf("+%u",c);
else
break;
}
Choose all that apply:
(A) +256+256+256+256
(B)  0000
(C)  +0+0+0+0
(D)  It will print +256 at infinite times
(E)  Compilation error
Explanation:c
In the above program c is signed (default) char variable. Range of signed char variable in Turbo c is from -128 to 127. But we are assigning 256 which is beyond the range of variable c. Hence variable c will store corresponding cyclic value according to following diagram:
Since 256 is positive number move from zero in clock wise direction. You will get final value of c is zero.
if(c==0)
It is true since value of c is zero.
Negation operator i.e. ! is always return either zero or one according to following rule:
!0 = 1
!(Non-zero number) = 0
So,
!c = !0 =1
As we know in c zero represents false and any non-zero number represents true. So
while(!c) i.e. while(1) is always true.
In the above program prt is character pointer. It is pointing to first character of string “Leon” according to following diagram:
In the above figure value in circle represents ASCII value of corresponding character.
Initially *ptr means ‘L’. So *ptr will return ASCII value of character constant ‘L’ i.e. 76
if(*ptr++) is equivalent to : if(‘L’) is equivalent to: if(76) . It is true so in first iteration it will print +0. Due to ++ operation in second iteration ptr will point to character constant ‘e’ and so on. When ptr will point ‘\0’ i.e. null character .It will return its ASCII value i.e. 0. So if(0) is false. Hence else part will execute.
 
10.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=2;
if(a--,--a,a)
printf("The Dalai Lama");
else
printf("Jim Rogers");
}
Choose all that apply:
(A) The Dalai Lama
(B) Jim Rogers
(C) Run time error
(D) Compilation error: Multiple parameters in if statement
(E) None of the above
Explanation:b
Consider the following expression:
a-- , --a , a
In c comma is behaves as separator as well as operator.In the above expression comma is behaving as operator.Comma operator enjoy lest precedence in precedence table andits associatively is left to right. So first of all left most comma operator will perform operation then right most comma will operator in the above expression.
After performing a-- : a will be 2
After performing --a : a will be 0
a=0
As we know in c zero represents false and any non-zero number represents true. Hence else part will execute.

No comments:

Post a Comment