Session 1 - Sample Exam Qs Flashcards

1
Q

Sample exam questions
Q1.1
name = ‘Alice”
# What is wrong with this line of code? - (3)

A

The quotes do not match.

Either use all single quotes or all double quotes.

For example, name=’Alice’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q1.2
age = ‘23’
print(age + 2)
# Why does this code raise an error? - (2)

A

age is a string variable but you are adding an integer to it.

Variable types have to match when doing arithmetic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q1.3
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits[3] = ‘orange’
# What is incorrect with this code? - (2)

A

The list only has 3 entries but you are trying to access the fourth one.

Python indexing starts at zero.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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)

A

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]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q1.5
my_tuple = (1, 2, 3)
my_tuple[1] = 4
# What concept does this example illustrate, and what is the mistake? - (3)

A

You cannot change tuples once you make them.

Here you are trying to alter the second element in the tupe.

You can’t.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q1.6
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(num)
# Identify and correct the mistake in this loop - (2)

A

The variable ‘num’ is not defined. That line should read

print(number)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

name = “John”
greeting = “Hello, name!”
print(greeting)
# Why doesn’t this code snippet print “Hello, John!”? - (3)

A

The variable ‘name’ and the word ‘name’ inside the string are different things.

You could write
print(“Hello”,name)

To get the right answer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

def multiply(x, y):
return x * y
print(multiply(2))
# What error will this code produce and why?

A

function looks like it takes two arguments (x and y) but you are only providing one argument when you call it (2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

%pwd
%cd ..
%ls
# Explain what each of these ‘magic’ / or ‘shell’ commands does - (3)

A

pwd = print the working directory

cd = change directory (in this case to the one above)

ls = list the contents of the current directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

my_string = “Hello, World!”
print(my_string[-1])
What will be printed when executing this code, and what concept does it illustrate? - (2)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

my_list = [1, 2, 3, 4, 5]
print(my_list[2:4])
What output will this code produce, and why? - (3)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly