Quiz Space

Programming in Python · Quiz 1 · 13 Jul 2025 · May 2025 term

Question 4: We want to print the following pattern.\ 1111\ 1001\ 1011…

Question 4

+4 marksOne correct option

We want to print the following pattern.
1111
1001
1011
1111
Which of these two snippets is correct?
Snippet-1
n = 4
for i in range(n):
for j in range(n):
if i == 0 or i == n-1 or j == 0 or j == n-1:
print('1', end='')
elif i == j:
print('1', end='')
else:
print('0', end='')
print()
Snippet-2
n = 4
for i in range(n):
row = ''
for j in range(n):
if i in [0, n-1] or j in [0, n-1]:
row += '1'
elif i == j:
row += '0'
else:
row += '1'
print(row)

  1. A

    Only Snippet-1 prints the pattern correctly

  2. B

    Only Snippet-2 prints the pattern correctly

  3. C

    Both snippets print the pattern correctly

  4. D

    Both snippets print incorrect patterns

Show answer

Correct answer

  • D

    Both snippets print incorrect patterns

Question 4 of 14 in the IIT Madras BS Programming in Python (Python) Quiz 1 paper sat on 13 Jul 2025, in the May 2025 term (IIT M DIPLOMA AN EXAM QDD2 13 July 2025). It carries 4 marks.

More questions from this paper

  1. Q1Consider the following code:\ text = " >>> Hello, Python! \<\<\< "\ to_strip = " \<>"\ A = text.strip(to_strip)\ B = te…
  2. Q2Consider a Python program that takes a sentence (which may contain both uppercase and lowercase letters, spaces, and pu…
  3. Q3What is the output of the following Python code?\ data = ["alpha", "beta", "gamma"]\ data[1] = data[1].replace("e", dat…
  4. Q5P is a non-empty list of distinct integers that has already been defined. Which of the following statements are true at…
  5. Q6Consider the following Python code:\ def process(x, y):\ if x > y:\ return x - y\ elif x \< y:\ return y - x\ else:\ re…
  6. Q7Consider the following snippet of code. What will the output be?\ result = 0\ for i in range(1, 4):\ for j in range(1, …
  7. Q8What is the output of the following snippet of code? Enter an integer as your answer. lst = ['IIT', ['Madras', '2025'],…
  8. Q9What is the output of the following snippet of code? Enter an integer as your answer. lst = [x for x in [1,0,-2]]\ resu…
  9. Q10Consider the following snippet of code. What will the output be?\ names = ["alice", "bob", "charlie", "david", "eve"]\ …
  10. Q11What is the value of output after executing the given code?
  11. Q12Which of the following changes would also include SQL in the final result?
  12. Q13What is the value of output after executing the given code snippet?
  13. Q14In the given code, if we change the condition to if element % 2 == 0 at line 7, then what is the value of output after …