Learn. Practice. Feel prepared.
Java Objective Questions and Answers - Set 4
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 4 of the series.
Practice Java MCQs with Detailed Explanations
Answer at least 12 questions to submit and verify answers.
46 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?
Correct answer:
Compilation fails because of an error on line 10
If you use a reference variable of type Object, you can access only those members defined in class Object.
47 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?
Correct answer:
public class MyRunnable implements Runnable{public void run(){}}
The class implements the Runnable interface with a legal run() method
48 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?
Correct answer:
0..1..2..
The thread MyThread will start and loop three times (from 0 to 2).
49 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?
Correct answer:
This code will not compile
The sleep() method must be enclosed in a try/catch block, or the method printAll() must declare it throws the InterruptedException
50 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?
Correct answer:
57 22
You can define an inner class as abstract, which means you can instantiate only concrete subclasses of the abstract inner class. The object referenced by the variable t
is an instance of an anonymous subclass of AbstractTest, and the anonymous class overrides the getNum() method to return 22. The variable referenced by f is an instance of an anonymous subclass of Bar, and the anonymous Bar subclass also overrides the getNum() method (to return 57). Remember that to create a Bar instance, we need an
instance of the enclosing AbstractTest class to tie to the new Bar inner class instance. AbstractTest can't be instantiated because it's abstract, so we created an anonymous
subclass (non-abstract) and then used the instance of that anonymous subclass to tie to the new Bar subclass instance.
51 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"?
Correct answer:
Cathedral.Sanctum s = c.new Sanctum();
It is the only code that uses the correct inner class instantiation syntax.
52 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?
Correct answer:
middle
The "inner" version of class A isn't used because its declaration comes
after the instance of class A is created in the go() method.
53 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?
Correct answer:
No output is produced
The inner classes are valid, and all the methods (including main()), correctly throw an Exception, given that doStuff() throws an Exception. The doStuff() in class
TimesSquare overrides class Manhattan's doStuff() and produces no output.
54 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?
Correct answer:
21
The inner class Gazer has access to Navel's private static and private instance variables.
55 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?
Correct answer:
Compilation fails
the inner class Sorter must be declared static to be called from the static method main(). If Sorter had been static, answer E would be correct
56 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?
Correct answer:
2
Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.
57 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?
Correct answer:
Compilation fails
The var-args declaration is fine, but invade takes a short, so the argument 7 needs to be cast to a short. With the cast, the answer is B, 'a few'.
58 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?
Correct answer:
An exception is thrown at runtime
A ClassCastException is thrown at line 7 because o1 refers to an int[][] not an int[]. If line 7 was removed, the output would be 4.
59 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?
Correct answer:
hi hi, followed by an exception
The m2 object’s m1 instance variable is never initialized, so when m5 tries to use it a NullPointerException is thrown.
60 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?
Correct answer:
true true
The references f1, z, and f3 all refer to the same instance of Fizz. The final modifier assures that a reference variable cannot be referred to a different object, but final
doesn’t keep the object’s state from changing.