AP Computer Science A Final Study Guide
What will be the output of the program? public class X { public static void main(String [] args) { String names [] = new String[5]; for (int x=0; x < args.length; x++) names[x] = args[x]; System.out.println(names[2]); } } and the command line invocation is > java X a b
null
What will be the output of the program? class Test { public static void main(String[] args) { int x=20; String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge"; System.out.println(sup); } }
tiny
Which two of the following are legal declarations for nonnested classes and interfaces?
1. final abstract class Test {} 2. public static interface Test {} 3. final public class Test {} 4. protected abstract class Test {} 5. protected interface Test{} 6. abstract public class Test {}
Which of the following is/are legal method declarations?
1. protected abstract void m1(); 2. static final void m1(){} 3. synchronized public final void m1() {} 3. private native m1();
Which statement is true?
A. Memory is reclaimed by calling Runtime.gc(). B. Objects are not collected if they are accessible from live threads. C. An OutOfMemory error is only thrown if a single block of memory cannot be found that is large enough for a particular requirement. D. Objects that have finalize() methods always have their finalize() methods called before the program ends.
Which is a valid keyword in Java?
A.) interface B.) string C.) float D.) unsigned
What will be the output of the program? String d = "bookkeeper"; d.substring(1,7); d = "w" + d; d.append("woo"); /* Line 4 */ System.out.println(d);
Compilation fails
What will be the output of the program? public class A { void A() /* Line 3 */ { System.out.println("Class A"); } public static void main(String[] args) { new A(); } }
The code will execute with no output
Which three pieces of code are equivalent to line 3? public interface Foo { int k = 4 /* Line 3 */ }
1. final int k = 4; 2. public int k = 4; 3. static int k = 4; 4. abstract int k = 4; 5. volatile int k = 4; 6. protected int k = 4;
What is the value of "d" after this line of code has been executed? double d = Math.round (2.5 + Math.random());
3
Which of the following statements is true?
A.) It is sometimes good practice to throw an AssertionError explicitly. B.) Private getter() and setter() methods should not use assertions to verify arguments. C.) If an AssertionError is thrown in a try-catch block, the finally block will be bypassed. D.) It is proper to handle assertion statement failures using a catch (AssertionException ae) block.
Which answer most closely indicates the behavior of the program? public class MyProgram { public static void throwit () { throw new RuntimeException(); } public static void main (String args[] ) { try { System.out.println("Hello World"); throwit(); System.out.println("Done with try block"); } finally { System.out.println("Finally executing"); } }
A.) The program will not compile B.) The program will print Hello world, then will print that RuntimeException has occured, then will print Done with try block, and then will print Finally executing C.) The program will print Hello world, then will print that a RuntimeException has occured, and then will print Finally executing. D.) The program will print Hellow world, then will print Finally executing, then will print that RuntimeException has occured
Given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true? import java.io.*; public class MyProgram { public static void main(String args[]) { FileOutputStream out = null; try { out = new FileOutputStream("test.txt"); out.write(122); } catch(IOException io) { System.out.println("IO Error."); } finally { out.close(); } } }
A.) This program will compile successfully. B.) This program fails to compile due to an error at line 4. C.) This program fails to compile due to an error at line 6. D.) This program fails to compile due to an error at line 18.
Which causes a compiler error?
A.) int[ ] scores = {3, 5, 7}; B.) int[ ] [ ] scores = {2, 7, 6}, {9, 3, 45}; C.) String cats[] = {"Fluffy", "Spot", "Zeus"}; D.) boolean results[ ] = new boolean [] {true, false, true}; E.) Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};
class Bar { } class Test { Bar doBar() { Bar b = new Bar(); /* Line 6 */ return b; /* Line 7 */ } public static void main (String args[]) { Test t = new Test(); /* Line 11 */ Bar newBar = t.doBar(); /* Line 12 */ System.out.println("newBar"); newBar = new Bar(); /* Line 14 */ System.out.println("finishing"); /* Line 15 */ } } At what point is the Bar object, created on line 6, eligible for garbage collection?
After line 14
What will be the output of the program? public class TestDogs { public static void main(String [] args) { Dog [][] theDogs = new Dog[3][]; System.out.println(theDogs[2][0].toString()); } } class Dog { }
An exception is thrown at runtime
What will be the output of the program? public class CommandArgsTwo { public static void main(String [] argh) { int x; x = argh.length; for (int y = 1; y <= x; y++) { System.out.print(" " + argh[y]); } } } and the command-line invocation is > java CommandArgsTwo 1 2 3
An exception is thrown at runtime
What will be the output of the program? int x = l, y = 6; while (y--) { x++; } System.out.println("x = " + x +" y = " + y);
Compilation fails
What will be the output of the program? public class Switch2 { final static short x = 2; public static int y = 0; public static void main(String [] args) { for (int z=0; z < 3; z++) { switch (z) { case y: System.out.print("0 "); /* Line 11 */ case x-1: System.out.print("1 "); /* Line 12 */ case x: System.out.print("2 "); /* Line 13 */ } } } }
Compilation fails at line 11
What will be the output of the program? public class Test { public static void main(String args[]) { class Foo { public int i = 3; } Object o = (Object)new Foo(); Foo foo = (Foo)o; System.out.println("i = " + foo.i); } }
i = 3