Q-1: Which of the following is not a boolean expression?
True
Try again. True and False are both Boolean literals.
3 == 4
Try again. The comparison between two numbers via == results in either True or False (in this case False), both Boolean values.
3 + 4
3 + 4 evaluates to 7, which is a number, not a Boolean value.
3 + 4 == 7
Try again. 3 + 4 evaluates to 7. 7 == 7 then evaluates to True, which is a Boolean value.
2.
Q-2: Which of the following properly expresses the precedence of operators (using parentheses) in the following expression: 5*3 > 10 and 4+6==11
((5*3) > 10) and ((4+6) == 11)
Yes, * and + have higher precedence, followed by > and ==, and then the keyword "and"
(5*(3 > 10)) and (4 + (6 == 11))
Arithmetic operators (*, +) have higher precedence than comparison operators (>, ==)
((((5*3) > 10) and 4)+6) == 11
This grouping assumes Python simply evaluates from left to right, which is incorrect.
((5*3) > (10 and (4+6))) == 11
This grouping assumes that "and" has a higher precedence than ==, which is not true.
3.
Q-3: What does the following code print?
if 4 + 5 == 10:
print("TRUE")
else:
print("FALSE")
print("TRUE")
a. TRUE
b.
TRUE
FALSE
c.
FALSE
TRUE
d.
TRUE
FALSE
TRUE
Output a
Although TRUE is printed after the if-else statement completes, both blocks within the if-else statement print something too. In this case, Python would have had to have skipped both blocks in the if-else statement, which it never would do.
Output b
Because there is a TRUE printed after the if-else statement ends, Python will always print TRUE as the last statement.
Output c
Python will print FALSE from within the else-block (because 5+4 does not equal 10), and then print TRUE after the if-else statement completes.
Output d
To print these three lines, Python would have to execute both blocks in the if-else statement, which it can never do.
4.
Q-4: What does the following code print?
x = -10
if x < 0:
print("The negative number ", x, " is not valid here.")
print("This is always printed")
a.
This is always printed
b.
The negative number -10 is not valid here
This is always printed
c.
The negative number -10 is not valid here
Output a
Because -10 is less than 0, Python will execute the body of the if-statement here.
Output b
Python executes the body of the if-block as well as the statement that follows the if-block.
Output c
Python will also execute the statement that follows the if-block (because it is not enclosed in an else-block, but rather just a normal statement).
It will cause an error because every if statement must have an else statement.
It is valid to have an if-block without a corresponding else-block (though you cannot have an else-block without a corresponding if-block).
5.
Q-5: Which of I, II, and III below gives the same result as the following nested if?
# nested if-else statement
x = -10
if x < 0:
print("The negative number ", x, " is not valid here.")
else:
if x > 0:
print(x, " is a positive number")
else:
print(x, " is 0")
I.
if x < 0:
print("The negative number ", x, " is not valid here.")
else x > 0:
print(x, " is a positive number")
else:
print(x, " is 0")
II.
if x < 0:
print("The negative number ", x, " is not valid here.")
elif x > 0:
print(x, " is a positive number")
else:
print(x, " is 0")
III.
if x < 0:
print("The negative number ", x, " is not valid here.")
if x > 0:
print(x, " is a positive number")
else:
print(x, " is 0")
I only
You can not use a Boolean expression after an else.
II only
Yes, II will give the same result.
III only
No, III will not give the same result. The first if statement will be true, but the second will be false, so the else part will execute.
II and III
No, Although II is correct III will not give the same result. Try it.
I, II, and III
No, in I you can not have a Boolean expression after an else.
6.
Q-6: Which of the following is true about the code below?
x = 3
if (x > 2):
x = x * 2;
if (x > 4):
x = 0;
print(x)
x will always equal 0 after this code executes for any value of x
If x was set to 1 originally, then it would still equal 1.
if x is greater than 2, the value in x will be doubled after this code executes
What happens in the original when x is greater than 2?
if x is greater than 2, x will equal 0 after this code executes
If x is greater than 2, it will be set to 0.
7.
Q-7: What is the total for 12 items that weigh 3 pounds?
weight = 0.5
numItems = 5
if weight < 1:
price = 1.45
if weight >= 1:
price = 1.15
total = weight * price
if numItems >= 10:
total = total * 0.9
print(weight)
print(price)
print(total)
$3.45
This would be the answer without the 10% discount for buying 10 or more items
$3.11
Python doesn't automatically round up
$3.105
This is the actual result. But, can you pay $3.105?
$3.10
Python doesn't automatically change $3.105 to $3.10.
8.
Q-8: Which of the following will evaluate to true?
I. True AND False II. False or True III. False AND (True or False)
I
Incorrect. This evaluates to False - a statement cannot be True AND False.
II
The statement can be either True or False which evaluates to True.
I and II
Incorrect. I evaluates to False.
II and III
Incorrect. III evaluates to False because a statement cannot be False AND True.
9.
Q-9: Given two variables, num1 and num2, which of the following would mean that both num1 and num2 are positive integers?
(num1 == num2)
Incorrect. The two variables can be equal to each other and still be negative integers.
(num1 == num2) or (num1 > 0)
Incorrect. This is true if the two numbers are equal or num1 is greater than 0.
(num1 == num2) and (num1 < 0)
Incorrect. The two variables would both be negative in this case.
(num1 == num2) and (num1 > 0)
If num1 is equal to num2 and num1 is greater than 0, then both values must be positive.
10.
Q-10: True is what type of variable?
float
Try again! True is not a float.
string
Try again! True is not a string.
boolean
True is a boolean value.
integer
Try again! True is not an integer.
11.
Q-11: What is the output from the following code?
a = 3
b = (a != 3)
print(b)
True
This would be true if a was not equal to 3, but it is.
False
A is equal to three so this statement is false.
0
Some languages use 0 for false, but Python will print False.
3
This does not set b to the value of a.
Syntax error
This code is legal.
12.
Q-12: Which of the following evaluates to True when a is equal to b or when a is equal to 5?
a == b == 5
You can not join conditionals in this way.
a = b or a = 5
You must use == to test for equality.
a == b or a == 5
This will be true if a is equal to b or a is equal to 5.