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 histogram = new HashMap(); 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. ----------------------------------------------------------( 7)------------------------ 8. ----------------------------------------------------------( 8)------------------------ 9. ----------------------------------------------------------( 9)------------------------ 10. ----------------------------------------------------------(10)------------------------ 11. ----------------------------------------------------------(11)------------------------ 12. ----------------------------------------------------------(12)------------------------ 13. ----------------------------------------------------------(13)------------------------ 14. ----------------------------------------------------------(14)------------------------ 15. ----------------------------------------------------------(15)------------------------ 16. ----------------------------------------------------------(16)------------------------ 17. ----------------------------------------------------------(17)------------------------ 18. ----------------------------------------------------------(18)------------------------ 19. ----------------------------------------------------------(19)------------------------ 20. ----------------------------------------------------------(20)------------------------ 21. ----------------------------------------------------------(21)------------------------ 22. ----------------------------------------------------------(22)------------------------ 23. ----------------------------------------------------------(23)------------------------ 24. ----------------------------------------------------------(24)------------------------ 25. ----------------------------------------------------------(25)------------------------ 26. ----------------------------------------------------------(26)------------------------ 27. ----------------------------------------------------------(27)------------------------ 28. ----------------------------------------------------------(28)------------------------ 29. ----------------------------------------------------------(29)------------------------ 30. ----------------------------------------------------------(30)------------------------ 31. ----------------------------------------------------------(31)------------------------ 32. ----------------------------------------------------------(32)------------------------ 33. ----------------------------------------------------------(33)------------------------ 34. ----------------------------------------------------------(34)------------------------ 35. ----------------------------------------------------------(35)------------------------ 36. ----------------------------------------------------------(36)------------------------ 37. ----------------------------------------------------------(37)------------------------ 38. ----------------------------------------------------------(38)------------------------ 39. ----------------------------------------------------------(39)------------------------ 40. ----------------------------------------------------------(40)------------------------