TCS NQT - cap ability

Test Number 10/11

Q: What will be the output of following code :

#include
int main()
{
if(sizeof(0))
printf(“Hai”);
else
printf(“Bye”);
return 0;
}
A. 2
B. Bye
C. Runtime error
D. Hai
Solution: sizeof(0) is 4 hence it is true, Hai will be printed as the message.
Q: What will be the output of following code :

#include
int main()
{
if(sizeof(‘’))
printf(“inside if block”);
else
printf(“inside else block”);
return 0;
}
A. inside if block
B. inside else block
C. Null Pointer Exception
D. None of these
Solution: A is correct as sizeof(‘’) will return 1, hence if condition is true.
Q:  What will be the output of following code :

#include
int main()
{
int i = 65;
switch(i)
{
case 65:
printf(“Integer 65”);
break;
case ‘A’:
printf(“Char 65”);
break;
default:
printf(“Bye”);
}
return 0;
}
A. Integer 65
B. Char 65
C. Bye
D. Error : Duplicate Values
Solution: D is correct as case ‘A’ and case 65 both are same thing and cases can not have duplicate values.
Q: What will be the output of following code :

#include
int main()
{

switch(2/3)
{
case 1:
printf(“case 1 executed “);

case 2:
printf(“case 2 executed “);
break;
default:
printf(“Default block executed”);
}
return 0;
}
A. case 1 executed
B. case 2 executed
C. Default block executed
D. Error : Switch statements can not hold
Solution: C is correct as none of the above cases follow the condition so default block will be executed.
Q: What will be the output of following code :

#include
int main()
{
int i = 1;
switch(i)
{
case i:
printf(“case 1 executed”);
break;
case i + 1;
printf(“case 2 executed”);
break;
default:
printf(“default block executed”);
break;
}
return 0;
}
A. case 1 executed
B. case 2 executed
C. default block executed
D. Error : i is not usable
Solution: Above code will produce error: the value of ‘i’ is not usable in a constant expression.
Q: What will be the output of following code :

#include
int main(){
while(printf(“%d”, 5) < 4)
printf(“Loop “);
return 0;
}
A. Loop Loop Loop Loop Loop
B. Infinite loop
C. 5Loop 5Loop 5Loop 5Loop 5Loop
D. None of these
Solution: Above code will result in an infinite loop as condition will always be true.
Q: What will be the output of following code :

#include
#define NULL 0
int main()
{
while (NULL == 0)
{
printf(“Loop”);
break;
}
return 0;
}
A. Loop
B. Null
C. 0
D. Error : Null can not be compared
Solution: Null is equivalent to 0. And hence, Option A is correct.
Q: What will be the output of following code :

#include
int main(){
float ft = 7.5;
while(ft)
{
printf(“Loop”);
ft = ft – .5;
if(ft == 5.0f)
break;
}
return 0;
}
A. LoopLoopLoopLoopLoop
B. Loop
C. No output
D. None of these
Solution: while loop will run for 5 times, i.e for the values 7.5, 7.0, 6.5, 6.0, 5,5. Hence, printing “Loop” for 5 times.
Q: What will be the output of following code :

#include
int main()
{
while(!!7)
printf(“Hai”);
return 0;
}
A. Hai
B. HaiHai
C. Infinite loop
D. None of these
Solution: Above code will result in infinite loop.
Q: What will be the output of following code :

#include
int main(){
while(!printf(“awesome”));
return 0;
}
A. awesome
B. Error
C. Infinite loop
D. None of these
Solution: Above code will just print “awesome” as the message.

You Have Score    /10