Example test
1. What will the following program output (if you think an error occurs, answer error)?
def add(a, b): c = a + b x = 3 y = 4 c = add(x, y) print(c)
................................
2. The following code asks the user to enter a number and it has a try-except part so the program does not crash if the input is not a valid number. But the problem is that even when the user enters a number, the user is prompted again. To fix the code, we need to add the break statement somewhere. After which line should the break statement be added?
1 while True: 2 try: 3 number = int(input("Enter number: ")) 4 except: 5 print("Enter a number")
................................
3. What will the following program output?
a = [2, 6, 3] a.sort() if a[1] % 2 == 0: a.append(5) else: a[2] = 10 print(sum(a))
................................
4. What will the following program output?
words = "I like dogs,rainbows,cats".split(",") lengths = [] for x in words: lengths.append(len(x)) print(max(lengths))
a. 11
b. 8
c. 12
d. 0
e. None
f. It gives an error
g. 18
5. How many lines will the program print out?
i = 0 while i < 3: print(i) i = i - 1
a. 3
b. 4
c. 2
d. 1
e. 0
f. infinite number
6. What will the following program output?
answer = "0" + "0" + "7" if answer == "007": print("The same!") else: print("Not the same!" )
a. The same!
b. Not the same!
c. Error message
d. Nothing
7. What numbers will the following program output?
for i in range(10): if i == 5: break else: print(i)
a. 0 1 2 3 4
b. 1 2 3 4
c. 0 1 2 3 4 5
d. 1 2 3 4 5
e. 0 1 2 3 4 5 6 7 8 9
f. 1 2 3 4 5 6 7 8 9
g. 0 1 2 3 4 5 6 7 8 9 10
h. 1 2 3 4 5 6 7 8 9 10
8. How many letters a will the program print out?
def function_a(): print("a") def function_b(): function_a() print("b") function_b() function_a()
................................
9. What will the following program output?
s = 0 for i in [2,-4,-5,1,3]: if i > 0 : s += i else: s -= i print(s)
a. 0
b. 15
c. -3
d. Error message
10. What will the following program output?
example = "snow world" print(example[3:6].strip())
a. wo
b. ww
c. w w
d. wwo