x = 4
x += 3
print(x)
7
What is wrong with this code?
if x > 5
print("Big!")
Missing a colon : after x > 5.
How will the following code run:
A — Runs normally
B — Syntax Error
C — Runtime Error
D — Infinite Loop
print(2 ** 3)
Answer: A — Runs normally
Binary only uses 0 and 1.
What is:
1010
in regular decimal numbers?
10
You try to visit a website and get:
404
What does that usually mean?
Answer: Page Not Found
print("CS" * 3)
CSCSCS
Find the bug:
score = 10
if score = 10:
print("Perfect!")
Answer: It should be ==, not =.
How will the following code run:
A — Runs normally
B — Syntax Error
C — Runtime Error
D — Infinite Loop
print("5" + 5)
Answer: C — Runtime Error
Python can't add a string and an integer.
What does this Boolean expression evaluate to?
True AND (False OR True)
True
You accidentally delete half your code.
What keyboard shortcut might save your life?
Answer: Ctrl + Z ( undo)
nums = [1, 2, 3]
nums[1] = nums[0] + nums[2]
print(nums)
answer: [1,4,3]
Why does this crash?
age = 16
print("Age: " + age)
Answer: You can't concatenate a string and an integer.
How will the following code run:
A — Runs normally
B — Syntax Error
C — Runtime Error
D — Infinite Loop
for i in range(3)
print(i)
Answer: B — Syntax Error (missing :)
Imagine a stack:
You perform:
PUSH A
PUSH B
PUSH C
POP
What is now on top of the stack
B
(c was the most recent item, so POP removes it)
Which one is NOT technically a programming language?
A. Python
B. Java
C. HTML
D. C++
Answer: C. HTML
HTML is a markup language.
word = "PYTHON"
print(word[::-2])
NHY
Find the bug:
nums = [10, 20, 30]
for i in range(len(nums) + 1):
print(nums[i])
Answer: + 1 makes the loop try to access nums[3], which doesn't exist.
fix: range(len(nums))
How will the following code run:
A — Runs normally
B — Syntax Error
C — Runtime Error
D — Infinite Loop
x = 1
while x < 5:
x -= 1
print(x)
Answer: D — Infinite Loop
How many times does this loop run?
x = 1
while x <= 20:
x *= 2
5 times
Question:
Python was NOT named after the snake.
What was it named after?
A. A scientist
B. Monty Python
C. A video game
D. A computer company
Answer: B. Monty Python(a famous british comedy group)
x = 5
for i in range(3):
x = x - i
print(i, x)
2 2
This programmer expects [2, 4, 6], but gets [1, 2, 3].
Why?
def double(nums):
for n in nums:
n = n * 2
return nums
print(double([1, 2, 3]))
Answer: Changing n does not change the actual values inside the list.
How will the following code run:
A — Runs normally
B — Syntax Error
C — Runtime Error
D — Infinite Loop
x = 0
while x < 3:
x += 1
if x == 2:
x = 0
print(x)
Answer: D — Infinite Loop
A computer is trying to find the shortest path from A to D.
The connections have these costs:
A → B = 2
A → C = 6
B → C = 1
B → D = 5
C → D = 2
What is the cheapest path?
Answer: A → B → C → D
cost: 2+1+2 =5
A programmer gets stuck, puts a rubber duck next to their computer, and explains their entire code to the duck line by line.
What debugging technique is this?
Answer: Rubber Duck Debugging
Explaining your code out loud can help you notice your own mistake.