1.
#include <stdio.h>
int main()
{
int x = 10;
int y = 20;
x += y += 10;
printf (" %d %d", x, y);
return 0;
}
Correct Answer
B. 40 30
Explanation
The code starts by initializing x as 10 and y as 20. Then, it performs the operation x += y += 10. This is equivalent to y += 10; x += y; which means y is increased by 10 and then x is increased by the new value of y. Therefore, y becomes 30 and x becomes 40. The printf statement then prints the values of x and y, resulting in "40 30".
2.
#include<stdio.h>
int main(void)
{
int a = 1;
int b = 0;
b = a++ + a++;
printf("%d %d",a,b);
return 0;
}
Correct Answer
B. Compiler Dependent
Explanation
The correct answer is "Compiler Dependent". The output of this program can vary depending on the compiler being used. In some compilers, the order of evaluation of the operands in the expression "a++ + a++" is not defined. Therefore, the result can be different in different compilers.
3.
#include <stdio.h>
int main()
{
int x = 10;
int y = (x++, x++, x++);
printf("%d %d\n", x, y);
return 0;
}
Correct Answer
A. 13 12
Explanation
In this code, the comma operator is used to separate multiple expressions in the initialization of variable y. The comma operator evaluates each expression from left to right and returns the value of the last expression. In this case, the expressions x++, x++, x++ are evaluated sequentially. The value of x is incremented by 1 three times, resulting in x being 13. The value of y is the value of the last expression, which is the value of the third x++, before it was incremented, which is 12. Therefore, the output is 13 12.
4.
#include <stdio.h>
int main()
{
printf("%d", 1 << 2 + 3 << 4);
return 0;
}
Correct Answer
C. 512
Explanation
The expression 1
5.
#include
int main()
{
if(printf("happ"))
printf("y");
else
printf("iness");
return 0;
}
Correct Answer
B. Happy
Explanation
The code snippet is using the printf() function to print "happ". Since the printf() function returns the number of characters printed, which is 4 in this case, the if statement evaluates to true. Therefore, the code inside the if statement is executed, which prints "y". Hence, the output will be "happy".
6.
Int main()
{
int q= 3;
int u= 3;
int o= 5;
int n= ++q + u*o + u++;
switch(n)
{
case 22: printf("word");
break;
case 21: printf("game");
break;
case 70: printf("puzzle");
}
return 0;
}
Correct Answer
A. Word
Explanation
The value of n is calculated as (++q + u*o + u++), which is equivalent to (4 + 3*5 + 3). This evaluates to 22. Since the value of n matches the case 22, the statement "word" will be printed as the output.
7.
#include
int main()
{
int i;
for (i=0; i<20; i++)
{
switch(i)
{
case 0:
i += 5;
case 1:
i += 2;
case 5:
i += 5;
case 17:
i += 1;
default:
i += 4;
}
printf("%d ", i);
}
return 0;
}
Correct Answer
C. 17 22
Explanation
The correct answer is 17 22 because the switch statement is used to check the value of the variable "i" and perform different actions based on the value. In this code, when "i" is 0, it will add 5 to "i" and then fall through to the next case, adding 2 to "i". Similarly, when "i" is 5, it will add 5 to "i" and fall through to the next case, adding 1 to "i". When "i" is 17, it will add 1 to "i" and fall through to the default case, adding 4 to "i". Therefore, the final values of "i" printed are 17 and 22.
8.
#include
int main()
{
int i;
if (printf("0"))
i = 3;
else
i = 5;
printf("%d", i);
return 0;
}
Correct Answer
C. 03
Explanation
The code starts with an if statement that checks the return value of the printf function. Since the string "0" is successfully printed, the printf function returns a non-zero value, which is considered true in C. Therefore, the code inside the if block is executed and the variable i is assigned a value of 3. After that, the printf function is called again to print the value of i, which is 3. However, the format specifier in the printf function is "%d", which expects an integer argument. Since the string "03" is not a valid integer, it is printed as it is. Therefore, the output of the program is "03".
9.
Include
int main()
{
int a = 10, b = 20, c = 30;
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
}
Correct Answer
B. False
Explanation
The expression "c > b > a" is not evaluated as expected. In C, the relational operators are evaluated from left to right. So, the expression "c > b" is evaluated first, which results in the value 0 (False), since c is not greater than b. Then, the value 0 (False) is compared to a, which also results in 0 (False). Therefore, the if statement is false and the program will print "FALSE".
10.
#include<stdio.h>
int main()
{
int n;
for (n = 9; n!=0; n--)
printf("n = %d ", n--);
return 0;
}
Correct Answer
C. Infinite Loop
Explanation
The code starts with the value of n as 9. In the for loop, the condition is n!=0, so as long as n is not equal to 0, the loop will continue. Inside the loop, the printf statement prints the value of n and then n is decremented twice (n--). This means that the value of n is decremented by 2 in each iteration. Since n starts at 9, it will become 7, then 5, then 3. At this point, the condition n!=0 is still true, so the loop will continue. However, n will never become 0 because it is being decremented by 2 in each iteration. Therefore, the loop will continue infinitely, resulting in an infinite loop.
11.
#include
int main()
{
int y = 0;
int x = (~y == 1);
printf("%d", x);
return 0;
}
Correct Answer
A. 0
Explanation
The code snippet declares two integer variables, y and x. The expression (~y == 1) is assigned to x. The ~ operator is the bitwise complement operator, which flips all the bits in the operand. Since y is initialized to 0, ~y will result in all bits being flipped, which is a 1 in binary representation. However, the comparison (~y == 1) evaluates to false because it checks if the bitwise complement of y is equal to 1. Therefore, x is assigned the value of 0. Finally, the value of x is printed, which is 0.
12.
What is the output of this C code?
#include <stdio.h>
int main()
{
printf("before continue ");
break;
printf("after continue\n");
return 0;
}
Correct Answer
D. Compile-time error
Explanation
The given code will result in a compile-time error. This is because the "break" statement is used outside of a loop or switch statement, where it is expected to be used. The "break" statement is used to exit a loop or switch statement prematurely, but since there is no loop or switch statement in the code, it is not valid and will result in a compile-time error.
13.
#include <stdio.h>
int main()
{
int i = 0;
do
{
printf("C-Quiz ");
i = i++;
}
while (i < 5);
return 0;
}
Correct Answer
C. Undefined Behavior
Explanation
The code snippet contains undefined behavior. In the do-while loop, the expression "i = i++" is used. This expression results in undefined behavior because it attempts to modify the value of "i" twice in the same sequence point. The order of evaluation of the operands in the expression is not defined, leading to unpredictable results. Therefore, the output of the program cannot be determined and can vary depending on the compiler and platform.
14.
What is the output of this C code?
#include<stdio.h>
int main()
{
int a=0,b=0;
if(a>0)
if(b>0)
printf("Hello");
else
printf("World");
return 0;
}
Correct Answer
C. No output
Explanation
The output of this C code is "No output". This is because the condition "a>0" in the first if statement is false, so the code inside the if statement is not executed. Therefore, neither "Hello" nor "World" is printed.
15.
#include <stdio.h>
void main()
{
int k;
for (k = -3; k < -5; k++)
printf("Hello");
}
Correct Answer
D. Nothing
Explanation
The given code snippet contains a for loop that initializes the variable "k" with the value -3. The loop condition checks if "k" is less than -5, which is false in this case. Therefore, the loop is never executed, and the printf statement inside the loop is never executed. As a result, the program does not output anything.