COMP560 W2 Zych3_Decisions

    1. What statement is used to implement a decision?
     A) if

    2. What are the two parts of an if statement?
    A) A condition and a body
    3. Which of the following statements is true about the if statement?
    A) The else block is optional.

    4. Which of the following is the correct syntax for an if statement?
    A) 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)

    A) The statement after the if statement and the statement after the else statement must be indented
     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)

    A) x: 303

    7. 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)

    A) Your cost is: 100

    8. 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:

    A) 4

    9. 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?

    A) 1

    10. 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?

    A) 1

    11. Which statement about if statements is not correct?
    A) The statements in a statement block must be indented 2 spaces more than the header.

    12. 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)

    A) Syntax error: missing colon after if statement

    13. 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)

    A) x: 0

    14. 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)

    A) Total discounted price is: 64.0

    15. 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"

    A)

    0“inexpensive”
    25“cheap”

    16. What is the error in this statement?

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

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

    17. What is the opposite of this condition: count > 10?
    A) count <= 10

    18. 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")

    A) Nothing, there is a syntax error

    19. 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")

    A) Count is an even number

    20. What type of operator is <= operator?
    A) Relational

    21. The operator >= stands for
    A) greater than or equal to

    22. Which statement correctly tests if the user entered the letter Y?
    A) if userInput == “Y” :

    23. 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")

    A) The number is small

    24. 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)

    A) 35

    25. 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)

    A) 49

    26. In Python, which of the following orderings is used to compare strings?
    A) Lexicographic
    27. Which of the following if statements is problematic because of the limited precision of floating-point numbers?
    A) if sqrt(2) * sqrt(2) == 2.0 :
    28. Consider the following code segment:

    s1 = "CAT"
    s2 = "cat"

    Which of the following if statements has a condition that evaluates to True?

     A) if s1 < s2 :

    29. Given the following list of strings, what is the correct order using lexicographic ordering: "Ann", "amy", "Heather", "hanna", "joe", "john", "Leo", "Jim" ?
    A) Ann, Heather, Jim, Leo, amy, hanna, joe, john

    30. Which of the following is not an example of a relational operator?
    A) =

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

    floor - 1 < 13

    A) floor – 1 <= 12

    32. 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?
    A) discount = 0.10 * price
    if price <= 100 :
    discount = 0

    33. Which of the following conditions is true, given that num1 contains 3 and num2 contains 4?
    A) num1 – num2 <= 0

    34. Which condition will cause the statement block of the if statement to execute only when count is 0?
    A) if count == 0 :

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

    name1 = "Heather"
    name2 = "hanna"

    A) name1 < name2

    36. 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)

    A)  30

    37. 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)

    A)  110

    38. Which of the following options refers to the technique of simulating program execution on a sheet of paper?

    A) Tracing

    39. Which of the following code segments is an example of a nested if statement?
    A) if a == b :
    print(a)
    if c == d :
    print(c)

    40. 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?

    A) X

    41. 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?

    A)  Nothing

    42. 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?

    A) Nothing

    43. Consider the following code segment:

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

    If ab and c are all 0 then the output generated by this code segment is:

    A) W

    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 0 then the output generated by this code segment is:

    A) X followed by Z on the next line

    45. 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:

    A) X followed by Y on the next line

    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")

    A) Python will display an error indicating that there is a problem with the indentation

    47. 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")

    A) Python will display an error indicating that = is invalid sytax
    48. What is the definition of a nested statement?
    A) A decision statement that is contained inside the statement block of another decision statement

    49. 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)

    A) D
    50. 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)

    A) Anyone with a grade higher than 60 will receive a “D”

    51. 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="")

    A)  123
    52. 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?

    A) if number > 30 : …

    53. 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 code snippet?

    A) High

    54. 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?

    A) num1 = 20 num2 = 12 num3 = 12 num4 = 20 num5 = 0

    55. 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

    A) 64

    56. 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?

    A) Impressively old

    57. 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"

    A) All instances of if, except the first, need to be replaced with elif

    58. Consider the following 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")

    A) The order of the conditions (and bodies) must be changed

    59. 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

    A) There is nothing wrong with the code segment (it works as intended)

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

    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.mime.image import MIMEImage
    from email.mime.application import MIMEApplication
    

    Which statement creates a new email message that can contain both text and images?

    A) msg = MIMEMultipart()

    61. Which part of an email message includes information about the sender and the recipient?
    A) The header
    62. What type of object needs to be created to attach a PDF file to an email message?
    A) MIMEApplication

    63. What library needs to be imported to send a message after it has been created?
    A) smtplib

    64. Which type of statement should be used to choose exactly one of several alternatives?
    A) if-elif-else
    65. 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)

    A) Change the if statements to elif statements (except the first one)

    66. 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?

    A) OldImpressively old

    67. 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?

    A) ChildYoung adultOldImpressively old

    68. Flowcharts are made up of all the following elements, EXCEPT:
    A) elements for pseudocode

    69. The flowchart shows the order in which steps should be executed, and the diamond-shaped boxes indicate:
    A) decision statements
    70. A messy network of possible pathways through a program is referred to as:
    A) spaghetti code
    71. When testing code for correctness, it always makes sense to
    A) Aim for complete coverage of all decision points
    72. 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?

    A) 3
    73. Which operators listed below are considered boolean operators:
    A) and / or

    74. 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:

    A) Not Liquid

    75. Which of the following variables is used to store a condition that can be either True or False?
    A) Boolean
    76. Rewrite the following algebraic expression to an equivalent Python expression: 32 <= temp <= 100
    A) if temp >= 32 and temp <= 100

    77. What value causes the following logical expression to ‘short-circuit’?

    if temp >= 32 and temp <= 100

     A) temp = 0

    78. Using De Morgan’s law, what is the equivalent to this statement?

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

    A) if state != “PA” and state != “OH”

    79. Using De Morgan’s law, what is the equivalent to this statement?

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

    A) if state != “PA” or state != “OH”

    80. Consider the following code snippet:

    attendance = True
    failed = False

    Which of the following if statements include a condition that evaluates to True?

    A) if attendance :

    81. 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?

    A) Young at heart

    82. 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?

    A) “C”

    83. Which of the following operators is used to invert a conditional statement?

    A) not
    84. 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?

    A) mayVote = age >= 18 and not isFelon

    85. 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?

    A) 55

    86. 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?

    A) The code snippet runs, but there is no output
    87. Which of the following options checks that city is neither Atlanta or Philadelphia?
    A) if not (city == "Atlanta" or city == "Philadelphia"

    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 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)

    A) 30
    89. 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)

     A) 55
     90. Which of the following conditions is True only when the variables ab, and c contain three different values?
    A) if a != b and a != c and b != c :

    91. 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?

    A) cost >= 50 and cost <= 75

    92. 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?

    A) temp < 0 or temp > 100

    93. Suppose that b is False and x is 0. Which of the following expressions evaluates to True?
    A) not b or x == 1

    94. Suppose that b is False and x is 0. Which of the following expressions evaluates to True?
    A) not b or b
    95. Which operator has the lowest precedence?
    A) and  

    96. Which of the following values make the expression not x == y and z > x true?
    A) x = 10, y = 20, z = 15

    97. What two values does the Boolean (bool) data type have in Python?
    A) True / False

    98. Given two variables x and y, how do you test whether exactly one of them is zero?
    A) if x == 0 and y != 0 or y == 0 and x != 0 :

    99) Given two variables x and y, how do you test whether at least one of them is zero?
    A) if x == 0 or y == 0 :

    100) The following logical expression will ‘short-circuit’…

    quantity > 0 and price/quantity < 10

    A) When quantity is equal to 0

    101) 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)?
    A) if num >= 0 and num <= 150 :

    102. 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?
    A) if num < 100 or num > 200 :

    103. Which of the following conditions is true only when the integer variable middle is between 0 and 10 inclusive?
    A) middle >= 0 and middle <= 10

    104. What string method can be used to determine if the string contained in the variable text only consists of numbers?
    A) text.isdigit()

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

    name = "Ravi Avalon"
    counter = name.count("av")
    print(counter)

    A)  1

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

    name = "Dino the Dinosaur"
    counter = name.count("Di")
    print(counter)

    A) 2

    107. Which of the following statements returns the number of blank spaces contained in the string sentence?
    A)  sentence.count(" ")

    108. 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?

    A) Betty Jane is first

    109. How do you test if a filename (given as a string) has an extension of “.png”, “.jpg” or “.gif”?
    A) if filename.endswith(“.png”) or filename.endswith(“.jpg”) or filename.endswith(“.gif”) :

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

    s = "Computer Science"
    x = s.find("TER")
    print(x)

    A) -1
    111. What value is displayed by the following code segment?

    name = "John Smith"
    print(name.startswith("john"))

    A) False

    112. Which of the following checks to see if the string variable sentence starts with the string "Dear"?

    A) if sentence.startswith("Dear") :

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

    name = "John R. Johnson"
    firstName = "John"
    location = name.find(firstName)
    print(location)

    A) 0

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

    name = "John R. Johnson"
    firstName = "Joe"
    location = name.find(firstName)
    print(location)

    A) -1

    115. What string method can be used to determine if the string contained in the variable text only consists of letters?
    A) text.isalpha()

    116. What string method can be used to determine if all characters within a string are lowercase?
    A) text.islower()

    117. 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?

    A) if firstCh.isupper() :

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

    s = "Jonathan"
    print(s.endswith("n"))

    A) True

    119. Which of the following checks to see if there is a comma anywhere in the string variable name?
    A) if “,” in name :

    120. 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?
    A) if grade >= 0 and grade <= 100 :

    121. Which of the following statements is the best choice to validate user input when entering a marital status as a single letter?
    A) if (maritalStatus == “s” or maritalStatus == “m” or
    maritalStatus == “S” or maritalStatus == “M”) :

    122. Review the code snippet below:

    maritalStatus = input("Enter your marital status (s for single, m for married): ")
    maritalStatus = maritalStatus.upper()

    Which of the following statements can be used to validate whether the user entered a valid marital status?

    A) if maritalStatus == “S” or maritalStatus == “M” :

    123. 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?

    A) if month >= 1 and month <= 12

    124. Which statement will successfully import the pyplot submodule?
    A) from matplotlib import pyplot

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

    from matplotlib import pyplot
    

    A) pyplot.bar(4, 44.5)









     
     
     
     
     
     
     


     


     

    Leave a Reply

    Your email address will not be published. Required fields are marked *