Skip to main content

Section 5.4 Selection-WE2-P1

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

Subsection 5.4.1

Exercises Exercises

1.
    Q6: What is the output of the following code?
    if (false)
       System.out.println("it is true");
    else
       System.out.println("it is false");
    
  • it is true
  • it is false
  • There is no output because a compiler error occurs
  • There is no output because an exception occurs
2.
    Q7: What is the output of the following code?
    if (12 < 12)
       System.out.println("Never");
    else
       System.out.println("Always");
    
  • Never
  • Always
  • There is no output because a compiler error occurs
  • There is no output because an exception occurs
3.
    Q8: What is the output of the following code?
    double var1 = 15.0;
    double var2 = 25.12;
    if ((2 * var1) >= var2)
       System.out.println("O.K.");
    else
       System.out.println("Not O.K.");
    
  • O.K.
  • Not O.K.
  • There is no output because a compiler error occurs
  • There is no output because an exception occurs
4.
    Q9: What is the output of the following code?
    int x = 0;
    if (x = 0)  /* look closely! */
       System.out.println("0 is true");
    else
       System.out.println("0 is false");
    
  • 0 is true
  • 0 is false
  • There is no output because a compiler error occurs
  • There is no output because an exception occurs
You have attempted of activities on this page.