Advanced Java Programming Quiz

Test your knowledge of advanced Java concepts including inner classes, garbage collection, method overloading, varargs, autoboxing, final modifiers, and Java version compatibility.

20 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

Given: class CardBoard { Short story = 5; CardBoard go(CardBoard cb) { cb = null; return cb; } public static void main(String[] args) { CardBoard c1 = new CardBoard(); CardBoard c2 = new CardBoard(); CardBoard c3 = c1.go(c2); c1 = null; // do Stuff } } When // doStuff is reached, how many objects are eligible for GC?

  1. 0
  2. 1
  3. 2
  4. Compilation fails
  5. It is not possible to know
  6. An exception is thrown at runtime
Question 2 Multiple Choice (Single Answer)

Given: class Alien { String invade(short ships) { return "a few"; } String invade(short... ships) { return "many"; } } class Defender { public static void main(String [] args) { System.out.println(new Alien().invade(7)); } } What is the result?

  1. many
  2. a few
  3. Compilation fails
  4. The output is not predictable
  5. An exception is thrown at runtime
Question 3 Multiple Choice (Single Answer)

Given: 1. class Dims { 2. public static void main(String[] args) { 3. int[] [] a = {{1,2,}, {3,4}}; 4. int[] b = (int []) a [1] ; 5. Object o1 = a; 6. int[] [] a2 = (int[] []) o1; 7. int[] b2 = (int []) o1; 8. System.out.println(b[1]); 9. } } What is the result?

  1. 2
  2. 4
  3. An exception is thrown at runtime
  4. Compilation fails due to an error on line 4
  5. Compilation fails due to an error on line 5
  6. Compilation fails due to an error on line 6
Question 4 Multiple Choice (Single Answer)

Given: class Eggs { int doX(Long x, Long y) { return 1; } int doX(long... x) { return 2; } int doX(Integer x, Integer y) { return 3; } int doX(Number n, Number m) { return 4; } public static void main(String[] args) { new Eggs().go(); } void go () { short s = 7; System.out.print(doX(s,s) + " "); System.out.println(doX(7,7)); } }

  1. 1 1
  2. 2 1
  3. 3 1
  4. 4 1
  5. 3 3
  6. 4 3
Question 5 Multiple Choice (Multiple Answers)

Which is true? (Choose all that apply.)

  1. The invocation of an object's finalize() method is always the last thing that happens before an object is garbage collected (GCed).
  2. When a stack variable goes out of scope it is eligible for GC.
  3. Some reference variables live on the stack, and some live on the heap.
  4. Only objects that have no reference variables referring to them can be eligible for GC.
  5. It's possible to request the GC via methods in either java. lang. Runtime or java.lang.System classes.
Question 6 Multiple Choice (Multiple Answers)

Given: 1. class Convert { 2. public static void main(String[] args) { 3. Long xL = new Long(456L); 4. long x1 = Long.valueOf("123"); 5. Long x2 = Long.valueOf("123"); 6. long x3 = xL.longValue(); 7. Long x4 = xL.longValue(); 8. Long x5 = Long.parseLong("456"); 9. long x6 = Long.parseLong("123"); 10. } 11. } Which will compile using Java 5, but will NOT compile using Java 1.4? (Choose all that apply.)

  1. Line 4
  2. Line 5
  3. Line 6
  4. Line 7
  5. Line 8
  6. Line 9
Question 7 Multiple Choice (Multiple Answers)

Given: 1. class Bigger { 2. public static void main(String[] args) { 3. // insert code here 4. } 5. } 6. class Better { 7. enum Faster {Higher, Longer}; 8. } Which, inserted independently at line 3, will compile? (Choose all that apply.)

  1. Faster f = Faster.Higher;
  2. Faster f = Better.Faster.Higher;
  3. Better.Faster f = Better.Faster.Higher;
  4. Bigger.Faster f = Bigger.Faster.Higher;
  5. Better. Faster f2; f2 = Better.Faster.Longer;
  6. Better b; b.Faster = f3; f3 = Better.Faster.Longer;
Question 8 Multiple Choice (Multiple Answers)

Given: 1. class Example { 2. public static void main(String[] args) { 3. Short s = 15; 4. Boolean b; 5. // insert code here 6. } 7. } Which, inserted independently at line 5, will compile? (Choose all that apply.)

  1. b = (Number instanceof s);
  2. b = (s instanceof Short);
  3. b = s.instanceof(Short);
  4. b = (s instanceof Number);
  5. b = s.instanceof(Object);
  6. b = (s instanceof String);
Question 9 Multiple Choice (Multiple Answers)

Given two files: 1. class One { 2. public static void main(String[] args) { 3. int assert = 0; 4. } 5. } 1. class Two { 2. public static void main(String[] args) { 3. assert(false); 4. } 5. } And the four command-line invocations: javac -source 1.3 One.java javac -source 1.4 One.java javac -source 1.3 Two.java javac -source 1.4 Two.java What is the result? (Choose all that apply.)

  1. Only one compilation will succeed.
  2. Exactly two compilations will succeed.
  3. Exactly three compilations will succeed.
  4. All four compilations will succeed.
  5. No compiler warnings will be produced.
  6. At least one compiler warning will be produced.
Question 10 Multiple Choice (Single Answer)

Given: class Mixer { Mixer() { } Mixer(Mixer m) { ml = m;} Mixer m1; public static void main(String[] args) { Mixer m2 = new Mixer(); Mixer m3 = new Mixer(m2); m3.go(); Mixer m4 = m3.m1; m4.go(); Mixer m5 = m2.m1; m5.go(); } void go() { System.out.print("hi "); } } What is the result?

  1. hi
  2. hi hi
  3. hi hi hi
  4. Compilation fails
  5. hi, followed by an exception
  6. hi hi, followed by an exception
Question 11 Multiple Choice (Single Answer)

Given: 1. class Zippy { 2. String[] x; 3. int[] a [] = {{1,2}, {l}}; 4. Object c = new long [4] ; 5. Object[] d = x; 6. } What is the result?

  1. Compilation succeeds.
  2. Compilation fails due only to an error on line 3
  3. Compilation fails due only to an error on line 4
  4. Compilation fails due only to an error on line 5
  5. Compilation fails due to errors on lines 3 and 5
  6. Compilation fails due to errors on lines 3, 4, and 5
Question 12 Multiple Choice (Single Answer)

Given: class Fizz { int x = 5; public static void main(String[] args) { final Fizz f1 = new Fizz(); Fizz f2 = new Fizz(); Fizz f3 = FizzSwitch(f1,f2); System.out.println((f1 == f3) + " " + (f1.x == f3.x)); } static Fizz FizzSwitch(Fizz x, Fizz y) { final Fizz z = x; z.x = 6; return z; } } What is the result?

  1. true true
  2. false true
  3. true false
  4. false false
  5. Compilation fails.
  6. An exception is thrown at runtime
Question 13 Multiple Choice (Single Answer)

Given: class Knowing { static final long tooth = 343L; static long doIt(long tooth) { System.out.print(++tooth + " "); return ++tooth; } public static void main(String[] args) { System.out.print(tooth + " "); final long tooth = 340L; new Knowing().doIt(tooth); System.out.println(tooth); } } What is the result?

  1. 343 340 340
  2. 343 340 342
  3. 343 341 342
  4. 343 341 340
  5. Compilation fails.
  6. An exception is thrown at runtime
Question 14 Multiple Choice (Single Answer)

Given: 1. class Eco { 2. public static void main(String[] args) { 3. Eco e1 = new Eco(); 4. Eco e2 = new Eco(); 5. Eco e3 = new Eco(); 6. e3.e = e2; 7. e1.e = e3; 8. e2 = null; 9. e3 = null; 10. e2.e = el; 11. e1 = null; 12. } 13. Eco e; 14. } At what point is only a single object eligible for GC?

  1. After line 8 runs
  2. After line 9 runs.
  3. After line 10 runs.
  4. After line 11 runs.
  5. An exception is thrown at runtime.
  6. Never in this program
Question 15 Multiple Choice (Single Answer)

Given: class Clidders { public final void flipper() { System.out.print(" Flip a Clidder"); } } public class Clidlets extends Clidders { public void flipper() { System.out.print(" Flip a Clidlet"); super.flipper(); } public static void main(String [] args) { new Clidlets().flipper(); } } What is the result?

  1. Flip a Clidlet
  2. Flip a Clidder
  3. Flip a Clidder Flip a Clidlet
  4. Flip a Clidlet Flip a Clidder
  5. Compilation fails
Question 16 Multiple Choice (Single Answer)

Given:class Clidder { private final void flipper() { System.out.print(" Flip a Clidder"); }}public class Clidlet extends Clidder { public final void flipper() { System.out.print(" Flip a Clidlet"); } public static void main(String [] args) { new Clidlet().flipper(); }}What is the result?

  1. Flip a Clidlet
  2. Flip a Clidder
  3. Flip a Clidlet Flip a Clidder
  4. Flip a Clidder Flip a Clidlet
  5. Compilation fails.
Question 17 Multiple Choice (Single Answer)

Given: 1. class Comp2 { 2. public static void main(String[] args) { 3. float f1 = 2.3f; 4. float[][] f2 = {{42.Of}, {l.7f, 2.3f}, {2.6f, 2.7f}}; 5. float[] f3 = {2.7f}; 6. Long x = 42L; 7. // insert code here 8. System.out.println("true"); 9. } 10. } And the following five code fragments: F1. if (f1 == f2) F2. if (f1 == f2[2][1]) F3. if (x == f2[0][0]) F4. if (f1 == f2 [1,1]) F5. if (f3 == f2 [2]) What is true?

  1. One of them will compile, only one will be true.
  2. Two of them will compile, only one will be true.
  3. Two of them will compile, two will be true.
  4. Three of them will compile, only one will be true
  5. Three of them will compile, exactly two will be true.
  6. Three of them will compile, exactly three will be true.
Question 18 Multiple Choice (Single Answer)

Given: public class MyOuter { public static class MyInner { public static void foot) { } } } Which, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class?

  1. MyOuter.MyInner m = new MyOuter.Mylnner();
  2. MyOuter.MyInner mi = new MyInner();
  3. MyOuter m = new Myouter(); MyOuter.MyInner mi = m.new Myouter.MyInner();
  4. MyInner mi = new MyOuter.MyInner();
Question 19 Multiple Choice (Single Answer)

Given: public class Foo { Foo() {System.out.print("foo");} class Bar{ Bar() {System.out.print("bar");} public void go() {System.out.print("hi");} } public static void main(String[] args) { Foo f = new Foo (); f.makeBar(); } void makeBar() { (new Bar() {}).go(); } } What is the result?

  1. Compilation fails.
  2. An error occurs at runtime.
  3. foobarhi
  4. barhi
  5. hi
  6. foohi
Question 20 Multiple Choice (Single Answer)

package loop; public class TestLoop5 { public static void main(String[] args) { for(;;) { System.out.println("I am in for loop"); } } }

  1. I am in for loop
  2. Infinite Loop
  3. Compiler Error
  4. Exception