Peer Instruction: Conditionals Multiple Choice Questions¶
- Nothing
- Incorrect! Here, x=1 which is less than 3 making the condition true. It will first print x = 1 and then x = x+4 = 1+4 = 5.
- 1
- Incorrect! Here, x=1 which is less than 3 making the condition true. It will first print x = 1 and then x = x+4 = 1+4 = 5.
- 5
- Incorrect! Here, x=1 which is less than 3 making the condition true. It will first print x = 1 and then x = x+4 = 1+4 = 5.
- 1 5
- Correct! Here, x=1 which is less than 3 making the condition true. It will first print x = 1 and then x = x+4 = 1+4 = 5.
- I don't know
- Incorrect! Here, x=1 which is less than 3 making the condition true. It will first print x = 1 and then x = x+4 = 1+4 = 5.
Q-1: What does the following code print?
x = 1
if (x < 3):
print(x)
x = x + 4
print(x)
- Nothing
- Incorrect! Here, x = 1 which is less than 3 making the 'if' condition true. So, the code will print x = 1.
- 1
- Correct! Here, x = 1 which is less than 3 making the 'if' condition true. So, the code will print x = 1.
- 8
- Incorrect! Here, x = 1 which is less than 3 making the 'if' condition true. So, the code will print x = 1.
- 1 8
- Incorrect! Here, x = 1 which is less than 3 making the 'if' condition true. So, the code will print x = 1.
- I don't know
- Incorrect! Here, x = 1 which is less than 3 making the 'if' condition true. So, the code will print x = 1.
Q-2: What does the following code print?
x = 1
if (x < 3):
print(x)
else:
print(x+7)
- You got an A!
- Incorrect! Here, grade=98 which is greater than both 90 and 80 making both the 'if' conditions true. So, the code will print 'You got an A!' and 'You got a B!' respectively.
- You got a B!
- Incorrect! Here, grade=98 which is greater than both 90 and 80 making both the 'if' conditions true. So, the code will print 'You got an A!' and 'You got a B!' respectively.
- You got something else!
- Incorrect! Here, grade=98 which is greater than both 90 and 80 making both the 'if' conditions true. So, the code will print 'You got an A!' and 'You got a B!' respectively.
- More than one of the above
- Correct! Here, grade=98 which is greater than both 90 and 80 making both the 'if' conditions true. So, the code will print 'You got an A!' and 'You got a B!' respectively.
- I don't know
- Incorrect! Here, grade=98 which is greater than both 90 and 80 making both the 'if' conditions true. So, the code will print 'You got an A!' and 'You got a B!' respectively.
Q-3: What does the following code print?
grade = 98
if (grade >= 90):
print(“You got an A!”)
if (grade >= 80):
print(“You got a B!”)
else:
print(“You got something else”)
- You got an A!
- Incorrect! Here, grade=98 which is greater than 90 making the first 'if' condition true and will print 'You got an A!'. However, the second 'if' condition is not satisfied as grade doesn't lie between 80 and 90. So, the code will execute the statements under 'else' and print 'You got something else!'. Note that True 'and' False equals 'False' in boolean logic.
- You got a B!
- Incorrect! Here, grade=98 which is greater than 90 making the first 'if' condition true and will print 'You got an A!'. However, the second 'if' condition is not satisfied as grade doesn't lie between 80 and 90. So, the code will execute the statements under 'else' and print 'You got something else!'. Note that True 'and' False equals 'False' in boolean logic.
- You got something else!
- Incorrect! Here, grade=98 which is greater than 90 making the first 'if' condition true and will print 'You got an A!'. However, the second 'if' condition is not satisfied as grade doesn't lie between 80 and 90. So, the code will execute the statements under 'else' and print 'You got something else!'. Note that True 'and' False equals 'False' in boolean logic.
- More than one of the above
- Correct! Here, grade=98 which is greater than 90 making the first 'if' condition true and will print 'You got an A!'. However, the second 'if' condition is not satisfied as grade doesn't lie between 80 and 90. So, the code will execute the statements under 'else' and print 'You got something else!'. Note that True 'and' False equals 'False' in boolean logic.
- I don't know
- Incorrect! Here, grade=98 which is greater than 90 making the first 'if' condition true and will print 'You got an A!'. However, the second 'if' condition is not satisfied as grade doesn't lie between 80 and 90. So, the code will execute the statements under 'else' and print 'You got something else!'. Note that True 'and' False equals 'False' in boolean logic.
Q-4: What does the following code print?
grade = 98
if (grade >= 90):
print(“You got an A!”)
if (grade >= 80 and grade < 90):
print(“You got a B!”)
else:
print(“You got something else”)
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")
-
Correct! Here, grade = 98 which is greater than 90 satisfying the first ‘if’ condition. So, this code will output ‘You got an A!’
grade = 98 if (grade >= 90): print("You got an A!") elif (grade >= 80): print("You got a B!") else: print("You got something else")
-
Correct! Here, grade = 98 which is greater than 90 satisfying the first ‘if’ condition. So, this code will output ‘You got an A!’
grade = 98 if (grade >= 90): print("You got an A!") elif (grade >= 80): print("You got a B!") elif(grade < 80): print("You got something else")
-
Correct! Here, grade = 98 which is greater than 90 satisfying the first ‘if’ condition. So, this code will output ‘You got an A!’
None of the above
-
Incorrect! Here, grade = 98 which is greater than 90 satisfying the first ‘if’ condition in all the options above.
I don’t know
-
Incorrect! Here, grade = 98 which is greater than 90 satisfying the first ‘if’ condition in all the options above.
Q-5: Which code prints the correct value? If grade >= 90 it should print “You got an A!”. If grade < 90 and >= 80 it should print “You got a B!”. If you got less than 80 it should print “You got something else”.
- Spaces before * = r, Spaces after * = r
- Incorrect! Skipping rows with 2 stars, the total no. of rows n = 3. If r = 1, the spaces before * = r = 1 and the spaces after * = n-r-1 = 3-1-1 = 1. The same can be computed with r=0 and r=3.
- Spaces before * = r, Spaces after * = n-r
- Incorrect! Skipping rows with 2 stars, the total no. of rows n = 3. If r = 1, the spaces before * = r = 1 and the spaces after * = n-r-1 = 3-1-1 = 1. The same can be computed with r=0 and r=3.
- Spaces before * = n-r, Spaces after * = r
- Incorrect! Skipping rows with 2 stars, the total no. of rows n = 3. If r = 1, the spaces before * = r = 1 and the spaces after * = n-r-1 = 3-1-1 = 1. The same can be computed with r=0 and r=3.
- Spaces before * = r, Spaces after * = n-r-1
- Correct! Skipping rows with 2 stars, the total no. of rows n = 3. If r = 1, the spaces before * = r = 1 and the spaces after * = n-r-1 = 3-1-1 = 1. The same can be computed with r=0 and r=3.
- I don't know
- Incorrect! Skipping rows with 2 stars, the total no. of rows n = 3. If r = 1, the spaces before * = r = 1 and the spaces after * = n-r-1 = 3-1-1 = 1. The same can be computed with r=0 and r=3.
Q-6: How do we print row r
of this pattern? Skip rows with 2 stars.
* *
** *
* * *
* **
* *
- Yes
- Correct! Try inserting n=3 and running the code for any value of row and col.
- No
- Incorrect! Try inserting n=3 and running the code for any value of row and col.
- Sometimes
- Incorrect! Try inserting n=3 and running the code for any value of row and col.
- I don't know
- Incorrect! Try inserting n=3 and running the code for any value of row and col.
Q-7: Will the codeblock print the following pattern?
for row in range(n+2):
for col in range(n+2):
if (row == col or col == 0 or col == n+1):
print("*", end='')
else:
print(" ",end='')
print()
Output-
* *
** *
* * *
* **
* *
- -3
- Incorrect! Here x = 5 which is greater than 2. So, the 'if' condition is satisfied. The value of x is first assigned -3 and then finally 1.
- 1
- Correct! Here x = 5 which is greater than 2. So, the 'if' condition is satisfied. The value of x is first assigned -3 and then finally 1.
- 2
- Incorrect! Here x = 5 which is greater than 2. So, the 'if' condition is satisfied. The value of x is first assigned -3 and then finally 1.
- 3
- Incorrect! Here x = 5 which is greater than 2. So, the 'if' condition is satisfied. The value of x is first assigned -3 and then finally 1.
- 5
- Incorrect! Here x = 5 which is greater than 2. So, the 'if' condition is satisfied. The value of x is first assigned -3 and then finally 1.
Q-8: What is the value of x
after this code runs?
x = 5
if x > 2:
x = -3
x = 1
else:
x = 3
x = 2
- -3
- Incorrect! Here x = 1 which is smaller than 2. So, the 'else' condition is satisfied. The value of x is first assigned 3 and then finally 2.
- 1
- Incorrect! Here x = 1 which is smaller than 2. So, the 'else' condition is satisfied. The value of x is first assigned 3 and then finally 2.
- 2
- Correct! Here x = 1 which is smaller than 2. So, the 'else' condition is satisfied. The value of x is first assigned 3 and then finally 2.
- 3
- Incorrect! Here x = 1 which is smaller than 2. So, the 'else' condition is satisfied. The value of x is first assigned 3 and then finally 2.
- 5
- Incorrect! Here x = 1 which is smaller than 2. So, the 'else' condition is satisfied. The value of x is first assigned 3 and then finally 2.
Q-9: What is the value of x
after this code runs?
x = 1
if x > 2:
x = -3
x = 1
else:
x = 3
x = 2
- Yes
- Correct! In both Code 1 and Code 2, the else and elif conditions at the end are essentially the same i.e. temperature < 0.
- No
- Incorrect! In both Code 1 and Code 2, the else and elif conditions at the end are essentially the same i.e. temperature < 0.
Q-10: Does Code 1 do exactly the same thing as Code 2? (Assume temperature already refers to some numeric value.)
Code 1-
if temperature > 0:
print("above freezing")
elif temperature == 0:
print("at freezing")
else:
print("below freezing")
Code 2-
if temperature > 0:
print("above freezing")
elif temperature == 0:
print("at freezing")
elif temperature < 0:
print("below freezing")
- Yes
- Incorrect! In Code 1, "below freezing" prints only if the other two conditions don't satisfy. However in Code 2, "below freezing" prints everytime as it is not bound to any condition.
- No
- Correct! In Code 1, "below freezing" prints only if the other two conditions don't satisfy. However in Code 2, "below freezing" prints everytime as it is not bound to any condition.
Q-11: Does Code 1 do exactly the same thing as Code 2? (Assume temperature already refers to some numeric value.)
Code 1-
if temperature > 0:
print("above freezing")
elif temperature == 0:
print("at freezing")
else:
print("below freezing")
Code 2-
if temperature > 0:
print("above freezing")
elif temperature == 0:
print("at freezing")
print("below freezing")
- Yes
- Incorrect! Code 2 will print "below freezing" for temperature <= 0.
- No
- Correct! Code 2 will print "below freezing" for temperature <= 0.
Q-12: Does Code 1 do exactly the same thing as Code 2? (Assume temperature already refers to some numeric value.)
Code 1-
if temperature > 0:
print("above freezing")
elif temperature == 0:
print("at freezing")
else:
print("below freezing")
Code 2-
if temperature == 0:
print("at freezing")
elif temperature <= 0:
print("below freezing")
else:
print("above freezing")
- one
- Incorrect! Here, x = 5 which is smaller than 15 satistying the 'if' condition and smaller than 8 satisfying the nested 'else' condition. So, the output is 'two'.
- two
- Correct! Here, x = 5 which is smaller than 15 satistying the 'if' condition and smaller than 8 satisfying the nested 'else' condition. So, the output is 'two'.
- three
- Incorrect! Here, x = 5 which is smaller than 15 satistying the 'if' condition and smaller than 8 satisfying the nested 'else' condition. So, the output is 'two'.
- More than one of the above
- Incorrect! Here, x = 5 which is smaller than 15 satistying the 'if' condition and smaller than 8 satisfying the nested 'else' condition. So, the output is 'two'.
- No output
- Incorrect! Here, x = 5 which is smaller than 15 satistying the 'if' condition and smaller than 8 satisfying the nested 'else' condition. So, the output is 'two'.
Q-13: What does the following code print?
x = 5
if x < 15:
if x > 8:
print('one')
else:
print('two')
else:
print('three')
- def is_odd(x): return x % 2
- Incorrect! This will return either 1 or 0.
- def is_odd(x): return x % 2 == 1
- Correct! This will return True if x is Odd and False if x is Even.
- def is_odd(x): return x % 2 == 0
- Incorrect! This will return True if x is Even and False if x is Odd.
- None of the above
- Incorrect! Option B will return True if x is Odd and False if x is Even.
Q-14: What does the following code print?
def is_odd(x):
if x % 2 == 1:
return True
else:
return False
- 'age >= 18 and' can be removed
- Incorrect! This will print both 'minor' and 'adult' for ages less than 18.
- 'else: print("ageless")' can be removed
- Correct! This will not change the output of the code regardless of the age entered.
- Both A and B
- Incorrect! If 'age >= 18 and' is removed, it will print both 'minor' and 'adult' for ages less than 18.
- Nothing can be removed
- Incorrect! Option B will not change the output of the code regardless of the age entered.
Q-15: What parts of the code can be removed without changing what the code does? Assume that the user enters a non-negative integer.
age = int(input("Enter your age: "))
if age < 18:
print("minor")
elif age >= 18 and age < 30:
print("adult")
elif age >= 30:
print("older than Dan")
else:
print("ageless")
- (1) experience > 0, (2) experience >= 1
- Incorrect! In this option, wages for experience > 0 and experience >= 1 encomapes wages $12 and $11 as well.
- (1) age < 18, (2) experience != 3
- Incorrect! In this option, age < 18 and experience !=3 includes wages $6.5, and $12 as well.
- (1) age < 18, (2) experience == 1 or experience == 2
- Correct! Wages $9.5 and $11 are for ages less than 18. So, (1) = age < 18. Wage $9.5 is for experience 1-2. So, experience == 1 or experience == 2.
Q-16: What goes in the two numbered blanks?
if experience == 0:
wage = 6.5
elif (1) :
if (2) :
wage = 9.5
else:
wage = 11
else:
wage = 12
- (1) plan == 'silver', (2) months >= 2 and months <= 6
- Correct! Here, cost = $60 for plan = 'silver' and months != 1. So, (1) plan = 'silver'. And cost = $70 for plan = 'gold' and months 2-6. The former is inherent to the 'else' condition while the latter is encompassed through (2) months >= 2 and months <= 6.
- (1) plan == 'silver', (2) months >= 6
- Incorrect! Here, cost = $60 for plan = 'silver' and months != 1. So, (1) plan = 'silver'. And cost = $70 for plan = 'gold' and months 2-6. The former is inherent to the 'else' condition while the latter is encompassed through (2) months >= 2 and months <= 6 but not months >=6.
- (1) months >= 2 and months <= 6, (2) plan == 'gold'
- Incorrect! This option doesn't include cost = $60 for months >= 7. Furthermore, plan = 'gold' also includes $35 in addition to $70. Here, cost = $60 for plan = 'silver' and months != 1. So, (1) plan = 'silver' and not . And cost = $70 for plan = 'gold' and months 2-6. The former is inherent to the 'else' condition while the latter is encompassed through (2) months >= 2 and months <= 6.
Q-17: What goes in the two numbered blanks?
if months == 1:
cost = 90
elif (1) :
cost = 60
else:
if (2) :
cost = 70
else:
cost = 35