Java Toughest Exam: Trivia Quiz!

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Marikannan
M
Marikannan
Community Contributor
Quizzes Created: 2 | Total Attempts: 5,974
Questions: 20 | Attempts: 155

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.

    Correct Answer(s)
    B. The hashCode method for a given class can be used to test for object inequality, but NOT object equality, for that class.
    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.
    Explanation
    The first statement is false because the hashCode method can be used to test for both object equality and object inequality. The second statement is true because the hashCode method is primarily used to generate a unique identifier for an object, which can be used to test for object inequality. The third statement is false because the hashCode method is not used by the java.util.SortedSet collection class to order elements. The fourth statement is true because the hashCode method is used by the java.util.HashSet collection class to group elements into hash buckets for efficient retrieval.

    Rate this question:

  • 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.

    Correct Answer
    A. The equals method does NOT properly override the Object.equals method.
    Explanation
    The equals method in line 4 does not properly override the Object.equals method because it takes a parameter of type Person instead of Object. Therefore, it does not satisfy the contract of the equals method in the Object class. To properly override the Object.equals method, the method signature should be changed to "public boolean equals(Object obj)".

    Rate this question:

  • 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.

    Correct Answer
    D. A NullPointerException occurs at runtime.
    Explanation
    The code is trying to add the value of "y" to the uninitialized variable "i". Since "i" is an Integer object and has not been assigned a value, it is null by default. Therefore, when trying to add "y" to "i" in line 5, a NullPointerException occurs because you cannot perform arithmetic operations on a null object.

    Rate this question:

  • 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;

    Correct Answer(s)
    A. P0 = p1;
    E. P1 = (ClassB)p3;
    F. P2 = (ClassC)p4;
    Explanation
    The three valid statements are:
    1. p0 = p1; - This is valid because p1 is an instance of ClassB, which is a subclass of ClassA. Therefore, it can be assigned to a variable of type ClassA.
    2. p1 = (ClassB)p3; - This is valid because p3 is an instance of ClassB, which is a subclass of ClassA. Therefore, it can be cast to ClassB and assigned to p1.
    3. p2 = (ClassC)p4; - This is valid because p4 is an instance of ClassC, which is a subclass of ClassA. Therefore, it can be cast to ClassC and assigned to p2.

    Rate this question:

  • 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) { };

    Correct Answer
    C. 18. try { 19. d = df.parse(ds); 20. } catch(ParseException e) { };
    Explanation
    The correct answer is 18. try {
    19. d = df.parse(ds);
    20. } catch(ParseException e) { };

    This code block tries to parse the string representation of a date, "Dec 15, 2018", using the SimpleDateFormat object df. If the parsing is successful, the parsed date is assigned to the variable d. If an exception of type ParseException is thrown during the parsing process, it is caught and handled in the catch block.

    Rate this question:

  • 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.

    Correct Answer
    C. 2 3
    Explanation
    The code first checks if the condition `(x == 4) && !b2` is true. Since `x` is not equal to 4 and `b2` is false, the condition evaluates to false and the code skips the first `System.out.print("1 ")` statement.

    Next, the code checks the condition `(b2 = true) && b1`. The variable `b2` is assigned the value true and the condition `b1` is true, so the overall condition evaluates to true. As a result, the code executes the `System.out.print("3 ")` statement.

    Therefore, the output of the code is "2 3".

    Rate this question:

  • 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;

    Correct Answer
    B. Foo f = (Delta)x;
    Explanation
    The code "Foo f = (Delta)x;" will cause a java.lang.ClassCastException. This is because the object "x" is of type Beta, and it cannot be cast to type Delta. The inheritance hierarchy is Beta -> Alpha -> Foo, so it is only possible to cast "x" to Beta, Alpha, or Foo. Casting "x" to Delta, which is a subclass of Beta, is not allowed and will result in a ClassCastException.

    Rate this question:

  • 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.

    Correct Answer
    C. 000120
    Explanation
    The go() method contains nested for loops. The outer loop iterates from 0 to 2, and the inner loop iterates from 0 to 1. Inside the loops, there are two if statements. The first if statement checks if x is equal to 1, and if so, it breaks out of the inner loop. The second if statement checks if x is equal to 2 and y is equal to 1, and if so, it breaks out of both the inner and outer loops using the label "z".

    Since the second if statement is only true when x is equal to 2 and y is equal to 1, the loop will only run for those values. The variable "o" is then updated by concatenating the values of x and y.

    After the loops finish, the value of "o" is printed, which is "000120".

    Rate this question:

  • 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

    Correct Answer
    D. Test exception end
    Explanation
    The code defines a method called "test" that throws a RuntimeException. Inside the method, it prints "test" and then throws the exception. In the main method, the "test" method is called within a try block. Since the "test" method throws a RuntimeException, it is caught by the catch block in the main method. Inside the catch block, "exception" is printed. Finally, "end" is printed outside of the try-catch block. Therefore, the result is "test exception end".

    Rate this question:

  • 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

    Correct Answer
    D. Ac
    Explanation
    If an exception is thrown at line 34, the catch block at line 35 will catch the exception since it is a NullPointerException. Then, the code at line 36 will print "a". After that, the finally block at line 39 will always execute regardless of whether an exception occurred or not, so it will print "c". Therefore, the possible output in this scenario is "ac".

    Rate this question:

  • 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.

    Correct Answer(s)
    B. The code on line 33 throws an exception.
    C. The code on line 35 throws an exception.
    E. The code on line 33 executes successfully.
    Explanation
    The code on line 37 will be executed under the following three circumstances:
    1. The code on line 33 throws an exception, causing the program to enter the catch block.
    2. The code on line 35 throws an exception, also causing the program to enter the catch block.
    3. The code on line 33 executes successfully, regardless of whether an exception is thrown or not, as the finally block is always executed.

    Rate this question:

  • 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

    Correct Answer
    B. 5,5
    Explanation
    The code snippet starts with initializing x to 0 and y to 10. Then it enters a do-while loop. In each iteration of the loop, y is decremented by 1 and x is incremented by 1. This continues until x becomes 5. At that point, the condition of the do-while loop becomes false and the loop is exited. Finally, the values of x and y are printed, which are 5 and 5 respectively.

    Rate this question:

  • 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.

    Correct Answer
    E. Compilation fails.
    Explanation
    The code fails to compile because the "finally" block is placed after the "if-else" statement. In Java, the "finally" block should be placed after the "if-else" statement or any other control flow statement. Moving the "finally" block to the correct position will resolve the compilation error.

    Rate this question:

  • 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.

    Correct Answer
    A. A NullPointerException occurs at runtime.
    Explanation
    The code in line 2 declares an Integer object variable "i" but does not initialize it. In line 5, when trying to calculate "x" by adding the uninitialized "i" and the value of "y", a NullPointerException occurs because "i" is null. Therefore, the correct answer is that a NullPointerException occurs at runtime.

    Rate this question:

  • 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.

    Correct Answer
    A. The equals method does NOT properly override the Object.equals method.
    Explanation
    The equals method does not properly override the Object.equals method because it takes a parameter of type Person instead of Object. To properly override the equals method, it should take an Object parameter and then check if it is an instance of Person before comparing the names.

    Rate this question:

  • 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

    Correct Answer
    B. 82
    Explanation
    The result is 82 because the method go() is called in the main method, which then calls the incr() method and passes the value of squares as an argument. However, the incr() method only increments the local variable squares by 10, not the instance variable squares. Therefore, the value of squares remains 81, but when it is printed in the go() method, it has been incremented to 82.

    Rate this question:

  • 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

    Correct Answer
    A. DoStuff x = 5 main x = 5
    Explanation
    The result is "doStuff x = 5 main x = 5" because the variable x is passed by value to the method doStuff(). Therefore, any changes made to the parameter x within the method doStuff() do not affect the original variable x in the main method.

    Rate this question:

  • 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

    Correct Answer
    E. 4
    Explanation
    When line 14 is reached, there are four objects that are eligible for the garbage collector. The objects i1, i2, i3, and the original i1 object that was assigned to i3 are all eligible for garbage collection because they are no longer referenced by any variables.

    Rate this question:

  • 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.

    Correct Answer
    A. The variable first is set to elements[0].
    Explanation
    The variable "first" is set to the value of elements[0], which is "for".

    Rate this question:

  • 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

    Correct Answer
    A. B 3
    Explanation
    The result is "b 3". This is because the method `addFive()` is overridden in the subclass `Bar`, and when the method is called on an instance of `Bar`, it will access the `a` variable in the subclass instead of the superclass. Therefore, the `a` variable in the superclass `Foo` remains unchanged at 3, and the output is "b" followed by the value of `a` in the superclass, which is 3.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 20, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Aug 27, 2018
    Quiz Created by
    Marikannan
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.