Institute of Computer Science
  1. Courses
  2. 2018/19 fall
  3. Computer Programming (Narva College) (SVNC.00.054)
ET
Log in

Computer Programming (Narva College) 2018/19 fall

  • Home
  • Grading
  • Links

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

  • 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