Java Toughest Exam: Trivia Quiz!

20 Questions | Total Attempts: 71

SettingsSettingsSettings
Java Toughest Exam: Trivia Quiz! - Quiz

.


Questions and Answers
  • 1. 
    Which two statements are true about the hashCode method? 
    • A. 

      The hashCode method for a given class can be used to test for object equality and object inequality for that class.

    • B. 

      The hashCode method for a given class can be used to test for object inequality, but NOT object equality, for that class.

    • C. 

      The hashCode method is used by the java.util.SortedSet collection class to order the elements within that set.

    • D. 

      The hashCode method is used by the java.util.HashSet collection class to group the elements within that set into hash buckets for swift retrieval.

  • 2. 
    Given: 1. public class Person { 2. private String name; 3. public Person(String name) { this.name = name; } 4. public boolean equals(Person p) { 5. return p.name.equals(this.name); 6. } 7. } Which statement is true?
    • A. 

      The equals method does NOT properly override the Object.equals method.

    • B. 

      Compilation fails because the private attribute p.name cannot be accessed in line 5.

    • C. 

      To work correctly with hash-based data structures, this class must also implement the hashCode method.

    • D. 

      When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.

  • 3. 
    Given: 1. public class Boxer1{ 2. Integer i; 3. int x; 4. public Boxer1(int y) { 5. x = i+y; 6. System.out.println(x); 7. } 8. public static void main(String[] args) { 9. new Boxer1(new Integer(4)); 10. } 11. } What is the result?
    • A. 

      The value "4" is printed at the command line.

    • B. 

      Compilation fails because of an error in line 5.

    • C. 

      Compilation fails because of an error in line 9.

    • D. 

      A NullPointerException occurs at runtime.

  • 4. 
    Given: 11. class ClassA {} 12. class ClassB extends ClassA {} 13. class ClassC extends ClassA {} and: 21. ClassA p0 = new ClassA(); 22. ClassB p1 = new ClassB(); 23. ClassC p2 = new ClassC(); 24. ClassA p3 = new ClassB(); 25. ClassA p4 = new ClassC(); Which three are valid? (Choose three.)
    • A. 

      P0 = p1;

    • B. 

      P1 = p2;

    • C. 

      P2 = p4;

    • D. 

      P2 = (ClassC)p1;

    • E. 

      P1 = (ClassB)p3;

    • F. 

      P2 = (ClassC)p4;

  • 5. 
    Given a valid SimpleDateFormat object named df, and 16. Date d = new Date(0L); 17. String ds = "Dec 15, 2018"; 18. // insert code here What updates d's value with the date represented by ds?
    • A. 

      18. d = df.parse(ds);

    • B. 

      18. d = df.getDate(ds);

    • C. 

      18. try { 19. d = df.parse(ds); 20. } catch(ParseException e) { };

    • D. 

      18. try { 19. d = df.getDate(ds); 20. } catch(ParseException e) { };

  • 6. 
    Given 11. public class Test { 12. public static void main(String [] args) { 13. int x = 5; 14. boolean b1 = true; 15. boolean b2 = false; 16. 17. if ((x == 4) && !b2 ) 18. System.out.print("1 "); 19. System.out.print("2 "); 20. if ((b2 = true) && b1 ) 21. System.out.print("3 "); 22. } 23. } What is the result?
    • A. 

      3

    • B. 

      1 2

    • C. 

      2 3

    • D. 

      1 2 3

    • E. 

      Compilation fails.

  • 7. 
    Given: 10. interface Foo {} 11. class Alpha implements Foo {} 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args ) { 15. Beta x = new Beta(); 16. // insert code here 17. } 18. } Which code, inserted at line 16, will cause a java.lang.ClassCastException?
    • A. 

      Alpha a = x;

    • B. 

      Foo f = (Delta)x;

    • C. 

      Foo f = (Alpha)x;

    • D. 

      Beta b = (Beta)(Alpha)x;

  • 8. 
    Given: 22. public void go() { 23. String o = ""; 24. z: 25. for(int x = 0; x < 3; x++) { 26. for(int y = 0; y < 2; y++) { 27. if(x==1) break; 28. if(x==2 && y==1) break z; 29. o = o + x + y; 30. } 31. } 32. System.out.println(o); 33. } What is the result when the go() method is invoked?
    • A. 

      00

    • B. 

      0001

    • C. 

      000120

    • D. 

      00012021

    • E. 

      Compilation fails.

  • 9. 
    Given: 11. static void test() throws RuntimeException { 12. try { 13. System.out.print("test "); 14. throw new RuntimeException(); 15. } 16. catch (Exception ex) { System.out.print("exception "); } 17. } 18. public static void main(String[] args) { 19. try { test(); } 20. catch (RuntimeException ex) { System.out.print("runtime "); } 21. System.out.print("end "); 22. } What is the result?
    • A. 

      Test end

    • B. 

      Compilation fails

    • C. 

      Test runtime end

    • D. 

      Test exception end

  • 10. 
    Given: 33. try { 34. // some code here 35. } catch (NullPointerException e1) { 36. System.out.print("a"); 37. } catch (Exception e2) { 38. System.out.print("b"); 39. } finally { 40. System.out.print("c"); 41. } If some sort of exception is thrown at line 34, which output is possible?
    • A. 

      A

    • B. 

      B

    • C. 

      C

    • D. 

      Ac

    • E. 

      Abc

  • 11. 
    Given: 31. // some code here 32. try { 33. // some code here 34. } catch (SomeException se) { 35. // some code here 36. } finally { 37. // some code here 38. } Under which three circumstances will the code on line 37 be executed? (Choose three.)
    • A. 

      The instance gets garbage collected.

    • B. 

      The code on line 33 throws an exception.

    • C. 

      The code on line 35 throws an exception.

    • D. 

      The code on line 31 throws an exception.

    • E. 

      The code on line 33 executes successfully.

  • 12. 
    Given: 10. int x = 0; 11. int y = 10; 12. do { 13. y--; 14. ++x; 15. } while (x < 5); 16. System.out.print(x + "," + y); What is the result?
    • A. 

      5,6

    • B. 

      5,5

    • C. 

      6,5

    • D. 

      6,6

  • 13. 
    Given: 11. Float pi = new Float(3.14f); 12. if (pi > 3) { 13. System.out.print("pi is bigger than 3. "); 14. } 15. else { 16. System.out.print("pi is not bigger than 3. "); 17. } 18. finally { 19. System.out.println("Have a nice day."); 20. } What is the result?
    • A. 

      Pi is not bigger than 3. Have a nice day.

    • B. 

      Pi is bigger than 3. Have a nice day.

    • C. 

      An exception occurs at runtime.

    • D. 

      Pi is bigger than 3.

    • E. 

      Compilation fails.

  • 14. 
    Given: 1. public class Boxer1{ 2. Integer i; 3. int x; 4. public Boxer1(int y) { 5. x = i+y; 6. System.out.println(x); 7. } 8. public static void main(String[] args) { 9. new Boxer1(new Integer(4)); 10. } 11. } What is the result?
    • A. 

      A NullPointerException occurs at runtime.

    • B. 

      Compilation fails because of an error in line 9.

    • C. 

      Compilation fails because of an error in line 5.

    • D. 

      The value "4" is printed at the command line.

  • 15. 
    Given: 1. public class Person { 2. private String name; 3. public Person(String name) { this.name = name; } 4. public boolean equals(Person p) { 5. return p.name.equals(this.name); 6. } 7. } Which statement is true?
    • A. 

      The equals method does NOT properly override the Object.equals method.

    • B. 

      Compilation fails because the private attribute p.name cannot be accessed in line 5.

    • C. 

      To work correctly with hash-based data structures, this class must also implement the hashCode method.

    • D. 

      When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.

  • 16. 
    Given: 3. public class Batman { 4. int squares = 81; 5. public static void main(String[] args) { 6. new Batman().go(); 7. } 8. void go() { 9. incr(++squares); 10. System.out.println(squares); 11. } 12. void incr(int squares) { squares += 10; } 13. } What is the result?
    • A. 

      81

    • B. 

      82

    • C. 

      91

    • D. 

      92

  • 17. 
    Given: 13. public class Pass { 14. public static void main(String [] args) { 15. int x = 5; 16. Pass p = new Pass(); 17. p.doStuff(x); 18. System.out.print(" main x = " + x); 19. } 20. 21. void doStuff(int x) { 22. System.out.print(" doStuff x = " + x++); 23. } 24. } What is the result?
    • A. 

      DoStuff x = 5 main x = 5

    • B. 

      DoStuff x = 6 main x = 6

    • C. 

      Compilation fails

    • D. 

      An exception is thrown at runtime

  • 18. 
    Given: 3. interface Animal { void makeNoise(); } 4. class Horse implements Animal { 5. Long weight = 1200L; 6. public void makeNoise() { System.out.println("whinny"); } 7. } 8. public class Icelandic extends Horse { 9. public void makeNoise() { System.out.println("vinny"); } 10. public static void main(String[] args) { 11. Icelandic i1 = new Icelandic(); 12. Icelandic i2 = new Icelandic(); 12. Icelandic i3 = new Icelandic(); 13. i3 = i1; i1 = i2; i2 = null; i3 = i1; 14. } 15. } When line 14 is reached, how many objects are eligible for the garbage collector?
    • A. 

      0

    • B. 

      1

    • C. 

      2

    • D. 

      3

    • E. 

      4

  • 19. 
    Given: 11. String[] elements = { "for", "tea", "too" }; 12. String first = (elements.length > 0)? elements[0] : null; What is the result?
    • A. 

      The variable first is set to elements[0].

    • B. 

      The variable first is set to null.

    • C. 

      An exception is thrown at runtime.

    • D. 

      Compilation fails.

  • 20. 
    Given: 31. class Foo { 32. public int a = 3; 33. public void addFive() { a += 5; System.out.print("f "); } 34. } 35. class Bar extends Foo { 36. public int a = 8; 37. public void addFive() { this.a += 5; System.out.print("b " ); } 38. } Invoked with: Foo f = new Bar(); f.addFive(); System.out.println(f.a); What is the result?
    • A. 

      B 3

    • B. 

      B 8

    • C. 

      B 13

    • D. 

      F 3

    • E. 

      F 8

    • F. 

      F 13

    • G. 

      Compilation fails

Back to Top Back to top