Preview

18 - If statements and control flow

 1. An if statement takes the following basic form. Fill in the blanks on the first line.
if (____________?) 
{
    // Segment of code that will run if the boolean expression is true
}

  statement

  integer

  boolean expression

  object

 2. Analyse the code below. Which of the following statements is true of this code?
public class IfElseStatementExample {

  public static boolean rain = true;

  public static void main(String[] args) {
    // "== true" can be omitted simplify code
    if (rain == true /* expression */) {
      // "true branch"
      System.out.println("Use your umbrella");
    } else {
      // "false branch"
      System.out.println("Don't use your umbrella");
    }
  }
}

  The if statement tests to see if the variable "rain" contains one or the other string

  The if statement tests to see if the boolean value of "rain" is true.

  The if statement assigns one of the statements to the string variable "rain"

  The if statement tests to see if the boolean value of "rain" is false.

 3. The following code segment is equivalent to: varOne = 0;
if (varOne > 0){
  varOne = -varOne;
}
if (varOne < 0){
 varOne = 0;
}

  FALSE

  TRUE

 4. Unlike the if statement, the else if statement has not two outcomes but __________________.

  the programmer can create boolean variables that spontaneously arise.

  the programmer can create as many possible outcomes as they want.

  a maximum of four outcomes.

  the programmer can only create a single outcome.

 5. Examine the following code. If the expression exampleVariableOne < 10 returns a value of ______, only the statements within the curly brackets of the if statement are executed (everything else ignored).
public class ElseIfStatementExample {

  public static int exampleVariableOne = 37;

  public static void main(String[] args) {
    // else if statements have to include boolean expressions
    if (exampleVariableOne < 10 /* expression */) {
      System.out.println("The number is smaller than 10");
    } else if (exampleVariableOne < 20 /* expression */) {
      System.out.println("The number is between 10 and 20");
    } else if (exampleVariableOne < 30 /* expression */) {
      System.out.println("The number is between 20 and 30");
    } else {
      System.out.println("The number is larger than 30");
    }
  }
}

  "false"

  "true"

  1

  37

 6. The following program uses if statements. What is the purpose of the program?
public class TestProgram extends ConsoleProgram
{
    public void run()
    {
        System.out.println("This program has a specific purpose:");
        int number = readInt("Enter a number: ");
        if (number < 0) 
        {
            System.out.println("You entered a number that was not positive!");
        }
    }
}

  Tests for strings

  Tests for negative numbers

  Tests for integers

  Tests for errors

 7. A user enters an input of "125", what is the output?
public class Age extends ConsoleProgram
{
    public void run()
    {
        int age = readInt("How old are you? ");
        if (age < 0) 
        {
            System.out.println("You can't have a negative age!");
        }
        else if (age > 123)
        {
            System.out.println("Unlikely..");
        }
        else
        {
            System.out.println("Your age: " + age);   
        }
    }
}

  Error

  How old are you?

  Unlikely..

  Your age: 125

 8. Which of the following rules are true of working with if statements?
1 - The if statement may contain zero or one else statement that must come after any else if expressions.
2 - The if statement may have zero or more else if expressions that must come before the else statement.
3 - Once the expression in an else if statement evaluates to true, all the remaining else if and else expressions are ignored.

  Only 2

  None of them

  All of them

  Only 1 and 3

 9. What is the value of varTwo after the following switch statement is executed?
int varOne = 3;
int varTwo = 4;
switch (varOne+3){
  case 6:
    varTwo = 0;
  case 7:
    varTwo = 1;
  default:
    varTwo += 1;
}

  1

  2

  4

  3

 10. What is printed as a result of executing exampleMethod(3,-2)?
public static void exampleMethod(int paramOne, int paramTwo)
{
  if ((paramOne > 0) && (paramTwo > 0)) {
    if (paramOne > paramTwo){
      System.out.println("A");
    }
    else{
      System.out.println("B");
    }
  }
  else if ((paramTwo < 0) || (paramOne < 0)) {
    System.out.println("C");
  }
  else if (paramTwo < 0) {
    System.out.println("D");
  }
  else { 
    System.out.println("E");
  }
}

  D

  B

  A

  C

 11. The following example demonstrates a:
public class DemoEvaluationExample {

  public static boolean exampleVariableOne = true;
  public static boolean exampleVariableTwo = false;
  public static int exampleVariableThree = 5;
  public static int exampleVariableFour = 0;

  public static void main(String[] args) {
    // does not evaluate "0 == 5/0" because the result will always be true
    // as long as exampleVariableOne is true
    if (exampleVariableOne || 0 == exampleVariableThree/exampleVariableFour) {
      System.out.println("The boolean expression is true");
    } else {
      System.out.println("The boolean expression is false");
    }
    // does not evaluate "0 == 5/0" because the result will always be false
    // as long as exampleVariableTwo is false
    if (exampleVariableTwo && 0 == exampleVariableThree/exampleVariableFour) {
      System.out.println("The boolean expression is true");
    } else {
      System.out.println("The boolean expression is false");
    }
  }
}

  Boolean error

  Run time error

  Short Circuit

  Logical error

 12. What is the output of the following code?
int x=20;
int y=10;
if(x>y)
   {
       if (y>10)
       system.out.println(?y is ?+y);
   }
else
     system.out.println(?x is  ?+x);

  x is 20

  Error

  y is 10

  No Output

 13. Analyse the following code segment. What are the values of x and y if n = 1?
x=1;
y=1;
 if(n>0)
  x=x+1;
  y=y-1;

  x=2, y=1

  x=2, y=0

  x=0, y=2

   x=1, y=1

 14. Which of the following control expressions are valid for an if statement?

  either an integer or Boolean expression

  a Boolean expression

  Neither an integer or Boolean expression

  an integer expression

 15. What is the output of the following code?
int x, y;
x=15; y=20;
if (x>15)
   if(y>15)
       {
           system.ptintln(?y is  ?+y);
        }
   else
         system.out.ptintln(?x is ?+x);

  x is 15

  y is 20

  Error

  No output