CBSE Class 11 Computer Science Question 120 of 161

Flow of Control — Question 29

Back to all questions
29
Question

Question 12

What is the output of the following code?

for i in range(4):
  for j in range(5):
      if i + 1 == j or j + i == 4:
         print ("+", end = ' ')
      else:
         print ("o", end = ' ')
print()

Answer

Output
o + o o + o o + + o o o + + o o + o o +
Explanation

Outer loop executes 4 times. For each iteration of outer loop, inner loop executes 5 times. Therefore, the total number of times body of inner loop gets executed is 4 * 5 = 20. Thats why there are 20 characters in the output (leaving spaces). When the condition is true then + is printed else o is printed.

Answer