Institute of Computer Science
  1. Courses
  2. 2018/19 fall
  3. Introduction to Programming (MTAT.03.236)
ET
Log in

Introduction to Programming 2018/19 fall

  • Home
  • Grading
  • Links

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

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment