1. Find output of the following program
#include “stdio.h”
int main()
{
int i;
if (printf(“0”))
i = 4;
else
i = 6;
printf(“%d”, i);
return 0;
}
(a) 4
(b) 6
(c) 04
(d) 06
2. Find output of the following program
#include “stdio.h”
int i;
int main()
{
if (i);
else
printf(“Ëlse”);
return 0;
}
(a) if block is executed.
(b) else block is executed.
(c) It is unpredictable as i is not initialized.
(d) Error: misplaced else
3. Find output of the following program
#include “stdio.h”
int i;
int main()
{
if (i);
else
printf(“Ëlse”);
return 0;
}
(a) if block is executed.
(b) else block is executed.
(c) It is unpredictable as i is not initialized.
(d) Error: misplaced else
Answer
Explanation: Since i is defined globally, it is initialized with default value 0. The Else block is executed as the expression within if evaluates to FALSE. Please note that the empty block is equivalent to a semi-colon(;). So the statements if (i); and if (i) {} are equivalent.
4. Find output of the following program
#include “stdio.h”
int main(){
int a=2;
if(a–,–a,a)
printf(“The Dalai Lama”);
else
printf(“Jim Rogers”);
return 0;
}
(a) The Dalai Lama
(b) Jim Rogers
(c) Run time error
(d) Compilation error: Multiple parameters in if statement
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.
5. Find output of the following program
#include “stdio.h”
int main(){
int a=100;
if(a>10)
printf(“C”);
else if(a>20)
printf(“C++”);
else if(a>30)
printf(“Java”);
return 0;
}
(a) C
(b) C++
(c) Java
(d) PHP
6. Find output of the following program
#include “stdio.h”
int main(){
int x=-1,y=-1;
if(++x=++y)
printf(“Cricket”);
else
printf(“Football”);
return 0;
}
(A) Cricket
(B) Football
(C) lvalue required
(D) Warning: Condition is always true
Answer
As we know ++ is pre increment operator in the above statement. This operator increments the value of any integral variable by one and return that value. After performing pre increments above statement will be:
0=0
It is illegal to assign a constant value to another constant. Left side of = operator must be a container i.e. a variable. So compiler will show an error message: Lvalue required
If you assign any value to variable but you don’t perform any operator or perform operation only using unary operator on the variable the compiler will show a warning message: Variable is assigned a value that is never used
#include “stdio.h”
void main(){
if(!printf(“Hi”))
if(printf(” Hello”));
}
(a) Hi
(b) Hello
(c) It will print nothing
(d) Mukesh Ambani Lakashmi Mittal
Return type of printf function is int. This function return a integral value which is equal to number of characters a printf function will print on console. First of all printf function will: Hi. Since it is printing 2 character so it will return 2. So,
!printf(“Hi”)
= !2
= 0
In c language zero represents false. So if(0) is false so next statement which inside the body of first if statement will not execute.
8. Find output of the following program
#include “stdio.h”
int main(){
if(“ABC”) printf(“Sachin\n”);
if(-1) printf(“Saurabh\n”);
if(.92L) printf(“Param\n”);
if(0) printf(“Seema\n”);
if(‘W’) printf(“Welcome\n”);
return 0;
}
(a) It will print nothing
(b)
Sachin
Saurabh
Param
Welcome
(c)
Sachin
Saurabh
Param
Seema
Welcome
(d)
Saurabh
Param
Welcome
“ABC”: It is string constant and it will always return a non-zero memory address.
0.92L: It is long double constant.
‘W’: It is character constant and its ASCII value is 96
As we know in codeblock language zero represents false and any non-zero number represents true. In this program condition of first, second, third and fifth if statements are true.
9. Find output of the following program
#include “stdio.h”
void main(){
if(0xA)
if(052)
if(‘\xeb’)
if(‘\012’)
printf(“Tom hanks”);
else;
else;
else;
else;
}
(a) Tom hanks
(b) Compilation error: Misplaced else
(c) Compilation error: If without any body
(d) Compilation error: Undefined symbol
oxA: It is hexadecimal integer constant.
052: It octal integer constant.
‘\xeb’: It is hexadecimal character constant.
‘\012’: It is octal character constant.
As we know zero represents false and any non-zero number represents true. All of the above constants return a non-zero value. So all if conditions in the above program are true.
It is possible to write else clause without any body.
10. Find output of the following program
#include “stdio.h”
int main(){
int a=10;
if(printf(“%d”,a>=10)-10)
for(;;)
break;
else;
return 0;
}
(a) It will print nothing
(b) 0
(c) 1
(d) Compilation error: Misplaced else
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 hence if clause will execute which contains an infinite loop but due to break keyword it will come out of loop.