Skip to main content

Section 4.9 Assessment: Sequential If-Statements

Subgoals for Evaluating Selection Statements.

  1. Diagram which statements go together
  2. For if statement, determine whether expression is true or false
  3. If true – follow true branch, if false – follow else branch or do nothing if no else branch

Exercises Exercises

    1.

      Q1: A game has a series of three levels, but you only get the bonus points if you pass all three levels on the first try. Assume the following declarations:
      boolean level1, level2, level3;
      The variables indicate whether the level was passed on the first try (true indicates yes, false indicates no).
      Which of the following code segments will properly update the points variable?
      1. if (level1 && level2 && level3)
           points += 5;
        
      2. if (level1 || level2 || level3)
           points += 5;
        
      3. if (level1)
           points += 5;
        if (level2)
           points += 5;
        if (level3)
           points += 5;
        
    • I only
    • II only
    • III only
    • I and III
    • II and III

    2.

      Q2: A company offers a discount depending on the number of units of the product that are ordered:
      Table 4.9.1.
      Number of Units
      Discount
      1 up to but not including 10
      0
      10 up to but not including 50
      10%
      50 or more
      20%
      Assume that the variables numUnits, discount have been declared and have values.
      Which of the following code segments will work as intended?
      1. if (numUnits >= 50)
           discount = 0.2;
        if (numUnits >= 10)
           discount = 0.1;
        if (numUnits > 0)
           discount = 0;
        
      2. if (numUnits >= 50)
           discount = 0.2;
        else if (numUnits >=10)
           discount = 0.1;
        else
           discount = 0;
        
      3. if (numUnits > 0)
           discount = 0;
        if (numUnits > 10)
           discount = 0.1;
        if (numUnits > 50)
           discount = 0.2;
        
    • I only
    • II only
    • III only
    • I and III
    • II and III
You have attempted of activities on this page.