Answers to the problems on the practice exam:

 1. ----------------------------------------------------------( 1)------------------------

Math.random() produces random values uniformly distributed in the interval [0, 1); that's a given. 

The expression Math.random() * 7 produces values uniformly distributed in the interval [0, 7) 

So  Math.random() * 7  + 6  will produce values uniformly distributed in the interval [6, 13)

So the answer is: 5

A proof of sorts can be obtained as follows: 

-bash-4.1$ ls -ld Two.java
-rw-r--r-- 1 dgerman www 556 Jan 29 13:19 Two.java
-bash-4.1$ cat Two.java
import java.util.*;

public class Two {
  public static void main(String[] args) {
    Map<Integer, Integer> histogram = new HashMap<Integer, Integer>();
    for (int i = 0; i < Integer.parseInt( args[0] ); i++) {
      Integer a = Two.genValue();
      if (histogram.containsKey( a )) {
        histogram.put( a , histogram.get( a ) + 1 );
      } else {
        histogram.put( a , 1 );
      }
    }
    System.out.println( histogram );
  }

  public static int genValue() {
    int x = (int)(Math.random() * (12 - 5) + 6);
    return x;
  }
}
-bash-4.1$ javac Two.java
-bash-4.1$ java Two 10
{7=3, 8=1, 9=3, 10=1, 12=2}
-bash-4.1$ java Two 100
{6=12, 7=15, 8=17, 9=9, 10=16, 11=12, 12=19}
-bash-4.1$ java Two 1000
{6=138, 7=150, 8=132, 9=129, 10=145, 11=152, 12=154}
-bash-4.1$ java Two 10000
{6=1425, 7=1399, 8=1417, 9=1473, 10=1394, 11=1429, 12=1463}
-bash-4.1$ java Two 100000
{6=14307, 7=14233, 8=14310, 9=14364, 10=14185, 11=14329, 12=14272}
-bash-4.1$

 2. ----------------------------------------------------------( 2)------------------------

    !(x % 4 != 0 || !(x % 100 == 0 && x % 400 == 0))

is equivalent to: 

    ((x % 400) == 0)

But it's not equivalent to: 

    ((x % 100) == 0)

    ((x % 100) != 0)

    ((x % 400) != 0)

You can prove this with DeMorgan: 


    !(x % 4 != 0 || !(x % 100 == 0 && x % 400 == 0))

is the same as 

      x % 4 == 0 && (x % 100 == 0 && x % 400 == 0)

which is

      x % 4 == 0 && (x % 400 == 0)

which is

      x % 4 == 0 && x % 400 == 0

which is

      x % 400 == 0

To prove it with a program you could randomly generate integers x. 

Then feed them in all five expressions. 

You should obtain counterexamples in all but one case, hence the clue. 

 3. ----------------------------------------------------------( 3, 4, 5, 6)------------------------

-bash-4.1$ cat Seven.java
class Seven {

  public static double f(double x) { return g(x) + Math.sqrt(h(x)); }
  public static double g(double x) { return 4 * h(x); }
  public static double h(double x) { return x * x + k(x) - 1; }
  public static double k(double x) { return 2 * (x + 1); }

  public static void main(String[] args) {
    double x1 = f(2);
    System.out.println( x1 );
    double x2 = g(h(2));
    System.out.println( x2 );
    double x3 = k(g(2) + h(2));
    System.out.println( x3 );
    double x4 = f(0) + f(1) + f(2);
    System.out.println( x4 );
  }
}
-bash-4.1$ javac Seven.java
-bash-4.1$ java Seven
39.0
400.0
92.0
62.0
-bash-4.1$

    -------------------------------------------------------( 7-16)------------------------

-bash-4.1$ cat One.java
class One {
  public static void main(String[] args) {
    { // 7
      String tsew = "stue".charAt(0) + "tsue".charAt(0) + "ew";
      System.out.println(tsew);
    }
    { // 8
      String tsew = "stue".charAt(0) + ("tsue".charAt(0) + "ew");
      System.out.println(tsew);
    }
    { //9, 10, 11
      int i = 3, j = 5, a;
      a = i++ + j++;
      System.out.println( "(" + i + ", " + j + ", " + a + ")" );
    }
    { // 12
      int i = 6;
      System.out.print( i + " " );
      i = i++;
      System.out.println( i );
    }
    { // 13
      int x = 3, y = 11;
      x = x + y;
      y = x - y;
      x = x - y;
      System.out.println("(" + x + ", " + y + ")");
    }
    { // 14
      String plum = "coconut";
      System.out.println(plum.substring(0,"plum".length()));
    }
    { // 15
      String plum = "coconut";
      System.out.println((char)('Q' - plum.length()));
    }
    { // 16
      String plum = "nectarine";
      System.out.println((char)('Q' - "plum".length()));
    }
  }
}
-bash-4.1$ javac One.java
-bash-4.1$ java One
231ew
stew
(4, 6, 8)
6 6
(11, 3)
coco
J
M
-bash-4.1$

    -------------------------------------------------------(17-23)------------------------

-bash-4.1$ cat Two.java
class Two {
  public static void main(String[] args) {
    { // 17
      for (int i=-10; i<=10; i=i+3)
        System.out.print("?");
    }
    System.out.println();
    { // 18
      for (int i=7; i>=0; i--)
        System.out.print("?");
    }
    System.out.println();
    // 19
    int count = 0;
    for (int i = 1; i <= 10; i++) { count += 1; }
    System.out.println( "19. " + count + " iterations. ");
    // 20
    count = 0;
    for (int i = 0; i <= 10; i++) { count += 1; }
    System.out.println( "20. " + count + " iterations. ");
    // 21
    count = 0;
    for (int i = 10; i > 0; i--) { count += 1; }
    System.out.println( "21. " + count + " iterations. ");
    // 22
    count = 0;
    for (int i = -10; i <= 10; i++) { count += 1; }
    System.out.println( "22. " + count + " iterations. ");
    // 23
    count = 0;
    for (int i = -10; i <= 10; i = i + 2) { count += 1; }
    System.out.println( "23. " + count + " iterations. ");

  }
}

-bash-4.1$ javac Two.java
-bash-4.1$ java Two
???????
????????
19. 10 iterations.
20. 11 iterations.
21. 10 iterations.
22. 21 iterations.
23. 11 iterations.
-bash-4.1$

    -------------------------------------------------------(24-26)------------------------

24. b = ( n == 0 ); 

25. b = ( n != 0 ); 

26. b = (1 < n && n < 2); 

which can be further simplified to 

    b = false; 

(assuming that n is an int). 

    -------------------------------------------------------(27-29)------------------------

-bash-4.1$ cat Three.java
class Three {
  public static void main(String[] args) {
    int n = 17, m = 18;
    System.out.println( n / 10 + n % 10  );
    System.out.println( n % 2 + m % 2  );
    System.out.println( ( m + n ) / 2  );
  }
}
-bash-4.1$ javac Three.java
-bash-4.1$ java Three
8
1
17
-bash-4.1$

30. ----------------------------------------------------------(30)------------------------

-bash-4.1$ cat Four.java
class Four {
  public static void main(String[] args) {
    System.out.println( mix("bb", "aa") );
  }
  public static String mix(String one, String two) {
    String answer = "";
    int lengthOne = one.length(), lengthTwo = two.length();
    for (int i = 0; i < lengthOne && i < lengthTwo; i++) {
      answer = one.charAt(i) + two.charAt(i) + answer;
    }
    return answer;
  }
}
-bash-4.1$ javac Four.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-bash-4.1$ java Four
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
195195
-bash-4.1$

    -------------------------------------------------------(31-36)------------------------

31. False. 

A method may have zero, one or more return statements. 

Maybe we should give some examples here: 

class One {
  public static void main(String[] args) { // this method, main, has zero return statements
    for (int i = 0; i < 10; i++) {
      System.out.println( One.randomValue( 100, 30 ); // prints random int in [30, 130)
    }   
    System.out.println( One.contains("aeiouAEIOU", "watermelon".substring( 2, 3)) ); // checks if 3rd letter in "watermelon" is a vowel or not
  }
  public static void randomValue(int range, int offset) { // this method has one return statement 
    return Math.random( (int) (Math.random() * range + offset) ); // the returned value is one of many (range)
  } 
  public static boolean contains(String one, String letter) { // this method has two return statements 
    for (int i = 0; i < one.length(); i++) 
      if (one.substring(i, i+1).equals( letter )) {
        return true; 
      }
    } 
    return false; 
  }   
}

32. False, see above. 

33. False.

On page 203 of your textbook the term "return value" is defined. 

It doesn't mean "return type". A method has only one return type. 

But as is shown in the answer to the first question a method can return two or more values. 

In fact the simplest justification to this question is: no return type has cardinality one. 

void has cardinality zero, boolean has cardinality two and everything else is by and large infinite. 

35. False. 

A return is not necessary if the return type is void, but if it occurs (with no argument) it ends the method. 

If used that way it reminds the use of break in loops, only in a more generalized way, at the method level. 

Maybe I should give an example from your book: take a look at page 215. 

What does this method do: 

public static void mystery(int size) {
  if (size <= 6) {
    return; // too small, won't draw it.
  } 
  for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) { 
      if (i == j || j == 0 || j == size-1) 
        System.out.print("* "); 
      else 
        System.out.print("  "); 
    }  
    System.out.println(); 
  }
}

Here's a test:

-bash-4.1$ cat Five.java
class Five {
  public static void main(String[] args) {
    Five.mystery(Integer.parseInt(args[0]));
  }
  public static void mystery(int size) {
    if (size <= 6) {
      return; // too small, won't draw it.
    }
    for (int i = 0; i < size; i++) {
      for (int j = 0; j < size; j++) {
        if (i == j || j == 0 || j == size-1)
          System.out.print("* ");
        else
          System.out.print("  ");
      }
      System.out.println();
    }
  }
}
-bash-4.1$ javac Five.java
-bash-4.1$ java Five 7
*           *
* *         *
*   *       *
*     *     *
*       *   *
*         * *
*           *
-bash-4.1$ java Five 15
*                           *
* *                         *
*   *                       *
*     *                     *
*       *                   *
*         *                 *
*           *               *
*             *             *
*               *           *
*                 *         *
*                   *       *
*                     *     *
*                       *   *
*                         * *
*                           *
-bash-4.1$ java Five 6
-bash-4.1$ java Five -23
-bash-4.1$

So hopefully this settles it. 

34. True. See the example above. 

36. False. See Math.random()

37. ----------------------------------------------------------(37)------------------------

Yes. 

38. ----------------------------------------------------------(38)------------------------

No.