Practice Questions — Loop Control and Pattern Printing
← Back to NotesTopic-Specific Questions
Question 1
Easy
What is the output?
for (int i = 1; i <= 5; i++) {
if (i == 3) break;
System.out.print(i + " ");
}break exits the loop when i is 3.
1 2 Question 2
Easy
What is the output?
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
System.out.print(i + " ");
}continue skips the current iteration.
1 2 4 5 Question 3
Easy
What is the output?
for (int i = 10; i >= 1; i--) {
if (i % 2 != 0) continue;
System.out.print(i + " ");
}Odd numbers are skipped by continue.
10 8 6 4 2 Question 4
Easy
How many stars are printed?
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}Row 1: 1 star, Row 2: 2, Row 3: 3, Row 4: 4.
10 stars total (1+2+3+4)Question 5
Medium
What is the output?
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) continue outer;
System.out.print(i + "" + j + " ");
}
}continue outer skips to the next iteration of the outer loop.
11 21 31 Question 6
Medium
What is the output?
outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) break outer;
System.out.print(i + "" + j + " ");
}
}break outer exits both loops at i=1, j=1.
00 01 02 10 Question 7
Medium
What is the output?
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}Each row prints (n-i) spaces then i copies of the row number.
1 2 2 3 3 3 4 4 4 4
Question 8
Medium
What is the output?
int num = 1;
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num++ + " ");
}
System.out.println();
}This is Floyd's triangle. num increments continuously.
1 2 3 4 5 6 Question 9
Hard
What is the output?
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
if (i + j == 5) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}Print * only where i + j equals 5. Otherwise print spaces.
*
*
*
* Question 10
Hard
What is the output?
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}Stars print on the borders (first/last row, first/last column).
* * * * * * * * * * * *
Question 11
Medium
What is the difference between break and continue? When would you use each?
Think about whether you want to stop completely or skip one iteration.
break terminates the entire loop immediately. continue skips only the current iteration and proceeds to the next. Use break when a search is complete or an error requires stopping. Use continue when certain iterations should be skipped (e.g., skipping invalid inputs) while the loop continues processing others.Question 12
Hard
Describe the step-by-step approach to solve any pattern printing problem.
Think about rows, columns, spaces, and the relationship with i and n.
Step 1: Count total rows (determines outer loop from 1 to n). Step 2: For each row, identify what is printed (spaces and characters). Step 3: Express the count of spaces and characters as a formula in terms of row number i and total rows n. Step 4: Write inner loop(s) for spaces and characters. Step 5: Verify by plugging in i=1 and i=n to check the first and last rows match expectations.
Question 13
Easy
What is the output?
for (int i = 1; i <= 5; i++) {
if (i == 4) break;
if (i == 2) continue;
System.out.print(i + " ");
}continue skips 2, break stops at 4.
1 3 Question 14
Medium
What is the output?
int n = 3;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}Inverted right triangle: row 1 has 3 stars, row 2 has 2, row 3 has 1.
* * * * * * Question 15
Medium
What is the output?
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}Each row prints 1 through i.
1 1 2 1 2 3 1 2 3 4 Question 16
Hard
What is the output?
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}Each row prints the row number i, repeated i times.
1 2 2 3 3 3 4 4 4 4 Question 17
Hard
What is the output?
int n = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((char)('A' + j) + " ");
}
System.out.println();
}Character pattern: (char)('A' + 0) = 'A', (char)('A' + 1) = 'B', etc.
A A B A B C A B C D Mixed & Application Questions
Question 1
Easy
Print numbers 1 to 20, but skip multiples of 4 using continue.
Use continue when i % 4 == 0.
for (int i = 1; i <= 20; i++) {
if (i % 4 == 0) continue;
System.out.print(i + " ");
}Question 2
Easy
Print the following right triangle pattern for n = 5 using numbers:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Outer loop for rows, inner loop j from 1 to i.
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}Question 3
Medium
Print an inverted right triangle of stars for n = 5:
* * * * *
* * * *
* * *
* *
*
Row i prints (n - i + 1) stars.
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++) {
System.out.print("* ");
}
System.out.println();
}Question 4
Medium
Print a number pyramid for n = 5:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Each row has (n-i) leading spaces, then numbers 1 to i.
int n = 5;
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}Question 5
Medium
What is the output?
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}i counts down from 5 to 1. Inner loop prints 1 to i.
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Question 6
Medium
Print a hollow rectangle of stars with width 6 and height 4.
Print stars on first/last row and first/last column. Spaces elsewhere.
int rows = 4, cols = 6;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
if (i == 1 || i == rows || j == 1 || j == cols) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}Question 7
Easy
Print the following right triangle number pattern for n = 4:
1
2 2
3 3 3
4 4 4 4
Row i prints the number i, repeated i times.
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}Question 8
Medium
Print a right-aligned number triangle for n = 4:
1
2 1
3 2 1
4 3 2 1
Row i has (n-i) spaces then numbers from i down to 1.
int n = 4;
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = i; j >= 1; j--) System.out.print(j + " ");
System.out.println();
}Question 9
Hard
Print a sandglass pattern for n = 5:
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
Upper half: inverted pyramid. Lower half: pyramid. Each row i has i-1 spaces then (n-i+1) or (i) stars.
int n = 5;
for (int i = 0; i < n; i++) {
for (int s = 0; s < i; s++) System.out.print(" ");
for (int j = 0; j < n - i; j++) System.out.print("* ");
System.out.println();
}
for (int i = 1; i < n; i++) {
for (int s = 0; s < n - i - 1; s++) System.out.print(" ");
for (int j = 0; j <= i; j++) System.out.print("* ");
System.out.println();
}Question 10
Hard
Print the following butterfly pattern for n = 4:
* *
** **
*** ***
********
********
*** ***
** **
* *
Upper half: i stars, (2n - 2i) spaces, i stars. Lower half: mirror.
int n = 4;
// Upper half
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) System.out.print("*");
for (int j = 1; j <= 2 * (n - i); j++) System.out.print(" ");
for (int j = 1; j <= i; j++) System.out.print("*");
System.out.println();
}
// Lower half
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) System.out.print("*");
for (int j = 1; j <= 2 * (n - i); j++) System.out.print(" ");
for (int j = 1; j <= i; j++) System.out.print("*");
System.out.println();
}Question 11
Hard
Print a number diamond for n = 4:
1
121
12321
1234321
12321
121
1
Each row prints numbers ascending then descending. Use spaces for alignment.
int n = 4;
// Upper half
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = 1; j <= i; j++) System.out.print(j);
for (int j = i - 1; j >= 1; j--) System.out.print(j);
System.out.println();
}
// Lower half
for (int i = n - 1; i >= 1; i--) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = 1; j <= i; j++) System.out.print(j);
for (int j = i - 1; j >= 1; j--) System.out.print(j);
System.out.println();
}Question 12
Hard
Print Pascal's triangle for n = 5 rows.
Each element = previous * (row - col) / (col + 1) using 0-indexed.
int n = 5;
for (int i = 0; i < n; i++) {
for (int s = 0; s < n - i - 1; s++) System.out.print(" ");
int val = 1;
for (int j = 0; j <= i; j++) {
System.out.printf("%4d", val);
val = val * (i - j) / (j + 1);
}
System.out.println();
}Multiple Choice Questions
MCQ 1
What does the break statement do inside a loop?
Answer: B
B is correct.
B is correct.
break terminates the innermost enclosing loop or switch statement.MCQ 2
What does the continue statement do?
Answer: C
C is correct.
C is correct.
continue skips the remaining body of the current iteration and proceeds to the next iteration.MCQ 3
What is a labeled break used for?
Answer: B
B is correct. A labeled break allows you to exit an outer loop from within an inner loop. The label is placed before the outer loop.
B is correct. A labeled break allows you to exit an outer loop from within an inner loop. The label is placed before the outer loop.
MCQ 4
In a right triangle star pattern of n rows, how many stars does row i have?
Answer: B
B is correct. In a right triangle, row 1 has 1 star, row 2 has 2 stars, ..., row i has i stars.
B is correct. In a right triangle, row 1 has 1 star, row 2 has 2 stars, ..., row i has i stars.
MCQ 5
In a pyramid pattern of n rows, how many leading spaces does row i have?
Answer: C
C is correct. In a pyramid, row 1 has (n-1) spaces, row 2 has (n-2), ..., row i has (n-i) spaces.
C is correct. In a pyramid, row 1 has (n-1) spaces, row 2 has (n-2), ..., row i has (n-i) spaces.
MCQ 6
In a pyramid of n rows, how many stars does row i have?
Answer: C
C is correct. Row 1: 1 star, Row 2: 3, Row 3: 5. The formula is 2*i - 1 (odd numbers).
C is correct. Row 1: 1 star, Row 2: 3, Row 3: 5. The formula is 2*i - 1 (odd numbers).
MCQ 7
What is Floyd's triangle?
Answer: B
B is correct. Floyd's triangle fills rows with consecutive natural numbers: 1 / 2 3 / 4 5 6 / ... Row i has i numbers.
B is correct. Floyd's triangle fills rows with consecutive natural numbers: 1 / 2 3 / 4 5 6 / ... Row i has i numbers.
MCQ 8
What does return do inside a loop in a method?
Answer: B
B is correct.
B is correct.
return exits the entire method, not just the loop. If the method has a return type, you must return a value.MCQ 9
How many total stars are printed in a pyramid of n rows?
Answer: C
C is correct. Row i has (2i-1) stars. Total = sum of (2i-1) for i=1 to n = 2 * n*(n+1)/2 - n = n^2.
C is correct. Row i has (2i-1) stars. Total = sum of (2i-1) for i=1 to n = 2 * n*(n+1)/2 - n = n^2.
MCQ 10
In Pascal's triangle, what is the value of the element at row 4, column 2 (0-indexed)?
Answer: C
C is correct. C(4,2) = 4! / (2! * 2!) = 24 / 4 = 6. Pascal's row 4 is: 1, 4, 6, 4, 1.
C is correct. C(4,2) = 4! / (2! * 2!) = 24 / 4 = 6. Pascal's row 4 is: 1, 4, 6, 4, 1.
MCQ 11
What is the output?
outer: for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { if (j==1) break; } System.out.print(i); }Answer: A
A is correct. The inner break (without label) exits only the inner loop. The outer loop continues, printing i values: 0, 1, 2.
A is correct. The inner break (without label) exits only the inner loop. The outer loop continues, printing i values: 0, 1, 2.
MCQ 12
What is a diamond pattern's total number of rows for n = 5 (upper half)?
Answer: B
B is correct. A diamond with n = 5 has n rows in the upper half and n-1 rows in the lower half. Total = n + (n-1) = 2n - 1 = 9.
B is correct. A diamond with n = 5 has n rows in the upper half and n-1 rows in the lower half. Total = n + (n-1) = 2n - 1 = 9.
MCQ 13
What is the correct syntax for a labeled break?
Answer: A
A is correct. Labeled break syntax is
A is correct. Labeled break syntax is
break labelName; where the label is declared before the loop as labelName:.MCQ 14
In a right triangle of n rows, what is the total number of stars printed?
Answer: B
B is correct. Row 1 has 1 star, row 2 has 2, ..., row n has n. Total = 1 + 2 + ... + n = n(n+1)/2.
B is correct. Row 1 has 1 star, row 2 has 2, ..., row n has n. Total = 1 + 2 + ... + n = n(n+1)/2.
MCQ 15
What is the output?
for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i + j > 4) break; System.out.print(i+""+j+" "); } }Answer: B
B is correct. i=1: j=1(11), j=2(12), j=3(13, 1+3=4 not >4 so prints). i=2: j=1(21), j=2(22), j=3(2+3=5>4, break inner). i=3: j=1(31, 3+1=4 not >4), j=2(3+2=5>4, break inner). Output: 11 12 13 21 22 31.
B is correct. i=1: j=1(11), j=2(12), j=3(13, 1+3=4 not >4 so prints). i=2: j=1(21), j=2(22), j=3(2+3=5>4, break inner). i=3: j=1(31, 3+1=4 not >4), j=2(3+2=5>4, break inner). Output: 11 12 13 21 22 31.
Coding Challenges
Challenge 1: Right-Aligned Star Triangle
EasyPrint a right-aligned triangle for n = 5:
*
**
***
****
*****
Sample Input
n = 5
Sample Output
*
**
***
****
*****
Use spaces for alignment.
int n = 5;
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = 1; j <= i; j++) System.out.print("*");
System.out.println();
}Challenge 2: Hollow Pyramid
MediumPrint a hollow pyramid for n = 5. Only the border stars are printed; interior is spaces.
Sample Input
n = 5
Sample Output
*
* *
* *
* *
*********
Print star on first column, last column, and last row. Spaces elsewhere.
int n = 5;
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1 || i == n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}Challenge 3: Hourglass Pattern
MediumPrint an hourglass (inverted pyramid followed by a pyramid) for n = 5.
Sample Input
n = 5
Sample Output
*********
*******
*****
***
*
***
*****
*******
*********
Combine inverted pyramid (top) and pyramid (bottom).
int n = 5;
for (int i = n; i >= 1; i--) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.println();
}
for (int i = 2; i <= n; i++) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.println();
}Challenge 4: Zigzag Number Pattern
MediumPrint a number pattern where odd rows go left-to-right and even rows go right-to-left:
1
3 2
4 5 6
10 9 8 7
Sample Input
n = 4
Sample Output
1
3 2
4 5 6
10 9 8 7
Track a global counter. For even rows, store numbers and print in reverse.
int n = 4;
int num = 1;
for (int i = 1; i <= n; i++) {
int[] row = new int[i];
for (int j = 0; j < i; j++) {
row[j] = num++;
}
if (i % 2 == 0) {
for (int j = i - 1; j >= 0; j--) System.out.print(row[j] + " ");
} else {
for (int j = 0; j < i; j++) System.out.print(row[j] + " ");
}
System.out.println();
}Challenge 5: Star Diamond
MediumPrint a full diamond for n = 5 (upper half rows).
Sample Input
n = 5
Sample Output
*
***
*****
*******
*********
*******
*****
***
*
Combine pyramid (upper) and inverted pyramid (lower, skip middle).
int n = 5;
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.println();
}Challenge 6: Alphabet Diamond
HardPrint a character diamond for n = 5:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
Sample Input
n = 5
Sample Output
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
Each row: spaces, characters A to A+i-1, then back to A.
int n = 5;
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = 0; j < i; j++) System.out.print((char)('A' + j));
for (int j = i - 2; j >= 0; j--) System.out.print((char)('A' + j));
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = 0; j < i; j++) System.out.print((char)('A' + j));
for (int j = i - 2; j >= 0; j--) System.out.print((char)('A' + j));
System.out.println();
}Challenge 7: Spiral Number Pattern
HardPrint an n x n matrix filled with numbers 1 to n*n in spiral order. n = 4:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Sample Input
n = 4
Sample Output
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Use a 2D array and fill in spiral order: right, down, left, up.
int n = 4;
int[][] matrix = new int[n][n];
int num = 1, top = 0, bottom = n-1, left = 0, right = n-1;
while (num <= n * n) {
for (int i = left; i <= right; i++) matrix[top][i] = num++;
top++;
for (int i = top; i <= bottom; i++) matrix[i][right] = num++;
right--;
for (int i = right; i >= left; i--) matrix[bottom][i] = num++;
bottom--;
for (int i = bottom; i >= top; i--) matrix[i][left] = num++;
left++;
}
for (int[] row : matrix) {
for (int val : row) System.out.printf("%3d", val);
System.out.println();
}Challenge 8: Pascal's Triangle (Print N Rows)
HardPrint the first N rows of Pascal's triangle with proper alignment.
Sample Input
n = 6
Sample Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Use the binomial coefficient formula iteratively.
int n = 6;
for (int i = 0; i < n; i++) {
for (int s = 0; s < n - i - 1; s++) System.out.print(" ");
int val = 1;
for (int j = 0; j <= i; j++) {
System.out.printf("%4d", val);
val = val * (i - j) / (j + 1);
}
System.out.println();
}Need to Review the Concepts?
Go back to the detailed notes for this chapter.
Read Chapter NotesWant to learn Java with a live mentor?
Explore our Java Masterclass