Answers to Self-Test Questions

  1. 1.

    if (goals > 10)
       System.out.println("Wow");
    else
       System.out.println("Oh Well");
  2. 2.

    if ((goals > 10) && (errors == 0))
          System.out.println("Wow"); 
    else
          System.out.println("Oh Well");
  3. 3.

    if (salary >= deductions)
    {
       System.out.println("OK");
       net = salary - deductions;
    }
    else
    {
       System.out.println("No Way");
    }

    It is also acceptable to omit the braces in the else part.

  4. 4.

    if ((speed > 25) && (visibility < 20))
    {
       speed = 25;
       System.out.println("Caution");
    }
  5. 5.

    if ((salary >= MIN_SALARY) || (bonus >= MIN_BONUS))
       System.out.println("OK");
    else
       System.out.println("Too low");
  6. 6.

    String upperWord = nextWord.toUpperCase();
    if (upperWord.compareTo("N") < 0)
       System.out.println("First half of the alphabet");
    else System.out.println("Second ...

Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.