Session 2 - Exam Qs Flashcards

1
Q

Question 1: String Formatting Issue

What is problem with code?

A

Use {} not [] to embed variables in f-strings

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

Question 2: Dictionary Key Error

The above code snippet attempts to print the location of the person. Identify the error and suggest a quick fix. In addition, suggest a general way to handle missing keys in a dictionary so that the code does not produce an error. - (3)

A

There is no key called ‘location’.

Probably ‘city’ is what you wanted.

Use the “get” function to try to access dict values by keys and return a None value rather than an error if the key is missing.

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

Question 3: While Loop Condition Error

Explain why the loop might not work as expected. How would you fix it to count down from 10 to 1. - (3)

A

The loop will never stop because the count is always <=10. There are lots of ways of fixing it. For example…

1: Add in an ‘and’ statement to the while loop (while (count<=10 and count >=1):

2: Just use a for loop with a range statement (for count in range(10,0,-1):

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

Question 4: Conditional Statement Syntax Error

Identify the syntax error in the conditional statements.

A

The last ‘else’ is missing a colon

else:

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

Question 5: Incorrect Use of the ‘is’ Operator

Explain why the is operator might not be appropriate in this context and suggest a correct approach. - (2)

A

‘is’ is usually used for lists or dictionaries or strings. For numbers, just use == instead

if (x>3) and (y==10):

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

Question 6: While loop and conditional

Describe the purpose of the else clause in this while loop and under what circumstances it is executed. - (2)

A

It is evaluated when the countdown gets to zero.

It tells the program what to do when the countdown is not 3,2,1…

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

Question 7: Rounding and mixing Data Types in F-String

How would you make this f-print statement round to the nearest integer? - (2)

A

Use the formatting string :something.0f following ‘score’ inside the first {}. The something part says how many figures to have in front of the decimal place, the ‘0’ part says to have nothing after the decimal place. For example

print(f”Your rounded score is {score:2.0f}, which is a {result}”)

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

name = “Emily”
age = 35
print(“My name is “ + name + “ and I am “ + age + “ years old.”)

What is the problem with this code? - (4)

A

The problem with this code is that the variable age is of integer type, and it cannot be directly concatenated with strings using the + operator.

To fix this, you should convert the age variable to a string using the str() function before concatenating it with other strings.

Here is corrected answer:

name = “Emily”
age = 35
print(“My name is “ + name + “ and I am “ + str(age) + “ years old.”)

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

numbers = [1, 2, 3, 4, 5]
print(numbers[6])

**Identify the error in the code and suggest a solution to avoid this error - (8)

A

Identify the error:

The error in the code is an “IndexError,” specifically stating that the index is out of range.

This means that the index 6 is beyond the bounds of the list numbers, which has indices ranging from 0 to 4.

Solution:

To avoid this error, ensure that the index used to access elements from the list is within the bounds of the list.

In this case, you should use an index that is valid for the given list.

For example, if you want to access the last element of the list, you can use index 4, not 6 e.g,, print(numbers[4])

Alternatively, you can handle such errors using try-except blocks to gracefully manage situations where an invalid index is used

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

Example of using try block for this example:

numbers = [1, 2, 3, 4, 5]
print(numbers[6])

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

def greet(name, message):
print(“Hello “ + name)
print(message)
greet(“Alex”)

Identify the syntax error in the function call and suggest a solution. - (5)

A

The syntax error occurs in the function call greet("Alex").

The function definition ‘greet()’ expects two arguments (name and message), but only one argument (name) is provided in the function call.

This results in a TypeError.

To fix this error, provide both name and message arguments in the function call, matching the expected arguments in the function definition.

Example solution:
greet(“Alex”, “Good morning!”)

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

Explain why the loop does not sum the numbers correctly and suggest a fix - (6)

total = 0
i = 1
while i <= 5:
total += i
print(“Total is:”, total)

A

The loop does not sum the numbers correctly because the loop control variable ‘i’ is never incremented inside the loop.

As a result, the loop condition while i <= 5 always remains true, leading to an infinite loop.

Consequently, the program keeps adding the same value of ‘i’ to ‘total’ repeatedly without progressing to the next number in the sequence.

To fix this issue, you should increment the value of ‘i’ inside the loop to ensure that it progresses through the numbers from 1 to 5.

By incrementing ‘i’, the loop iterates through each number in the sequence and correctly accumulates the sum of these numbers in ‘total’.

Example solution:
total = 0
i = 1
while i <= 5:
total += i
i += 1 # Increment ‘i’ inside the loop
print(“Total is:”, total)

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

What does ‘total+= i’ mean?

total = 0
i = 1
while i <= 5:
total += i
print(“Total is:”, total)

A

total += i is equivalent to total = total + i, which means the value of i is added to the current value of total, and the updated value is stored back in total.

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

my_list = [1, 2, 3, 4,]
print(my_list)

Identify the syntax error in the list initialization and suggest a fix - (2)

A

The trailing comma after the last element is unnecessary.

Remove it to avoid syntax errors.

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