nerdexam
Python_Institute

PCEP-30-02 · Question #337

How many stars will the following code print to the monitor? data = [[x for x in range(3)] for y in range(3)] for i in range(3): for j in range(3): if data[i][j] % 2 != 0: print('*')

The correct answer is C. three. Option C is correct because data is a 3×3 matrix where every row is [0, 1, 2] - only the value 1 is odd, so exactly one star prints per row, giving 3 stars total across the 3 rows. Why the distractors fail: A (nine) - would require every element to be odd, but 0 and 2 are even…

Question

How many stars will the following code print to the monitor? data = [[x for x in range(3)] for y in range(3)] for i in range(3): for j in range(3): if data[i][j] % 2 != 0: print('*')

Options

  • Anine
  • Bzero
  • Cthree
  • Dsix

How the community answered

(43 responses)
  • A
    19% (8)
  • B
    5% (2)
  • C
    70% (30)
  • D
    7% (3)

Explanation

Option C is correct because data is a 3×3 matrix where every row is [0, 1, 2] - only the value 1 is odd, so exactly one star prints per row, giving 3 stars total across the 3 rows.

Why the distractors fail:

  • A (nine) - would require every element to be odd, but 0 and 2 are even and never trigger print('*')
  • B (zero) - would require all elements to be even, but 1 is odd and appears in every row
  • D (six) - would require two-thirds of elements to be odd; in [0, 1, 2], only one out of three is odd

Memory tip: When you see a list comprehension like [[x for x in range(3)] for y in range(3)], notice that y is never used - the inner list [0, 1, 2] is simply repeated 3 times unchanged. Count the odd values in one row first, then multiply by the number of rows.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice