public class Main { public static void main(String[] args) { A[] a = new A[5]; a[0] = new A(3); a[1] = new A(1, 4); a[2] = new B( ); a[3] = a[0]; a[4] = a[1]; for(int i = 0; i <= 4; i++) { a[i].f(i); System.out.println(a[i]); } } } public class A { int x, y; public A(int s) { x = s + 10; } public A(int s, int t) { x = s + t + 20; } public void f(int s) { x += 2 * s; } public String toString( ) { return "*" + (x + y) + "*"; } } public class B extends A { int z; public B( ) { super(1, 1); z = 2; } public void f(int s) { super.f(s); } public String toString( ) { return super.toString( ) + z + "*"; } }
Ans: You must get the recursive calls (your subordinates) to do most of the work. The executive only does the nonrecursive work in the call, which is usually relatively easy.
public A( ) { int x = A( ); }Ans: It makes a recursive call to itself, which will form an infinite chain of recursions.
int[ ] a = {5, 3, 7, 5, 1, 3};What is the output of f(5) if f is defined by
public static void f(int n) {     f(n - 1);     System.out.print(a[n] + " "); }Ans: The output is 3 1 5 7 3 5.
Ans: It is the part of a try/catch block that is always executed, whether or not an exception occurs:
try { // Put code here that might cause an exception. } catch (ExceptionName e) { // Put code here to run if exception occurs. } finally { // Put code here that always runs whether of not // an exception occurs. }
Ans: It is always easier just to throw the exception (let it occur) than it is to write the code for the catch block.
Ans: Look at the list of exceptions in the Final Review Guide.
System.out.println(2 + 3 + "4" + 5 + 6);Ans: 5456
Ans: char gender = args[0].charAt(0);
Ans: double gpa = Double.parseDouble(args[0]);
public interface C { public void f(int x); public int g(String y); }What must you do to implement this interface?
Ans: (1) Use implements C in the first line of the class, (2) Provide definitions of the functions f and g that have the same signatures of the f and g in the interface.
Ans:
Arrays.sort(a); for(int i = a.length - 1; i >= 0; i++) System.out.println(a[i]);
while(s.isEmpty( )) s.pop( );
Ans:
public boolean equals(Object other) { char otherGender = ((Person) other).gender; return (int) gender - (int) otherGender; }
Ans: It means that a local variable is defined with the same name as an instance variable. The local variable hides the instance variable unless the instance variable is prefixed with the this reference.