1/98
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What is the most appropriate value for a variable named 'state'?
B. 'Virginia'
How do you declare a single-line comment in Python?
C. # This is a comment
What does the function sillyString print when my_string is 'apple'?
D. 'applea'
What is the output of the function looping(5)?
D. 15
Which function call would raise an error for function f(a, b=6, c='hi')?
G. Options D and E would raise an error
Evaluate the boolean expression 'cat and dog' when cat=True and dog=False.
F (false)
What is the output of the code 'print((True and not False) and (True and False and True))'?
F (false)
What is the output of the code 'y = 5 % 10'?
5
What is the output of the code 'z = 2**3; answer = z/2; print(answer)'?
4
What is the expected output of 'print(1,2,3,sep='a')'?
1a2a3
How do you make the turtle called 'my_turtle' spin 5 times?
my_turtle.spin(5)
What does grade(90) return in the grading function?
'A'
What does grade(50) return in the grading function?
'F'
What is the bug in line 7 of the orchard function?
Bug on line 7: There is an assignment operator '=' instead of a comparison operator '=='
What would be printed by 'print(carrots_left())' in the given code?
17
What is the scope of the variable 'adrian' in the function 'carrots_left'?
Local variable
Would line 11 'print(adrian)' run without errors?
No, it will raise a NameError because 'adrian' is not defined in the global scope.
Name one debugging technique.
Using Print Statements to trace variable values. It’s useful because it lets you see what the program is actually doing at each step and helps identify where values go wrong or where the logic fails.
What is one property of good algorithms?
Unambiguous: Each step in the algorithm is clear and precise, with no room for confusion.
What is another property of good algorithms?
Terminating: The algorithm must stop after a finite number of steps; it can’t run forever.
What is the equivalent while loop for printing each letter of 'python'?
i = 0; while i < len(word): print(word[i]); i += 1
How can you fix the infinite loop in the countdown from 10?
Decrement 'i' inside the loop: i -= 1
How can you convert multiple if statements into one if-elif-else statement?
if x < 7: print('x is less than 7'); elif x > 7: print('x is greater than 7'); else: print('x is equal to 7')
What is a valid variable name in Python?
A valid variable name cannot start with a number, cannot contain spaces, and cannot use special characters like hyphens. Example: 'my_name' is valid.
What is the variable type of x if x = '24.0'?
The variable type of x is 'string'.
Which keyword is used to define a function in Python?
The keyword used to define a function in Python is 'def'.
How would you best describe a function in Python?
A function in Python is a set of statements that performs a specific task.
What will be the output of the following code snippet?
def add_list(numbers):
result = 0
for num in numbers:
result += num
return result
my_numbers = [2, 3, 4, 5]
output = add_list(my_numbers)
print(output)
The output will be 14.
True or False: In a chained conditional statement, Python evaluates each if and elif until one is True.
True.
True or False: A Python if-statement can have multiple else clauses.
False.
True or False: The '==' operator in Python is used to assign a value to a variable.
False; it is used to compare two values for equality.
True or False: The behaviors of two code snippets can differ even with the same input.
True.
True or False: A boolean expression can be made up of one or more boolean expressions chained with 'and's and 'or's.
True.
True or False: A function must take in arguments.
False; a function can be defined without any arguments.
How can you modify the code to print 'Zero' if the variable 'number' is 0?
Add an if statement: 'if number == 0: print("Zero")'.
What is the error in the following code?
number = 6
if number % 2 != 0 and number % 3 == 0:
print("The number is divisible by both 2 and 3.")
else:
print("The number is not divisible by both 2 and 3.")
The condition is incorrect; it should check for divisibility by both 2 and 3 separately.
What line can replace #ADD LINE in the function to print 9?
Replace #ADD LINE with 'return x'.
What is the error in the following code?
number = 10
target = 5
while number >= target:
print("Number:", number)
number -= 1
The indentation of the print statement is incorrect; it should be indented to be part of the while loop.
How can you write a for loop equivalent to the given while loop?
word = 'python'
i = 0
while (i < len(word)):
print(word[i])
i += 1
for letter in word: print(letter)
What is the output of the following code: x = 'cs'; y = 1112; print(x, y)?
cs 1112
What is the output of the following code: x = 'cs'; y = 1112; print(x + ' ' + y)?
TypeError (because y is an integer)
What is the output of the following code: print('CS 1112\n\tIS FUN')?
CS 1112
IS FUN
What is the output of the following code: y = 5 % 10; print(y)?
5
What is the output of the following code: z = 2**3; answer = z/2; print(answer)?
4
What is the output of the following code: print(1, 2, 3, sep='a')?
1a2a3
What value for the variable year would result in off_grounds being True?
Any value greater than 1 and less than or equal to 4 (e.g., 2, 3, or 4)
What value for the variable year would result in the print statement being executed in the given code?
Any value less than 1 or greater than 4 (e.g., 0 or 5)
What does the function grade(percent) return for grade(90)?
A
What does the function grade(percent) return for grade(50)?
F
Does the function grade(percent) work as intended?
Yes, it correctly categorizes grades into letter grades.
What is the output of the following code: y = 4; def add_nums(num1, num2): z = num1 + num2; w = z + x; return w; x = 10; print(add_nums(x, y))?
14
In the code provided, which variables are local and which are global?
Local: num1, num2, z; Global: y, x
Can a variable be both local and global? Explain.
No, a variable cannot be both local and global at the same time; it is either defined within a function (local) or outside of it (global).
What is the difference between a for-loop and a while-loop?
A for-loop iterates over a sequence or range, while a while-loop continues until a specified condition is false.
Give an example of a scenario when you might want to use a for-loop over a while-loop.
When you need to iterate over a fixed number of items, such as processing elements in a list.
Name one debugging technique and explain why it can be useful.
Print debugging: Inserting print statements helps track variable values and program flow, making it easier to identify where errors occur.
Name and explain two properties of good algorithms.
1) Efficiency: A good algorithm should solve problems using the least amount of resources possible. 2) Clarity: A good algorithm should be easy to understand and follow, facilitating maintenance and updates.
print("hello")
hello
print(a, b)
hi bye
print("CS", "1112", sep="*", end="!")
CS*1112!
\n
Newline
\t
Tab (spacing)
Four main Python data types
int, float, string, boolean
7 // 3
2 (integer division)
7 / 3
2.333... (float division)
7 % 3
1 (remainder)
2 ** 3
8 (exponentiation)
= vs ==
= is assignment, == is equality comparison
'and' in a condition
Both conditions must be True
'or' in a condition
At least one condition must be True
'not' in a condition
Reverses the truth value
Truthy and falsy values
Truthy values evaluate to True in conditionals (e.g. nonzero numbers, non-empty strings/lists). Falsy values evaluate to False (0, None, '', [], False).
Conditional statement output
A is bigger
Condition for x between 5 and 10 inclusive
if 5 <= x <= 10:
General syntax for defining a function
def function_name(parameters): # code return value
Positional argument
An argument passed based on its order
Keyword argument
An argument passed by explicitly naming it
Default parameter
A parameter with a value used if no argument is provided
Function with zero parameters
Yes
Error in function definition
Parameters with defaults must come after required parameters
Local vs global variables
Local exist only inside a function; global exist throughout the file
global keyword
Allows a function to modify a global variable
Preventing infinite while loop
The guard condition will eventually become False
Convert while loop to for loop
for i in range(2, 5): print(i)
Rewrite while loop as for loop
for i in range(5): print(i)
Code print output
d-o-g-
Every for loop to while loop
Yes
Every while loop to for loop
Yes
Common debugging technique
Using print statements to check values step by step
Rubber ducking in debugging
Explaining your code out loud to find mistakes in logic
Commenting in debugging
Helps break down logic so errors are easier to spot
5 conditions for correct code
1. Syntax is correct (no typos/indent errors) 2. Runs without crashing (no runtime errors) 3. Produces expected outputs (logic is correct) 4. Handles edge cases properly 5. Easy to read and maintain (clear structure)
Different debugging techniques
1. Print statements: show variable values 2. Rubber ducking: explain logic aloud 3. Code tracing: step through execution manually 4. Break down problem: test small pieces separately 5. Commenting: clarify and check intent vs result
Find the bug in code
Mistake: Missing indentation on print(i). Fix: for i in range(5): print(i) print('done')
Pseudocode
A simplified, plain-language outline of an algorithm
Good algorithm characteristics
Clear, finite steps that always produce the correct result
Function random(x, y)
a) x % 3 == 1 and y == x (e.g. x=1, y=1) b) x % 3 == 1 and y != x (e.g. x=1, y=2) c) x % 3 == 0 and y != x (e.g. x=3, y=2)
Code print output for square function
13
Code print output for a and b comparison
B bigger