Session 4 - Sample Exam Qs Flashcards

1
Q

Q1:
Question: What type of error is demonstrated by attempting to execute print (‘Welcome to the programming course!)?

A

A1:
A syntax error (the bracket and close quote are in the wrong places)

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

Q2:
Question: What happens when you try to convert a string input to an integer using int(input(‘Enter a number’)) but enter a string instead of a number?

A

A2:
You will get an ‘exception’ (Note for the exam we don’t expect you to know what >sort< of exception - just what sort of error it is.

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

Q3:
Question: In the context of exception handling in Python, which keywords are used to catch exceptions that are raised?

A

A3:
‘try’ and ‘except’

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

Q4:
Question: Given the code snippet
data = input(‘Enter a number: ‘)

followed by

print(int(data)+2), what type of error occurs if the user enters a string like “hello”

A

A4:
Again, an exception. Note that the error will happen when the data is cast to ‘int’ , not when it is input.

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

Q5:
Question: In a try-except block, what will the following code do if a ValueError occurs?

try:
start = int(input(‘Starting number: ‘))
except ValueError as e:
print(“Would you please give me an actual number next time?”)
print(e)

A

A5:
It will execute the print statements inside the ‘except’ block: Printing the error message >and< the name of the exception.

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

Q6:
Question: How do you access the error message within an except block?

A

A6:
Via the ‘e’ variable which will contain the error message.

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

Q7:
Question: Given the function below, what will printNTimes(‘Hello, World!’, 3)

output?

def printNTimes(my_string, n):
for x in range(n):
print(my_string)

A

A7:
It will print the string ‘Hello World!’ three times

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

Q8:
Question: What will the following Numpy code snippet output, and why?

import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(a[2])

A

A8:
It will print the number ‘3’. This is the third element in the array (and Python starts indexing at zero)

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

Q9:
Question: Consider the following plotting code. What does it aim to plot, and how can you describe the relationship between x and y?

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.show()

A

A9:
It will plot sin(x) as a function of x - in other words a sine wave. There will be 100 points for x values between 0 and 10.

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

Q10:
Question: Analyze the following code snippet that creates a plot. What mistake will prevent the code from executing correctly (plotting y as a function of x)?

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5, 5, 100)
y = x**2

plt.plot(x)
plt.show()

A

A10
The plot statement should take two arguments: x and y. Here it just gets x and it will just plot the values of x (which will be 100 points in a straight line from -5 to 5)

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

What will be the output of the following code?

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

A

An IndexError will occur because the index 3 is out of range for the list my_list.

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

Explain the purpose of the range() function in Python and provide an example - (3)

A

The range() function generates a sequence of numbers within a specified range. It is commonly used for iterating over a sequence of numbers using loops.

Example:

for i in range(5):
print(i)

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

What does the following code snippet do? - (3)

my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3}
print(my_dict.get(‘d’, 0))

A

The code will print 0.

The .get() method retrieves the value associated with the specified key (‘d’) in the dictionary (my_dict).

If the key does not exist, it returns the default value specified as the second argument, which is 0 in this case.

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

How do you concatenate two lists in Python? - (3)

A

You can concatenate two lists using the + operator or the .extend() method. Example:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2
print(concatenated_list)

This will output [1, 2, 3, 4, 5, 6].

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

What is the purpose of the len() function in Python? - (3)

A

The len() function returns the number of items in an object. It can be used to get the length of strings, lists, tuples, dictionaries, and other iterable objects.

Example:

python
Copy code
my_string = “Hello”
print(len(my_string)) # Output: 5

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