0
0
Javaprogramming~10 mins

Nested for loop in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 3 using a for loop.

Java
for (int i = 1; i [1] 4; i++) {
    System.out.println(i);
}
Drag options to blanks, or click blank then click option'
A>
B>=
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= will not run the loop as expected.
Using <= 3 is correct but not the option here.
2fill in blank
medium

Complete the code to print a 3x3 grid of stars using nested for loops.

Java
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j [1] 4; j++) {
        System.out.print("*");
    }
    System.out.println();
}
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= 3 would also work but is not the correct answer here.
Using > or >= will cause the loop not to run properly.
3fill in blank
hard

Fix the error in the nested loop to print numbers 1 to 3 in each row.

Java
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j [1] 3; j++) {
        System.out.print(j + " ");
    }
    System.out.println();
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using < 3 will stop before printing 3.
Using > or >= will cause the loop not to run correctly.
4fill in blank
hard

Fill both blanks to create a nested loop that prints a 4x4 grid of numbers.

Java
for (int i = 1; i [1] 5; i++) {
    for (int j = 1; j [2] 5; j++) {
        System.out.print(j + " ");
    }
    System.out.println();
}
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= 5 would run 5 times, printing numbers up to 5.
Using > or >= will cause the loops not to run correctly.
5fill in blank
hard

Fill all three blanks to create a nested loop that prints a triangle of stars.

Java
for (int i = 1; i [1] 5; i++) {
    for (int j = 1; j [2] i; j[3]) {
        System.out.print("*");
    }
    System.out.println();
}
Drag options to blanks, or click blank then click option'
A<=
B<
C++
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <= will print one less star per row.
Using -- will cause an infinite loop.