Core Java Objective Questions

Core Java thumbnail
Please attempt at least 12 questions to get the test result.

1) Given: import java.util.regex.*; class Regex2 { public static void main(String[] args) { Pattern p = Pattern.compile(args[0]); Matcher m = p.matcher(args[1]); boolean b = false; while(b = m.find()) { System.out.print(m.start() + m.group()); } } } And the command line: java Regex2 "\d*" ab34ef What is the result?


2) Given: import java.io.*; class Player { Player() { System.out.print("p"); } } class CardPlayer extends Player implements Serializable { CardPlayer() { System.out.print("c"); } public static void main(String[] args) { CardPlayer c1 = new CardPlayer(); try { FileOutputStream fos = new FileOutputStream("play.txt"); ObjectOutputStream os = new ObjectOutputStream(fos); os.writeObject(c1); os.close(); FileInputStream fis = new FileInputStream("play.txt"); ObjectInputStream is = new ObjectInputStream(fis); CardPlayer c2 = (CardPlayer) is.readObject(); is.close(); } catch (Exception x ) { } } } What is the result?


3) Given: class TKO { public static void main(String[] args) { String s = "-"; Integer x = 343; long L343 = 343L; if(x.equals(L343)) s += ".e1 "; if(x.equals(343)) s += ".e2 "; Short s1 = (short)((new Short((short)343)) / (new Short((short)49))); if(s1 == 7) s += "=s "; if(s1 < new Integer(7+1)) s += "fly "; System.out.println(s); } } Which of the following will be included in the output String s? (Choose all that apply.) A. .e1 B. .e2 C. =s D. fly E. None of the above F. Compilation fails


4) Which is true?


5) Given: 1. class Voop { 2. public static void main(String[] args) { 3. doStuff(1); 4. doStuff(1,2); 5. } 6. // insert code here 7. } Which, inserted independently at line 6, will compile? (Choose all that apply.) A. static void doStuff(int... doArgs) { } B. static void doStuff(int[] doArgs) { } C. static void doStuff(int doArgs...) { } D. static void doStuff(int... doArgs, int y) { } E. static void doStuff(int x, int... doArgs) { }


6) Given: 1. enum Animals { 2. DOG("woof"), CAT("meow"), FISH("burble"); 3. String sound; 4. Animals(String s) { sound = s; } 5. } 6. class TestEnum { 7. static Animals a; 8. public static void main(String [] args) { 9. System.out.println(a.DOG.sound + " " + a.FISH.sound); 10. } 11. } What is the result?


7) Given two files: 1. package pkgA; 2. public class Foo { 3. int a = 5; 4. protected int b = 6; 5. public int c = 7; 6. } 3. package pkgB; 4. import pkgA.*; 5. public class Baz { 6. public static void main(String[] args) { 7. Foo f = new Foo(); 8. System.out.print(" " + f.a); 9. System.out.print(" " + f.b); 10. System.out.print(" " + f.c); 11. } 12. } What is the result? (Choose all that apply.) A. 5 6 7 B. 5 followed by an exception C. Compilation fails with an error on line 7 D. Compilation fails with an error on line 8 E. Compilation fails with an error on line 9 F. Compilation fails with an error on line 10


8) Given: 1. public class Electronic implements Device { public void doIt() { } } 2. 3. abstract class Phone1 extends Electronic { } 4. 5. abstract class Phone2 extends Electronic { public void doIt(int x) { } } 6. 7. class Phone3 extends Electronic implements Device { public void doStuff() { } } 8. 9. interface Device { public void doIt(); }


9) Given: 4. class Announce { 5. public static void main(String[] args) { 6. for(int __x = 0; __x < 3; __x++) ; 7. int #lb = 7; 8. long [] x [5]; 9. Boolean []ba[]; 10. enum Traffic { RED, YELLOW, GREEN }; 11. } 12. } What is the result? (Choose all that apply.) A. Compilation succeeds B. Compilation fails with an error on line 6 C. Compilation fails with an error on line 7 D. Compilation fails with an error on line 8 E. Compilation fails with an error on line 9 F. Compilation fails with an error on line 10


10) Given: 3. public class TestDays { 4. public enum Days { MON, TUE, WED }; 5. public static void main(String[] args) { 6. for(Days d : Days.values() ) 7. ; 8. Days [] d2 = Days.values(); 9. System.out.println(d2[2]); 10. } 11. } What is the result?


11) Given: 4. public class Frodo extends Hobbit { 5. public static void main(String[] args) { 6. Short myGold = 7; 7. System.out.println(countGold(myGold, 6)); 8. } 9. } 10. class Hobbit { 11. int countGold(int x, int y) { return x + y; } 12. } What is the result?


12) Which statements are true about comparing two instances of the same class, given that the equals() and hashCode() methods have been properly overridden? (Choose all that apply.) A. If the equals() method returns true, the hashCode() comparison == might return false B. If the equals() method returns false, the hashCode() comparison == might return true C. If the hashCode() comparison == returns true, the equals() method must return true D. If the hashCode() comparison == returns true, the equals() method might return true E. If the hashCode() comparison != returns true, the equals() method might return true


13) Given: public static void before() { Set set = new TreeSet(); set.add("2"); set.add(3); set.add("1"); Iterator it = set.iterator(); while (it.hasNext()) System.out.print(it.next() + " "); } Which statements are true?


14) Given: import java.util.*; class MapEQ { public static void main(String[] args) { Map<ToDos, String> m = new HashMap<ToDos, String>(); ToDos t1 = new ToDos("Monday"); ToDos t2 = new ToDos("Monday"); ToDos t3 = new ToDos("Tuesday"); m.put(t1, "doLaundry"); m.put(t2, "payBills"); m.put(t3, "cleanAttic"); System.out.println(m.size()); } } class ToDos{ String day; ToDos(String d) { day = d; } public boolean equals(Object o) { return ((ToDos)o).day == this.day; } // public int hashCode() { return 9; } } Which is correct? (Choose all that apply.) A. As the code stands it will not compile B. As the code stands the output will be 2 C. As the code stands the output will be 3 D. If the hashCode() method is uncommented the output will be 2 E. If the hashCode() method is uncommented the output will be 3


15) Given: class Top { public Top(String s) { System.out.print("B"); } } public class Bottom2 extends Top { public Bottom2(String s) { System.out.print("D"); } public static void main(String [] args) { new Bottom2("C"); System.out.println(" "); } } What is the result?