Learn. Practice. Feel prepared.
Java Objective Questions and Answers - Set 2
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 2 of the series.
Practice Java MCQs with Detailed Explanations
Answer at least 12 questions to submit and verify answers.
16 Given: class Clidder { private final void flipper() { System.out.println("Clidder"); } } public class Clidlet extends Clidder { public final void flipper() { System.out.println("Clidlet"); } public static void main(String [] args) { new Clidlet().flipper(); } } What is the result?
Correct answer:
Clidlet
Although a final method cannot be overridden, in this case, the method is private, and therefore hidden. The effect is that a new, accessible, method flipper is
created. Therefore, no polymorphism occurs in this example, the method invoked is simply that of the child class, and no error occurs.
17 Which statement(s) are true?
Correct answer:
Cohesion is the OO principle most closely associated with making sure that a class is designed with a single, well-focused purpose
A refers to encapsulation, B refers to coupling, and D refers to polymorphism
18 Given the following, 1. class X { void do1() { } } 2. class Y extends X { void do2() { } } 3. 4. class Chrome { 5. public static void main(String [] args) { 6. X x1 = new X(); 7. X x2 = new Y(); 8. Y y1 = new Y(); 9. // insert code here 10. } 11. } Which, inserted at line 9, will compile?
Correct answer:
((Y)x2).do2();
Before you can invoke Y’s do2 method you have to cast x2 to be of type Y. Statement B looks like a proper cast but without the second set of parentheses, the
compiler thinks it’s an incomplete statement
19 Given: 3. class Dog { 4. public void bark() { System.out.print("woof "); } 5. } 6. class Hound extends Dog { 7. public void sniff() { System.out.print("sniff "); } 8. public void bark() { System.out.print("howl "); } 9. } 10. public class DogShow { 11. public static void main(String[] args) { new DogShow().go(); } 12. void go() { 13. new Hound().bark(); 14. ((Dog) new Hound()).bark(); 15. ((Dog) new Hound()).sniff(); 16. } 17. } What is the result?
Correct answer:
Compilation fails with an error at line 15
Class Dog doesn’t have a sniff method
20 Given: import java.io.*; class Keyboard { } public class Computer implements Serializable { private Keyboard k = new Keyboard(); public static void main(String[] args) { Computer c = new Computer(); c.storeIt(c); } void storeIt(Computer c) { try { ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream("myFile")); os.writeObject(c); os.close(); System.out.println("done"); } catch (Exception x) {System.out.println("exc"); } } } What is the result?
Correct answer:
exc
An instance of type Computer Has-a Keyboard. Because Keyboard doesn't implement Serializable, any attempt to serialize an instance of Computer will cause an
exception to be thrown.
21 Given: import java.io.*; public class TestSer { public static void main(String[] args) { SpecialSerial s = new SpecialSerial(); try { ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream("myFile")); os.writeObject(s); os.close(); System.out.print(++s.z + " "); ObjectInputStream is = new ObjectInputStream( new FileInputStream("myFile")); SpecialSerial s2 = (SpecialSerial)is.readObject(); is.close(); System.out.println(s2.y + " " + s2.z); } catch (Exception x) {System.out.println("exc"); } } } class SpecialSerial implements Serializable { transient int y = 7; static int z = 9; } Which are true? (Choose all that apply.) A. Compilation fails B. The output is 10 0 9 C. The output is 10 0 10 D. The output is 10 7 9 E. The output is 10 7 10 F. In order to alter the standard deserialization process you would implement the readObject() method in SpecialSerial
Correct answer:
C, F
C is correct because static and transient variables are not
serialized when an object is serialized. F is a valid statement.
22 3. public class Theory { 4. public static void main(String[] args) { 5. String s1 = "abc"; 6. String s2 = s1; 7. s1 += "d"; 8. System.out.println(s1 + " " + s2 + " " + (s1==s2)); 9. 10. StringBuffer sb1 = new StringBuffer("abc"); 11. StringBuffer sb2 = sb1; 12. sb1.append("d"); 13. System.out.println(sb1 + " " + sb2 + " " + (sb1==sb2)); 14. } 15. } Which are true? A. Compilation fails B. The first line of output is abc abc true C. The first line of output is abc abc false D. The first line of output is abcd abc false E. The second line of output is abcd abc false F. The second line of output is abcd abcd true G. The second line of output is abcd abcd false
Correct answer:
D, F
While String objects are immutable, references to Strings are mutable. The code s1 += "d"; creates a new String object. StringBuffer objects are mutable, so the
append() is changing the single StringBuffer object to which both StringBuffer references refer.
23 Given: 3. import java.io.*; 4. public class Talker { 5. public static void main(String[] args) { 6. Console c = System.console(); 7. String u = c.readLine("%s", "username: "); 8. System.out.println("hello " + u); 9. String pw; 10. if(c != null && (pw = c.readPassword("%s", "password: ")) != null) 11. // check for valid password 12. } 13. } If line 6 creates a valid Console object, and if the user enters fred as a username and 1234 as a password, what is the result?
Correct answer:
Compilation fails
The readPassword() method returns a char[]. If a char[] were used,
answer B would be correct.
24 Given: interface Hungry<E> { void munch(E x); } interface Carnivore<E extends Animal> extends Hungry<E> {} interface Herbivore<E extends Plant> extends Hungry<E> {} abstract class Plant {} class Grass extends Plant {} abstract class Animal {} class Sheep extends Animal implements Herbivore<Sheep> { public void munch(Sheep x) {} } class Wolf extends Animal implements Carnivore<Sheep> { public void munch(Sheep x) {} } Which of the following changes (taken separately) would allow this code to compile?
Correct answer:
Change the Herbivore interface to
interface Herbivore<E extends Animal> extends Hungry<E> {}
The problem with the original code is that Sheep tries to implement Herbivore and Herbivore declares that its type parameter E can be any type that
extends Plant. Since a Sheep is not a Plant, Herbivore makes no sense— the type Sheep is outside the allowed range of Herbivore's parameter E. Only solutions
that either alter the definition of a Sheep or alter the definition of Herbivore will be able to fix this. So A, E are eliminated. B works, changing the definition of an Herbivore
to allow it to eat Sheep solves the problem. C doesn't work because an Herbivore must have a munch(Plant) method, not munch(Grass). And D doesn't work, because
in D we made Sheep extend Plant, now the Wolf class breaks because its munch(Sheep) method no longer fulfills the contract of Carnivore.
25 Which collection class(es) allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?
Correct answer:
java.util.ArrayList
All of the collection classes allow you to grow or shrink the size of your collection. ArrayList provides an index to its elements. The newer collection classes
tend not to have synchronized methods. Vector is an older implementation of ArrayList functionality and has synchronized methods; it is slower than ArrayList.
26 Given: 3. public class Redwood extends Tree { 4. public static void main(String[] args) { 5. new Redwood().go(); 6. } 7. void go() { 8. go2(new Tree(), new Redwood()); 9. go2((Redwood) new Tree(), new Redwood()); 10. } 11. void go2(Tree t1, Redwood r1) { 12. Redwood r2 = (Redwood)t1; 13. Tree t2 = (Tree)r1; 14. } 15. } 16. class Tree { } What is the result?
Correct answer:
An exception is thrown at runtime
a ClassCastException will be thrown when the code attempts to downcast a Tree to a Redwood.
27 Given: 3. public class Tenor extends Singer { 4. public static String sing() { return "fa"; } 5. public static void main(String[] args) { 6. Tenor t = new Tenor(); 7. Singer s = new Tenor(); 8. System.out.println(t.sing() + " " + s.sing()); 9. } 10. } 11. class Singer { public static String sing() { return "la"; } } What is the result?
Correct answer:
fa la
The code is correct, but polymorphism doesn’t apply to static methods.
28 Given: 3. class Alpha { 4. static String s = " "; 5. protected Alpha() { s += "alpha "; } 6. } 7. class SubAlpha extends Alpha { 8. private SubAlpha() { s += "sub "; } 9. } 10. public class SubSubAlpha extends Alpha { 11. private SubSubAlpha() { s += "subsub "; } 12. public static void main(String[] args) { 13. new SubSubAlpha(); 14. System.out.println(s); 15. } 16. } What is the result?
Correct answer:
alpha subsub
Watch out, SubSubAlpha extends Alpha! Since the code doesn’t attempt to make a SubAlpha, the private constructor in SubAlpha is okay.
29 Given: 3. class Building { 4. Building() { System.out.print("b "); } 5. Building(String name) { 6. this(); System.out.print("bn " + name); 7. } 8. } 9. public class House extends Building { 10. House() { System.out.print("h "); } 11. House(String name) { 12. this(); System.out.print("hn " + name); 13. } 14. public static void main(String[] args) { new House("x "); } 15. } What is the result?
Correct answer:
b h hn x
constructors call their superclass constructors, which execute first, and that constructors can be overloaded.
30 Given: 3. class Mammal { 4. String name = "furry "; 5. String makeNoise() { return "generic noise"; } 6. } 7. class Zebra extends Mammal { 8. String name = "stripes "; 9. String makeNoise() { return "bray"; } 10. } 11. public class ZooKeeper { 12. public static void main(String[] args) { new ZooKeeper().go(); } 13. void go() { 14. Mammal m = new Zebra(); 15. System.out.println(m.name + m.makeNoise()); 16. } 17. } What is the result?
Correct answer:
furry bray
Polymorphism is only for instance methods