### Find Output Of Program

Question 1 :

What will be the output of the program?

``````#include<stdio.h>
int main()
{
int i=0;
for(; i<=5; i++);
printf("%d", i);
return 0;
}``````

A). 0, 1, 2, 3, 4, 5
B). 5
C). 1, 2, 3, 4
D). 6

Explanation :

Step 1: int i = 0; here variable i is an integer type and initialized to '0'.
Step 2: for(; i<=5; i++); variable i=0 is already assigned in previous step. The semi-colon at the end of this for loop tells, "there is no more statement is inside the loop".

Loop 1: here i=0, the condition in for(; 0<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 2: here i=1, the condition in for(; 1<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 3: here i=2, the condition in for(; 2<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 4: here i=3, the condition in for(; 3<=5; i++) loop satisfies and then i is increemented by '1'(one)
Loop 5: here i=4, the condition in for(; 4<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 6: here i=5, the condition in for(; 5<=5; i++) loop satisfies and then i is incremented by '1'(one)
Loop 7: here i=6, the condition in for(; 6<=5; i++) loop fails and then i is not incremented.

Step 3: printf("%d", i); here the value of i is 6. Hence the output is '6'.

Question 2 :

What will be the output of the program?

``````#include<stdio.h>
int main()
{
char str[]="C-program";
int a = 5;
printf(a >10?"Ps\n":"%s\n", str);
return 0;
}``````

A). C-program
B). Ps
C). Error
D). None of above

Explanation :

Step 1: char str[]="C-program"; here variable str contains "C-program".
Step 2: int a = 5; here variable a contains "5".
Step 3: printf(a >10?"Ps\n":"%s\n", str); this statement can be written as

``````if(a > 10)
{
printf("Ps\n");
}
else
{
printf("%s\n", str);
}``````

Here we are checking a > 10 means 5 > 10. Hence this condition will be failed. So it prints variable str.
Hence the output is "C-program".

Question 3 :

What will be the output of the program?

``````#include<stdio.h>
int main()
{
int a = 500, b = 100, c;
if(!a >= 400)
b = 300;
c = 200;
printf("b = %d c = %d\n", b, c);
return 0;
}``````

A). b = 300 c = 200
B). b = 100 c = garbage
C). b = 300 c = garbage
D). b = 100 c = 200

Explanation :

Initially variables a = 500, b = 100 and c is not assigned.
Step 1: if(!a >= 400)
Step 2: if(!500 >= 400)
Step 3: if(0 >= 400)
Step 4: if(FALSE) Hence the if condition is failed.
Step 5: So, variable c is assigned to a value '200'.
Step 6: printf("b = %d c = %d\n", b, c); It prints value of b and c.
Hence the output is "b = 100 c = 200"

Question 4 :

What will be the output of the program?

``````#include<stdio.h>
int main()
{
unsigned int i = 65535; /* Assume 2 byte integer*/
while(i++ != 0)
printf("%d",++i);
printf("\n");
return 0;
}``````

A). Infinite loop
B). 0 1 2 ... 65535
C). 0 1 2 ... 32767 - 32766 -32765 -1 0
D). No output

Explanation :

Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.

Step 1:unsigned int i = 65535;
Step 2:
Loop 1: while(i++ != 0) this statement becomes while(65535 != 0). Hence the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '1'(variable 'i' is already increemented by '1' in while statement and now increemented by '1' in printf statement) Loop 2: while(i++ != 0) this statement becomes while(1 != 0). Hence the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '3'(variable 'i' is already increemented by '1' in while statement and now increemented by '1' in printf statement)
....

The while loop will never stops executing, because variable i will never become '0'(zero). Hence it is an 'Infinite loop'.

Question 5 :

What will be the output of the program?

``````#include<stdio.h>
int main()
{
int x = 3;
float y = 3.0;
if(x == y)
printf("x and y are equal");
else
printf("x and y are not equal");
return 0;
}``````

A). x and y are equal
B). x and y are not equal
C). Unpredictable
D). No output

Explanation :

Step 1: int x = 3; here variable x is an integer type and initialized to '3'.
Step 2: float y = 3.0; here variable y is an float type and initialized to '3.0'
Step 3: if(x == y) here we are comparing if(3 == 3.0) hence this condition is satisfied.
Hence it prints "x and y are equal".

Question 6 :

What will be the output of the program, if a short int is 2 bytes wide?

``````#include<stdio.h>
int main()
{
short int i = 0;
for(i<=5 && i>=-1; ++i; i>0)
printf("%u,", i);
return 0;
}``````

A). 1 ... 65535
B). Expression syntax error C. No output D. 0, 1, 2, 3, 4, 5
C). No output
D). 0, 1, 2, 3, 4, 5

Explanation :

for(i<=5 && i>=-1; ++i; i>0) so expression i<=5 && i>=-1 initializes for loop. expression ++i is the loop condition. expression i>0 is the increment expression.
In for( i <= 5 && i >= -1; ++i; i>0) expression i<=5 && i>=-1 evaluates to one.
Loop condition always get evaluated to true. Also at this point it increases i by one.
An increment_expression i>0 has no effect on value of i.so for loop get executed till the limit of integer (ie. 65535)

Question 7 :

What will be the output of the program?

``````#include<stdio.h>
int main()
{
char ch;
if(ch = printf(""))
printf("It matters\n");
else
printf("It doesn't matters\n");
return 0;
}``````

A). It matters
B). It doesn't matters
C). matters
D). No output

Explanation :

printf() returns the number of charecters printed on the console.

Step 1: if(ch = printf("")) here printf() does not print anything, so it returns '0'(zero).
Step 2: if(ch = 0) here variable ch has the value '0'(zero).
Step 3: if(0) Hence the if condition is not satisfied. So it prints the else statements.
Hence the output is "It doesn't matters".

Note: Compiler shows a warning "possibly incorrect assinment".

Question 8 :

What will be the output of the program?

``````#include<stdio.h>
int main()
{
unsigned int i = 65536; /* Assume 2 byte integer*/
while(i != 0)
printf("%d",++i);
printf("\n");
return 0;
}``````

A). Infinite loop
B). 0 1 2 ... 65535
C). 0 1 2 ... 32767 - 32766 -32765 -1 0
D). No output

Explanation :

Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.
Step 1:unsigned int i = 65536; here variable i becomes '0'(zero). because unsigned int varies from 0 to 65535.
Step 2: while(i != 0) this statement becomes while(0 != 0). Hence the while(FALSE) condition is not satisfied. So, the inside the statements of while loop will not get executed.
Hence there is no output.

Note: Don't forget that the size of int should be 2 bytes. If you run the above program in GCC it may run infinite loop, because in Linux platform the size of the integer is 4 bytes.

Question 9 :

What will be the output of the program?

``````#include<stdio.h>
int main()
{
float a = 0.7;
if(0.7 > a)
printf("Hi\n");
else
printf("Hello\n");
return 0;
}``````

A). Hi
B). Hello
C). Hi Hello
D). None of above

Explanation :

if(0.7 > a) here a is a float variable and 0.7 is a double constant. The double constant 0.7 is greater than the float variable a. Hence the if condition is satisfied and it prints 'Hi'
Example:

``````#include<stdio.h>
int main()
{
float a=0.7;
printf("%.10f %.10f\n",0.7, a);
return 0;
}``````

Output:
0.7000000000 0.6999999881

Question 10 :

What will be the output of the program?

``````#include<stdio.h>
int main()
{
int a=0, b=1, c=3;
*((a) ? &b : &a) = a ? b : c;
printf("%d, %d, %d\n", a, b, c);
return 0;
}``````

A). 0, 1, 3
B). 1, 2, 3
C). 3, 1, 3
D). 1, 3, 1

Explanation :

Step 1: int a=0, b=1, c=3; here variable a, b, and c are declared as integer type and initialized to 0, 1, 3 respectively.
Step 2: *((a) ? &b : &a) = a ? b : c; The right side of the expression(a?b:c) becomes (0?1:3). Hence it return the value '3'.
The left side of the expression *((a) ? &b : &a) becomes *((0) ? &b : &a). Hence this contains the address of the variable a *(&a).
Step 3: *((a) ? &b : &a) = a ? b : c; Finally this statement becomes *(&a)=3. Hence the variable a has the value '3'.
Step 4: printf("%d, %d, %d\n", a, b, c); It prints "3, 1, 3".

Question 11 :

What will be the output of the program?

``````#include<stdio.h>
int main()
{
int k, num = 30;
k = (num < 10) ? 100 : 200;
printf("%d\n", num);
return 0;
}``````

A). 200
B). 30
C). 100
D). 500

Question 12 :

What will be the output of the program?

``````
#include
int main()
{
int a = 300, b, c;
if(a >= 400)
b = 300;
c = 200;
printf("%d, %d, %d\n", a, b, c);
return 0;
}``````

A). 300, 300, 200
B). Garbage, 300, 200
C). 300, Garbage, 200
D). 300, 300, Garbage

Explanation :

Step 1: int a = 300, b, c; here variable a is initialized to '300', variable b and c are declared, but not initialized.
Step 2: if(a >= 400) means if(300 >= 400). Hence this condition will be failed.
Step 3: c = 200; here variable c is initialized to '200'.
Step 4: printf("%d, %d, %d\n", a, b, c); It prints "300, garbage value, 200". because variable b is not initialized.

Question 13 :

What will be the output of the program?

``````#include
int main()
{
int x=1, y=1;
for(; y; printf("%d %d\n", x, y))
{
y = x++ <= 5;
}
printf("\n");
return 0;
}``````

A). 2 1
3 1
4 1
5 1
6 1
7 0
B). 2 1
3 1
4 1
5 1
6 1

C). 2 1
3 1
4 1
5 1
D). 2 2
3 3
4 4
5 5

Question 14 :

What will be the output of the program?

``````#include
int main()
{
int i = 5;
while(i-- >= 0)
printf("%d,", i);
i = 5;
printf("\n");
while(i-- >= 0)
printf("%i,", i);
while(i-- >= 0)
printf("%d,", i);
return 0;
}``````

A).
4, 3, 2, 1, 0, -1
4, 3, 2, 1, 0, -1
B).
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
C). Error
D). 5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0

Explanation :

Step 1: Initially the value of variable i is '5'.
Loop 1: while(i-- >= 0) here i = 5, this statement becomes while(5-- >= 0) Hence the while condition is satisfied and it prints '4'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 2: while(i-- >= 0) here i = 4, this statement becomes while(4-- >= 0) Hence the while condition is satisfied and it prints '3'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 3: while(i-- >= 0) here i = 3, this statement becomes while(3-- >= 0) Hence the while condition is satisfied and it prints '2'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 4: while(i-- >= 0) here i = 2, this statement becomes while(2-- >= 0) Hence the while condition is satisfied and it prints '1'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 5: while(i-- >= 0) here i = 1, this statement becomes while(1-- >= 0) Hence the while condition is satisfied and it prints '0'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 6: while(i-- >= 0) here i = 0, this statement becomes while(0-- >= 0) Hence the while condition is satisfied and it prints '-1'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 7: while(i-- >= 0) here i = -1, this statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits.
The output of first while loop is 4,3,2,1,0,-1

Step 2: Then the value of variable i is initialized to '5' Then it prints a new line character(\n).
See the above Loop 1 to Loop 7 .
The output of second while loop is 4,3,2,1,0,-1

Step 3: The third while loop, while(i-- >= 0) here i = -1(because the variable 'i' is decremented to '-1' by previous while loop and it never initialized.). This statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits.

Hence the output of the program is
4,3,2,1,0,-1
4,3,2,1,0,-1

Question 15 :

What will be the output of the program?

``````#include
int main()
{
int i=3;
switch(i)
{
case 1:
printf("Hello\n");
case 2:
printf("Hi\n");
case 3:
continue;
default:
printf("Bye\n");
}
return 0;
}``````

A). Error: Misplaced continue
B). Bye
C). No output
D). Hello Hi

Explanation :

The keyword continue cannot be used in switch case. It must be used in for or while or do while loop. If there is any looping statement in switch case then we can use continue.

Question 16 :

What will be the output of the program?

``````#include
int main()
{
int x = 10, y = 20;
if(!(!x) && x)
printf("x = %d\n", x);
else
printf("y = %d\n", y);
return 0;
}``````

A). y =20
B). x = 0
C). x = 10
D). x = 1

Explanation :

The logical not operator takes expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it reverses the value of the expression.

Step 1: if(!(!x) && x)
Step 2: if(!(!10) && 10)
Step 3: if(!(0) && 10)
Step 3: if(1 && 10)
Step 4: if(TRUE) here the if condition is satisfied. Hence it prints x = 10.

Question 17 :

What will be the output of the program?

``````#include
int main()
{
int i=4;
switch(i)
{
default:
printf("This is default\n");
case 1:
printf("This is case 1\n");
break;
case 2:
printf("This is case 2\n");
break;
case 3:
printf("This is case 3\n");
}
return 0;
}``````

A). This is default This is case 1
B). This is case 3 This is default
C). This is case 1 This is case 3
D). This is default Answer & Explanation

Explanation :

In the very begining of switch-case statement default statement is encountered. So, it prints "This is default".
In default statement there is no break; statement is included. So it prints the case 1 statements. "This is case 1".
Then the break; statement is encountered. Hence the program exits from the switch-case block.

Question 18 :

What will be the output of the program?

``````#include
int main()
{
int i = 1;
switch(i)
{
printf("Hello\n");
case 1:
printf("Hi\n");
break;
case 2:
printf("\nBye\n");
break;
}
return 0;
}``````

A). Hello Hi
B). Hello Bye
C). Hi
D). Bye

Explanation :

switch(i) has the variable i it has the value '1'(one).
Then case 1: statements got executed. so, it prints "Hi". The break; statement make the program to be exited from switch-case statement.
switch-case do not execute any statements outside these blocks case and default

Hence the output is "Hi".

Question 19 :

What will be the output of the program?

``````#include
int main()
{
char j=1;
while(j < 5)
{
printf("%d, ", j);
j = j+1;
}
printf("\n");
return 0;
}``````

A). 1 2 3 ... 127
B). 1 2 3 ... 255
C). 1 2 3 ... 127 128 0 1 2 3 ... infinite times
D). 1, 2, 3, 4

Question 20 :

What will be the output of the program?

``````#include
int main()
{
int x, y, z;
x=y=z=1;
z = ++x || ++y && ++z;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}``````

A). x=2, y=1, z=1
B). x=2, y=2, z=1
C). x=2, y=2, z=2
D). x=1, y=2, z=1