Peer Instruction: Exception Multiple Choice Questions¶
- int, int, int
- Try again. A float is a number with a fractional part and a boolean is a true or false value of an expression.
- int, float, boolean
- Correct! 3 + 4 evaluates to 7, which is a whole number. 3.1415 has a fracional part, and 7 < 3 is the false value of an expression.
- int, float, int
- Try again. 7 < 3 here represents the false value of an expression.
- float, real, boolean
- Try again. 3 + 4 evaluates to 7, which is a whole number, and 3.1415 has a fracional part.
- I don’t know
- Try again. 3 + 4 evaluates to 7, which is a whole number. 3.1415 has a fracional part, and 7 < 3 is the false value of an expression.
Q-1: Select the correct type for each variable
a = 7 + 3
b = 3.1415
c = 7 < 3
- int
- Correct! add(5,1) outputs 6, which is a whole number. And type() returns class type of the object.
- function
- Try again. add(5,1) = 5 + 1 = 6. And type() returns class type of the object.
- real
- Try again. add(5,1) = 5 + 1 = 6. And type() returns class type of the object.
- This will cause an error
- Try again. add(5,1) generates a new number. And type() returns class type of the object while print() prints the result out.
- I don’t know
- Try again. add(5,1) = 5 + 1 = 6. And type() returns class type of the object while print() prints the result out.
Q-2: What type will this print?
def add(x, y):
z = x + y
return z
print(type(add(5,1)))
- Try again. i*i multiplies i with i rather than output value.
- Try again. str(i) is a string type and it cannot get multiplied by another string.
- Correct! str(i)*i will multiply i in the string type for i times.
- None of the above
- Try again. str(i) can change i into a string type. Multiply str(i) and i will have the value of i for i times.
- I don't know
- Try again. str(i) can change i into a string type. Multiply str(i) and i will have the value of i for i times.
Q-3: Which code will print the value of i
, i
times?
A.
i = 5
print(i*i)
B.
i = 5
print(str(i)*str(i))
C.
i = 5
print(str(i)*i)
- "Math is lame"
- Try again. Because x = 1/0 causes an error. Once the error appears, it will be detected by Exceptions. And the program will jump to Exception execution.
- "Algebraic!"
- Correct! Because x=1/10 causes an error which is detected by Exceptions.
- Nothing, this will cause an error
- Try again. Yes, this will cause an error. But the error will be detected by Exceptions. So the program will not stop.
- I don’t know
- Try again. Because x = 1/0 can cause an error. Once the error appears, it will be detected by Exceptions. And the program will jump to Exception execution.
Q-4: What does the following code print?
try:
x = 1/0
print("Math is lame!")
except Exception as e:
print("Algebraic!")
- Try again. You can get a right result if grade is 98. But if there is any grade under 90, the result will always be 'You got a B!' since the second condition can always be satisfied.
- Correct! If the condition for if is False , it checks the condition of the next elif block and so on. If previous condition is satisfied, the body of elif will not be executed.
- Try again. This will cause a syntax error because there is no If leading the whole block.
- Try again. You can get a right result but grade < 90 in elif (grade >= 80) and (grade < 90) is redundant because elif block will be executed only if if block is not satisfied.
- I don't know
- Try again. There are three different conditions and output: grade > 90 --> 'You got an A!'; 80 <= grade < 90 --> 'You got a B!'; grade < 80 --> 'You got something else'. Considering to use the 'if...elif...else' structure to distinguish these conditions.
Q-5: Which of the following rewrites this code using Elif?
grade = 98 if grade > = 90: print('You got an A!) if grade > = 80 and grade < 90: print('You got a B!') if grade < 80: print ('You got something else')
A.
grade = 98
if (grade >= 90):
print('You got an A!')
elif (grade < 90):
print('You got a B!')
elif (grade >= 80):
print('You got a B!')
elif (grade < 80):
print ('You got something else')
B.
grade = 98
if (grade >= 90):
print('You got an A!')
elif (grade >= 80):
print('You got a B!')
else:
print ('You got something else')
C.
grade = 98
elif (grade >= 90):
print('You got an A!')
elif (grade >= 80):
print('You got a B!')
elif (grade < 80):
print ('You got something else')
D.
grade = 98
if (grade >= 90):
print('You got an A!')
elif (grade >= 80) and (grade < 90):
print('You got a B!')
else:
print ('You got something else')
You have attempted of activities on this page