Example test
1. What is the output of the following program (if you think it is an error, answer error)?
def add(a, b): c = a + b x = 3 y = 4 c = add(x, y) print(c)
................................
2. The following piece of code prompts the user for a number. The program has a try-except block to prevent the program from the crash if the user enters an invalid number. However, the program is incomplete. Even if the user enters a number, the user is prompted again for a number. To fix the code, a break statement has to be added somewhere. After which line should the break statement be added?
1 while True: 2 try: 3 number = int(input("Enter a number: ")) 4 except: 5 print("Enter a number")
................................
3. What is the output of the following program?
a = [2, 6, 3] a.sort() if a[1] % 2 == 0: a.append(5) else: a[2] = 10 print(sum(a))
................................
4. What is the output of the following program?
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. 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. An infinite number
6. What is the output of the following program?
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 print out?
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 times will the letter a be printed out?
def function_a(): print("a") def function_b(): function_a() print("b") function_b() function_a()
................................
9. What is the output of the following program?
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. An error message
10. What is the output of the following program?
example = "snow world" print(example[3:6].strip())
a. wo
b. ww
c. w w
d. wwo