1.
What is the output of this piece of code?
#include <stdio.h>
void main()
{
int i;
for(i=0;i<10;i++)
printf(“%d ”,i);
}
Correct Answer
C. 10
Explanation
The given code is a simple for loop that iterates from 0 to 9. Inside the loop, it prints the value of 'i' followed by a space. After the loop completes, it prints the value of 'i' one more time, which is 10. Therefore, the output of the code is "0 1 2 3 4 5 6 7 8 9 10".
2.
What is the output of this C code?
#include <stdio.h>
int main()
{
int x = 0;
if (x++)
printf("true\n");
else if (x == 1)
printf("false\n");
}
Correct Answer
B. False
Explanation
The output of this C code is "false". This is because the initial value of x is 0. In the if statement, x is incremented using the post-increment operator, but the value of x before the increment is evaluated in the condition. Since the value of x before the increment is 0, the condition evaluates to false and the corresponding printf statement is executed.
3.
What is the output of this C code?
#include <stdio.h>
int main()
{
int n;
for (n = 9; n!=0; n--)
printf("n = %d", n--);
return 0;
}
Correct Answer
C. Infinite
Explanation
The code starts with n = 9 and enters a for loop. In each iteration of the loop, it prints the value of n and then decrements n by 2. However, the decrement operator (--) is used twice in each iteration, once in the printf statement and once in the loop itself. This leads to n being decremented by 2 in each iteration instead of 1. As a result, n will never be equal to 0, causing an infinite loop. Therefore, the output of the code is infinite.
4.
What is the output of this C code?
#include <stdio.h>
int main()
{ int i = 4, j = 7;
do
{ printf("Loop");
}while(++i < --j);
return 0;
}
Correct Answer
B. LoopLoop
Explanation
The code uses a do-while loop to repeatedly print "Loop" as long as the condition (++i < --j) is true. Initially, i is incremented to 5 and j is decremented to 6, so the condition is true. "Loop" is printed once. Then, i is incremented to 6 and j is decremented to 5, still making the condition true. "Loop" is printed again. After this, i is incremented to 7 and j is decremented to 4, making the condition false. The loop terminates and the program ends, resulting in the output "LoopLoop".
5.
How many times is a do while loop guaranteed to loop?
Correct Answer
A. 1
Explanation
A do while loop is guaranteed to loop at least once because the condition is checked at the end of the loop. This means that the loop will always execute the code block once before checking if the condition is true or false. Therefore, even if the condition is false from the beginning, the loop will still run once.
6.
What is the output of this C code?
#include <stdio.h>
int main()
{
do
printf("In while loop ");
while (0);
printf("After loop\n");
}
Correct Answer
B. In while loop After loop
Explanation
The code uses a do-while loop with a condition of 0, which means the loop will execute at least once. Inside the loop, it prints "In while loop ". After the loop, it prints "After loop". Therefore, the output of the code will be "In while loop After loop".
7.
What is the output of this C code?
int main ()
{
int i=0;
for (i=0; i<20; i++)
{ switch (i) { case 0 : i += 5; case 1 : i += 2; case 5 : i += 5;
default : i += 4; break;
}
printf("%d, ", i);
}
}
Correct Answer
D. 16,21
Explanation
The code starts with initializing the variable i to 0. Then, it enters a for loop that iterates until i is less than 20. Inside the loop, there is a switch statement that checks the value of i.
When i is 0, it adds 5 to i and continues to the next case. When i is 1, it adds 2 to i and continues to the next case. When i is 5, it adds 5 to i and continues to the next case.
In the default case, it adds 4 to i and then breaks out of the switch statement. Finally, the value of i is printed using printf.
The output of the code is 16,21 because after the loop iterates 16 times, the value of i becomes 16 and then it adds 4 in the default case before printing it.
8.
What is the output of the given code:
#include <stdio.h>
void main()
{
int i = 0;
while (++i)
{
printf("H");
}
}
Correct Answer
C. H is printed infinite times
Explanation
The code contains an infinite loop. The condition in the while loop is "++i", which means that the value of "i" will be incremented by 1 before each iteration of the loop. Since the condition is always true (non-zero), the loop will continue indefinitely. Inside the loop, the code prints "H" repeatedly, resulting in "H" being printed infinite times.
9.
What is the output of this C code?
#include
int main()
{
int x = 1;
if (x > 0)
printf("inside if\n");
else if (x > 0)
printf("inside elseif\n");
}
Correct Answer
A. Inside if
Explanation
The output of this C code is "inside if". This is because the variable x is equal to 1, which is greater than 0. Therefore, the condition in the if statement is true and the code inside the if block is executed. The else if statement is not evaluated because the condition x > 0 is already satisfied.
10.
What is the output of the given code:
int main()
{
int i;
int arr[5] = {1};
for (i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0;
}
Correct Answer
D. 00000
Explanation
The given code initializes an integer array `arr` with size 5 and assigns the first element as 1 while leaving the rest of the elements uninitialized. The code then enters a for loop that iterates 5 times. Each iteration, it prints the value of `arr[i]`, which in this case will be 1 for the first iteration and garbage values for the rest of the iterations since the rest of the elements in the array are uninitialized. Therefore, the output will be "1 followed by four garbage values".