Chapter 10 Beginner 57 Questions

Practice Questions — Pattern Printing in Python

← Back to Notes
8 Easy
11 Medium
10 Hard

Topic-Specific Questions

Question 1
Easy
What is the output of the following code?
for i in range(1, 4):
    print("*" * i)
"*" * i repeats the star character i times.
*
**
***
Question 2
Easy
What is the output?
for i in range(3):
    for j in range(3):
        print("*", end=" ")
    print()
Both loops run 3 times. The inner loop prints 3 stars per row.
* * *
* * *
* * *
Question 3
Easy
What is the output?
for i in range(1, 5):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()
The inner loop prints numbers from 1 to i.
1
1 2
1 2 3
1 2 3 4
Question 4
Easy
What is the output?
for i in range(4, 0, -1):
    print("*" * i)
The loop counts from 4 down to 1.
****
***
**
*
Question 5
Easy
What is the output?
for i in range(3):
    for j in range(i + 1):
        print(chr(65 + j), end=" ")
    print()
chr(65) is 'A', chr(66) is 'B', chr(67) is 'C'.
A
A B
A B C
Question 6
Medium
What is the output?
n = 4
for i in range(1, n + 1):
    print(" " * (n - i) + "*" * (2 * i - 1))
Each row has (n - i) spaces and (2*i - 1) stars. Row 1: 3 spaces, 1 star. Row 2: 2 spaces, 3 stars.
*
***
*****
*******
Question 7
Medium
What is the output?
num = 1
for i in range(1, 5):
    for j in range(i):
        print(num, end=" ")
        num += 1
    print()
num is a continuous counter that does not reset between rows. This is Floyd's triangle.
1
2 3
4 5 6
7 8 9 10
Question 8
Medium
What is the output?
for i in range(1, 5):
    for j in range(1, i + 1):
        print(i, end=" ")
    print()
Notice: it prints i (the row number), not j.
1
2 2
3 3 3
4 4 4 4
Question 9
Medium
What is the output?
n = 4
for i in range(1, n + 1):
    for j in range(1, n + 1):
        if i == 1 or i == n or j == 1 or j == n:
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()
Stars are printed only on the borders of a 4x4 grid.
* * * *
* *
* *
* * * *
Question 10
Medium
What is the output?
for i in range(5, 0, -1):
    for j in range(5 - i):
        print(" ", end="")
    for j in range(i):
        print("* ", end="")
    print()
The first inner loop prints leading spaces. The second prints stars. Stars decrease while spaces increase.
* * * * *
* * * *
* * *
* *
*
Question 11
Medium
What is the output?
for i in range(1, 5):
    for j in range(i, 5):
        print(j, end=" ")
    print()
The inner loop starts at i and goes up to 4.
1 2 3 4
2 3 4
3 4
4
Question 12
Hard
What is the output?
n = 4
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(i * j, end="\t")
    print()
Each cell prints i * j. This creates a multiplication table triangle.
1
2 4
3 6 9
4 8 12 16
Question 13
Hard
What is the output?
n = 4
for i in range(n):
    for j in range(n):
        if (i + j) % 2 == 0:
            print("1", end=" ")
        else:
            print("0", end=" ")
    print()
When i + j is even, print 1. When odd, print 0. This creates a checkerboard.
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1
Question 14
Hard
What is the output?
n = 5
for i in range(1, n + 1):
    for j in range(1, i + 1):
        if j % 2 == 1:
            print(1, end=" ")
        else:
            print(0, end=" ")
    print()
Odd column numbers get 1, even column numbers get 0.
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
Question 15
Hard
What is the output?
n = 4
for i in range(1, n + 1):
    for j in range(n, 0, -1):
        if j <= i:
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()
The inner loop goes from n down to 1. Stars are printed when j <= i.
*
* *
* * *
* * * *
Question 16
Hard
What is the output?
n = 3
for i in range(1, n + 1):
    print(" " * (n - i) + "* " * i)
for i in range(n - 1, 0, -1):
    print(" " * (n - i) + "* " * i)
The first loop prints the top half of a diamond. The second loop prints the bottom half.
*
* *
* * *
* *
*
Question 17
Medium
Explain the 'rows and columns' approach for pattern printing. How do you figure out the code for any pattern?
Think about analyzing the pattern row by row before writing any code.
The rows and columns approach involves: (1) Count the total rows in the pattern. (2) For each row, count the number of spaces and characters. (3) Express the counts as formulas based on the row number (i) and total rows (n). (4) The outer loop iterates through rows. (5) Inner loops use the formulas as range() arguments to print spaces and characters. Example: for a pyramid with n rows, row i has (n-i) spaces and i stars.
Question 18
Hard
What is the output?
for i in range(1, 6):
    for j in range(1, 6):
        print(min(i, j), end=" ")
    print()
Each cell prints the minimum of its row and column number.
1 1 1 1 1
1 2 2 2 2
1 2 3 3 3
1 2 3 4 4
1 2 3 4 5

Mixed & Application Questions

Question 1
Easy
Write the code to print this pattern:
1
2 2
3 3 3
4 4 4 4
Row i should print the number i, repeated i times.
for i in range(1, 5):
    for j in range(i):
        print(i, end=" ")
    print()
Question 2
Easy
Write the code to print this pattern:
* * * *
* * *
* *
*
Start with 4 stars and decrease by 1 each row.
for i in range(4, 0, -1):
    for j in range(i):
        print("*", end=" ")
    print()
Question 3
Easy
What is the output?
for i in range(3):
    print(str(i) * 3)
str(i) converts the number to a string. Multiplying a string repeats it.
000
111
222
Question 4
Medium
What is the output?
n = 5
for i in range(1, n + 1):
    for j in range(i, n + 1):
        print(j, end=" ")
    print()
The inner loop starts at i and goes to n. The starting point increases each row.
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
Question 5
Medium
What is the output?
for i in range(4):
    for j in range(4):
        print(i + j, end=" ")
    print()
Each cell prints the sum of its row and column index.
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
Question 6
Medium
Write the code to print this pattern:
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Each row starts at 5 and counts down. The number of values decreases each row.
for i in range(5, 0, -1):
    for j in range(5, 5 - i, -1):
        print(j, end=" ")
    print()
Question 7
Medium
What is the output?
for i in range(5):
    for j in range(5):
        if i == j or i + j == 4:
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()
i == j is the main diagonal. i + j == 4 is the anti-diagonal. Together they form an X.
* *
* *
*
* *
* *
Question 8
Hard
What is the output?
n = 4
for i in range(1, n + 1):
    for j in range(1, n + 1):
        print(abs(i - j), end=" ")
    print()
abs(i - j) gives the distance between the row and column number.
0 1 2 3
1 0 1 2
2 1 0 1
3 2 1 0
Question 9
Hard
What is the output?
n = 5
for i in range(1, n + 1):
    for j in range(1, n + 1):
        print(min(i, j, n + 1 - i, n + 1 - j), end=" ")
    print()
This finds the minimum distance from any border of a 5x5 grid.
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
Question 10
Hard
Write the code to print this hourglass (sandglass) pattern:
* * * * *
  * * *
    *
  * * *
* * * * *
Top half: inverted pyramid. Bottom half: pyramid. Both need leading spaces.
n = 5
for i in range(n, 0, -2):
    spaces = (n - i) // 2
    print("  " * spaces + "* " * ((i + 1) // 2))
for i in range(3, n + 1, 2):
    spaces = (n - i) // 2
    print("  " * spaces + "* " * ((i + 1) // 2))
Question 11
Hard
What is the output?
for i in range(1, 6):
    for j in range(1, 6):
        if j <= i:
            print(j, end=" ")
        else:
            print(" ", end=" ")
    for j in range(1, 6):
        if j < i:
            print(i - j, end=" ")
        else:
            print(" ", end=" ")
    print()
The first inner loop prints ascending numbers. The second prints a descending mirror.
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Multiple Choice Questions

MCQ 1
How many loops are typically needed to print a right triangle pattern?
  • A. 1 loop
  • B. 2 loops (one nested inside the other)
  • C. 3 loops
  • D. No loops needed
Answer: B
B is correct. A right triangle needs 2 loops: an outer loop for rows and an inner loop for printing stars in each row. A single loop (option A) cannot control both rows and columns. Some complex patterns may need 3+ loops (option C), but right triangles need exactly 2.
MCQ 2
What does print() with no arguments do in pattern printing?
  • A. Prints the word 'None'
  • B. Causes an error
  • C. Moves to the next line
  • D. Prints a space
Answer: C
C is correct. print() with no arguments prints nothing but adds a newline character, moving the cursor to the next line. This is essential in pattern printing to separate rows.
MCQ 3
In a right triangle with n rows, how many stars does row i have (counting from 1)?
  • A. n stars
  • B. i stars
  • C. n - i stars
  • D. i - 1 stars
Answer: B
B is correct. In a right triangle, row 1 has 1 star, row 2 has 2, row 3 has 3, and so on. Row i has exactly i stars. Option C (n - i) would give an inverted triangle. Option A (n) would give a rectangle.
MCQ 4
What does end="" do in print("*", end="")?
  • A. Ends the program
  • B. Prevents the newline, keeping output on the same line
  • C. Adds an empty string at the end
  • D. Causes an error
Answer: B
B is correct. By default, print() adds a newline after output. end="" replaces the newline with nothing, so the next print continues on the same line. This is essential for printing multiple characters on one row.
MCQ 5
In a pyramid pattern with n rows, how many leading spaces does row i have?
  • A. i spaces
  • B. n spaces
  • C. n - i spaces
  • D. i - 1 spaces
Answer: C
C is correct. In a pyramid, the first row has (n-1) spaces, the second has (n-2), and the last has 0. The formula is (n - i) spaces for row i. This positions the stars to create a centered triangle.
MCQ 6
What is chr(65) in Python?
  • A. 65
  • B. "65"
  • C. "A"
  • D. "a"
Answer: C
C is correct. chr(65) returns the character with ASCII value 65, which is 'A'. The uppercase letters A-Z have ASCII values 65-90. Lowercase 'a' (option D) has ASCII value 97. chr() always returns a string character.
MCQ 7
In Floyd's triangle, what makes it different from a regular number triangle?
  • A. Numbers reset to 1 each row
  • B. Numbers are consecutive across all rows
  • C. Numbers go from right to left
  • D. Only even numbers are printed
Answer: B
B is correct. Floyd's triangle uses a single counter that increases continuously: 1 / 2 3 / 4 5 6 / 7 8 9 10. In a regular number triangle, each row starts from 1: 1 / 1 2 / 1 2 3 (option A). The key is that the counter variable is declared outside the outer loop.
MCQ 8
How many total stars are printed in a right triangle with 5 rows?
  • A. 5
  • B. 10
  • C. 15
  • D. 25
Answer: C
C is correct. Row 1: 1 star. Row 2: 2. Row 3: 3. Row 4: 4. Row 5: 5. Total = 1+2+3+4+5 = 15. The formula is n*(n+1)/2 = 5*6/2 = 15. Option D (25) would be a 5x5 square.
MCQ 9
In a hollow square of size n, how many stars are printed in total?
  • A. n * n
  • B. 4 * n
  • C. 4 * n - 4
  • D. 2 * n + 2 * (n - 2)
Answer: C
C is correct (which equals option D). The top and bottom rows have n stars each (2n). The middle (n-2) rows have 2 stars each (only borders). Total: 2n + 2(n-2) = 2n + 2n - 4 = 4n - 4. For n=5: 4*5-4 = 16 stars.
MCQ 10
What does this code print?
print("*" * 0)
  • A. *
  • B. 0
  • C. An empty line (just a newline)
  • D. Error
Answer: C
C is correct. "*" * 0 produces an empty string "". Printing an empty string just outputs a newline (blank line). It does not cause an error. This is important for edge cases in patterns.
MCQ 11
In the expression " " * (n - i), if n = 5 and i = 5, what is printed?
  • A. 5 spaces
  • B. Nothing (empty string)
  • C. Error: cannot multiply by zero
  • D. One space
Answer: B
B is correct. " " * (5 - 5) = " " * 0 = "" (empty string). Multiplying a string by 0 gives an empty string, not an error. This is how the last row of a pyramid has no leading spaces.
MCQ 12
How many inner loops are needed for a centered pyramid pattern?
  • A. 1 (for stars only)
  • B. 2 (one for spaces, one for stars)
  • C. 3 (for spaces, stars, and newline)
  • D. It depends on the programming language
Answer: B
B is correct. A centered pyramid needs one inner loop to print leading spaces and another to print stars. The newline is handled by print(), not a loop (option C). While you can use string multiplication instead of loops, the standard approach uses 2 inner loops.
MCQ 13
In a diamond with n rows in the top half, how many total rows does the complete diamond have?
  • A. n rows
  • B. 2n rows
  • C. 2n - 1 rows
  • D. 2n + 1 rows
Answer: C
C is correct. The top half has n rows and the bottom half has n-1 rows (the middle row is not repeated). Total: n + (n-1) = 2n-1. For n=5: 2*5-1 = 9 rows. Option B (2n = 10) would duplicate the middle row.
MCQ 14
What formula gives the number of stars in row i of a solid pyramid (where stars are 1, 3, 5, 7...)?
  • A. i
  • B. i * 2
  • C. 2 * i - 1
  • D. i + 1
Answer: C
C is correct. Row 1: 2*1-1 = 1. Row 2: 2*2-1 = 3. Row 3: 2*3-1 = 5. Row 4: 2*4-1 = 7. The formula 2i-1 generates the odd number sequence, which is needed for a solid (no-gap) pyramid.
MCQ 15
What is the purpose of using "\t" in pattern printing?
  • A. Prints the letter 't'
  • B. Adds a tab space for column alignment
  • C. Terminates the line
  • D. Adds a newline
Answer: B
B is correct. \t is the tab character, which adds a fixed-width space. It is used in number patterns (like multiplication tables) to align columns neatly, especially when numbers have different digit counts.
MCQ 16
What pattern does this code print?
for i in range(1, 6):
    print(i, end=" ")
  • A. A triangle
  • B. A single row: 1 2 3 4 5
  • C. A column of numbers
  • D. A square
Answer: B
B is correct. There is no nested loop, just a single for loop with end=" ". This prints all numbers on one line: 1 2 3 4 5. A triangle (option A) would need nested loops. A column (option C) would need the default newline.
MCQ 17
What is the total number of iterations (inner loop body executions) for a right triangle with n = 100?
  • A. 100
  • B. 5,050
  • C. 10,000
  • D. 10,100
Answer: B
B is correct. The inner loop runs 1 + 2 + 3 + ... + 100 = n(n+1)/2 = 100*101/2 = 5050 times. This is the formula for the sum of the first n natural numbers.
MCQ 18
To print a right-aligned triangle (stars on the right), what must you add before the stars?
  • A. Numbers
  • B. Tab characters
  • C. Leading spaces
  • D. Nothing extra is needed
Answer: C
C is correct. A right-aligned triangle requires leading spaces to push the stars to the right. For a triangle with n rows, row i needs (n - i) spaces before the stars. Without spaces, the triangle is always left-aligned.

Coding Challenges

Challenge 1: Right Triangle with Numbers

Easy
Write a program to print the following pattern for n = 5 rows:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input
(No input required)
Sample Output
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Use nested for loops. n = 5.
n = 5
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()

Challenge 2: Inverted Star Triangle

Easy
Write a program to print an inverted right triangle of stars with 6 rows.
Sample Input
(No input required)
Sample Output
* * * * * * * * * * * * * * * * * * * * *
Use nested for loops.
n = 6
for i in range(n, 0, -1):
    for j in range(i):
        print("*", end=" ")
    print()

Challenge 3: Pyramid Pattern

Medium
Ananya wants to print a centered pyramid of stars with 5 rows. Write the program.
Sample Input
(No input required)
Sample Output
* * * * * * * * * * * * * * *
Use spaces for centering. The pyramid should look symmetric.
n = 5
for i in range(1, n + 1):
    for j in range(n - i):
        print(" ", end="")
    for j in range(i):
        print("* ", end="")
    print()

Challenge 4: Floyd's Triangle

Medium
Write a program to print Floyd's triangle with 5 rows. Numbers should be consecutive across all rows (1 / 2 3 / 4 5 6 / ...).
Sample Input
(No input required)
Sample Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Use a counter variable that does not reset between rows.
n = 5
num = 1
for i in range(1, n + 1):
    for j in range(i):
        print(num, end=" ")
        num += 1
    print()

Challenge 5: Diamond Pattern

Medium
Rohan wants to print a diamond pattern with the widest row having 5 stars. Write the complete diamond (top + bottom).
Sample Input
(No input required)
Sample Output
* * * * * * * * * * * * * * * * * * * * * * * * *
The diamond should have 9 rows total (5 top + 4 bottom). Use spaces for centering.
n = 5
for i in range(1, n + 1):
    print(" " * (n - i) + "* " * i)
for i in range(n - 1, 0, -1):
    print(" " * (n - i) + "* " * i)

Challenge 6: Multiplication Table Grid

Medium
Vikram wants to print a 5x5 multiplication table where each cell shows the product of its row and column number. Use tab spacing.
Sample Input
(No input required)
Sample Output
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
Use nested for loops and \t for tab spacing.
n = 5
for i in range(1, n + 1):
    for j in range(1, n + 1):
        print(i * j, end="\t")
    print()

Challenge 7: Alphabet Pyramid

Hard
Write a program to print this centered alphabet pyramid with 5 rows:
    A
   A B
  A B C
 A B C D
A B C D E
Sample Input
(No input required)
Sample Output
A A B A B C A B C D A B C D E
Use chr(65 + j) for letters. Add leading spaces for centering.
n = 5
for i in range(n):
    for j in range(n - i - 1):
        print(" ", end="")
    for j in range(i + 1):
        print(chr(65 + j), end=" ")
    print()

Challenge 8: Hollow Right Triangle

Hard
Priya wants to print a hollow right triangle with 5 rows. Stars appear only on the borders (first column, last column, and last row).
*
* *
*   *
*     *
* * * * *
Sample Input
(No input required)
Sample Output
* * * * * * * * * * * *
Print stars only at borders. Use spaces for interior.
n = 5
for i in range(1, n + 1):
    for j in range(1, i + 1):
        if j == 1 or j == i or i == n:
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()

Challenge 9: Butterfly Pattern

Hard
Write a program to print a butterfly pattern with n = 4:
*      *
* *  * *
* * ** * *
* * * ** * * *
* * * ** * * *
* * ** * *
* *  * *
*      *
Sample Input
(No input required)
Sample Output
* * ** ** *** *** ******** ******** *** *** ** ** * *
The butterfly has a top half and a mirrored bottom half. Use nested loops for stars and spaces.
n = 4
# Top half
for i in range(1, n + 1):
    print("*" * i + " " * (2 * (n - i)) + "*" * i)
# Bottom half
for i in range(n, 0, -1):
    print("*" * i + " " * (2 * (n - i)) + "*" * i)

Challenge 10: Pascal's Triangle First 5 Rows

Hard
Write a program to print the first 5 rows of Pascal's triangle. Each number is the sum of the two numbers above it. Row 1: 1. Row 2: 1 1. Row 3: 1 2 1. Row 4: 1 3 3 1. Row 5: 1 4 6 4 1.
Sample Input
(No input required)
Sample Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Calculate each value using the formula or build from the previous row.
n = 5
for i in range(n):
    # Leading spaces
    print(" " * (n - i - 1), end="")
    val = 1
    for j in range(i + 1):
        print(val, end=" ")
        val = val * (i - j) // (j + 1)
    print()

Need to Review the Concepts?

Go back to the detailed notes for this chapter.

Read Chapter Notes

Want to learn Python with a live mentor?

Explore our Python course