Question 17386 – NTA UGC NET Dec 2023 Paper-2
March 22, 2024NTA UGC NET Dec 2023 Paper-2
March 22, 2024NTA 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);
}
#include
int main()
{
int i=3;
while (i–)
{
int i=10;
i–;
printf(“%d”, i);
}
printf(“%d”, i);
}
990 | |
9990 | |
999-1 | |
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.
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.
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.
Subscribe
Login
0 Comments