1. Find output of the following program
#include “stdio.h”
int main()
{
int x, y = 5, z = 5;
x = y == z;
printf(“%d”, x);

getchar();
return 0;
}

(a) Compiler Error
(b) 0
(c) 1
(d) 8
Answer

(c) The statement x = y==z. The operator == is executed before = because precedence of comparison operators (<=, >= and ==) is higher than assignment operator =.
The result of a comparison operator is either 0 or 1 based on the comparison result. Since y is equal to z, value of the expression y == z becomes 1 and the value is assigned to x via the assignment operator.

2. Find output of the following program
#include

int main()
{
int a = (7, 8, 9);

printf(“%d”, a);

return 0;
}
(a) 7
(b) 9
(c) Garbage Value
(d) Compile time error
Answer

(B) The bracket operator has higher precedence than assignment operator. The expression within bracket operator is evaluated from left to right. But it is always the result of the last expression which gets assigned.

3. Find output of the following program
#include “stdio.h”
int main()
{
printf(“%d”, sizeof(printf(“SCABhopal”)));
return 0;
}

(a) SCABhopal4
(b) 4SCABhopal
(c) SCABhopal9
(d) 4

Answer
(D)An expression doesn’t get evaluated inside sizeof operator. SCABhopal will not be printed. Printf returns the number of characters to be printed i.e. 9 which is an integer value. sizeof operator returns sizeof(int).

4Find output of the following program
#include “stdio.h”
int main()
{
int a = 80, b = 50, c = 60;
if (c > b > a)
printf(“TRUE”);
else
printf(“FALSE”);
return 0;
}
(a) TRUE
(b) FALSE
(c) Compiler error
(d) Blank Screen

Answer
(B) Let us consider the condition inside the if statement. Since there are two greater than (>) operators in expression “c > b > a”, associativity of > is considered. Associativity of > is left to right. So, expression c > b > a is evaluated as ( (c > b) > a ) which is false.

5Find output of the following program
#include “stdio.h”
int main()
{
int x = 1;
int y = (x++, x++, x++);
printf(“%d %d\n”, x, y);
return 0;
}

(a) 4 3
(b) 4 4
(c) 1 1
(d) 3 3
Answer

(A) All expressions are executed from left to right and the value of right most expression is returned by the comma operator.

6. Find output of the following program
#include “stdio.h”
int main()
{
int B = 0;
int A = (~B == 1);
printf(“%d”, A);
return 0;
}

(a) 0
(b) 1
(c) Compiler error
(d) Run time error.
Answer

(A) ~ is a bitwise not operator. So the value of ~0 would be all 1s in binary representation which means decimal value of ~0 is not 1. Therefore the result of comparison operator becomes 0.

7. Find output of the following program
#include “stdio.h”
int main()
{
int i = 6, j = 8, k = 12;
printf(“%d “, sizeof(k /= i + j));
printf(“%d”, k);
return 0;
}

(a) 4 12
(b) 4 2
(c) 2 1
(d) compile –time error
Answer

(A) The main theme of the program lies here: sizeof(k /= i + j). An expression doesn’t get evaluated inside sizeof operator. sizeof operator returns sizeof(int) because the result of expression will be an integer. As the expression is not evaluated, value of k will not be changed.

8. Find output of the following program
#include “stdio.h”

int main()
{
int a = 1;
int b = 1;
int c = a || –b;
int d = a– && –b;
printf(“a = %d, b = %d, c = %d, d = %d”, a, b, c, d);
return 0;
}
(a) a = 0, b = 1, c = 1, d = 0
(b) a = 0, b = 0, c = 1, d = 0
(c) a = 1, b = 1, c = 1, d = 1
(d) a = 0, b = 0, c = 0, d = 0

Answer
(B)

9. Find output of the following program
#include “stdio.h”
int main()
{
printf(“%d”, 1 << 2 + 3 << 4);
return 0;
}
(a) 512
(b) 52
(c) 112
(d) 0

Answer
(A) The logic behind the program is the precedence and associativity of the operators. The addition(+) operator has higher precedence than shift(<<) operator. So, the expression boils down to 1<< (2 + 3) << 4 which in turn reduces to (1 << 5) << 4 as the shift operator has left-to-right associativity.

10. Find output of the following program
# include “stdio.h”
int main()
{
int A = 10;
int B = 20;
A += B += 10;
printf (” %d %d”, A, B);
return 0;
}
(a) 40 20
(b) 40 30
(c) 30 30
(d) 30 40

Answer
(B) The statement “A += B += 10″. Since there are two += operators in the statement, associativity comes into the picture. Associativity of compound assignment operators is right to left, so the expression is evaluated as A += (B += 10).
Operators in C