Learn. Practice. Feel prepared.
Java Objective Questions and Answers - Set 5
Sharpen your Core Java fundamentals with carefully curated multiple-choice questions covering Strings, Exception Handling, Collections Framework, Multithreading, JVM internals, Memory Management, OOP concepts, and more. This is part 5 of the series.
Practice Java MCQs with Detailed Explanations
Answer at least 12 questions to submit and verify answers.
61 Given: class Bird { { System.out.print("b1 "); } public Bird() { System.out.print("b2 "); } } class Raptor extends Bird { static { System.out.print("r1 "); } public Raptor() { System.out.print("r2 "); } { System.out.print("r3 "); } static { System.out.print("r4 "); } } class Hawk extends Raptor { public static void main(String[] args) { System.out.print("pre "); new Hawk(); System.out.println("hawk "); } } What is the result?
Correct answer:
r1 r4 pre b1 b2 r3 r2 hawk
Static init blocks are executed at class loading time, instance init blocks run right after the call to super() in a constructor. When multiple init blocks of a single type
occur in a class, they run in order, from the top down.
62 Given: 3. public class Ouch { 4. static int ouch = 7; 5. public static void main(String[] args) { 6. new Ouch().go(ouch); 7. System.out.print(" " + ouch); 8. } 9. void go(int ouch) { 10. ouch++; 11. for(int ouch = 3; ouch < 6; ouch++) 12. ; 13. System.out.print(" " + ouch); 14. } 15. } What is the result?
Correct answer:
Compilation fails
The parameter declared on line 9 is valid (although ugly), but the variable
name ouch cannot be declared again on line 11 in the same scope as the declaration on
line 9.
63 Given: 3. public class Bertha { 4. static String s = ""; 5. public static void main(String[] args) { 6. int x = 4; Boolean y = true; short[] sa = {1,2,3}; 7. doStuff(x, y); 8. doStuff(x); 9. doStuff(sa, sa); 10. System.out.println(s); 11. } 12. static void doStuff(Object o) { s += "1"; } 13. static void doStuff(Object... o) { s += "2"; } 14. static void doStuff(Integer... i) { s += "3"; } 15. static void doStuff(Long L) { s += "4"; } 16. } What is the result?
Correct answer:
212
It's legal to autobox and then widen. The first call to doStuff() boxes the int to an Integer then passes two objects. The second call cannot widen and then
box (making the Long method unusable), so it boxes the int to an Integer. As always, a var-args method will be chosen only if no non-var-arg method is possible. The third call is
passing two objects—they are of type 'short array.'
64 Given: 3. class Beta { } 4. class Alpha { 5. static Beta b1; 6. Beta b2; 7. } 8. public class Tester { 9. public static void main(String[] args) { 10. Beta b1 = new Beta(); Beta b2 = new Beta(); 11. Alpha a1 = new Alpha(); Alpha a2 = new Alpha(); 12. a1.b1 = b1; 13. a1.b2 = b1; 14. a2.b2 = b2; 15. a1 = null; b1 = null; b2 = null; 16. // do stuff 17. } 18. } When line 16 is reached, how many objects will be eligible for garbage collection?
Correct answer:
1
It should be clear that there is still a reference to the object referred to by a2, and that there is still a reference to the object referred to by a2.b2. What might be
less clear is that you can still access the other Beta object through the static variable a2.b1—because it's static
65 Given: 3. class Box { 4. int size; 5. Box(int s) { size = s; } 6. } 7. public class Laser { 8. public static void main(String[] args) { 9. Box b1 = new Box(5); 10. Box[] ba = go(b1, new Box(6)); 11. ba[0] = b1; 12. for(Box b : ba) System.out.print(b.size + " "); 13. } 14. static Box[] go(Box b1, Box b2) { 15. b1.size = 4; 16. Box[] ma = {b2, b1}; 17. return ma; 18. } 19. } What is the result?
Correct answer:
4 4
Although main()'s b1 is a different reference variable than go()'s b1, they refer to the same Box object.
66 Given: 3. public class Dark { 4. int x = 3; 5. public static void main(String[] args) { 6. new Dark().go1(); 7. } 8. void go1() { 9. int x; 10. go2(++x); 11. } 12. void go2(int y) { 13. int x = ++y; 14. System.out.println(x); 15. } 16. } What is the result?
Correct answer:
Compilation fails
In go1() the local variable x is not initialized.
67 Given: 1. public class WaitTest { 2. public static void main(String [] args) { 3. System.out.print("1 "); 4. synchronized(args){ 5. System.out.print("2 "); 6. try { 7. args.wait(); 8. } 9. catch(InterruptedException e){} 10. } 11. System.out.print("3 "); 12. } } What is the result of trying to compile and run this program?
Correct answer:
1 2
1 and 2 will be printed, but there will be no return from the wait call because no other thread will notify the main thread, so 3 will never be printed. It's frozen at line 7.
68 Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000); After calling this method, when will the thread A become a candidate to get another turn at the CPU?
Correct answer:
After object B is notified, or after two seconds
Either of the two events will make the thread a candidate for running again.
69 Given the scenario: This class is intended to allow users to write a series of messages, so that each message is identified with a timestamp and the name of the thread that wrote the message: public class Logger { private StringBuilder contents = new StringBuilder(); public void log(String message) { contents.append(System.currentTimeMillis()); contents.append(": "); contents.append(Thread.currentThread().getName()); contents.append(message); contents.append("\n"); } public String getContents() { return contents.toString(); } } How can we ensure that instances of this class can be safely used by multiple threads?
Correct answer:
Synchronize both log() and getContents()
Synchronizing the public methods is sufficient to make this safe, so F is false. This class is not thread-safe unless some sort of synchronization protects the changing data.
70 Given: public static synchronized void main(String[] args) throws InterruptedException { Thread t = new Thread(); t.start(); System.out.print("X"); t.wait(10000); System.out.print("Y"); } What is the result of this code?
Correct answer:
An exception is thrown at runtime
The code does not acquire a lock on t before calling t.wait(), so it throws an IllegalMonitorStateException. The method is synchronized, but it's not synchronized
on t so the exception will be thrown.
71 Given: class MyThread extends Thread { MyThread() { System.out.print(" MyThread"); } public void run() { System.out.print(" bar"); } public void run(String s) { System.out.print(" baz"); } } public class TestThreads { public static void main (String [] args) { Thread t = new MyThread() { public void run() { System.out.print(" foo"); } }; t.start(); } } What is the result?
Correct answer:
MyThread foo
The first line of main we're constructing an instance of an anonymous inner class extending from MyThread. So the MyThread constructor runs and prints MyThread.
Next, main() invokes start() on the new thread instance, which causes the overridden run() method (the run() method in the anonymous inner class) to be invoked.
72 Given: 3. public class Leader implements Runnable { 4. public static void main(String[] args) { 5. Thread t = new Thread(new Leader()); 6. t.start(); 7. System.out.print("m1 "); 8. t.join(); 9. System.out.print("m2 "); 10. } 11. public void run() { 12. System.out.print("r1 "); 13. System.out.print("r2 "); 14. } 15. } Which are true?
Correct answer:
Compilation fails
The join() must be placed in a try/catch block. If it were, answers B and D would be correct. The join() causes the main thread to pause and join the end of the
other thread, meaning "m2" must come last.
73 Given: 3. class Chicks { 4. synchronized void yack(long id) { 5. for(int x = 1; x < 3; x++) { 6. System.out.print(id + " "); 7. Thread.yield(); 8. } 9. } 10. } 11. public class ChicksYack implements Runnable { 12. Chicks c; 13. public static void main(String[] args) { 14. new ChicksYack().go(); 15. } 16. void go() { 17. c = new Chicks(); 18. new Thread(new ChicksYack()).start(); 19. new Thread(new ChicksYack()).start(); 20. } 21. public void run() { 22. c.yack(Thread.currentThread().getId()); 23. } 24. } Which are true?
Correct answer:
An exception is thrown at runtime
When run() is invoked, it is with a new instance of ChicksYack and c has not been assigned to an object. If c were static, then because yack is synchronized, answers
C and E would have been correct.
74 Given: class Plane { static String s = "-"; public static void main(String[] args) { new Plane().s1(); } void s1() { try { s2(); } catch (Exception e) { s += "c"; } } void s2() throws Exception { s3(); s += "2"; s3(); s += "2b"; } void s3() throws Exception { throw new Exception(); } } What is the result?
Correct answer:
-c
Once s3() throws the exception to s2(), s2() throws it to s1(), and no more of s2()’s code will be executed.
75 Given: try { int x = Integer.parseInt("two"); } Which could be used to create an appropriate catch block? (Choose all that apply.) A. ClassCastException B. IllegalStateException C. NumberFormatException D. IllegalArgumentException E. ExceptionInInitializerError F. ArrayIndexOutOfBoundsException
Correct answer:
C, D, E
Integer.parseInt can throw a NumberFormatException, and IllegalArgumentException is its superclass (i.e., a broader exception).