1. You compile this program ...
class One {
  public static void main(String[] args) {
    System.out.println(args[args.length - args[3].length()]);     
  }
} 
... then run it as follows:
java One this is my song for the asking
What does the program print (if anything)?
  1.   this
  2.   song
  3.   ing
  4.   the program compiles but does not run
  5.   other

2. Assuming that the following program fragment is syntactically correct...
  int[][] a = new int[10][10];
  ...
  a = fun(a[1], a[2][3]); 
... select the correct header for method fun.
  1.   int[][] fun(int[] a, int b)
  2.   int fun(int[] a, int[][] b)
  3.   int[] fun(int a, int[] b)
  4.   int fun(int[][] a, int b)
  5.   none of the above

3. If you compile and run this code, what is the output?
class One {
  public static void main(String[] args) {
    int[][] a = { {1, 2, 3}, 
                  {4, 5, 6}, 
                  {7, 8, 9}, 
                  {6, 2, 7}
                }; 
    System.out.println(a[a[0][1] - 1][0]); 
  }
} 
  1.   2
  2.   4
  3.   6
  4.   8
  5.   other

4. Consider the following fragment. What does it print?
int[] v = { 1, 2, 3, 4, 5, 6},
      m = { 6, 5, 4, 3, 2, 1};      
for (int i = 0, sum = 0; i < Math.min(v.length, m.length); i++) 
  sum += (v[i] - m[i]); 
System.out.print(sum)
  1.   0
  2.   21
  3.   42
  4.   84
  5.   other

5. Does this source code compile?
class Vegetable { } 
class Tomato extends Vegetable { }
class Kroger {
  public static void main(String[] args) {
    Tomato a = new Vegetable(); 
  }
}
  1.   yes
  2.   no

6. You compile and run this program. What's the output?
public class Five {
    public static void main(String[] args) {
	Five a = new Five(); 
	Five b = new Five(); 
	a.nuf(1); 
	a.fun(); 
	b.nuf(3); 
	b.fun(); 
	a.nuf(5); 
	System.out.println(Five.m + a.n); 
    } 
    void nuf(int p) {
	this.n = this.n + p; 
	Five.m = Five.m - p; 
    } 
    void fun() {
	int temp = this.n; 
	this.n = Five.m; 
	Five.m = temp; 
    } 
    int n; 
    static int m; 
} 
  1.   0
  2.   1
  3.   2
  4.   3
  5.   other

7. Given the following array declaration what does a[a[3][2]][1] evaluate to?
int[][] a = { {2, 4, 3}, 
              {1, 3, 2}, 
              {1, 2, 3}, 
              {3, 4, 1}
            }; 
  1.    1
  2.    2
  3.    3
  4.    4
  5.    other

8. If fun is defined as below:
public static int[] fun(int size, int valu) {
  int[] a = new int[size];
  for (int i = 0; i < size; ++i) a[i] = valu - i;
  return a;
}
What will fun(3, 3)[2] evaluate to? (Yes, it compiles and runs fine).
  1.    0
  2.    1
  3.    2
  4.    3
  5.    other

9. What is printed if you compile and run the following program?
class Nine { 
  public static void main(String[] args) {
    int[] x = {1, 2, 3};
    triple(x);
    System.out.println(x[0] + x[1] + x[2]);
  }
  public static void triple(int[] y) {
    y[2] = 3 * y[2];
  }
} 
  1.   6
  2.   8
  3.   10
  4.   12
  5.   other

10. What is printed if you compile and run the following program?
class Nine { 
  public static void main(String[] args) {
    int[] x = {1, 2, 3};
    triple(x[2]);
    System.out.println(x[0] + x[1] + x[2]);
  }
  public static void triple(int m) {
    m = 3 * m;
  }
} 


  1.   6
  2.   8
  3.   10
  4.   12
  5.   other

11. You compile and run this program. What is the output?
class A {
    public static void main(String[] args) {
        A a = new A(); 
        B b = new B(); 
        A c = new B(); 
        System.out.println(a.fun() + 
                           b.fun() + 
                           c.fun()); 
    } 
    int fun() { return 0; }
} 
class B extends A { int fun() { return 1; } } 
  1.   0
  2.   1
  3.   2
  4.   3
  5.   other

12. What letter should go in both blank spaces for the following program to compile?
class A extends D { } 
class D extends B { } 
class C extends D { 
  public static void main(String[] args) {
    ____ seq[] = new ____[10]; 
    seq[6] = new A(); 
    seq[3] = new B(); 
  } 
} 
class B { } 
class E extends Object { }
  1.   A
  2.   B
  3.   C
  4.   D
  5.   E

13. Assume the program above is in a file E.java and you want to compile and run it.
C:\> javac E.java
C:\> java ____
  1.   A
  2.   B
  3.   C
  4.   D
  5.   E
Fill in the blank with the name of the class that you need to run.

14. What is the output produced by the following code when embedded in a complete program?
boolean x; 
if (x = (false && (false || true))) System.out.print(0); 
else System.out.print(1); 
if (! x) System.out.print(2);   
else System.out.print(3);  
  1.   02
  2.   12
  3.   03
  4.   13
  5.   other

15. What is the output produced by the following code when embedded in a complete program?
boolean x; 
if (x = (false && false || true)) System.out.print(0); 
else System.out.print(1); 
if (! x) System.out.print(2);   
else System.out.print(3);  
  1.   02
  2.   12
  3.   03
  4.   13
  5.   other

June 17, 2004 (by Adrian German for A201)