Predict the output - int i=0; for (;i<5;){ i++; System.out.print(i); } A. 1234 B. 12345 C. 01234 D. 13
Question
Predict the output -
int i=0;
for (;i<5;) {
i++;
System.out.print(i);
}
A. 1234
B. 12345
C. 01234
D. 13
Solution
The correct answer is B. 12345
Here's the step by step explanation:
- The variable
i
is initialized to 0. - The
for
loop checks ifi
is less than 5. Sincei
is 0, it is less than 5, so the loop proceeds. - Inside the loop,
i
is incremented by 1 (i++
), makingi
equal to 1. - The value of
i
(which is now 1) is printed. - The loop then goes back to the condition check. Now
i
is 1, which is still less than 5, so the loop proceeds. - Steps 3-5 are repeated until
i
is no longer less than 5. This happens wheni
is 5. - So, the loop prints the numbers 1, 2, 3, 4, 5 in order, which corresponds to option B. 12345.
Similar Questions
Predict the output of the following C Programming Snippet:#include <stdio.h>int main() { int a = 8, b = 6; printf("%d %d", a & b, a | b); return 0;}
The output of the code will be ____. int i=5; printf(“%d %d %d”, i, i++, ++i); ans. 7 6 5 7 6 6 5 5 7 7 6 7
find the output of given code.#include <stdio.h>int main(){int a,b=110,c=20;printf("%d",a=b-=c*=5);return 0;}-1045010110
Write a program to print the following output: [3] 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
What will be the output of the following C code?#include <stdio.h>int main() { int i; for (i = 0; i < 5; i++); printf("%d\n", i); return 0;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.