CS121 Final Exam December 20, 2005 1) Identify and fix the errors in the following code: public class Q1 { public void main(string[] args) { int i; int k = 100.0; int j = i + 1; System.out.println("j is " + j + " and k is " + k); } } [10%] 2) Suppose x = 2 and y = 3, show the output, if any, of the following code. if (x > 2) if (y > 2) { int z = x + y; System.out.println("z is " + z); } else System.out.println("x is " + x); [10%] 3) What is y after the following swith statement is executed? x = 3; switch (x + 3) { case 6: y = 1; default: y += 1; } [10%] 4) Convert the following for loop statement to a while loop: long sum = 0; for (int i = 0; i <= 1000; i++) sum = sum + i; [10%] 5) What is the function of the following program? public class Q5 { public static void main(String[] args) { new Q5(args); } public Q5 (String[] s) { for (int i = s.length - 1; i >= 0; i--) System.out.println(s[i]); } } [10%] 6) What is wrong in the following program? public class Q6 { public static void method(int x) { } public static int method(int y) { return y + 1; } } [10%] 7) Identify and fix the errors in the following code: public class Q7 { public static void main(String[] args) { double[100] r; for (int i = 0; i < r.length(); i++); r(i) = i * 4; } } [10%] 8) Write a class Loan that a) supports three private data fields amount (int), interestRate (double), and nbrOfYears (int); b) provides a constructor for initializing the values of the three data fields; c) provides getter and setter methods for the three data fields; c) provides an overloaded toString() method for representing a Loan object as a string; e) provides method main() that creates a Loan object for a loan of $100,000 with interest rate of 5% (0.05) and number of years of 10; prints the object. [30%]