Quiz Space

Programming in Python · End Term · 21 Dec 2025 · September 2025 term

Question 16: You are inside the lift of a building. There are 8 level…

Question 16

+2 marksWritten answer

You are inside the lift of a building. There are 8 levels in the building: [−3,−2,−1,0,1,2,3,4] The number 0 is the ground floor. Positive numbers correspond to floors above the ground floor, negative numbers correspond to basement levels below the ground floor. The lift has only two buttons. The button U will take you one level up and the button D will take you one level down. You make a sequence of presses. # presses contains the sequence of button presses made by you presses = 'UDDUUUDDUDU' floor = 0 index = 0 while index < len(presses): char = presses[index] if char == 'U': floor += 1 elif char == 'D': floor -= 1 if floor == 2: print(index + 1) break index += 1 Based on the above data answer the subsequent questions.

What is the output of this snippet of code?

Show answer

A written answer, not marked automatically.

Question 16 of 20 in the IIT Madras BS Programming in Python (Python) End Term paper sat on 21 Dec 2025, in the September 2025 term (Intro to Python 18 Dec 25). It carries 2 marks.

More questions from this paper

  1. Q1Consider the following functions: def p(n): if n < 10: return 1 return 1 + p(n // 10) def q(n): if n < 10: return n ret…
  2. Q2Consider the following code snippet that writes student details to a CSV file named "employees.csv": def save_data(data…
  3. Q3What will be the output of the following Python code? string1 = 'database' string2 = 'baseball' L = [] for i in range(l…
  4. Q4What is the output of the following snippet of code? sentence = 'python,is,fun,and,learning,python,is,great' D = dict()…
  5. Q5If n is a positive integer, what does the following snippet compute? Q = [x for x in range(1, 3 n + 1) if x % 3 == 0] p…
  6. Q6Consider the following snippets of code: Code-1 T = (4, 5, 6) T[0] = 10 Code-2 S = set() S.insert(2) Code-3 L = [] L.ap…
  7. Q7employees is a list of tuples. Each tuple is of the form (name, salary) . Select all snippets of code that create a lis…
  8. Q8Consider the following snippet: f = open('employees.csv', 'r') D = dict() for line in f: name, department, salary = lin…
  9. Q9Consider the following snippet of code: def analyze_string(t, k): freq = {} for ch in t: freq[ch] = freq.get(ch, 0) + 1…
  10. Q10What is the output of the following snippet of code? def g(x, y): if x == 1: return 0 return 1 + g(x // y, y) print(g(1…
  11. Q11What is the output of the following snippet of code? total = 0 errors = 0 for s in ['5', '2.5', '7', '8']: try: n = int…
  12. Q12What is the output of the following snippet of code? total = 0 num = 10 while num <= 20: count = 0 for i in range(1, nu…
  13. Q13Consider the following snippet of code: L = [2, 3, 4] S = [] T = 0 i = 0 while i < len(L): S += L[:i] + L[i:] for j in …
  14. Q14What is the output of the following snippet of code? def doSomething(x, y): if x < y: return 0 return 1 + doSomething(x…
  15. Q15You are inside the lift of a building. There are 8 levels in the building: [−3,−2,−1,0,1,2,3,4] The number 0 is the gro…
  16. Q17Consider the following snippet: def ProcedureOne(M): n = len(M) for i in range(n): temp = M[i][i] M[i][i] = M[i][-i - 1…
  17. Q18Consider the following snippet: def ProcedureOne(M): n = len(M) for i in range(n): temp = M[i][i] M[i][i] = M[i][-i - 1…
  18. Q19You are inside the lift of a building. There are 8 levels in the building: [−3,−2,−1,0,1,2,3,4] The number 0 is the gro…
  19. Q20Consider the following snippet: def ProcedureOne(M): n = len(M) for i in range(n): temp = M[i][i] M[i][i] = M[i][-i - 1…