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. Designing database tables
D. Editing digital photographs

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

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

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

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

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

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

5 What is Google Colab?

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

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

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

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

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

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. edit program.py
B. python program.py
C. open program.py
D. save program.py

9 Which built-in function displays output in Python?

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

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

User input/output operations Easy
A. bool
B. str
C. int
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. int
B. float
C. bool
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. 1
B. 3.33
C. 3
D. 2

15 Which keyword begins a basic conditional statement in Python?

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

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

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

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

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

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

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

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

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

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

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

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. Writing it in the Shell window
B. Saving it as a text file
C. Opening it in the debugger
D. Running it from the file menu

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. Close IDLE after entering the code
B. Copy the output into the editor
C. Save the code in an editor window
D. Enter each line in the Shell

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 prevents all syntax errors
C. It removes the need for Python
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. Cells share the active kernel state
B. The notebook repeats every cell
C. Markdown automatically defines variables
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 disables notebook comments
B. It supports shared online notebooks
C. It runs only on local computers
D. It requires every user to install Python

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. Colab converts variables into comments
B. The variable was stored only in memory
C. The variable name was too short
D. The notebook cannot execute assignments

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.py
C. average_score.python
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. execute main.py
C. python main.py
D. open 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. Name: Hello Mira
B. HelloMira
C. Mira Hello
D. Hello Mira

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

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

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 4
B. a is 3.5, b is 3
C. a is 3.0, b is 3.0
D. a is 3, b is 3.5

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. 26
B. 144
C. 50
D. 80

34 What is printed by the following code?

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

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

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. C
C. B
D. A B

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. 0 2 4 6
B. 2 4 6 8
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. 5
C. 10
D. 8

39 What is printed by this program?

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

print(calculate(4))

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

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

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

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. 20, because the function resolves the global variable when called.
D. 10, because the function captures the original integer when defined.

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 outputs disappear automatically, but imported modules remain available in the new kernel.
B. The variables and outputs remain synchronized until one of the cells is edited.
C. The displayed outputs can remain visible even though the corresponding variables no longer exist.
D. The variables remain available because displayed outputs preserve the kernel's namespace.

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

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 a cloned runtime containing every file previously stored under /content.
C. The notebook and its saved outputs, but not the owner's runtime files or Drive authorization.
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 app and run; importing prints only __main__.
B. Direct execution prints only app; importing prints __main__ and run.
C. Direct execution prints only __main__; importing prints app and run.
D. Direct execution prints __main__ and run; importing prints only app.

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. project/tools/data.txt, because relative paths always start from the script's directory.
B. Neither file, because scripts cannot open files outside their own package 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. 7:34!
B. 34:7!
C. 3 4: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. -4 -2
B. -3 3
C. -4 3
D. -3 -2

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 False False
B. True False True
C. False False True
D. True True False

53 What does the following expression produce?

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

Operators Hard
A. 64 9 -9
B. 512 -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. 012:2
B. 01:1
C. 0:0
D. 02: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. False bool
B. True bool
C. 3 int
D. It raises SyntaxError.

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. 23
B. 24
C. 234
D. 34

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. 26
B. 16
C. 36
D. 46

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. [2, 3]
B. [1, 2, 3]
C. [2]
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. [11, 11, 11]
B. [1, 2, 3]
C. [1, 1, 1]
D. [10, 11, 12]