Skip to main content

Section 5.14 Assessment: Nested 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: Following this code what is the value of delta?
      int alpha = 5, beta = 2, gamma = 3, delta = 0, eta = 0;
      if (alpha > beta)
      {
          if (beta > gamma)
          {
            delta = 10;
          }
          else
          {
            delta = 20;
          }
      }
      else
      {
          if (gamma > beta)
          {
            delta = 30;
          }
          else
          {
            delta = 40;
          }
      }
      
    • 0
    • 10
    • 20
    • 30
    • 40

    2.

      Q2: Following this code, what is the value of eta?
      int alpha = 5, beta = 2, gamma = 3, delta = 0, eta = 0;
      if (alpha > beta)
      {
          if ((beta * gamma) <= alpha)
          {
            eta = 50;
          }
          else
          {
            eta = 60;
          }
      }
      else
      {
          if (delta > beta)
          {
            eta = 70;
          }
          else
          {
            eta = 80;
          }
      }
      
    • 0
    • 50
    • 60
    • 70
    • 80

    3.

      Q3: Following this code, what is the value of delta?
      int alpha = 5, beta = 2, gamma = 3, delta = 0, eta = 0;
      if (alpha > gamma)
      {
          if (gamma > beta)
          {
            delta += 1;
          }
          if (gamma > alpha)
          {
            delta += 2;
          }
      }
      if ((beta + gamma) >= alpha)
      {
          if ((alpha - beta) > gamma)
          {
            delta += 3;
          }
          else
          {
            delta += 4;
          }
      }
      
    • 1
    • 3
    • 4
    • 5
    • 6

    4.

      Q4: Following this code, what is the value of delta and eta?
      int alpha = 5, beta = 2, gamma = 3, delta = 0, eta = 0;
      if ((alpha > beta) && (alpha > gamma))
      {
          if (alpha > delta)
          {
            delta += 1;
            eta += 2;
          }
          if (beta > delta)
          {
            delta += 1;
            eta += 2;
          }
      }
      if ((beta < gamma) && (beta <= delta))
      {
          if (eta > gamma)
          {
            delta += 2;
            eta += 3;
          }
          else
          {
            delta += 3;
            eta += 4;
          }
      }
      
    • delta = 1, eta = 2
    • delta = 2, eta = 4
    • delta = 4, eta = 7
    • delta = 5, eta = 8
    • delta = 6, eta = 9

    5.

      Q5: Variables a, b, and c are all declared as characters. If a='A', b='B', c='C'; What is printed?
      if (a < b) {
          if (a < c) {
            System.out.println("a is smallest");
          }
          else {
            System.out.println("c is smallest");
          }
      }
      else if (b < c) {
          System.out.println("b is smallest");
      }
      else
          System.out.println("clueless");
      
    • a is smallest
    • b is smallest
    • c is smallest
    • clueless

    6.

      Q6: Variables a, b, and c are all declared as characters. If a='C', b='B', c='A'; What is printed?
      if (a < b) {
          if (a < c) {
            System.out.println("a is smallest");
          }
          else {
            System.out.println("c is smallest");
          }
      }
      else if (b < c) {
          System.out.println("b is smallest");
      }
      else
          System.out.println("clueless");
      
    • a is smallest
    • b is smallest
    • c is smallest
    • clueless
You have attempted of activities on this page.