Unit 1: Python basics - Practice Quiz

ECAP776 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which file extension is commonly used for Python source files?

Introduction Easy
A. .py
B. .html
C. .java
D. .cpp

2 Which function displays output on the screen in Python?

Introduction Easy
A. input()
B. print()
C. type()
D. len()

3 Which symbol begins a single-line comment in Python?

Introduction Easy
A. #
B. --
C. /*
D. //

4 Which function accepts input from the user in Python?

Introduction Easy
A. print()
B. input()
C. range()
D. open()

5 Which of the following is a valid Python variable name?

Introduction Easy
A. 2score
B. class
C. user-score
D. user_score

6 Which Python data type stores whole numbers?

Data types and operators Easy
A. bool
B. str
C. int
D. float

7 What is the data type of the value 3.14 in Python?

Data types and operators Easy
A. str
B. bool
C. int
D. float

8 Which operator is used for exponentiation in Python?

Data types and operators Easy
A. ^
B. %
C. **
D. //

9 What is the result of 10 % 3?

Data types and operators Easy
A. 3
B. 3.33
C. 0
D. 1

10 Which operator checks whether two values are equal?

Data types and operators Easy
A. !=
B. >=
C. =
D. ==

11 Which keyword begins a conditional statement in Python?

Control statements Easy
A. return
B. if
C. def
D. for

12 Which keyword provides an alternative when an if condition is false?

Control statements Easy
A. elif
B. break
C. else
D. continue

13 Which loop is commonly used to iterate over a sequence in Python?

Control statements Easy
A. for loop
B. def loop
C. else loop
D. if loop

14 Which statement immediately exits the nearest loop?

Control statements Easy
A. break
B. pass
C. return
D. continue

15 Which statement skips the rest of the current loop iteration?

Control statements Easy
A. break
B. else
C. pass
D. continue

16 Which keyword is used to define a function in Python?

Functions Easy
A. func
B. def
C. function
D. define

17 What is the purpose of the return statement in a function?

Functions Easy
A. To define a loop
B. To send back a value
C. To display every value
D. To import a module

18 In def greet(name):, what is name?

Functions Easy
A. A return value
B. A function call
C. A parameter
D. A module name

19 How do you call a function named show that takes no arguments?

Functions Easy
A. show()
B. show
C. call show
D. def show

20 Which built-in function returns the number of items in a list?

Functions Easy
A. type()
B. input()
C. len()
D. range()

21 What is printed by the following code?

x = 10

x = "10"

print(x * 2)

Introduction Medium
A. 10 10
B. TypeError
C. 20
D. 1010

22 A file named tools.py contains:

print("loaded")

if __name__ == "__main__":

print("running")

What is printed when another program executes import tools?

Introduction Medium
A. Nothing is printed
B. Only running
C. Only loaded
D. loaded then running

23 What is the output of this simultaneous assignment?

a, b = 2, 3

a, b = b, a + b

print(a, b)

Introduction Medium
A. 3 6
B. 5 3
C. 2 5
D. 3 5

24 What does this program print?

total = 4

Total = 7

TOTAL = total + Total

print(TOTAL)

Introduction Medium
A. 7
B. NameError
C. 8
D. 11

25 What is printed by print(type((5,)).__name__, type((5)).__name__)?

Data types and operators Medium
A. int tuple
B. int int
C. tuple int
D. tuple tuple

26 What is the output of print(-7 // 3, -7 % 3)?

Data types and operators Medium
A. -2 1
B. -3 2
C. -3 -1
D. -2 -1

27 What is printed by the following code?

a = [1, 2]

b = a

c = a[:]

b.append(3)

print(c, a)

Data types and operators Medium
A. [1, 2] [1, 2, 3]
B. [1, 2, 3] [1, 2, 3]
C. [1, 2] [1, 2]
D. [1, 2, 3] [1, 2]

28 What is the output of this dictionary operation?

d = {"a": 1, "b": 2, "a": 3}

print(len(d), d["a"])

Data types and operators Medium
A. 3 3
B. 3 1
C. 2 1
D. 2 3

29 Which set is produced by {1, 2, 3} & {2, 3, 4} | {4, 5}?

Data types and operators Medium
A. {2, 3}
B. {2, 3, 4, 5}
C. {1, 2, 3, 4}
D. {1, 2, 3, 4, 5}

30 What value is printed by print(-2 ** 2 + 2 ** 3)?

Data types and operators Medium
A. -4
B. -12
C. 4
D. 12

31 What is printed by the following loop?

total = 0

for n in range(2, 10, 3):

total += n

print(total)

Control statements Medium
A. 12
B. 20
C. 15
D. 18

32 Which letter is printed by this conditional?

x = 8

if x % 2 == 0 and x > 10:

print("A")

elif x % 2 == 0 or x > 10:

print("B")

else:

print("C")

Control statements Medium
A. A
B. C
C. B
D. No letter

33 What value is printed after this loop?

n = 20

count = 0

while n > 1:

n //= 2

count += 1

print(count)

Control statements Medium
A. 10
B. 5
C. 3
D. 4

34 What is the final value of result?

result = []

for n in range(2, 10):

if n % 3 == 0:

continue

if n > 7:

break

result.append(n)

Control statements Medium
A. [2, 4, 5, 7]
B. [2, 4, 5, 7, 8, 9]
C. [2, 3, 4, 5, 7]
D. [2, 4, 5, 7, 8]

35 What is printed by these nested loops?

count = 0

for i in range(1, 4):

for j in range(1, 4):

if i == j:

continue

count += 1

print(count)

Control statements Medium
A. 8
B. 9
C. 3
D. 6

36 What is printed by the following code?

def add_item(item, items=[]):

items.append(item)

return items

print(add_item(1))

print(add_item(2))

Functions Medium
A. [1, 2] then [1, 2]
B. [1] then [2]
C. [1] then [1, 2]
D. [1, 2] then [2]

37 What is the output of this program?

x = 10

def change():

x = 3

return x

print(change(), x)

Functions Medium
A. 3 3
B. 10 10
C. 3 10
D. 10 3

38 What value is returned by calculate(3, 4, 5, 6)?

def calculate(a, b=2, *numbers):

return a + b + sum(numbers)

Functions Medium
A. 12
B. 18
C. 14
D. 20

39 What is printed by this function call?

def divide_parts(n):

return n // 2, n % 2

q, r = divide_parts(7)

print(q, r)

Functions Medium
A. 2 3
B. 3 1
C. 4 1
D. 3 2

40 What is the value of ordered after this statement?

words = ["pear", "fig", "apple", "kiwi"]

ordered = sorted(words, key=lambda word: (len(word), word))

Functions Medium
A. ["fig", "pear", "kiwi", "apple"]
B. ["fig", "kiwi", "pear", "apple"]
C. ["pear", "fig", "apple", "kiwi"]
D. ["apple", "kiwi", "pear", "fig"]

41 What is printed by the following code?

PYTHON
a = [0, 0]
i = 0
i, a[i] = 1, 7
print(a, i)

Introduction Hard
A. [7, 0] 0
B. [0, 7] 0
C. [7, 0] 1
D. [0, 7] 1

42 Assuming no future imports, what is printed?

PYTHON
def f(x: int = '3') -> int:
    return x * 2

print(f(), f(4))

Introduction Hard
A. TypeError
B. 33 8
C. 33 44
D. 6 8

43 What happens when this code is executed?

PYTHON
x = 10

def f(flag):
    if flag:
        x = 20
    return x

print(f(False))

Introduction Hard
A. NameError
B. UnboundLocalError
C. 10
D. 20

44 In Python 3.10 or later, what is printed by this module-level code?

PYTHON
x = 100
s = [x := x + i for i in range(3)]
print(x, s, 'i' in globals())

Introduction Hard
A. 102 [100, 101, 102] False
B. 103 [100, 101, 103] False
C. 103 [100, 101, 103] True
D. 100 [100, 101, 103] True

45 A file named m.py contains only print(__name__). What does it print when run directly, and what does it print when first imported as m by another script?

Introduction Hard
A. Direct: __main__; imported: m.py
B. Direct: m.py; imported: m
C. Direct: m; imported: __main__
D. Direct: __main__; imported: m

46 What is printed by the following code?

PYTHON
x = [1, 2]
y = x
x += [3]
z = x
x = x + [4]
print(y, z, x, y is z, z is x)

Data types and operators Hard
A. [1, 2] [1, 2] [1, 2, 3, 4] True False
B. [1, 2, 3] [1, 2, 3] [1, 2, 3, 4] True False
C. [1, 2] [1, 2, 3] [1, 2, 3, 4] False False
D. [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] True True

47 What is printed?

PYTHON
result = 0 or [] or 'py' and 5
print(result, type(result).__name__)

Data types and operators Hard
A. [] list
B. 5 int
C. py str
D. False bool

48 What is printed by this dictionary code?

PYTHON
d = {True: 'A', 1: 'B', 1.0: 'C'}
print(len(d), list(d.keys()), d[1])

Data types and operators Hard
A. 1 [True] C
B. 1 [1.0] C
C. 2 [True, 1.0] B
D. 3 [True, 1, 1.0] C

49 What is printed by these slices?

PYTHON
a = [0, 1, 2, 3, 4, 5]
print(a[5:0:-2], a[::-2])

Data types and operators Hard
A. [5, 3] [5, 3, 1]
B. [5, 3, 1] [5, 3, 1]
C. [5, 3, 1] [4, 2, 0]
D. [4, 2] [5, 3, 1]

50 What exact text is printed?

PYTHON
def f(v):
    print(v, end='')
    return v

print(f(3) < f(2) < f(1))

Data types and operators Hard
A. 321False
B. 32True
C. 321True
D. 32False

51 What exact text is printed?

PYTHON
for i in range(3):
    try:
        if i == 1:
            continue
        print(i, end='')
    finally:
        print('F', end='')

Control statements Hard
A. 0F1F2F
B. 0FF2F
C. 01FF2F
D. 0F2F

52 What is printed?

PYTHON
n = 3
while n:
    n -= 1
    if n == 1:
        break
else:
    print('E', end='')
print(n)

Control statements Hard
A. E1
B. E0
C. 0
D. 1

53 What exact text is printed?

PYTHON
for i in range(4):
    for j in range(4):
        if i * j > 2:
            break
    else:
        print(i, end='')

Control statements Hard
A. 01
B. No text
C. 0123
D. 0

54 What is printed by the following code?

PYTHON
def g(x):
    try:
        r = 10 // x
    except ZeroDivisionError:
        r = 0
    else:
        r += 1
    finally:
        r *= 2
    return r

print(g(2), g(0))

Control statements Hard
A. 10 0
B. 12 0
C. 5 0
D. 12 2

55 What is printed by this comprehension?

PYTHON
print([i for i in range(6) if i % 2 if i % 3])

Control statements Hard
A. [1, 5]
B. [0, 2, 4]
C. [1, 3, 5]
D. [2, 4]

56 What is printed?

PYTHON
def f(x, acc=[]):
    acc += [x]
    return acc

a = f(1)
b = f(2, [])
c = f(3)
print(a, b, c, a is c)

Functions Hard
A. [1] [2] [1, 3] False
B. [1, 3] [2, 3] [1, 3] True
C. [1, 3] [2] [1, 3] True
D. [1] [2] [3] False

57 What is printed by these functions?

PYTHON
funcs = []
for i in range(3):
    funcs.append(lambda x=i: x + i)
i = 10
print([f() for f in funcs])

Functions Hard
A. [12, 12, 12]
B. [20, 20, 20]
C. [10, 11, 12]
D. [0, 2, 4]

58 In Python 3.10 or later, which call raises TypeError?

PYTHON
def f(a, /, b=2, *, c=3):
    return a + b + c

Functions Hard
A. f(1, c=3)
B. f(1, 2, c=3)
C. f(1, b=2, c=3)
D. f(a=1, b=2, c=3)

59 What is printed after applying these decorators?

PYTHON
def deco(tag):
    def outer(fn):
        def inner():
            return tag + fn() + tag
        return inner
    return outer

@deco('A')
@deco('B')
def f():
    return 'X'

print(f())

Functions Hard
A. AABXB
B. ABXBA
C. BBAAX
D. BAXAB

60 What is printed by this generator code?

PYTHON
def gen():
    yield 1
    return 7

g = gen()
a = next(g)
try:
    next(g)
except StopIteration as e:
    b = e.value
print(a, b)

Functions Hard
A. StopIteration
B. 1 7
C. 1 None
D. 1 0