NTA UGC NET Dec 2023 Paper-2

Question 45
What is the output of the following program?
#include
int main()
{
int i=3;
while (i–)
{
int i=10;
i–;

printf(“%d”, i);
}
printf(“%d”, i);
}
A
990
B
9990
C
999-1
D
99-1
Question 45 Explanation: 
Initialization:
The outer int i = 3; declares a variable i with value 3.
While loop:
The while (i–) loop runs as long as i is not zero.
Inside the loop:
int i = 10; declares a new variable i with value 10, but this only exists inside the loop’s scope.
i– decrements the inner i to 9.
printf(“%d”, i); prints the inner i, which is 9.
The outer i is decremented by the while condition, moving towards 0.
Second printf:
After the loop, printf(“%d”, i); prints the outer i, which has been decremented to -1 by the loop.
Therefore, the output is 9 (printed three times in the loop) followed by -1, resulting in 99-1.
Key points:
Variable scope: The inner i variable is only accessible within the loop’s block and doesn’t affect the outer i.
Decrement operator: The post-decrement operator (–) first uses the current value of i and then decrements it.
Loop condition: The while (i–) condition both checks i and decrements it, ensuring the loop runs three times.
Correct Answer: D
Question 45 Explanation: 
Initialization:
The outer int i = 3; declares a variable i with value 3.
While loop:
The while (i–) loop runs as long as i is not zero.
Inside the loop:
int i = 10; declares a new variable i with value 10, but this only exists inside the loop’s scope.
i– decrements the inner i to 9.
printf(“%d”, i); prints the inner i, which is 9.
The outer i is decremented by the while condition, moving towards 0.
Second printf:
After the loop, printf(“%d”, i); prints the outer i, which has been decremented to -1 by the loop.
Therefore, the output is 9 (printed three times in the loop) followed by -1, resulting in 99-1.
Key points:
Variable scope: The inner i variable is only accessible within the loop’s block and doesn’t affect the outer i.
Decrement operator: The post-decrement operator (–) first uses the current value of i and then decrements it.
Loop condition: The while (i–) condition both checks i and decrements it, ensuring the loop runs three times.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: <b>Alert: </b>Content selection is disabled!!
NTA UGC NET Dec 2023 Paper-2
March 22, 2024
NTA UGC NET Dec 2023 Paper-2
March 22, 2024
NTA UGC NET Dec 2023 Paper-2
March 22, 2024
NTA UGC NET Dec 2023 Paper-2
March 22, 2024