Conditional and Looping Statements: Answer: Explanation
Conditional and Looping Statements: Answer: Explanation
Conditional and Looping Statements: Answer: Explanation
71.
int num[5];
num[5]=;
The 5 in the first and the second statements denotes ?
72. main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love SWEETS");
else
printf("I hate MEDICINES");
}
Answer:
I hate MEDICINES
Explanation:
For floating point numbers (float, double, long double) the values cannot be
predicted exactly. Depending on the number of bytes, the precession with of the value represented
varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than
long double.
Rule of Thumb:
Never compare or at-least be cautious when using floating point numbers with
relational operators (== , >, <, <=, >=,!= ) .
73. 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).
74. main()
{
int i = 3;
for (;i++=0;) printf(“%d”,i);
}
Answer:
Compiler Error: Lvalue required.
www.Technicalsymposium.com
Explanation:
As we know that increment operators return rvalues and hence it cannot
appear on the left hand side of an assignment operation.
75. 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.
76. 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.
78. #include<conio.h>
main()
{
int x,y=2,z,a;
if(x=y%2) z=2;
a=2;
www.Technicalsymposium.com
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.
79. 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;
80. main()
{
int i = 3;
for (;i++=0;) printf(“%d”,i);
}
Answer:
Compiler Error: Lvalue required.
Explanation:
As we know that increment operators return rvalues and hence it cannot
appear on the left hand side of an assignment operation.
www.Technicalsymposium.com
}
Answer . k=4
82. Find the output for the following C program
int i =10
main()
{
int i =20,n;
for(n=0;n<=i; )
{
int i=10;
i++;
}
printf("%d", i);
}
Answer :. i=20
{
int Y=10;
if( Y++>9 && Y++!=10 && Y++>10)
printf("%d", Y);
else
printf("%d", Y);
}
Answer : 13
www.Technicalsymposium.com
*
***
*****
*******
n times
a) Pass 1, Pass 2
b) Fail 1, Fail 2
c) Pass 1, Fail 2
d) Fail 1, Pass 2
www.Technicalsymposium.com
e) syntax error during compilation
Answer :
91. main()
{
int l, b, con;
l=b=2;
con= (b==l) ? 1 : 0;
printf("%d",con);
}
what will be the value of con?
Answer :
www.Technicalsymposium.com
{
int k=0 i=0j=1;
if(i0)&&(k=2) printf(k);
if(i0|| k=0)
printf(k);
}
Answer : 2 2
main()
{
char ch;
int count=0;
while((ch = getch() != '\n')
{
while (ch == ' ')
ch = getch();
while((ch!=' ') && (ch!='.'))
{
count++;
ch=getch();
}
}
Answer : 11
95. What is the final value of i and how many times loop is
Executed ?
#include<stdio.h>
main()
{
int i,j,k,l,lc=0;
#include<stdio.h>
main()
{
int i;
for (i=1;i<100; i++)
www.Technicalsymposium.com
printf("%d %0x\n",i,i);
}
Answer: i is from 1 to 99 for the first format,
for the second format 1to9, ato f, 10 to 19,1ato1f, 20 to 29, etc
int zap(int n)
{
if(n<=1)then zap=1;
else zap=zap(n-3)+zap(n-1);
}
then the call zap(6) gives the values of zap
[a] 8 [b] 9 [c] 6 [d] 12 [e] 15
Answer : b
OPERATORS:
98. main()
{
int i=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).
99. 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).
100. main()
{
int i=-1;
-i;
printf("i = %d, -i = %d \n",i,-i);
}
Answer:
i = -1, -i = 1
Explanation:
www.Technicalsymposium.com
-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.
101. main()
{
int i=0;
while(+(+i--)!=0)
i- =i++;
printf("%d",i);
}
Answer: -1
Explanation:
Unary + is the only dummy operator in C. So it has no effect on the expression and
now the while loop is, while(i--!=0) which is false and so breaks out of while
loop. The value –1 is printed due to the post-decrement operator.
102. Comment:
int a ,b=7
a=b<4?b<<1:b>4?7>>1:a
Answer.3
107.Comment
(7^2)^3 and 7^2^3. What is the relation between the 2 . ie >,<,=
Answer : (7^2)^3<7^2^3.
www.Technicalsymposium.com
int x=5;
printf("%d %d %d\n",x,x<<2,x>>2);
}
Answer :
Answer :
A)47.0
8)64
C)47
Answer :
{
int a=10;
int b=6;
if(a=3)
b++;
printf("%d %d\n",a,b++);
}
www.Technicalsymposium.com