0% found this document useful (0 votes)
5 views33 pages

Python If Statement Quiz Questions

The document consists of a series of questions related to Python programming, specifically focusing on decision-making statements such as 'if' and 'else'. It covers syntax, logical errors, and the output of various code snippets. The questions test the reader's understanding of conditional statements, operators, and the behavior of code based on different inputs.

Uploaded by

eray950601
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views33 pages

Python If Statement Quiz Questions

The document consists of a series of questions related to Python programming, specifically focusing on decision-making statements such as 'if' and 'else'. It covers syntax, logical errors, and the output of various code snippets. The questions test the reader's understanding of conditional statements, operators, and the behavior of code based on different inputs.

Uploaded by

eray950601
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

You can read the following textbook to find the answers:

 Python for Everyone, 2nd Edition, Cay S. Horstmann, Rance D. Necaise,


Wiley, 2016.

1. What statement is used to implement a decision?


1. while
2. if
3. for
4. import

2. What are the two parts of an if statement?


1. A condition and a body
2. A check and an increment
3. An increment and a body
4. An increment and a return value

3. Which of the following statements is true about the if statement?


1. The |if| statement can have only one condition that evaluates to
an integer value.
2. The |if| block is optional.
3. The |else| block is optional.
4. The |if| and |else| blocks can be aligned to any indentation
level.

4.

Which of the following is the correct syntax for an if statement?

1.

if (x < 10) size = "small";

2.

if (x < 10)
size = "small"
else (x < 20)
size = "medium"

3.

if x < 10 :
size = "small"
else :
size = "medium"

4.
if x < 10 :
size = "small"
else
size = "medium"

5.

Which of the following correctly identifies what is wrong with the


code snippet below:

if y > 300 :
x = y
else :
x = 0
print("x:", x)

1. Nothing, the program runs as intended


2. The statement after the |if| statement must be indented
3. The statement after the |if| statement and the statement after
the |else| statement must be indented
4. No colon is needed after the |else| statement

6. Assuming that the user provides 303 as input, what is the output of
the following code snippet?

y = int(input("Please enter a number: "))


if y > 300 :
x = y
else :
x = 0
print("x:", x)

1. |x: 0|
2. |x: 303|
3. |x: 300|
4. There is no output due to a syntax error.

7. The following code snippet contains an error. What is the error?

cost = int(input("Enter the cost: "))


if cost > 100
cost = cost - 10
print("Discounted cost:", cost)

1. Syntax error: missing colon after |if| statement


2. Logical error: use of an uninitialized variable
3. Syntax error: missing an |else| statement
4. Syntax error: incorrect indentation

8. Assuming that the user provides 95 as input, what is the output of


the following code snippet?

y = int(input("Please enter a number: "))


if y > 300 :
x = y
else :
x = 0
print("x:", x)

1. |x: 0|
2. |x: 95|
3. |x: 300|
4. There is no output due to a syntax error

9. What is printed by the following code snippet if |itemCount|


contains a value of 10 and |cost| contains 80:

if itemCount > 5 :
discount = 0.8
totalCost = cost * discount
print("Total discounted price is:", totalCost)

1. Nothing, the program will run but not print any results
2. |Total discounted price is: 64.0|
3. |Total discounted price is: 0.0|
4. |Total discounted price is: 16.0|

10. What is the output of the following code snippet if the |cost|
contains 100:

if cost > 150 :


discount = 0.8 * cost
else :
discount = cost
print("Your cost is:", discount)

1. Nothing, the code contains a syntax error


2. |Your cost is: 0|
3. |Your cost is: 80|
4. |Your cost is: 100|

11. Consider the following code segment:


if count > 0 :
x = x + 1

print(x)

If |count| is initialized to -1 and |x| is initialized to 4 then the


value displayed by this code segment is:

1. -1
2. 0
3. 4
4. 5

12. Consider the following code segment:

numPizzas = 1
numPeople = 4

if numPeople == 5 :
numPizzas = 2

After this code segment executes, what value is in the variable


|numPizzas|?

1. 1
2. 2
3. 4
4. 5

13. Consider the following code segment:

c = 2
b = 1

if b == 0 :
c = c + 1
else :
c = c - 1

print(c)

What value is printed by this code segment?

1. 1
2. 2
3. 3
4. 4

14. What is the error in this statement?


if count = max :
print("You win")

1. Equality is evaluated using two equal signs (|==|), not one.


2. The |print| function should not be indented
3. There must be an |else| statement
4. Nothing, if |count| equals |max|, it would print |"You win"|

15. What is the opposite of this condition: |count > 10|?


1. |count >= 10|
2. |count < 9|
3. |count <= 10|
4. |count <= 9|

16. What is the output of the following code snippet if |count| contains
56?

if count % 2 == 0 :
print("Count is an even number")
else :
print("Count is an odd number")

1. |Count is an even number|


2. |Count is an odd number|
3. Nothing, there is a syntax error
4. Nothing, the program runs but does not print any output

17. What is the output of the following code snippet if |count| contains
56?

if count % 2 == 0 :
print("Count is an even number")
else :
print("Count is an odd number")

1. |Count is an even number|


2. |Count is an odd number|
3. Nothing, there is a syntax error
4. Nothing, the program runs but does not print any output

18. What type of operator is |<=| operator?


1. Lexicographic
2. Arithmetic
3. Inequality
4. Relational

19. The operator |>=| stands for


1. greater than
2. greater than or equal to
3. not equal to
4. this is not a valid Python operator

20. Which statement correctly tests if the user entered the letter |Y|?
1. |if userInput = "y" :|
2. |if userInput = "Y" :|
3. |if userInput == "Y" :|
4. |if userInput == "y" :|

21. Assuming the user enters 15 as input, what is the output of the
following code snippet?

number = int(input("Please enter a number: "))


if number >= 20 :
print("The numer is big")
else :
print("The number is small")

1. There is no output due to a syntax error


2. |The number is big|
3. |The number is small|
4. The program runs successfully but does not print any output

22. What is the output of the following code snippet if the input is 34?

number = int(input("Please enter a number: "))


if number != 20 :
number = number + 1
else :
number = number - 1
print(number)

1. |34|
2. |33|
3. |35|
4. |36|

23. Assuming that the user enters a value of 45, what is the output of
the following code snippet?

number = int(input("Please enter a number: "))


if number < 100 :
number = number + 5
if number < 500 :
number = number - 2
if number > 15 :
number = number + 1
else :
nmber = number - 1
print(number)

1. |105|
2. |45|
3. |43|
4. |49|

24. A store provides a 10% discount on items with a price of at least


$100. Otherwise, no discount is applicable. Which of the following
DOES NOT correctly compute the discount amount when the item's price
is stored in the |price| variable?
1.

discount = 0
if price >= 100 :
discount = 0.10 * price

2.

discount = 0.10 * price


if price <= 100 :
discount = 0

3.

discount = 0
if price >= 100 :
discount = price / 10

4.

discount = 10
if price >= 100 :
discount = 0.1 * price
else :
discount = 0

25. Which of the following conditions is true, given that |num1|


contains 3 and |num2| contains 4?
1. |num1 + 1 < num2|
2. |num1 + 1 > num2|
3. |num1 + num2 != 7|
4. |num1 - num2 <= 0|

26. In Python, which of the following orderings is used to compare


strings?
1. Semantic
2. Alphabetic
3. Syntatic
4. Lexicographic

27. Which condition will cause the statement block of the if statement
to execute only when |count| is 0?
1. |if count = 0 :|
2. |if count < 0 :|
3. |if count =< 0 :|
4. |if count == 0 :|

28. Which of the following if statements is problematic because of the


limited precision of floating-point numbers?
1. |if 4 // 3 == 1 :|
2. |if sqrt(2) * sqrt(2) == 2.0 :|
3. |if "10" == 5 :|
4. |if 4 <= 4 :|

29. Consider the following code segment:

s1 = "CAT"
s2 = "cat"

Which of the following if statements has a condition that evaluates


to |True|?

1. |if s1 == s2 :|
2. |if s1 = s2 :|
3. |if s1 < s2 :|
4. |if s1 >= s2 :|

30. Which statement evaluates to |True| when comparing the two strings:

name1 = "Heather"
name2 = "hanna"

1. |name1 == name2|
2. |name1 > name2|
3. |name1 < name2|
4. Relational operators cannot be used to compare strings

31. Given the following list of strings, what is the correct order using
lexicographic ordering: |"Ann", "amy", "Heather", "hanna", "joe",
"john", "Leo", "Jim"| ?
1. amy, Ann, hanna, Heather, Jim, joe, john, Leo
2. Ann, Heather, Jim, Leo, amy, hanna, joe, john
3. amy, hanna, joe, john, Ann, Heather, Jim, Leo
4. Leo, john, joe, Jim, Heather, hanna, Ann, amy
32. What is the definition of a nested statement?
1. A decision statement that is contained inside the statement
block of another decision statement
2. A compound statement that consists of a header and a statement
block
3. A decision statement that immediately follows another decision
statement at the same indentation level
4. A statement that is used to validate user input

33. Assuming a user enters 30, 20, and 10 as the input values, what is
the output of the following code snippet?

num1 = int(input("Enter a number: "))


num2 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
if num1 > num2 :
if num1 > num3 :
print(num1)
else :
print(num3)
else :
if num2 > num3 :
print(num2)
else :
print(num3)

1. 0
2. 10
3. 20
4. 30

34. Which of the following values make the expression |not x == y and z
> x| true?
1. |x = 10, y = 10, z = 15|
2. |x = 10, y = 20, z = 15|
3. |x = 10, y = 2, z = 5|
4. |x = 10, y = 20, z = 10|

35. What is the output of the following code snippet?

num1 = 100
if num1 < 100 :
if num1 < 50 :
num1 = num1 - 5
else :
num1 = num1 - 10
else :
if num1 > 150 :
num1 = num1 + 5
else :
num1 = num1 + 10
print(num1)

1. 95
2. 100
3. 105
4. 110

36. Which of the following options refers to the technique of simulating


program execution on a sheet of paper?
1. Compiling
2. Prototyping
3. Debugging
4. Tracing

37. Assuming that a user enters 25 for the price of an item, which of
the following hand-trace tables is valid for the given code snippet?

price = 0
status = ""
price = float(input("Enter the price for your item: "))
if price >= 50 :
status = "reasonable"
if price >= 75 :
status = "costly"
else :
status = "inexpensive"
if price <= 25 :
status = "cheap"

1.
price status
0 "inexpensive"
25 "cheap"

2.
price status
0 "inexpensive"
25 "reasonable"

3.
price status
0 "inexpensive"
25 "reasonable"
"costly"

4.
price status
0 "inexpensive"
25 "costly"
38. Which of the following code segments is an example of a nested |if|
statement?
1.

if a == b :
print(a)

2.

if a == b :
print(a)
else :
print(b)

3.

if a == b :
print(a)
if c == d :
print(c)

4.

a = a - 1 if a > 0 else a = a + 1

39. Consider the following code segment:

if a > b :
print("X")
if a == b :
print("Y")

What is displayed if |a| is 1 and |b| is 0?


1. |X|
2. |Y|
3. |X| followed by |Y| on the next line
4. Nothing

40. Consider the following code segment:

if a > b :
print("X")
if a == b :
print("Y")

What is displayed if |a| is 0 and |b| is 0?


1. |X|
2. |Y|
3. |X| followed by |Y| on the next line
4. Nothing

41. Consider the following code segment:

if a > b :
print("X")
if a == b :
print("Y")

What is displayed if |a| is 1 and |b| is 2?


1. |X|
2. |Y|
3. |X| followed by |Y| on the next line
4. Nothing

42. Consider the following code segment:

if a == b :
print("W")
else :
print("X")
if b == c :
print("Y")
else :
print("Z")

If |a|, |b| and |c| are all 0 then the output generated by this code
segment is:

1. |W|
2. |W| followed by |Y| on the next line
3. |X| followed by |Y| on the next line
4. |W| followed by |X| on the next line, followed by |Y| on the
next line

43. Consider the following code segment:

if a == b :
print("W")
else :
print("X")
if b == c :
print("Y")
else :
print("Z")

If |a| is 0, |b| is 1 and |c| is 0 then the output generated by this


code segment is:

1. |W|
2. |X|
3. |X| followed by |Y| on the next line
4. |X| followed by |Z| on the next line

44. Consider the following code segment:

if a == b :
print("W")
else :
print("X")
if b == c :
print("Y")
else :
print("Z")

If |a| is 0, |b| is 1 and |c| is 1 then the output generated by this


code segment is:

1. |W|
2. |X|
3. |X| followed by |Y| on the next line
4. |X| followed by |Z| on the next line

45. What error will Python display when it attempts to execute the
following if/else statement?

if a == b :
print("Equal")
else :
print("Not Equal")
if a > b :
print("a is larger")
else :
print("b is larger")

1. Python will display an error indicating that |==| should be


replaced with |=|
2. Python will display an error indicating that an if statement
cannot reside inside the body of an else
3. Python will display an error indicating that there is a problem
with the indentation
4. No error will be displayed

46. What error will Python display when it attempts to execute the
following if/else statement?

if a = b :
print("Equal")
else :
print("Not Equal")
if a > b :
print("a is larger")
else :
print("b is larger")

1. Python will display an error indicating that |=| is invalid sytax


2. Python will display an error indicating that an if statement
cannot reside inside the body of an else
3. Python will display an error indicating that there is a problem
with the indentation
4. No error will be displayed

47. What is the output of the following code snippet when the user
enters 75 as the grade?

grade = int(input("Enter student grade: "))


if grade >= 90 :
letterGrade = "A"
if grade >= 80 :
letterGrade = "B"
if grade >= 70 :
letterGrade = "C"
if grade >= 60 :
letterGrade = "D"
else :
letterGrade = "E"
print(letterGrade)

1. A
2. B
3. C
4. D

48. What is the wrong with the following code snippet?

grade = int(input("Enter student grade: "))


if grade >= 90 :
letterGrade = "A"
if grade >= 80 :
letterGrade = "B"
if grade >= 70 :
letterGrade = "C"
if grade >= 60 :
letterGrade = "D"
else :
letterGrade = "E"
print(letterGrade)

1. Everyone will get an "E"


2. Anyone with a grade higher than 60 will receive a "D"
3. Nothing is wrong, students will get the correct grade
4. The code block will not compile
49. Given that the following code is incorrect, what code would fix the
following code snippet?

grade = int(input("Enter student grade: "))


if grade >= 90 :
letterGrade = "A"
if grade >= 80 :
letterGrade = "B"
if grade >= 70 :
letterGrade = "C"
if grade >= 60 :
letterGrade = "D"
else :
letterGrade = "E"
print(letterGrade)

1. Change the |if| statements to |elif| statements (except the


first one)
2. Change the |if| statements to |else| statements (except the
first one)
3. Reverse the order of the |if| statements
4. Change the last statement to |if| instead of |else|

50. What is the output of the following code snippet?

x = 20
if x <= 20 :
print("1", end="")
if x <=40 :
print("2", end="")
if x <= 30 :
print("3", end="")

1. 1
2. 2
3. 3
4. 123

51. Consider the following code snippet:

number = int(input("Enter a number: "))


if number > 30 :
...
elif number > 20 :
...
elif number > 10 :
...
else :
...
Assuming that the user input is 40, which block of statements is
executed?
1. |if number > 30 : ...|
2. |else if number > 20 : ...|
3. |else if number > 10 : ...|
4. |else : ...|

52. Consider the following code snippet:

number = int(input("Enter a number: "))


if number < 10 :
print("Too small")
elif number < 50 :
print("Intermediate")
elif number < 100 :
print("High")
else :
print("Too high")

Assuming that the user input is 60, what is the output of the this
code snippet?
1. |Too high|
2. |High|
3. |Intermediate|
4. |Too small|

53. Consider the following code snippet.

num1 = 0
num2 = 0
num3 = 0
num4 = 0
num5 = 0
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
if num1 < num2 :
num3 = num1
else :
num3 = num2

if num1 < num2 + 10 :


num4 = num1
elif num1 < num2 + 20 :
num5 = num1
print("num1 =", num1, "num2 =", num2, "num3 =", num3,
"num4 =", num4, "num5 =", num5)

Assuming that the user enters the numbers 20 and 12 as the two input
values, what is the output of the code snippet?
1. |num1 = 20 num2 = 12 num3 = 20 num4 = 0 num5 = 20|
2. |num1 = 20 num2 = 12 num3 = 12 num4 = 20 num5 = 0|
3. |num1 = 20 num2 = 12 num3 = 12 num4 = 0 num5 = 20|
4. |num1 = 20 num2 = 12 num3 = 20 num4 = 20 num5 = 0|

54. What is the value of the price variable after the following code
snippet is executed?

price = 42
if price < 40 :
price = price + 10
if price > 30 :
price = price * 2
if price < 100 :
price = price - 20

1. |42|
2. |52|
3. |84|
4. |64|

55. Consider the following code snippet:

age = int(input("Enter your age: "))


if age < 10 :
print("Child")
if age < 30 :
print("Young Adult")
if age < 70 :
print("Old")
if age < 100 :
print("Impressively old")

Assuming that the user inputs 80 as the age, what is the output?
1.

Child
Young adult
Old

2.

Young adult
Old

3.

Impressively old

4.
Child
Young adult
Old
Impressive old

56. Consider the following code snippet:

age = int(input("Enter your age:"))


if age < 10 :
print("Child", end="")
if age < 30 :
print("Young Adult", end="")
if age < 70 :
print("Old", end="")
if age < 100 :
print("Impressively old", end="")

Assuming that the user inputs 30 as the age, what is the output?
1. |ChildYoung adultOldImpressively old|
2. |Young adultOldImpressively old|
3. |OldImpressively old|
4. |Impressively old|

57. Consider the following code snippet:

age = int(input("Enter your age: "))


if age < 10 :
print("Child", end="")
if age < 30 :
print("Young Adult", end="")
if age < 70 :
print("Old", end="")
if age < 100 :
print("Impressively old", end="")

Assuming that the user inputs 5 as the age, what is the output?
1. |Child|
2. |ChildYoung Adult|
3. |ChildYoung AdultOld|
4. |ChildYoung adultOldImpressively old|

58. Consider the follow code segment. It is supposed to convert numeric


marks to letter grades. However, it may contain a bug. Examine the
program, and identify what bug, if any, is present.

grade = "F"
if mark >= 80 :
grade = "A"
if mark >= 70 :
grade = "B"
if mark >= 60 :
grade = "C"
if mark >= 50 :
grade = "D"

1. The greater than or equal signs need to be replaced with equal


signs
2. All instances of if, except the first, need to be replaced with
elif
3. All instances of if, except the first, need to be replaced with
else
4. There is nothing wrong with the code segment (it works as
intended)

59. Consider the follow code segment. It is designed to classify widgets


as too small if they are less than 10mm in diameter or too large if
they are 15mm in diameter or more. Otherwise they should be
classified as just right. However, this code may contain a bug.
Examine the program, and identify what bug, if any, is present.

if size >= 0 :
print("Too small")
elif size >= 10 :
print("Just right")
elif size >= 15 :
print("Too big")

1. The greater than or equal signs need to be replaced with greater


than signs
2. All instances of elif need to be replaced with else
3. The order of the conditions (and bodies) must be changed
4. There is nothing wrong with the code segment (it works as
intended)

60. Consider the following code segment. It is designed to convert


letter grades to grade points. Examine the program, and identify
what bug, if any, is present.

if letter == "A" :
gradePoints = 4.0
elif letter == "B" :
gradePoints = 3.0
elif letter == "C" :
gradePoints = 2.0
elif letter == "D" :
gradePoints = 1.0
else :
gradePoints = 0.0

1. The double equal signs need to be replaced with greater than or


equal signs
2. All instances of elif need to be replaced with else
3. The order of the conditions (and bodies) must be changed
4. There is nothing wrong with the code segment (it works as
intended)

61. Flowcharts are made up of all the following elements, EXCEPT:


1. elements for tasks
2. elements for input/output
3. elements for pseudocode
4. elements for decisions

62. The flowchart shows the order in which steps should be executed, and
the diamond-shaped boxes indicate:
1. input
2. algorithms
3. tasks
4. decision statements

63. When testing code for correctness, it always makes sense to


1. Aim for complete coverage of all decision points
2. Identify boundary cases and test them
3. Check all cases using hand-tracing
4. Assume invalid input will never occur

64. Consider the following code segment:

if a == 0 :
print("a is 0")
elif a < 0 :
print("a is less than 0")
else :
print("a is greater than 0")

What is the minimum number of test cases needed to test every line
of code in this segment?

1. 2
2. 3
3. 4
4. 5

65. What two values does the Boolean (|bool|) data type have in Python?
1. Yes / No
2. True / False
3. 0 / 1
4. -1 / 1

66. Which operators listed below are considered boolean operators:


1. |<| / |>|
2. |and| / |or|
3. |==| / |!=|
4. |<=| / |>=|

67. Consider the following code snippet:

emp = int(input("Enter Celsius temperature: "))


if temp > 0 and temp < 100 :
print("Liquid")
if temp <= 0 or temp >= 100 :
print("Not liquid")

Assuming the user enters a value of 120, what will be the output:
1. Nothing is printed
2. |Liquid|
3. |Not Liquid|
4. |LiquidNotLiquid|

68. Which of the following variables is used to store a condition that


can be either |True| or |False|?
1. Logical
2. Boolean
3. Algebraic
4. Conditional

69. Given two variables x and y, how do you test whether exactly one of
them is zero?
1. |if x == 0 or y == 0 :|
2. |if x = 0 or y = 0 :|
3. |if x == 0 and y != 0 or y == 0 and x != 0 :|
4. |if x == 0 and y != 0 and y == 0 and x != 0 :|

70. Given two variables x and y, how do you test whether at least one of
them is zero?
1. |if x == 0 or y == 0 :|
2. |if x = 0 or y = 0 :|
3. |if x == 0 and y != 0 or y == 0 and x != 0 :|
4. |if x == 0 and y != 0 and y == 0 and x != 0 :|

71. Rewrite the following algebraic expression to an equivalent Python


expression:

32 <= temp <= 100

1. |if temp <= 32 and temp <= 100|


2. |if temp <= 32 or temp <= 100|
3. |if temp >= 32 and temp <= 100|
4. |if temp >= 32 or temp <= 100|
72. What value causes the following logical expression to 'short-circuit'?

if temp >= 32 and temp <= 100

1. |temp = 0|
2. |temp = 32|
3. |temp = 100|
4. |temp = 75|

73. The following logical expression will 'short-circuit'...

quantity > 0 and price/quantity < 10

1. When |quantity| is equal to 0


2. When |quantity| is equal to 5
3. When |price/quantity| is less than 10
4. When |price/quantity| is greater than 10

74. Using De Morgan's law, what is the equivalent to this statement?

if not (state == "PA" or state == "OH")

1. |if state != "PA" and state != "OH"|


2. |if state != "PA" or state != "OH"|
3. |if state == "PA" and state == "OH"|
4. |if state == "PA" or state == "OH"|

75. Using De Morgan's law, what is the equivalent to this statement?

if not (state == "PA" and state == "OH")

1. |if state != "PA" and state != "OH"|


2. |if state != "PA" or state != "OH"|
3. |if state == "PA" and state == "OH"|
4. |if state == "PA" or state == "OH"|

76. Consider the following code snippet:

attendance = True
failed = False

Which of the following |if| statements include a condition that


evaluates to |True|?
1. |if attendance == "true" :|
2. |if attendance :|
3. |if failed :|
4. |if attendance == failed :|

77. Consider the following code snippet:


age = int(input("Enter your age: "))
if age < 13 :
print("Child", end="")
if age >= 13 and age <= 19 :
print("Teen", end="")
if age > 19 and age < 30 :
print("Young adult", end="")
if age >= 30 and age <= 50 :
print("Adult", end="")
if age > 50 :
print("Young at heart", end="")

Assuming that the user enters 55 as the age, what is the output?
1. |Teen|
2. |Young at heart|
3. |Child|
4. |Adult|

78. Which of the following expressions represents a legal way of


checking whether a value assigned to the |num| variable falls within
the range 0 to 150 (inclusive)?
1. |if num >= 150 and num <= 0 :|
2. |if num >= 0 and num <= 150 :|
3. |if num >= 0 or num <= 150 :|
4. |if num >= 150 or num <= 0 :|

79. Which of the following expressions represents a legal way of


checking whether a value assigned to the |num| variable is either
less than 100 or more than 200?
1. |if num < 100 and num > 200 :|
2. |if num < 100 and num > 200 :|
3. |if num < 100 or num > 200 :|
4. |if num <= 100 or num >= 200 :|

80. Given the following code snippet:

grade = int(input("Enter student grade: "))


if grade >= 90 :
letterGrade = "A"
elif grade >= 80 and grade < 90 :
letterGrade = "B"
elif grade >= 70 and grade < 80 :
letterGrade = "C"
elif grade >= 60 and grade < 70 :
letterGrade = "D"
else :
letterGrade = "E"
print(letterGrade)

what is value of |grade| when the user enters 75?


1. |"A"|
2. |"B"|
3. |"C"|
4. |"D"|

81. Which of the following operators is used to invert a conditional


statement?
1. or
2. and
3. not
4. equal

82. Which of the following conditions is true only when the integer
variable middle is between 0 and 10 inclusive?
1. |middle >= 0 and middle <= 10|
2. |0 < middle < 10|
3. |0 <= middle or middle <= 10|
4. |middle > 0 and middle < 10|

83. Given that the following code snippet:

isFelon = False
answer = input("have you ever committed a felony? ")
if answer == "Yes" or answer == "yes" :
isFelon = True
age = int(input("what is your age? "))

which statement assigns the variable |mayVote| a value of |True| if


a person may vote if they are 18 or older and not a felon?
1. |mayVote = age > 18 or not isFelon|
2. |mayVote = not ( age >= 18 and isFelon)|
3. |mayVote = age >= 18 and not isFelon|
4. |mayVote = not ( age >= 18 or isFelon)|

84. Given the following code snippet:

MIN_SPEED = 45
MAX_SPEED = 65
speed = 55
if not (speed < MAX_SPEED) :
speed = speed - 10
if not (speed > MIN_SPEED) :
speed = speed + 10
print(speed)

what output is produced?


1. |45|
2. |55|
3. |65|
4. |50|
85. Given the following code snippet:

score = 0
price = 100.0
if score > 0 and price < 200 and price / score > 10 :
print("buy!")

which of the following statements is true?


1. The output is |buy!|
2. The code snippet runs, but there is no output
3. The code snippet has syntax errors
4. The code snippet causes a divide-by-zero exception

86. Which of the following options checks that |city| is neither Atlanta
or Philadelphia?
1. |if not city == "Atlanta" or not city == "Philadelphia"|
2. |if not (city == "Atlanta" or city == "Philadelphia"|)
3. |if not (city == "Atlanta" and city == "Philadelphia"|)
4. |if not city == "Atlanta" or city == "Philadelphia"|

87. Assuming a user enters 30, 55, and 10 as the input, what is the
output of the following code snippet?

num1 = int(input("Enter a number: "))


num2 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
if not (num1 > num2 and num1 > num3) :
print(num1)
elif not(num2 > num1 and num2 > num3) :
print(num2)
elif not (num3 > num1 and num3 > num2) :
print(num3)

1. 55
2. 10
3. 0
4. 30

88. Assuming a user enters 30, 55, and 10 as the input, what is the
output of the following code snippet?

num1 = int(input("Enter a number: "))


num2 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
if num1 > num2 and num1 > num3 :
print(num1)
elif num2 > num1 and num2 > num3 :
print(num2)
elif num3 > num1 and num3 > num2 :
print(num3)

1. 55
2. 10
3. 0
4. 30

89. Which of the following conditions is |True| only when the variables
|a|, |b|, and |c| contain three different values?
1. |if a != b and a != c and b != c :|
2. |if a != b or a != c or b != c :|
3. |if not (a == b and b == c and a == c) :|
4. |if a != b != c :|

90. Consider the following code segment. It should display a message


only if the cost is between 50 and 75 dollars. The message should
also be displayed if the cost is exactly 50 dollars or exactly 75
dollars.

if _________________ :
print("The cost is in the desired range")

What condition should be placed in the blank to achieve the desired


behavior?

1. |cost > 50|


2. |cost < 75|
3. |cost >= 50 and cost <= 75|
4. |cost >= 50 or cost <= 75|

91. Water is liquid between 0 and 100 degrees Celsius. The following
code segment should display a message if the water is *not* liquid.
For this question, we will assume that water is liquid if it is
exactly 0 degrees or exactly 100 degrees.

if _________________ :
print("The water is not liquid")

What condition should be placed in the blank to achieve the desired


behavior?

1. |temp < 0|
2. |temp > 100|
3. |temp < 0 and temp > 100|
4. |temp < 0 or temp > 100|

92. Suppose that |b| is |False| and |x| is 0. Which of the following
expressions evaluates to |True|?
1. |b or x == 1|
2. |b and x == 0|
3. |not b and x == 1|
4. |not b or x == 1|

93. Suppose that |b| is |False| and |x| is 0. Which of the following
expressions evaluates to |True|?
1. |not b and x == 1|
2. |b or x == -1|
3. |not b or b|
4. |x == 1 or x == -1|

94. Which of the following checks to see if there is a comma anywhere in


the string variable |name|?
1. |if "," in name :|
2. |if [Link](",") :|
3. |if "," not in name :|
4. |if [Link](",") :|

95. Which of the following checks to see if the string variable


|sentence| starts with the string |"Dear"|?
1. |if "Dear" in sentence :|
2. |if [Link]("Dear") :|
3. |if "Dear" not in sentence :|
4. |if [Link]("Dear") :|

96. What value is printed by the following code snippet?

name = "John R. Johnson"


firstName = "John"
location = [Link](firstName)
print(location)

1. |-1|
2. |0|
3. |8|
4. |1|

97. What value is printed by the following code snippet?

name = "John R. Johnson"


firstName = "Joe"
location = [Link](firstName)
print(location)

1. |-1|
2. |0|
3. |8|
4. |1|
98. What string method can be used to determine if the string contained
in the variable |text| only consists of letters?
1. |[Link]()|
2. |[Link]()|
3. |[Link]()|
4. |[Link]()|

99. What string method can be used to determine if all characters within
a string are lowercase?
1. |[Link]()|
2. |[Link]()|
3. |[Link]()|
4. |[Link]()|

100. What string method can be used to determine if the string contained
in the variable |text| only consists of numbers?
1. |[Link]()|
2. |[Link]()|
3. |[Link]()|
4. |[Link]()|

101. What will be printed by the following code snippet?

name = "Ravi Avalon"


counter = [Link]("av")
print(counter)

1. |0|
2. |1|
3. |2|
4. |-1|

102. What will be printed by the following code snippet?

name = "Dino the Dinosaur"


counter = [Link]("Di")
print(counter)

1. |0|
2. |1|
3. |2|
4. |-1|

103. Which of the following statements returns the number of blank spaces
contained in the string |sentence|?
1. |[Link](" ")|
2. |" " in sentence|
3. |[Link](" ")|
4. |count(sentence)|
104. Review the code snippet below:

sentence = input("Enter some text: ")


firstCh = sentence[0]

Which of the following statements correctly determines if the first


letter of the string contained in |sentence| is an uppercase letter?
1. |if [Link]() :|
2. |if not ([Link]()) :|
3. |if [Link]() :|
4. |if not ([Link]()) :|

105. Review the code snippet below:

name1 = "Betty joe"


name2 = "Betty Jean"
name3 = "Betty Jane"
if name1 < name2 :
if name1 < name3 :
print(name1, "is first")
else :
print(name3, "is first")
else :
if name2 < name3 :
print(name2, "is first")
else :
print(name3, "is first")

what output is produced?


1. |Betty joe is first|
2. |Betty Jean is first|
3. |Betty Jane is first|
4. |Betty joe is firstBetty Jean is firstBetty Jane is first|

106. How do you test if a filename (given as a string) has an extension


of ".png", ".jpg" or ".gif"?
1. |if [Link](".png" or ".jpg" or ".gif") :|
2. |if [Link](".png") or [Link](".jpg") or
[Link](".gif") :|
3. |if ".png" in filename or ".jpg" in filename or ".gif" in
filename :|
4. |if [Link](".jpg", ".gif", ".png") :|

107. What value is displayed by the following code segment?

s = "Computer Science"
x = [Link]("TER")
print(x)
1. -1
2. 0
3. 5
4. 6

108. What value is displayed by the following code segment?

name = "John Smith"


print([Link]("john"))

1. |-1|
2. |0|
3. |False|
4. |True|

109. Which of the following statements can be used to validate whether


the value a user entered for a grade is in the range 0 to 100,
including both 0 and 100?
1. |if grade > 0 and grade < 100 :|
2. |if grade >= 0 and grade <= 100 :|
3. |if grade <= 0 and grade >= 100 :|
4. |if grade <=0 or grade >=100 :|

110. Which of the following statements is the best choice to validate


user input when entering a marital status as a single letter?
1.

if maritalStatus == "s" or maritalStatus == "m" :

2.

if maritalStatus == "S" or maritalStatus == "M" :

3.

if (maritalStatus == "s" or maritalStatus == "m" or


maritalStatus == "S" or maritalStatus == "M") :

4.

if maritalStatus == "s" or "S" or "m" or "M" :

111. Review the code snippet below:


maritalStatus = input("Enter your marital status (s for single, m for
married): ")
maritalStatus = [Link]()

Which of the following statements can be used to validate whether


the user entered a valid marital status?
1.

if maritalStatus == "S" or maritalStatus == "M" :

2.

if maritalStatus == "s" or maritalStatus == "m" :

3.

if (maritalStatus == "s" or maritalStatus == "m") and


(maritalStatus == "S" or maritalStatus == "M") :

4.

if maritalStatus == "s" or "S" or "m" or "M" :

112. Review the code snippet below:

month = int(input("Enter your two digit birth month: "))

Which of the following statements checks that the user entered a


valid month?
1. |if month >= 1 or month <= 12 :|
2. |if month >= 1 and month <= 12|
3. |if month > 1 or month < 12 :|
4. |if month > 1 and month < 12 :|

113. Assume that the following import statements appear at the beginning
of your program:

from [Link] import MIMEMultipart


from [Link] import MIMEText
from [Link] import MIMEImage
from [Link] import MIMEApplication

Which statement creates a new email message that can contain both
text and images?
1. |msg = MIMEApplication()|
2. |msg = MIMEImage()|
3. |msg = MIMEMultipart()|
4. |msg = MIMEText()|
114. Which part of an email message includes information about the sender
and the recipient?
1. The application
2. The attachment
3. The footer
4. The header

115. What type of object needs to be created to attach a PDF file to an


email message?
1. |MIMEApplication|
2. |MIMEDocument|
3. |MIMEImage|
4. |MIMEPDF|

116. What library needs to be imported to send a message after it has


been created?
1. |email|
2. |login|
3. |mimelib|
4. |smtplib|

117. Which statement about if statements is *not* correct?


1. A compound statement requires a colon at the end of the header.
2. All statements in a statement block must be indented to the same
indentation level.
3. Comments can be indented to any level.
4. The statements in a statement block must be indented 2 spaces
more than the header.

118. Which of the following is *not* an example of a relational operator?


1. |=|
2. |<|
3. |<=|
4. |!=|

119. Which expression is equivalent to the expression shown below?

floor - 1 < 13

1. |13 < floor - 1|


2. |13 >= floor - 1|
3. |floor < 12|
4. |floor - 1 <= 12|

120. Which type of statement should be used to choose exactly one of


several alternatives?
1. if
2. if-elif
3. if-else
4. if-elif-else

121. A messy network of possible pathways through a program is referred


to as:
1. knotted logic
2. spaghetti code
3. twisted conditions
4. zigzag functions

122. Which operator has the lowest precedence?


1. |!=|
2. |*|
3. |**|
4. |and|

123. What value is displayed when the following code segment is executed?

s = "Jonathan"
print([Link]("n"))

1. |-1|
2. |0|
3. |False|
4. |True|

124. Which statement will successfully import the pyplot submodule?


1. |from math import pyplot|
2. |from matplotlib import pyplot|
3. |import pyplot|
4. |import * from pyplot|

125. Which statement adds a bar to a pyplot graph after pyplot has been
imported by the following statement?

from matplotlib import pyplot

1. |bar(4, 44.5)|
2. |[Link](4, 44.5)|
3. |pyplot(4, 44.5)|
4. |[Link](4, 44.5)|

You might also like