Session 1 - Sample Exam Qs Flashcards
Sample exam questions
Q1.1
name = ‘Alice”
# What is wrong with this line of code? - (3)
The quotes do not match.
Either use all single quotes or all double quotes.
For example, name=’Alice’
Q1.2
age = ‘23’
print(age + 2)
# Why does this code raise an error? - (2)
age is a string variable but you are adding an integer to it.
Variable types have to match when doing arithmetic
Q1.3
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits[3] = ‘orange’
# What is incorrect with this code? - (2)
The list only has 3 entries but you are trying to access the fourth one.
Python indexing starts at zero.
Q1.4
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_list = list1 + list2
print(combined_list)
# What will this print, and why might it confuse a beginner? - (2)
This will concatenate the lists: [1,2,3,4,5,6]
You might be expecting it to add them element by element [5,7,9], or even include list2 as a member of list 1 [1,2,3,[4,5,6]]
Q1.5
my_tuple = (1, 2, 3)
my_tuple[1] = 4
# What concept does this example illustrate, and what is the mistake? - (3)
You cannot change tuples once you make them.
Here you are trying to alter the second element in the tupe.
You can’t.
Q1.6
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(num)
# Identify and correct the mistake in this loop - (2)
The variable ‘num’ is not defined. That line should read
print(number)
for i in range(5):
print(“Number is: “, i)
# What will be the output of this loop, and what common misunderstanding might this clarify? - (2)
0,1,2,3,4
It will demonstrate that python ranges are zero indexed and exclusive (they start at zero and do not include the last number)
for i in range(3):
for j in range(2):
print(i, j)
# What will be the output of this nested loop, and what concept does it illustrate? - (3)
0 0
0 1
1 0
1 1
2 0
2 1
Ranges start at zero and exclude the highest element. Also loop blocks are defined by indentation.
And nested loops are evaluated from the deepest loop out.
name = “John”
greeting = “Hello, name!”
print(greeting)
# Why doesn’t this code snippet print “Hello, John!”? - (3)
The variable ‘name’ and the word ‘name’ inside the string are different things.
You could write
print(“Hello”,name)
To get the right answer.
def multiply(x, y):
return x * y
print(multiply(2))
# What error will this code produce and why?
function looks like it takes two arguments (x and y) but you are only providing one argument when you call it (2)
%pwd
%cd ..
%ls
# Explain what each of these ‘magic’ / or ‘shell’ commands does - (3)
pwd = print the working directory
cd = change directory (in this case to the one above)
ls = list the contents of the current directory
my_string = “Hello, World!”
print(my_string[-1])
What will be printed when executing this code, and what concept does it illustrate? - (2)
The code will print the last character of the string, which is “!”.
This illustrates the concept of negative indexing in Python, where -1 refers to the last element, -2 refers to the second last, and so on.
my_list = [1, 2, 3, 4, 5]
print(my_list[2:4])
What output will this code produce, and why? - (3)
The code will print [3, 4].
This is because slicing with the notation [start:stop] returns a sublist containing elements from index start up to (but not including) index stop.
In this case, it extracts elements at indices 2 and 3 from my_list.
my_list = [1, 2, 3, 4, 5]
my_list[1:4] = [8, 9, 10]
print(my_list)
What will the final contents of my_list be after executing this code? - (2)
The final contents of my_list will be [1, 8, 9, 10, 5].
This code replaces elements at indices 1, 2, and 3 in my_list with the elements [8, 9, 10], resulting in the modified list.
my_list = [‘apple’, ‘banana’, ‘cherry’]
print(my_list[::-1])
What will be printed when running this code, and what is the purpose of the slicing notation used? - (2)
The code will print [‘cherry’, ‘banana’, ‘apple’], which is the reversed version of my_list.
The slicing notation [start:stop:step] with step as -1 is used to reverse the order of elements in the list.