Question Bank
137 questions total.
Last updated Dec. 10th, 2024.
1. Trace the following algorithm and write down the output (if any). What does the algorithm do?
For tracing, assume the input is 10 - 4
.
1. Read a
2. Read b
3. Repeat 4-6 until b is 0:
4. let temp = b
5. b = a mod b
6. a = temp
7. Write a
2. Trace the following algorithm and write down the output (if any). What does the algorithm do?
For tracing, assume the input is 13
.
1. Read number
2. If number <= 1:
3. Write "No"
4. Else if number == 2:
5. Write "Yes"
6. Else:
7. let i = 2
8. let sqrt = square root of number in integer (whole number)
9. Repeat 10-14 as long as i <= sqrt:
10. let m = number mod i
11. if m == 0:
12. Write "No"
13. Go to line 16
14. increment i
15. Write "Yes"
16. End
3. What's the output of this code?
print ("Hello, world!")
Hello, world!
- Error. The code doesn't run.
- Code runs but no output.
4. What's the output of this code?
Print("Hello, World!")
Hello, World!
- Error. The code doesn't run.
- Code runs but no output.
5. What's the output of this code?
print('Hi there, I'm Data!')
Hi there, I'm Data!
- Error. The code doesn't run.
- Code runs but no output.
6. What's the output of this code?
print("Hi there, I'm Data!")
Hi there, I'm Data!
- Error. The code doesn't run.
- Code runs but no output.
7. What's the output of this code?
number = 6
print("number is {number}.")
number is 6.
number is {number}.
- Error. The code doesn't run.
- Code runs but no output.
8. What's the output of this code?
number = 6
print(f"number is {number}.")
number is 6.
number is {number}.
- Error. The code doesn't run.
- Code runs but no output.
9. What's the output of this code?
def fact():
print("TED talks are highly overrated!")
TED talks are highly overrated!
- Error. Code doesn't run.
- Code runs but no output.
10. What does the following function return? Choose the best answer.
def yell():
print("yellinggggggggg!")
yellinggggggggg!
None
- Nothing
- There's syntax error.
11. Assuming we have the following code, which code snippet outputs the desired table:
captain = "Jean-Luc Picard"
first_officer = "William Riker"
ship_counsellor = "Deanna Troi"
Desired Table:
Captain | First Officer | Ship Counsellor
Jean-Luc Picard | William Riker | Deanna Troi
-
table = f"""\ {'Captain':^16} | {'First Officer':^16} | {'Ship Counsellor':^16} {captain:^16} | {first_officer:^16} | {ship_counsellor:^16} """ print(table)
-
table = f"""\ {'Captain':<16} | {'First Officer':<16} | {'Ship Counsellor':<16} {captain:<16} | {first_officer:<16} | {ship_counsellor:<16} """ print(table)
-
table = f"""\ Captain | First Officer | Ship Counsellor {captain} | {first_officer} | {ship_counsellor} """ print(table)
12. What's the output of this code if the input is 15
?
number = input("Give me a number: ")
half = number // 2
print(f"half of {number} is {half}.")
half of 15 is 7.
half of 15 is 7.5.
- Error. The code doesn't run.
- Code runs, but no output.
13. What's wrong with the following code and how to fix it?
number = input("Give me a number: ")
half = number // 2
print(f"half of {number} is {half}.")
14. According to Python's conventions, which one is a good name for a variable?
largetsNumber
LARGEST_NUMBER
largest_number
LargestNumber
15. According to Python's conventions, which one is a good name for a constant?
gravitational_pull
Gravitational_Pull
GravitationalPull
GRAVITATIONAL_PULL
16. Which one is correct?
- We can have constants in Python
- Only if we follow the naming rule (all uppercase), we can have constants in Python
- Even if we follow the naming rule, we still can't have constants in Python
17. What's the output of the following code?
CONSTANT = 10
CONSTANT = CONSTANT + 1
print(f"{CONSTANT=}")
- Error. Code doesn't run
CONSTANT=11
CONSTANT=10
- Code runs but no output
18. How to get the last character of a string variable named text
? Choose all correct answers.
text[0]
text[-1]
text[len(text)]
text[len(text)-1]
19. Write a Python program that takes a text from the user and prints out the first, middle, and last characters.
Examples:
for Chimichanga
:
First character: 'C' - Middle character: 'c' - Last character: 'a'
for Batman
:
First character: 'B' - Middle character: 'm' - Last character: 'n'
20. Write a Python program that achieves the previous goal, but now with a function named get_character_at_position
that takes two arguments: text
and position
and returns the character of the text
at position position
. The output of both programs must be the same.
21. Which of the following is a valid variable name in Python?
2nd_place
first-place
firstPlace
None
22. Which of the following is NOT a valid Python variable name?
total_sum
class
_variable
myVar
23. Which of the following contains an invalid character for a variable name?
amount_owed
total$amount
_count
var123
24. Which of the following variable names is invalid in Python?
age
Age
AGE
def
25. Which of the following is a valid function name in Python?
2calculate()
Calculate_sum()
calculate-sum()
def()
26. Which of the following follows Python's recommended convention for naming functions?
calculateArea()
CalculateArea()
calculate_area()
27. Which of the following contains an invalid character for a function name?
get_value()
calculate-sum()
fetchData()
_process()
28. What's true about the following function? Select all correct answers.
def cut_number_in_half(number: int) -> int:
return number / 2
- It's a void function
- It's a value-returning function
- Its name follows Python's conventions
- Its name does NOT follow Python's conventions
- It returns an
int
as the type hint suggests - It does NOT return an
int
, producing an error in runtime - It does NOT return an
int
, but the code runs ok
29. What's the output of the following code for this input: 10
and 2
?
number_1 = int(input("Give me a number: "))
number_2 = float(input("Give me another one: "))
mul = number_1 * number_2
print(f"result is {mul} and the type is {type(mul)}.")
result is 20.0 and the type is <class 'float'>.
result is 20 and the type is <class 'int'>.
result is 20.0 and the type is <class 'int'>.
result is 20 and the type is <class 'float'>.
30. What's the output of the following code for this input: 10
and 2
?
number_1 = int(input("Give me a number: "))
number_2 = int(input("Give me another one: "))
div = number_1 / number_2
print(f"result is {div} and the type is {type(div)}.")
result is 5.0 and the type is <class 'float'>.
result is 5.0 and the type is <class 'int'>.
result is 5 and the type is <class 'float'>.
result is 5 and the type is <class 'int'>.
31. Which of the following functions are value-returning? Select all correct answers.
int()
print()
type()
input()
32. Which of the following functions are void functions? Select all correct answers.
float()
print()
strip()
replace()
33. Write a Python program that reads a number as the radius and calculates and prints out the area of the circle. Declare PI as a constant with the value of 3.14
in the program. The area must have the precision of 2 digits after the decimal point. No need to use functions.
Example:
Give me the radius: 5
The area of the circle: 78.50
Give me the radius: 6.5
The area of the circle: 132.66
34. Write the same program to calcualte the area of a circle, but now with a function named calculate_circle_area
that take the radius and returns the area. The output must be identical to that of the previous problem.
35. Algorithms generally have which of the following? Select all that apply.
- Inputs
- Outputs
- Instructions
- Terminal
- Graphical User Interface
- Purpose
36. Which of the following are optional when it comes to algorithms? Select all that apply.
- Inputs
- Outputs
- Purpose
- Instructions
37. Which of the following is NOT a building block of algorithms? Select all that apply.
- Loops
- Constants
- Classes
- Functions
- Decision
- Variables
38. Pseudocode uses building blocks such as Functions.
- True
- False
39. What does completeness mean in an algorithm? Choose the best answer.
- The algorithm finishes successfully
- The algorithm works
- The algorithm is finite
- The algorithm produces correct outputs for all possible inputs
40. What does finiteness mean in an algorithm? Choose the best answer.
- The algorithm works
- The algorithm stops after a finite number of steps
- The algorithm succeeds fast
- The algorithm is efficient
41. What does it mean to trace an algorithm?
42. Which one is an example of Non-volatile memory in a computer? Select all that apply.
- Hard disk
- RAM
- Flash Memory
- Database
43. Which one is an example of volatile memory in a computer? Select all that apply.
- Hard disk
- RAM
- Flash Memory
- Database
44. What does the ALU do in a computer?
- Processing inputs
- Creating pixels
- Doing math and logic
- Decoding instructions
45. Which one is not a feature of high level programming languages? Select all that apply.
- Easier to read
- Slow execution
- Closer to the CPU
- Fast execution
46. Which one is not a feature of low level programming languages? Select all that apply.
- Easier to read
- Slow execution
- Closer to the CPU
- Fast execution
47. Python is a ________ level programming language.
- High
- Low
48. Assembly is a ________ level programming language.
- High
- Low
49. Python is a(n) _________ programming language.
- Compiled
- Interpreted
50. C++ is a(n) _________ programming language.
- Compiled
- Interpreted
51. Interpreters turn programs into machine code in runtime.
- True
- False
52. Compilers turn programs into machine code in runtime.
- True
- False
53. Which functions are among Python's built-in functions? Select all that apply.
print
type
sin
float
54. Which functions are among Python's functions from the standard library? Select all that apply.
print
type
sin
float
sqrt
55. You need to explicitly import functions from the standard library before using them in your program.
- True
- False
56. You need to explicitly import built-in functions before using them in your program.
- True
- False
57. Which one is the modulo operator in Python?
/
//
%
\\
**
58. Which one is the exponent operator in Python?
/
//
%
\\
**
59. Which one is the division operator in Python?
/
//
%
\\
**
60. Which one is the integer division operator in Python?
/
//
%
\\
**
61. What is the output of this code?
number = 4
result = number / 2
print(result)
2
2.0
- Error
- No output
62. What is the output of this code?
number = 4
result = number // 2
print(result)
2
2.0
- Error
- No output
63. What is the output of this code?
number = 4
result = number % 2
print(result)
0
0.0
- Error
- No output
64. What is the output of this code?
number = 4.0
result = number % 2
print(result)
0
0.0
- Error
- No output
65. What is the output of this code?
a = 2
b = 3.0
result = a + b
print(type(result))
<class 'float'>
<class 'int'>
<class 'bool'>
<class 'str'>
- No output
- Error
66. What is the output of this code?
a = 2
b = 3
result = a + b
print(type(result))
<class 'float'>
<class 'int'>
<class 'bool'>
<class 'str'>
- No output
- Error
67. What is the output of this code?
a = 2
b = '3'
result = a + b
print(type(result))
<class 'float'>
<class 'int'>
<class 'bool'>
<class 'str'>
- No output
- Error
68. What is the output of this code?
a = '2'
b = '3'
result = a + b
print(type(result))
<class 'float'>
<class 'int'>
<class 'bool'>
<class 'str'>
- No output
- Error
69. What is the output of this code?
a = '2'
b = '3'
result = a + b
print(result)
- No output
- Error
5
23
70. What is the output of this code?
a = True
b = '3'
result = a + b
print(result)
- No output
- Error
True
3
71. A variable data type can change in a Python program.
- True
- False
72. What's the data type of the variable result
?
a = 2
b = 3.2
result = a + b
int
float
bool
str
73. What's the data type of the variable result
?
a = 2
b = 3.2
result = a - b
int
float
bool
str
74. What's the data type of the variable result
?
a = 2
b = 3.2
result = a * b
int
float
bool
str
75. What's the data type of the variable result
?
a = 2
b = 3.2
result = a / b
int
float
bool
str
76. What's the data type of the variable result
?
a = 2
b = 3.2
result = a // b
int
float
bool
str
77. What's the data type of the variable result
?
a = 2
b = 3.2
result = a % b
int
float
bool
str
78. What's the data type of the variable result
?
a = 2
b = 3.2
result = b % a
int
float
bool
str
79. What's the data type of the variable result
?
a = 2
b = 3.2
result = a ** b
int
float
bool
str
80. What's the data type of the variable result
?
a = 2
b = 3.2
result = b ** a
int
float
bool
str
81. What's the output of this program?
a = 2
str_a = str(a)
print(type(str_a), str_a)
<class 'str'> 2.0
<class 'str'> 2
<class 'int'> 2.0
<class 'int'> 2
- No output
- Error
82. What's the output of this program?
a = 2.5
str_a = str(a)
print(type(str_a), str_a)
<class 'str'> 2.5
<class 'str'> 2
<class 'float'> 2.5
<class 'float'> 2.0
- No output
- Error
83. What's the output of this program?
a = "5.5"
int_a = int(a)
print(type(int_a), int_a)
<class 'int'> 5
<class 'int'> 5.5
<class 'str'> 5
<class 'str'> 5.5
- No output
- Error
84. What's the output of this program?
a = "5"
int_a = int(a)
print(type(int_a), int_a)
<class 'int'> 5
<class 'int'> 5.0
<class 'str'> 5
<class 'str'> 5.0
- No output
- Error
85. What's the output of this program?
a = "5.6"
float_a = float(a)
print(type(float_a), float_a)
<class 'float'> 5
<class 'float'> 5.6
<class 'str'> 5
<class 'str'> 5.6
- No output
- Error
86. What's the output of this program?
a = "5.6"
result = bool(a)
print(type(result), result)
<class 'bool'> True
<class 'bool'> False
- No output
- Error
87. What's the output of this program?
a = "0.0"
result = bool(a)
print(type(result), result)
<class 'bool'> True
<class 'bool'> False
- No output
- Error
88. What's the output of this program?
a = "False"
result = bool(a)
print(type(result), result)
<class 'bool'> True
<class 'bool'> False
- No output
- Error
89. What's the output of this program?
a = " "
result = bool(a)
print(type(result), result)
<class 'bool'> True
<class 'bool'> False
- No output
- Error
90. What's the output of this program?
a = ""
result = bool(a)
print(type(result), result)
<class 'bool'> True
<class 'bool'> False
- No output
- Error
91. Write a Python program that takes a diameter from the input and prints out the area of the circle in 2 digits precision. Example:
Enter diameter: 9.8
Area of the circle: 75.43
92. In the following function definition, how many variables are local to the function scope?
def add_2_numbers(x: float, y: float) -> None:
the_sum = x + y
print(the_sum)
- 0
- 1
- 2
- 3
- 4
93. In the following function definition, what do we call x
and y
?
def add_2_numbers(x: float, y: float) -> None:
the_sum = x + y
print(the_sum)
- Arguments
- Parameters
- Headers
- Type Hints
94. In the following function definition, what do we call float
and None
?
def add_2_numbers(x: float, y: float) -> None:
the_sum = x + y
print(the_sum)
- Arguments
- Parameters
- Headers
- Type Hints
95. In the following function definition and call, what do we call 2
and 3.5
?
def add_2_numbers(x: float, y: float) -> None:
the_sum = x + y
print(the_sum)
add_2_numbers(2, 3.5)
- Arguments
- Parameters
- Headers
- Type Hints
96. In the following function definition, what does the function add_2_numbers
return?
def add_2_numbers(x: float, y: float) -> None:
the_sum = x + y
print(the_sum)
float
int
None
str
97. What is the output of this program?
def add_2_numbers(x: float, y: float) -> None:
the_sum = x + y
print(the_sum)
result = add_2_numbers(3, 4.5)
print(f"result is {result:.2f}")
-
7.5 result is 7.5
-
7.5 result is None
- Error
- No output
98. What is the output of this program?
def add_2_numbers(x: float, y: float) -> None:
the_sum = x + y
print(the_sum)
add_2_numbers(3)
3
3.0
- Error
- No output
99. What is the output of this program?
def add_2_numbers(x: float, y: float) -> None:
the_sum = x + y
print(the_sum)
add_2_numbers()
0
0.0
- Error
- No output
100. In the following code, what do we call the description on line 2?
1. def add_2_numbers(x: float, y: float) -> None:
2. "Adds two numbers"
3. the_sum = x + y
4. print(the_sum)
- Documentation
- docstring
- Function definition
- Fundtion body
101. What's the output of the following code?
def subtract(a: int, b: int) -> None:
sub = a - b
a = 10
b = 12
subtract(a, b)
print(f"Result of subtraction is {sub}")
Result of subtraction is -2
Result of subtraction is 2
- Error
- No output
102. What will be printed when this code segment runs?
count = 4
while count != 0 and count < 10:
count -= 1
if count == 2:
break
print(count)
- 0
- 1
- 2
- 3
- This is an infinite loop
103. What will be printed when this code segment runs?
num = 7
while num <= 10:
if num % 2 == 0:
num += 1
else:
num -= 2
print(num)
- 5
- 6
- 9
- 10
- This is an infinite loop
104. What will be printed when this code segment runs?
value = 1
while value < 100:
value *= 3
if value > 30:
break
print(value)
- 27
- 30
- 81
- 100
- This is an infinite loop
105. What will be printed when this code segment runs?
n = 1
while n != 5:
n += 1
if n % 2 == 0:
n += 1
print(n)
- 4
- 5
- 6
- 7
- This is an infinite loop
106. What kind of error this code segment has?
name = "Batman"
print(name[0], name[5], name[10])
- syntax error
- semantic error
- run-time error
- logic error
- no error
107. What kind of error this code segment has?
from math import pi
def area(diameter: float) -> float
return pi * (diameter / 2) ** 2
- syntax error
- semantic error
- run-time error
- logic error
- no error
108. What kind of error this code segment has?
def add(a: int, b: int) -> int:
return a + b
def main() -> None:
print("program starts")
add(5)
print("program ends")
main()
- syntax error
- semantic error
- run-time error
- logic error
- no error
109. What kind of error this code segment could have, assuming the user enters a valid integer?
from math import sqrt
def main() -> None:
number = int(input("Enter number: "))
result = sqrt(number)
print(f"Square root of {number} is {result:.2f}")
main()
- syntax error
- semantic error
- run-time error
- logic error
- no error
110. What kind of error this code segment could have, assuming the user enters valid floats?
def floor_division(a: float, b: float) -> float:
return a // b
def main() -> None:
a = float(input("Enter number 1: "))
b = float(input("Enter number 2: "))
print(f"Floor division of {a} and {b} is {floor_division(a, b):.2f}")
main()
- syntax error
- semantic error
- run-time error
- logic error
- no error
111. What kind of error this code segment could have, assuming the user enters valid integers?
count = 0
number = int(input("Enter a number: "))
while number != -1:
num = int(input("Enter a number: "))
count += 1
print(f"You entered the total of {count} numbers")
- syntax error
- semantic error
- run-time error
- logic error
- no error
112. Implement the pseudocode in question 1 in Python. The algorithm finds the Greatest Common Divisor (GCD) of two integers. Use a while
loop.
113. Implement the pseudocode in question 2 in Python. The algorithm prints Yes
if the input is a prime number, and No
if it's not. Use a while
loop and break
.
114. What kind of error this code segment could have, assuming the user enters a valid integer?
x = int(input("Enter a number: "))
if x = 2:
print("You guessed correctly")
else:
print("You guessed incorrectly")
- syntax error
- semantic error
- run-time error
- logic error
- no error
115. For all possible values of x
, y
, and z
, which expression is equivalent to the following? (i.e.
they both evaluate to the same value)
not (x != y) and y == z
x < y and y < z
x > y and y >= z
x != y or y == z
x == y or y <= z
x == y and y == z
116. For all possible values of x
, y
, and z
, which expression is equivalent to the following? (i.e.
not (x <= y or y < z)
x < y and y < z
x > y and y >= z
x != y or y == z
x == y or y <= z
x == y and y == z
117. For all possible values of x
, y
, and z
, which expression is equivalent to the following? (i.e.
(y < z or y == z) or x == y
x < y and y < z
x > y and y >= z
x != y or y == z
x == y or y <= z
x == y and y == z
118. For all possible values of x
, y
, and z
, which expression is equivalent to the following? (i.e.
not (x >= y) and not (y >= z)
x < y and y < z
x > y and y >= z
x != y or y == z
x == y or y <= z
x == y and y == z
119. For all possible values of x
, y
, and z
, which expression is equivalent to the following? (i.e.
not (x == y and y != z)
x < y and y < z
x > y and y >= z
x != y or y == z
x == y or y <= z
x == y and y == z
120. What is the output of the following code segment?
def starfleet_rank(points: int) -> str:
"""Convert points to a Starfleet rank."""
if points >= 50:
rank = "Ensign"
elif points >= 60:
rank = "Lieutenant"
elif points >= 70:
rank = "Commander"
else:
rank = "Captain"
return rank
def main() -> None:
print(starfleet_rank(65))
print(starfleet_rank(75))
print(starfleet_rank(85))
main()
121. What is the output of the following code segment?
def starfleet_rank(points: int) -> str:
"""Convert points to a Starfleet rank."""
if points >= 50:
rank = "Ensign"
if points >= 60:
rank = "Lieutenant"
if points >= 70:
rank = "Commander"
else:
rank = "Captain"
return rank
def main() -> None:
print(starfleet_rank(55))
print(starfleet_rank(75))
print(starfleet_rank(85))
main()
122. What is the output of the following code segment?
a = [1, 2, 3]
b = a
b.append(4)
print(a, b)
123. What is the output of the following code segment?
a = 5
b = a
b += 1
print(a, b)
124. What is the output of the following code segment?
def some_func(param: list) -> None:
param.append(5 + 6)
def main() -> None:
my_list = list(range(5))
some_func(my_list)
print(my_list)
main()
125. What is the output of the following code segment?
a = [1, 2, 3]
b = a
b = list(range(5))
b.append(4)
print(a, b)
126. What is the output of the following code segment?
a = [1, 2, 3]
b = a
b = list(range(5))
print(id(a) == id(b))
127. What is the output of the following code segment?
a = [1, 2, 3]
b = a
b.reverse()
print(id(a) == id(b))
128. What is the output of the following code segment?
def some_func(param: list) -> int:
param.append(5 + 6)
return id(param)
def main() -> None:
my_list = list(range(5))
ref = some_func(my_list)
print(id(my_list) == ref)
main()
129. What is the output of the following code segment?
def some_func(param: list) -> int:
param = [1]
return id(param)
def main() -> None:
my_list = list(range(5))
ref = some_func(my_list)
print(id(my_list) == ref)
main()
130. What is the output of the following code segment?
name = "batman"
name.upper()
print(name)
131. What is the output of the following code segment?
name = "batman"
name = name.upper()
print(name)
132. What is the output of the following code segment?
name = "batman"
ref_1 = id(name)
name = name.upper()
ref_2 = id(name)
print(ref_1 == ref_2)
133. What is the output of the following code segment?
name = "batman"
name[0] = "B"
print(name)
134. What is the output of the following code segment?
def some_func(param: str) -> None:
param += "..."
def main() -> None:
text = "to be continued"
some_func(text)
print(text)
main()
135. What is the output of the following code segment?
def some_func(param: str) -> str:
param += "..."
return param
def main() -> None:
text = "to be continued"
text = some_func(text)
print(text)
main()
136. What is the output of the following code segment?
def some_func(param: str) -> int:
param += "..."
return id(param)
def main() -> None:
text = "to be continued"
ref = some_func(text)
print(id(text) == ref)
main()
137. What is the output of the following code segment?
def some_func(param: int) -> None:
param += 5
def main() -> None:
param = 5
some_func(param)
print(param)
main()