Unit 1: Basics of Python - Practice Quiz

CAP776 — Programming In Python 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is Python IDLE mainly used for?

Introduction to Python IDLE Easy
A. Creating presentation slides
B. Writing and running Python code
C. Editing digital photographs
D. Designing database tables

2 What does the Python Shell in IDLE allow you to do?

Introduction to Python IDLE Easy
A. Compile programs into hardware
B. Execute Python statements interactively
C. Manage computer user accounts
D. Create spreadsheet formulas

3 In a Jupyter Notebook, Python code is commonly written inside what?

Jupyter Notebook Easy
A. Slide panels
B. Data sheets
C. Text layers
D. Code cells

4 Which feature allows a Jupyter Notebook to combine code with formatted explanatory text?

Jupyter Notebook Easy
A. Markdown cells
B. System cells
C. Compiler cells
D. Binary cells

5 What is Google Colab?

Google Colab Easy
A. A relational database server
B. A Python game engine
C. A desktop operating system
D. A cloud-based notebook environment

6 Where does Google Colab commonly allow users to save notebooks?

Google Colab Easy
A. Google Drive
B. Windows Registry
C. Python Shell
D. System Clipboard

7 Which file extension is normally used when saving a Python program?

Creating, saving, and executing a Python file Easy
A. .py
B. .jpg
C. .csv
D. .html

8 Which command executes a file named program.py from a command line where Python is available?

Creating, saving, and executing a Python file Easy
A. save program.py
B. open program.py
C. python program.py
D. edit program.py

9 Which built-in function displays output in Python?

User input/output operations Easy
A. print()
B. type()
C. input()
D. range()

10 What type of value does input() return by default?

User input/output operations Easy
A. str
B. int
C. bool
D. float

11 Which Python data type represents a whole number such as ?

Numeric data types Easy
A. int
B. str
C. bool
D. float

12 Which Python data type represents a number such as ?

Numeric data types Easy
A. bool
B. float
C. int
D. str

13 Which operator is used for exponentiation in Python?

Operators Easy
A. //
B. %%
C. ==
D. **

14 What is the result of the Python expression 10 % 3?

Operators Easy
A. 3.33
B. 3
C. 1
D. 2

15 Which keyword begins a basic conditional statement in Python?

Conditional statements Easy
A. import
B. for
C. def
D. if

16 Which keyword provides an alternative block when an if condition is false?

Conditional statements Easy
A. break
B. else
C. return
D. while

17 Which loop is commonly used to iterate over items in a sequence?

Python loops Easy
A. if block
B. def block
C. else block
D. for loop

18 How many values are produced by range(4)?

Python loops Easy
A. One value
B. Five values
C. Four values
D. Three values

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

User-defined functions Easy
A. define
B. def
C. make
D. func

20 Which keyword sends a value back to the code that called a function?

User-defined functions Easy
A. input
B. print
C. pass
D. return

21 In Python IDLE, which action allows a programmer to test a single statement immediately and observe its result?

Introduction to Python IDLE Medium
A. Running it from the file menu
B. Writing it in the Shell window
C. Opening it in the debugger
D. Saving it as a text file

22 A programmer has written several lines of Python code in IDLE and wants to execute them repeatedly. Which approach is most suitable?

Introduction to Python IDLE Medium
A. Copy the output into the editor
B. Save the code in an editor window
C. Enter each line in the Shell
D. Close IDLE after entering the code

23 In a Jupyter Notebook, what is the main advantage of placing code and explanatory text in separate cells?

Jupyter Notebook Medium
A. It organizes code and documentation
B. It removes the need for Python
C. It prevents all syntax errors
D. It makes every cell run automatically

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?

Jupyter Notebook Medium
A. Markdown automatically defines variables
B. The notebook repeats every cell
C. Cells share the active kernel state
D. Cells are converted into one file

25 Which feature makes Google Colab particularly useful for collaborating on Python notebooks?

Google Colab Medium
A. It requires every user to install Python
B. It disables notebook comments
C. It supports shared online notebooks
D. It runs only on local computers

26 A Colab notebook loses the value of a variable after the runtime is disconnected. What is the most likely reason?

Google Colab Medium
A. The notebook cannot execute assignments
B. Colab converts variables into comments
C. The variable name was too short
D. The variable was stored only in memory

27 Which filename is most appropriate for a Python program that calculates a student's average score?

Creating, saving, and executing a Python file Medium
A. average_score.py
B. average_score.python
C. average score.py
D. average_score.txt

28 A file named main.py contains print("Ready"). Which command executes it from a terminal in the same directory?

Creating, saving, and executing a Python file Medium
A. run main.py
B. open main.py
C. python main.py
D. execute main.py

29 What is displayed by this code?

PYTHON
name = input("Name: ")
print("Hello", name)



if the user enters Mira?

User input/output operations Medium
A. HelloMira
B. Name: Hello Mira
C. Hello Mira
D. Mira Hello

30 Which statement correctly reads an integer from the user and stores it in age?

User input/output operations Medium
A. age = input().integer()
B. age = input(int())
C. age = int(input())
D. age = integer(input())

31 What are the values and types of a and b after this code executes?

PYTHON
a = 7 / 2
b = 7 // 2

Numeric data types Medium
A. a is 3.5, b is 3
B. a is 3, b is 3.5
C. a is 3.5, b is 4
D. a is 3.0, b is 3.0

32 What is the value and data type of result after this statement?

PYTHON
result = 5 + 2.0

Numeric data types Medium
A. The integer 7
B. The string "7.0"
C. The boolean True
D. The float 7.0

33 What is the value of answer after this code?

PYTHON
answer = 2 + 3 * 4 ** 2

Operators Medium
A. 144
B. 26
C. 50
D. 80

34 What is printed by the following code?

PYTHON
x = 10
print(x > 5 and x < 10)

Operators Medium
A. False
B. An error message
C. 10
D. True

35 What is printed by this code?

PYTHON
marks = 72
if marks >= 80:
    print("A")
elif marks >= 60:
    print("B")
else:
    print("C")

Conditional statements Medium
A. A B
B. A
C. B
D. C

36 Which condition correctly checks whether an integer n is even?

Conditional statements Medium
A. n / 2 == 0
B. n * 2 == 0
C. n // 2 == 0
D. n % 2 == 0

37 What is printed by this code?

PYTHON
for i in range(2, 8, 2):
    print(i, end=" ")

Python loops Medium
A. 2 4 6 8
B. 0 2 4 6
C. 2 4 6
D. 2 3 4 5 6 7

38 What is the final value of total after this code executes?

PYTHON
total = 0
for n in [3, 5, 2]:
    total += n

Python loops Medium
A. 15
B. 8
C. 10
D. 5

39 What is printed by this program?

PYTHON
def calculate(x, y=2):
    return x * y

print(calculate(4))

User-defined functions Medium
A. 4
B. 8
C. 6
D. 2

40 Which function correctly returns the larger of two numbers a and b?

User-defined functions Medium
A. def larger(a, b): return a if a > b else b
B. def larger(a, b): return a if a < b else b
C. def larger(a, b): print(a if a > b else b)
D. def larger(a, b): return a + b

41 Under IDLE's default configuration, what normally happens when Run → Run Module (F5) is selected for a saved program?

Introduction to Python IDLE Hard
A. A separate terminal opens, previous definitions are imported, and the file executes as a library.
B. The existing Shell state is retained, and the file executes with __name__ set to its filename.
C. The editor compiles the file without executing it, while syntax errors appear only in the Shell.
D. The Shell restarts, previous user definitions are cleared, and the file executes with __name__ == "__main__".

42 The following compound statement is entered directly into IDLE's interactive Shell:

PYTHON
for i in range(2):
    print(i)



After entering the indented line, what must normally be done to execute the statement?

Introduction to Python IDLE Hard
A. Enter a blank line to terminate the interactive compound statement.
B. Press Ctrl+D to compile and execute the current statement.
C. Press Backspace until the cursor returns to the first column.
D. Add a semicolon after the indented 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?

Jupyter Notebook Hard
A. 20, because Jupyter recompiles Cell 1 before executing Cell 3.
B. 10, because each notebook cell has an independent global namespace.
C. 10, because the function captures the original integer when defined.
D. 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?

Jupyter Notebook Hard
A. The variables remain available because displayed outputs preserve the kernel's namespace.
B. The outputs disappear automatically, but imported modules remain available in the new kernel.
C. The displayed outputs can remain visible even though the corresponding variables no longer exist.
D. The variables and outputs remain synchronized until one of the cells is edited.

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?

Google Colab Hard
A. The /content file is lost, while the file written to mounted Google Drive persists.
B. Both files persist because every file created by a notebook is stored with the notebook.
C. Both files are lost because deleting a runtime also deletes the associated Drive content.
D. The Drive file is lost, while the file in /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?

Google Colab Hard
A. The notebook and permanent access to the owner's mounted Drive through the original session.
B. The notebook and its saved outputs, but not the owner's runtime files or Drive authorization.
C. The notebook and a cloned runtime containing every file previously stored under /content.
D. The notebook source only, because Colab removes every saved output when sharing is enabled.

47 A file named app.py contains:

PYTHON
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"?

Creating, saving, and executing a Python file Hard
A. Direct execution prints only __main__; importing prints app and run.
B. Direct execution prints __main__ and run; importing prints only app.
C. Direct execution prints only app; importing prints __main__ and run.
D. Direct execution prints app and run; importing prints only __main__.

48 A project has this structure:

TEXT
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?

Creating, saving, and executing a Python file Hard
A. Neither file, because scripts cannot open files outside their own package directory.
B. project/tools/data.txt, because relative paths always start from the script's directory.
C. project/data.txt, because the relative path starts from the process's working directory.
D. Both files, because open searches the working directory and then the script directory.

49 The user enters 3 4 with two spaces when this program runs:

PYTHON
x, y = input().split()
print(x + y, int(x) + int(y), sep=":", end="!")



What is the exact output?

User input/output operations Hard
A. 3 4:7!
B. 7:34!
C. 34:7!
D. 34:34!

50 What exact character sequence is produced by this code, ignoring the prompt used by the environment?

PYTHON
print(*(str(i) for i in range(3)), sep=":", end=";")
print("X", end="")

User input/output operations Hard
A. 012;X with no trailing newline
B. 0:1:2;X with no trailing newline
C. 0:1:2 X followed by one newline
D. 0:1:2;X followed by one newline

51 What does the following program print?

PYTHON
print(-17 // 5, -17 % 5)

Numeric data types Hard
A. -3 -2
B. -3 3
C. -4 -2
D. -4 3

52 What is printed by this code on standard Python implementations using IEEE 754 binary floating-point?

PYTHON
n = 2 ** 53
print(float(n) == n, float(n + 1) == n + 1, float(n + 2) == n + 2)

Numeric data types Hard
A. True True False
B. True False True
C. True False False
D. False False True

53 What does the following expression produce?

PYTHON
print(2 ** 3 ** 2, -3 ** 2, (-3) ** 2)

Operators Hard
A. 512 -9 9
B. 64 9 -9
C. 512 9 9
D. 64 -9 9

54 What exact output is produced?

PYTHON
def mark(v):
    print(v, end="")
    return v

result = mark(0) and mark(1) or mark(2)
print(":", result, sep="")

Operators Hard
A. 01:1
B. 0:0
C. 02:2
D. 012:2

55 What is printed by this program?

PYTHON
def middle():
    print("F", end="")
    return 2

if 1 < middle() < 3:
    print("T")
else:
    print("N")

Conditional statements Hard
A. FFN followed by a newline
B. FT followed by a newline
C. FFT followed by a newline
D. FN followed by a newline

56 What does this code print?

PYTHON
if x := 3 > 2:
    print(x, type(x).__name__)
else:
    print("other")

Conditional statements Hard
A. It raises SyntaxError.
B. True bool
C. 3 int
D. False bool

57 What is printed by this nested loop?

PYTHON
for n in range(2, 5):
    for d in range(2, n):
        if n % d == 0:
            break
    else:
        print(n, end="")

Python loops Hard
A. 34
B. 234
C. 24
D. 23

58 What value is printed?

PYTHON
total = 0
for i in range(4):
    for j in range(i):
        if j == 1:
            break
        total += i + j
    else:
        total += 10
print(total)

Python loops Hard
A. 46
B. 16
C. 26
D. 36

59 What is printed by this program?

PYTHON
def collect(x, items=[]):
    items.append(x)
    return items

a = collect(1)
b = collect(2)
a.append(3)
print(b)

User-defined functions Hard
A. [1, 2, 3]
B. [2]
C. [2, 3]
D. [1, 2]

60 What is printed by this code?

PYTHON
functions = []
for i in range(3):
    functions.append(lambda x, i=i: x + i)

i = 10
print([f(1) for f in functions])

User-defined functions Hard
A. [1, 1, 1]
B. [11, 11, 11]
C. [1, 2, 3]
D. [10, 11, 12]