Corejava Objective Questions and Answers (SET - 5)
Please attempt at least 12 objective questions to see the test result.
1) 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?
2) 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?
3) 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?
4) 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?
5) 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?
6) 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?
7) 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?
8) 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?
9) 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?
10) 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?
11) 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?
12) 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?
13) 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?
14) 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?
15) 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