1. In the first loop n = 0 c = 1 + 1 = 2 b = 1 a = 2 In the second loop n = 1 c = 1 + 2 = 3 b = 2 a = 3 In the throed loop n = 2 c = 2 + 3 = 5 b = 3 a = 5 n++ (3) and it violates the condition n < 3. The loop terminates. The output is a = 5 b = 3 2. This program does not initialize sum. You will receive a error message when compiling. Another problem is that if you set sum 0 the result of the program will be 0 but not the desired result 30. 3. public static int prod(int n) { if (n == 1) return 1; return prod(n-1) * (2*n-1); } 4. The while version: public static int prod(int n) { int i = 1; int r = 1; while (i <= n) { r = r * (2*i-1); i ++; } return r; } The for version: public static int prod(int n) { int r = 1; for (int i = 1; i <= n; i ++) r = r * (2*n-1); return r; } 5. public class ProcessData { public static main(String[] args) { int n = 0; double sum; double max = Double.NEGATIVE_INFINITY; while (!StdIn.isEmpty()) { int a = StdIn.readInt(); double b = stdIn.readDouble(); if (a > 0) { n++; sum += a; } if (b > max) max = b; } Systme.out.println("Average= " + sum/n); System.out.println("Max= " + max); } } 6. public static boolean hasAllCharacters(String a, String b) { for (int i = 0; i < b.length(); i ++) for (int j = 0; j < a.length(); j ++) { if (b.charAt(i) != a.charAt(j)) return false; } return true; } 7. public static boolean isEmbeddedString(String a, String b) { if (a.length() == 0) return true; if (b.length() == 0) return false; if (a.charAt(0) == b.charAt(0)) return isEmbeddedString(a.substring(1), b.substring(1)); else return isEmbeddedString(a, b.substring(1)); }