Session 4 - Sample Exam Qs Flashcards
Q1:
Question: What type of error is demonstrated by attempting to execute print (‘Welcome to the programming course!)?
A1:
A syntax error (the bracket and close quote are in the wrong places)
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?
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.
Q3:
Question: In the context of exception handling in Python, which keywords are used to catch exceptions that are raised?
A3:
‘try’ and ‘except’
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”
A4:
Again, an exception. Note that the error will happen when the data is cast to ‘int’ , not when it is input.
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)
A5:
It will execute the print statements inside the ‘except’ block: Printing the error message >and< the name of the exception.
Q6:
Question: How do you access the error message within an except block?
A6:
Via the ‘e’ variable which will contain the error message.
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)
A7:
It will print the string ‘Hello World!’ three times
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])
A8:
It will print the number ‘3’. This is the third element in the array (and Python starts indexing at zero)
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()
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.
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()
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)
What will be the output of the following code?
my_list = [1, 2, 3]
print(my_list[3])
An IndexError will occur because the index 3 is out of range for the list my_list.
Explain the purpose of the range() function in Python and provide an example - (3)
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)
What does the following code snippet do? - (3)
my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3}
print(my_dict.get(‘d’, 0))
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 do you concatenate two lists in Python? - (3)
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].
What is the purpose of the len() function in Python? - (3)
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