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
iis initialized to 0. - The
forloop checks ifiis less than 5. Sinceiis 0, it is less than 5, so the loop proceeds. - Inside the loop,
iis incremented by 1 (i++), makingiequal to 1. - The value of
i(which is now 1) is printed. - The loop then goes back to the condition check. Now
iis 1, which is still less than 5, so the loop proceeds. - Steps 3-5 are repeated until
iis no longer less than 5. This happens wheniis 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;}
Predict the output? int arr[] = {10, 20, 30, 40, 50}; for(int i=0; i < arr.length; i++) { System.out.print(" " + arr[i]); }
Determine the output:public class Test{ public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for(int i = 0; i < x.length; i++) System.out.print(y[i] + " "); }}Select one:a.1 2b.0 0c.0 0 0 0d.1 2 3 4
Predict the output of the following code snippet.123456789#include <stdio.h>int main() { int x = 5, y = 3; int *ptr1 = &x, *ptr2 = &y; int a = *ptr1 * *ptr2; printf("%d\n", x); return 0;}
Determine output:public class Test{ public static void main(String[] args){ int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for(int i = 0; i < x.length; i++) System.out.print(y[i] + " "); }}123400001200
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.