Answers to the Questions

  1. 4+9*3-2 = 29.

  2. 9/2 = 4.

  3. 10%(2+2) = 2.

  4. Yes, you can include any kind of statement, including a compound statement, inside a compound statement. For example, you can write

      {
        x = 3;
        {
          y = x;
          z = 2;
        }
        w = z;
      }
      
    There are three things done in sequence here: (1) statement x = 3; (2) the compound statement {y = x; z = 2;}, and (3) statement w = z;. Notice that each of these three statements begins in the same column. That is, each has its first line indented the same amount.

  5.   {
        x = 1; 
        y = 2;
        z = 3;
      }
      

  6. There are two values of type boolean. They are called true and false.

  7. Variable x holds 16. It is set to 10 first, then 8, then 16.

  8.     num++;
      
    abbreviates
        num = num + 1;
      

  9. No. Statement

        x++;
      
    changes what variable x contains. For example, if x holds 2 and you perform x++;, then x will hold 3 afterwards. But statement
        x+1;
      
    just asks to compute x+1, but does nothing with the result. If x holds 2 before you do statement x+1;, then x still holds 2 afterwards.

  10. There are no parentheses around the condition.

  11. Variable w holds 4 after performing those statements.

  12. 24 <= 25 = true.

  13. No, 24 =< 25 is not allowed. The less-than-or-equal-to operator is written <=, not =<.

  14. No, 24 !> 25 is not allowed. !> is not one of the Java relational operators.

  15. Yes, 24 > 25 is allowed. Its value is false.

  16. Yes, x + 4 > 2 * y is allowed. You can compare the values of arbitrarily complicated expressions.

  17. Remember that you use == (two equal signs) to ask if two values are the same. The single equal sign, =, is used for writing assignment statements. You can actually write an assignment statement as an expression. Its value is the value that is put into the variable. So expression w = 64 has value 64. It also has a side-effect of putting w into variable w.

  18. If x holds 19 and y holds 84, then x > y is false, but y > 0 is true. So x > y || y > 0 is true.

  19. Write x > y && y > z.

  20. The word if must be written using all lower case letters. You cannot write it If.

  21. No. There are no parentheses around the condition.

  22. No. The word while must be written using all lower case letters.

  23. A convenient way to hand-simulate a loop like this is two write the variables that are changing, and to show their values each time the loop tests its condition, including the first and last times. Here is what happens for this loop.

              r            s
            -----        -----
              1            1
              2            3
              3            7
              4           15
              5           31
      
    Each row shows the values of r and s just before the condition r != 5 is tested. Now the loop stops, since it only keeps going as long as r is not 5. The value of s that is printed is 31.

  24. Yes, those two are the same. A while-loop goes through zero or more times. The if-statement just does the initial test twice.

    You can see this by doing a hand simulation. What if the condition C starts out true? Then you go into the if-statement, and then into the while-loop. That is just what it would have done if the if-statement were not there.

    If condition C starts out false, then this program fragment does nothing. But that is just what it would do even if the if-statement were not there.

  25. Yes, it works. When you are thinking about whether a program fragment works, it is a good idea to think about so-called boundary values. They are values that are at extremes, such as the smallest possible value. So try n = 0.

    Initialiation sets sum = 0. The for-loop initialization makes k = 1. But, since n = 0, the condition k <= n is false right away, and the body is not done at all. At the end of the loop (which is immediate), variable sum still contains 0. That is the correct sum of the integers up to 0.

    You can check that the loop still works for other values. Really, the only difference between this loop and the former one is that this one avoids adding 0, which has no effect anyway.

  26. The loop heading uses commas instead of semicolons to separate the parts.

  27. The heading of a for loop needs three parts separated by semicolons. This looks like it should be a while loop.

  28. Yes, it gives the correct answer for n = 1. Try it. The initial value of i is 2. Since 2 < 1 is false, the loop body will not be done at all. The if-statement after the loop will see i = 2 and n = 1. So it does its else part, and sets isComposite to false. That is correct, since 1 is not composite.

  29. x = 6, y = 4 and z = 2.

  30. snore = 65, sleep = 133, snooze = 399.

  31. The function heading says that foo returns an integer. So it cannot return a string.

  32. When you use a function, you do not write the type of the argument.

  33. Function min does not return anything when x == y. It must always return a result.