Corejava Objective Questions and Answers (SET - 4)
Please attempt at least 12 objective questions to see the test result.
1) Given: 1. public class HorseTest { 2. public static void main(String[] args) { 3. class Horse { 4. public String name; 5. public Horse(String s) { 6. name = s; 7. } 8. } 9. Object obj = new Horse("Zippo"); 10. System.out.println(obj.name); 11. } 12. } What is the result?
2) The following block of code creates a Thread using a Runnable target: Runnable target = new MyRunnable(); Thread myThread = new Thread(target); Which of the following classes can be used to create the target, so that the preceding code compiles correctly?
3) Given: 3. class MyThread extends Thread { 4. public static void main(String [] args) { 5. MyThread t = new MyThread(); 6. Thread x = new Thread(t); 7. x.start(); 8. } 9. public void run() { 10. for(int i=0;i<3;++i) { 11. System.out.print(i + ".."); 12. } } } What is the result of this code?
4) Given: 3. class Test { 4. public static void main(String [] args) { 5. printAll(args); 6. } 7. public static void printAll(String[] lines) { 8. for(int i=0;i<lines.length;i++){ 9. System.out.println(lines[i]); 10. Thread.currentThread().sleep(1000); 11. } } } The static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?
5) Given: public abstract class AbstractTest { public int getNum() { return 45; } public abstract class Bar { public int getNum() { return 38; } } public static void main(String[] args) { AbstractTest t = new AbstractTest() { public int getNum() { return 22; } }; AbstractTest.Bar f = t.new Bar() { public int getNum() { return 57; } }; System.out.println(f.getNum() + " " + t.getNum()); } } What is the result?
6) Given: 3. public class Tour { 4. public static void main(String[] args) { 5. Cathedral c = new Cathedral(); 6. // insert code here 7. s.go(); 8. } 9. } 10. class Cathedral { 11. class Sanctum { 12. void go() { System.out.println("spooky"); } 13. } 14. } Which, inserted independently at line 6, compile and produce the output "spooky"?
7) Given: 5. class A { void m() { System.out.println("outer"); } } 6. 7. public class TestInners { 8. public static void main(String[] args) { 9. new TestInners().go(); 10. } 11. void go() { 12. new A().m(); 13. class A { void m() { System.out.println("inner"); } } 14. } 15. class A { void m() { System.out.println("middle"); } } 16. } What is the result?
8) Given: 3. public class City { 4. class Manhattan { 5. void doStuff() throws Exception { System.out.print("x "); } 6. } 7. class TimesSquare extends Manhattan { 8. void doStuff() throws Exception { } 9. } 10. public static void main(String[] args) throws Exception { 11. new City().go(); 12. } 13. void go() throws Exception { new TimesSquare().doStuff(); } 14. } What is the result?
9) Given: 3. public class Navel { 4. private int size = 7; 5. private static int length = 3; 6. public static void main(String[] args) { 7. new Navel().go(); 8. } 9. void go() { 10. int size = 5; 11. System.out.println(new Gazer().adder()); 12. } 13. class Gazer { 14. int adder() { return size * length; } 15. } 16. } What is the result?
10) Given: 3. import java.util.*; 4. public class Pockets { 5. public static void main(String[] args) { 6. String[] sa = {"nickel", "button", "key", "lint"}; 7. Sorter s = new Sorter(); 8. for(String s2: sa) System.out.print(s2 + " "); 9. Arrays.sort(sa,s); 10. System.out.println(); 11. for(String s2: sa) System.out.print(s2 + " "); 12. } 13. class Sorter implements Comparator<String> { 14. public int compare(String a, String b) { 15. return b.compareTo(a); 16. } 17. } 18. } What is the result?
11) Given: class CardBoard { Short story = 200; 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?
12) 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?
13) 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?
14) Given: class Mixer { Mixer() { } Mixer(Mixer m) { m1 = 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?
15) 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?