Skip to main content

Section 4.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 4.4.1

Each question below is independent, but they all use this given setup
int alpha = 2, beta = 1, delta = 3, eta = 0, gamma = 0;
double omega = 2.5, theta = -1.3, kappa = 3.0, lambda = 0.0, rho = 0.0;

Exercises Exercises

1.
    Q6: 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
2.
    Q7: 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
3.
    Q8: 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
4.
    Q9: 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
You have attempted of activities on this page.