Unit 1: Basics of Python - Practice Quiz
1 What is Python IDLE mainly used for?
2 What does the Python Shell in IDLE allow you to do?
3 In a Jupyter Notebook, Python code is commonly written inside what?
4 Which feature allows a Jupyter Notebook to combine code with formatted explanatory text?
5 What is Google Colab?
6 Where does Google Colab commonly allow users to save notebooks?
7 Which file extension is normally used when saving a Python program?
8
Which command executes a file named program.py from a command line where Python is available?
save program.py
open program.py
python program.py
edit program.py
9 Which built-in function displays output in Python?
print()
type()
input()
range()
10
What type of value does input() return by default?
str
int
bool
float
11 Which Python data type represents a whole number such as ?
int
str
bool
float
12 Which Python data type represents a number such as ?
bool
float
int
str
13 Which operator is used for exponentiation in Python?
//
%%
==
**
14
What is the result of the Python expression 10 % 3?
3.33
3
1
2
15 Which keyword begins a basic conditional statement in Python?
import
for
def
if
16
Which keyword provides an alternative block when an if condition is false?
break
else
return
while
17 Which loop is commonly used to iterate over items in a sequence?
if block
def block
else block
for loop
18
How many values are produced by range(4)?
19 Which keyword is used to define a function in Python?
define
def
make
func
20 Which keyword sends a value back to the code that called a function?
input
print
pass
return
21 In Python IDLE, which action allows a programmer to test a single statement immediately and observe its result?
22 A programmer has written several lines of Python code in IDLE and wants to execute them repeatedly. Which approach is most suitable?
23 In a Jupyter Notebook, what is the main advantage of placing code and explanatory text in separate cells?
24 A variable is created in one Jupyter code cell and used in a later cell. The later cell works only if the first cell has already been executed. Why?
25 Which feature makes Google Colab particularly useful for collaborating on Python notebooks?
26 A Colab notebook loses the value of a variable after the runtime is disconnected. What is the most likely reason?
27 Which filename is most appropriate for a Python program that calculates a student's average score?
28
A file named main.py contains print("Ready"). Which command executes it from a terminal in the same directory?
29
What is displayed by this code?
name = input("Name: ")
print("Hello", name)
if the user enters Mira?
30
Which statement correctly reads an integer from the user and stores it in age?
31
What are the values and types of a and b after this code executes?
a = 7 / 2
b = 7 // 2
a is 3.5, b is 3
a is 3, b is 3.5
a is 3.5, b is 4
a is 3.0, b is 3.0
32
What is the value and data type of result after this statement?
result = 5 + 2.0
7
"7.0"
True
7.0
33
What is the value of answer after this code?
answer = 2 + 3 * 4 ** 2
34
What is printed by the following code?
x = 10
print(x > 5 and x < 10)
35
What is printed by this code?
marks = 72
if marks >= 80:
print("A")
elif marks >= 60:
print("B")
else:
print("C")
36
Which condition correctly checks whether an integer n is even?
37
What is printed by this code?
for i in range(2, 8, 2):
print(i, end=" ")
38
What is the final value of total after this code executes?
total = 0
for n in [3, 5, 2]:
total += n
39
What is printed by this program?
def calculate(x, y=2):
return x * y
print(calculate(4))
40
Which function correctly returns the larger of two numbers a and b?
41 Under IDLE's default configuration, what normally happens when Run → Run Module (F5) is selected for a saved program?
__name__ set to its filename.
__name__ == "__main__".
42
The following compound statement is entered directly into IDLE's interactive Shell:
for i in range(2):
print(i)
After entering the indented line, what must normally be done to execute the statement?
print statement.
43
The following Jupyter cells are executed in order:
python
# Cell 1
a = 10
def value():
return a
python
# Cell 2
a = 20
python
# Cell 3
print(value())
What is printed?
20, because Jupyter recompiles Cell 1 before executing Cell 3.
10, because each notebook cell has an independent global namespace.
10, because the function captures the original integer when defined.
20, because the function resolves the global variable when called.
44 A Jupyter notebook displays old output beneath several cells. The kernel is then restarted without clearing outputs. Which statement is correct?
45
A Colab notebook writes one file to /content/result.csv and another to /content/drive/MyDrive/result.csv after Google Drive has been mounted. The runtime is later deleted. Which outcome is expected?
/content file is lost, while the file written to mounted Google Drive persists.
/content is restored when Colab reconnects.
46
A Colab notebook with saved outputs is shared with another user. It previously accessed an uploaded file in /content and the owner's mounted Google Drive. What does the recipient automatically receive?
/content.
47
A file named app.py contains:
print(__name__)
if __name__ == "__main__":
print("run")
Assuming a fresh process each time, what is printed by python app.py and then by python -c "import app"?
__main__; importing prints app and run.
__main__ and run; importing prints only app.
app; importing prints __main__ and run.
app and run; importing prints only __main__.
48
A project has this structure:
project/
├── data.txt # contains P
└── tools/
├── data.txt # contains T
└── report.py # calls open("data.txt").read()
From the project directory, the command python tools/report.py is executed. What is read under normal Python path handling?
project/tools/data.txt, because relative paths always start from the script's directory.
project/data.txt, because the relative path starts from the process's working directory.
open searches the working directory and then the script directory.
49
The user enters 3 4 with two spaces when this program runs:
x, y = input().split()
print(x + y, int(x) + int(y), sep=":", end="!")
What is the exact output?
3 4:7!
7:34!
34:7!
34:34!
50
What exact character sequence is produced by this code, ignoring the prompt used by the environment?
print(*(str(i) for i in range(3)), sep=":", end=";")
print("X", end="")
012;X with no trailing newline
0:1:2;X with no trailing newline
0:1:2 X followed by one newline
0:1:2;X followed by one newline
51
What does the following program print?
print(-17 // 5, -17 % 5)
-3 -2
-3 3
-4 -2
-4 3
52
What is printed by this code on standard Python implementations using IEEE 754 binary floating-point?
n = 2 ** 53
print(float(n) == n, float(n + 1) == n + 1, float(n + 2) == n + 2)
True True False
True False True
True False False
False False True
53
What does the following expression produce?
print(2 ** 3 ** 2, -3 ** 2, (-3) ** 2)
512 -9 9
64 9 -9
512 9 9
64 -9 9
54
What exact output is produced?
def mark(v):
print(v, end="")
return v
result = mark(0) and mark(1) or mark(2)
print(":", result, sep="")
01:1
0:0
02:2
012:2
55
What is printed by this program?
def middle():
print("F", end="")
return 2
if 1 < middle() < 3:
print("T")
else:
print("N")
FFN followed by a newline
FT followed by a newline
FFT followed by a newline
FN followed by a newline
56
What does this code print?
if x := 3 > 2:
print(x, type(x).__name__)
else:
print("other")
SyntaxError.
True bool
3 int
False bool
57
What is printed by this nested loop?
for n in range(2, 5):
for d in range(2, n):
if n % d == 0:
break
else:
print(n, end="")
34
234
24
23
58
What value is printed?
total = 0
for i in range(4):
for j in range(i):
if j == 1:
break
total += i + j
else:
total += 10
print(total)
46
16
26
36
59
What is printed by this program?
def collect(x, items=[]):
items.append(x)
return items
a = collect(1)
b = collect(2)
a.append(3)
print(b)
[1, 2, 3]
[2]
[2, 3]
[1, 2]
60
What is printed by this code?
functions = []
for i in range(3):
functions.append(lambda x, i=i: x + i)
i = 10
print([f(1) for f in functions])
[1, 1, 1]
[11, 11, 11]
[1, 2, 3]
[10, 11, 12]
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →