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