Core Java Full Mock Test1

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 Lakshmi312308
L
Lakshmi312308
Community Contributor
Quizzes Created: 1 | Total Attempts: 128
Questions: 35 | Attempts: 128

SettingsSettingsSettings
Core Java Quizzes & Trivia

Questions and Answers
  • 1. 

    Which statement is true?

    • A.

      A class's finalize() method CANNOT be invoked explicitly.

    • B.

      Super.finalize() is called implicitly by any overriding finalize() method.

    • C.

      The finalize() method for a given object is called no more than once by the garbage collector.

    • D.

      The order in which finalize() is called on two objects is based on the order in which the two objects became finalizable.

    Correct Answer
    C. The finalize() method for a given object is called no more than once by the garbage collector.
    Explanation
    The finalize() method for a given object is called no more than once by the garbage collector. This means that even if an object is not immediately garbage collected after becoming eligible for garbage collection, the finalize() method will only be called once when it is eventually collected.

    Rate this question:

  • 2. 

    Given:   11. class Snoochy {   12. Boochy booch;   13. public Snoochy() { booch = new Boochy(this); }   14. }   15.   16. class Boochy {   17. Snoochy snooch;   18. public Boochy(Snoochy s) { snooch = s; }   19. }   And the statements:   21. public static void main(String[] args) {   22. Snoochy snoog = new Snoochy();   23. snoog = null;   24. // more code here   25. }   Which statement is true about the objects referenced by snoog, snooch, and booch immediately after line 23 executes?

    • A.

      None of these objects are eligible for garbage collection.

    • B.

      Only the object referenced by booch is eligible for garbage collection.

    • C.

      Only the object referenced by snoog is eligible for garbage collection.

    • D.

      Only the object referenced by snooch is eligible for garbage collection.

    • E.

      The objects referenced by snooch and booch are eligible for garbage collection.

    Correct Answer
    E. The objects referenced by snooch and booch are eligible for garbage collection.
    Explanation
    After line 23 executes, the object referenced by snoog becomes eligible for garbage collection because it is set to null and there are no other references to it. However, the objects referenced by snooch and booch still have references to them and are not eligible for garbage collection.

    Rate this question:

  • 3. 

    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();   13. Icelandic i3 = new Icelandic();   14. i3 = i1; i1 = i2; i2 = null; i3 = i1;   15. }   16. }   When line 15 is reached, how many objects are eligible for the garbage collector?  

    • A.

      0

    • B.

      1

    • C.

      4

    • D.

      6

    Correct Answer
    C. 4
    Explanation
    When line 15 is reached, there are four objects eligible for the garbage collector. The objects are i1, i2, i3, and the original i1 object that was assigned to i3. Since there are no references to these objects anymore, they can be garbage collected.

    Rate this question:

  • 4. 

    Given:   5. class Payload {   6. private int weight;   7. public Payload (int w) { weight = w; }   8. public void setWeight(int w) { weight = w; }   9. public String toString() { return Integer.toString(weight); }   10. }   11. public class TestPayload {   12. static void changePayload(Payload p) { /* insert code */ }   13. public static void main(String[] args) {   14. Payload p = new Payload(200);   15. p.setWeight(1024);   16. changePayload(p);   17. System.out.println("p is " + p);   18. } }   Which code fragment, inserted at the end of line 12, produces the output p is 420?

    • A.

      P.setWeight(420);

    • B.

      P.changePayload(420);

    • C.

      Payload.setWeight(420);

    • D.

      P = Payload.setWeight(420);

    Correct Answer
    A. P.setWeight(420);
    Explanation
    The code fragment "p.setWeight(420);" should be inserted at the end of line 12. This will set the weight of the Payload object "p" to 420.

    Rate this question:

  • 5. 

    Given:   11. public static void test(String str) {   12. int check = 4;   13. if (check = str.length()) {   14. System.out.print(str.charAt(check -= 1) +", ");   15. } else {   16. System.out.print(str.charAt(0) + ", ");   17. }   18. } and the invocation:   21. test("four");   22. test("tee");   23. test("to");   What is the result?

    • A.

      R, t, t,

    • B.

      R, e, o,

    • C.

      Compilation fails.

    • D.

      An exception is thrown at runtime

    Correct Answer
    C. Compilation fails.
    Explanation
    The code will not compile because there is an error in line 13. The condition inside the if statement should use the comparison operator "==" instead of the assignment operator "=". This is because the if statement expects a boolean expression, but the assignment operator returns an int. Therefore, the code will fail to compile.

    Rate this question:

  • 6. 

    Given:   15. public class Pass2 {   16. public void main(String [] args) {   17. int x = 6;   18. Pass2 p = new Pass2();   19. p.doStuff(x);   20. System.out.print(" main x = " + x);   21. }   22.   23. void doStuff(int x) {   24. System.out.print(" doStuff x = " + x++);   25. }   26. }   And the command-line invocations:   javac Pass2.java   java Pass2 5   What is the result?

    • A.

      Compilation fails.

    • B.

      An exception is thrown at runtime.

    • C.

      DoStuff x = 6 main x = 6

    • D.

      DoStuff x = 6 main x = 7

    • E.

      DoStuff x = 7 main x = 6

    • F.

      DoStuff x = 7 main x = 7

    Correct Answer
    B. An exception is thrown at runtime.
    Explanation
    The code compiles successfully. However, when the method doStuff is called with the argument x=6, it prints "doStuff x = 6" and increments the value of x by 1. But when the value of x is printed in the main method, it still remains 6. This is because the post-increment operator (x++) increments the value of x after it is printed in the doStuff method. Therefore, the output is "doStuff x = 6 main x = 6".

    Rate this question:

  • 7. 

    12. public class Test { 13. public enum Dogs {collie, harrier}; 14. public static void main(String [] args) { 15. Dogs myDog = Dogs.collie; 16. switch (myDog) { 17. case collie: 18. System.out.print("collie "); 19. case harrier: 20. System.out.print("harrier "); 21. } 22. } 23. } What is the result?

    • A.

      Collie

    • B.

      Harrier

    • C.

      Compilation fails.

    • D.

      Collie harrier

    • E.

      An exception is thrown at runtime.

    Correct Answer
    D. Collie harrier
    Explanation
    The result of the given code is "collie harrier". This is because the code uses a switch statement to check the value of the variable "myDog". Since "myDog" is set to "collie", it matches the "collie" case in the switch statement. The code then prints "collie" and continues to the next case, which is "harrier". Therefore, it also prints "harrier".

    Rate this question:

  • 8. 

    Given:   11. static void test() {   12. try {   13. String x = null;   14. System.out.print(x.toString() + " ");   15. }   16. finally { System.out.print("finally "); }   17. }   18. public static void main(String[] args) {   19. try { test(); }   20. catch (Exception ex) { System.out.print("exception "); }   21. }   What is the result?

    • A.

      Null

    • B.

      Finally

    • C.

      Null finally

    • D.

      Compilation fails.

    • E.

      Finally exception

    Correct Answer
    E. Finally exception
    Explanation
    The code will throw a NullPointerException at line 14 because it is trying to call the toString() method on a null object. However, since there is a finally block at line 16, the code will still execute the code within the finally block. Therefore, the output will be "finally exception".

    Rate this question:

  • 9. 

    Given:   11. static void test() throws Error {   12. if (true) throw new AssertionError();   13. System.out.print("test ");   14. }   15. public static void main(String[] args) {   16. try { test(); }   17. catch (Exception ex) { System.out.print("exception "); }   18. System.out.print("end ");   19. }   What is the result?

    • A.

      End

    • B.

      Compilation fails.

    • C.

      Exception end

    • D.

      Exception test end

    • E.

      A Throwable is thrown by main.

    • F.

      An Exception is thrown by main

    Correct Answer
    E. A Throwable is thrown by main.
    Explanation
    The correct answer is "A Throwable is thrown by main." In the given code, the test() method throws an AssertionError if the condition is true. In the main() method, the test() method is called within a try-catch block. Since the test() method throws an Error (which is a subclass of Throwable), it is caught by the catch block and the catch block is executed. Therefore, the output will be "exception end".

    Rate this question:

  • 10. 

    Given:   11. static class A {   12. void process() throws Exception { throw new Exception(); }   13. }   14. static class B extends A {   15. void process() { System.out.println("B"); }   16. }   17. public static void main(String[] args) {   18. new B().process();   19. }   What is the result?

    • A.

      The code runs with no output.

    • B.

      B

    • C.

      Compilation fails because of an error in line 12.

    • D.

      Compilation fails because of an error in line 15.

    • E.

      Compilation fails because of an error in line 18.

    Correct Answer
    B. B
    Explanation
    The code will output "B". This is because the main method creates a new instance of class B and calls the process method on it. Since class B overrides the process method from class A, the overridden method in class B will be called, which simply prints "B" to the console.

    Rate this question:

  • 11. 

    Given:   10. public class Foo {   11. static int[] a;   12. static { a[0]=2; }   13. public static void main( String[] args ) {}   14. }   Which exception or error will be thrown when a programmer attempts to run this code?

    • A.

      Java.lang.StackOverflowError

    • B.

      Java.lang.IllegalStateException

    • C.

      Java.lang.ExceptionInInitializerError

    • D.

      Java.lang.ArrayIndexOutOfBoundsException

    Correct Answer
    C. Java.lang.ExceptionInInitializerError
    Explanation
    The correct answer is java.lang.ExceptionInInitializerError. This error is thrown when an exception occurs during the initialization of a class. In this code, the static block at line 12 tries to assign a value to the first element of the array 'a', but the array has not been initialized yet. This causes an exception to be thrown, resulting in an ExceptionInInitializerError.

    Rate this question:

  • 12. 

    Given:   11. public static Iterator reverse(List list) {   12. Collections.reverse(list);   13. return list.iterator();   14. }   15. public static void main(String[] args) {   16. List list = new ArrayList();   17. list.add("1"); list.add("2"); list.add("3");   18. for (Object obj: reverse(list))   19. System.out.print(obj + ", ");   20. }   What is the result?

    • A.

      3, 2, 1,

    • B.

      1, 2, 3,

    • C.

      Compilation fails.

    • D.

      The code runs with no output.

    • E.

      An exception is thrown at runtime.

    Correct Answer
    C. Compilation fails.
    Explanation
    The code will fail to compile because the type of the elements in the list is not specified. In line 16, when creating the ArrayList, the type parameter is missing. It should be specified as `List list = new ArrayList();` to indicate that the list will contain Strings. Without this specification, the code will not compile.

    Rate this question:

  • 13. 

    Given:   1. public class BuildStuff {   2. public static void main(String[] args) {   3. Boolean test = new Boolean(true);   4. Integer x = 343;   5. Integer y = new BuildStuff().go(test, x);   6. System.out.println(y);   7. }   8. int go(Boolean b, int i) {   9. if(b) return (i/7);   10. return (i/49);   11. }   12. }   What is the result?

    • A.

      7

    • B.

      49

    • C.

      343

    • D.

      Compilation fails.

    • E.

      An exception is thrown at runtime.

    Correct Answer
    B. 49
    Explanation
    The result is 49 because the method "go" takes a Boolean and an int as parameters. If the Boolean value is true, it returns the integer divided by 7, otherwise it returns the integer divided by 49. In this case, the Boolean value is true (as it is initialized as "new Boolean(true)"), so the integer value 343 is divided by 7, resulting in 49.

    Rate this question:

  • 14. 

    Which capability exists only in java.io.FileWriter?

    • A.

      Closing an open stream.

    • B.

      Flushing an open stream.

    • C.

      Writing to an open stream

    • D.

      Writing a line separator to an open stream.

    Correct Answer
    D. Writing a line separator to an open stream.
    Explanation
    The capability that exists only in java.io.FileWriter is writing a line separator to an open stream. This means that FileWriter has a specific method or functionality that allows the user to write a line separator (such as a newline character) to the output stream. This capability is not available in other options like closing an open stream, flushing an open stream, or writing to an open stream, as these functionalities can be performed by other stream classes as well.

    Rate this question:

  • 15. 

    1. public class LineUp {   2. public static void main(String[] args) {   3. double d = 12.345;   4. // insert code here   5. }   6. }   Which code fragment, inserted at line 4, produces the output | 12.345|?

    • A.

      System.out.printf("|%7.3f| \n", d);

    • B.

      System.out.printf("|%7d| \n", d);

    • C.

      System.out.printf("|%7f| \n", d);

    • D.

      System.out.printf("|%3.7d| \n", d);

    Correct Answer
    A. System.out.printf("|%7.3f| \n", d);
    Explanation
    The code fragment "System.out.printf("|%7.3f| ", d);" produces the output "| 12.345|". This is because the printf method is used to format and print the value of the variable "d" as a floating-point number with a precision of 3 decimal places. The "%7.3f" format specifier specifies that the output should be displayed in a field width of 7 characters, with 3 decimal places. The "|" characters are included in the format string to indicate the beginning and end of the output.

    Rate this question:

  • 16. 

    Given:   1. interface A { public void aMethod(); }   2. interface B { public void bMethod(); }   3. interface C extends A,B { public void cMethod(); }   4. class D implements B {   5. public void bMethod(){}   6. }   7. class E extends D implements C {   8. public void aMethod(){}   9. public void bMethod(){}   10. public void cMethod(){}   11. }   What is the result?

    • A.

      Compilation fails because of an error in line 3.

    • B.

      Compilation fails because of an error in line 7.

    • C.

      Compilation fails because of an error in line 9.

    • D.

      If you define D e = new E(), then e.bMethod() invokes the version of bMethod() defined in Line 5.

    • E.

      If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9.

    Correct Answer
    E. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9.
    Explanation
    The correct answer is "If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9." This is because class E extends class D and implements interface C, which means that class E inherits the method bMethod() from class D. When the object e of type D is created and initialized with a new instance of class E, the method bMethod() in class D is overridden by the method bMethod() in class E. Therefore, when e.bMethod() is called, it will invoke the version of bMethod() defined in Line 9.

    Rate this question:

  • 17. 

    Given:   1. public class Base {   2. public static final String FOO = "foo";   3. public static void main(String[] args) {   4. Base b = new Base();   5. Sub s = new Sub();   6. System.out.print(Base.FOO);   7. System.out.print(Sub.FOO);   8. System.out.print(b.FOO);   9. System.out.print(s.FOO);   10. System.out.print(((Base)s).FOO);   11. } }   12. class Sub extends Base {public static final String FOO="bar";}   What is the result?

    • A.

      Foofoofoofoofoo

    • B.

      Foobarfoobarbar

    • C.

      Foobarfoofoofoo

    • D.

      Foobarfoobarfoo

    • E.

      Barbarbarbarbar

    Correct Answer
    D. Foobarfoobarfoo
    Explanation
    The result is "foobarfoobarfoo" because the code first prints "foo" which is the value of Base.FOO, then it prints "foo" again which is the value of Sub.FOO, then it prints "foo" again which is the value of b.FOO, then it prints "foo" again which is the value of s.FOO, and finally it prints "bar" which is the value of ((Base)s).FOO.

    Rate this question:

  • 18. 

    Given:   1. package geometry;   2. public class Hypotenuse {   3. public InnerTriangle it = new InnerTriangle();   4. class InnerTriangle {   5. public int base;   6. public int height;   7. }   8. }   Which statement is true about the class of an object that can reference the variable base?

    • A.

      It can be any class.

    • B.

      No class has access to base.

    • C.

      The class must belong to the geometry package.

    • D.

      The class must be a subclass of the class Hypotenuse.

    Correct Answer
    C. The class must belong to the geometry package.
    Explanation
    The variable "base" is a member variable of the InnerTriangle class, which is defined within the geometry package. Therefore, any class that can reference the variable "base" must also belong to the geometry package.

    Rate this question:

  • 19. 

    Given:   1. class Alligator {   2. public static void main(String[] args) {   3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};   4. int [][]y = x;   5. System.out.println(y[2][1]);   6. }   7. }   What is the result?

    • A.

      3

    • B.

      4

    • C.

      5

    • D.

      6

    • E.

      7

    • F.

      Compilation Error

    Correct Answer
    E. 7
    Explanation
    The code creates a 2D array called "x" with three rows, each containing a different number of elements. Then, it creates another 2D array called "y" and assigns "x" to it. Finally, it prints the value at index [2][1] of "y", which is 7. Therefore, the result is 7.

    Rate this question:

  • 20. 

    Given:   21. abstract class C1 {   22. public C1() { System.out.print(1); }   23. }   24. class C2 extends C1 {   25. public C2() { System.out.print(2); }   26. }   27. class C3 extends C2 {   28. public C3() { System.out.println(3); }   29. }   30. public class Ctest {   31. public static void main(String[] a) { new C3(); }   32. }   What is the result?

    • A.

      3

    • B.

      23

    • C.

      32

    • D.

      123

    • E.

      321

    • F.

      Compilation error

    • G.

      An exception is thrown at runtime.

    Correct Answer
    D. 123
    Explanation
    The code defines three classes: C1, C2, and C3. C1 is an abstract class with a constructor that prints 1. C2 extends C1 and has a constructor that prints 2. C3 extends C2 and has a constructor that prints 3. In the main method of the Ctest class, a new instance of C3 is created. When this instance is created, the constructors of C1, C2, and C3 are called in order, resulting in the output "123".

    Rate this question:

  • 21. 

    Given:   10. class One {   11. public One foo() { return this; }   12. }   13. class Two extends One {   14. public One foo() { return this; }   15. }   16. class Three extends Two {   17. // insert method here   18. }   Which two methods, inserted individually, correctly complete the Three class? (Choose two.)

    • A.

      Public void foo() {}

    • B.

      Public int foo() { return 3; }

    • C.

      Public Two foo() { return this; }

    • D.

      Public One foo() { return this; }

    • E.

      Public Object foo() { return this; }

    Correct Answer(s)
    C. Public Two foo() { return this; }
    D. Public One foo() { return this; }
    Explanation
    The correct answer is "public Two foo() { return this; }" and "public One foo() { return this; }". These methods correctly complete the Three class because they override the foo() method from the superclass (One) and return an instance of the respective class (Two and One).

    Rate this question:

  • 22. 

    Given:   11. public interface A { public void m1(); }   12.   13. class B implements A { }   14. class C implements A { public void m1() { } }   15. class D implements A { public void m1(int x) { } }   16. abstract class E implements A { }   17. abstract class F implements A { public void m1() { } }   18. abstract class G implements A { public void m1(int x) { } }   What is the result?

    • A.

      Compilation succeeds.

    • B.

      Exactly one class does NOT compile

    • C.

      Exactly two classes do NOT compile.

    • D.

      Exactly four classes do NOT compile

    • E.

      Exactly three classes do NOT compile

    Correct Answer
    C. Exactly two classes do NOT compile.
    Explanation
    Exactly two classes do NOT compile. Classes D and G do not compile because they have a method m1 with a different signature than the one defined in the interface A. In class D, the method m1 has an additional parameter, while in class G, the method m1 has the same name but a different parameter type. Since these classes do not implement the interface A correctly, they fail to compile.

    Rate this question:

  • 23. 

    Given:   1. class TestA {   2. public void start() { System.out.println("TestA"); }   3. }   4. public class TestB extends TestA {   5. public void start() { System.out.println("TestB"); }   6. public static void main(String[] args) {   7. ((TestA)new TestB()).start();   8. }   9. }   What is the result?

    • A.

      TestA

    • B.

      TestB

    • C.

      Compilation fails.

    • D.

      An exception is thrown at runtime

    Correct Answer
    B. TestB
    Explanation
    The result of the given code is "TestB" because the main method creates a new instance of TestB and then casts it to a TestA object. Since the start() method is overridden in TestB, the version of start() in TestB is called, which prints "TestB" to the console.

    Rate this question:

  • 24. 

    Given:   1. public class TestOne implements Runnable {   2. public static void main (String[] args) throws Exception {   3. Thread t = new Thread(new TestOne());   4. t.start();   5. System.out.print("Started");   6. t.join();   7. System.out.print("Complete");   8. }   9. public void run() {   10. for (int i = 0; i < 4; i++) {   11. System.out.print(i);   12. }   13. }   14. }   What can be a result?

    • A.

      Compilation fails.

    • B.

      An exception is thrown at runtime.

    • C.

      The code executes and prints "StartedComplete".

    • D.

      The code executes and prints "StartedComplete0123".

    • E.

      The code executes and prints "Started0123Complete".

    Correct Answer
    E. The code executes and prints "Started0123Complete".
    Explanation
    The code creates a new thread and starts it. The main thread then waits for the new thread to complete using the join() method. Meanwhile, the new thread runs the run() method which prints the numbers 0, 1, 2, and 3. After the new thread completes, the main thread prints "Complete". Therefore, the code executes and prints "Started0123Complete".

    Rate this question:

  • 25. 

    11. public class PingPong implements Runnable {   12. synchronized void hit(long n) {   13. for(int i = 1; i < 3; i++)   14. System.out.print(n + "-" + i + " ");   15. }   16. public static void main(String[] args) {   17. new Thread(new PingPong()).start();   18. new Thread(new PingPong()).start();   19. }   20. public void run() {   21. hit(Thread.currentThread().getId());   22. }   23. }   Which two statements are true? (Choose two.)

    • A.

      The output could be 8-1 7-2 8-2 7-1

    • B.

      The output could be 7-1 7-2 8-1 6-1

    • C.

      The output could be 8-1 7-1 7-2 8-2

    • D.

      The output could be 8-1 8-2 7-1 7-2

    Correct Answer(s)
    C. The output could be 8-1 7-1 7-2 8-2
    D. The output could be 8-1 8-2 7-1 7-2
    Explanation
    The output could be 8-1 7-1 7-2 8-2 because the hit() method is synchronized, which means that only one thread can access it at a time. When the first thread calls hit(), it prints "8-1" and "7-1". Then, the second thread calls hit() and prints "7-2" and "8-2". The order in which the threads are scheduled to run is not guaranteed, so the output could vary between these two possibilities.

    Rate this question:

  • 26. 

    Given:   11. public class Person {   12. private String name;   13. public Person(String name) {   14. this.name = name;   15. }   16. public boolean equals(Object o) {   17. if ( ! ( o instanceof Person) ) return false;   18. Person p = (Person) o;   19. return p.name.equals(this.name);   20. }   21. }   Which statement is true?

    • A.

      Compilation fails because the hashCode method is not overridden.

    • B.

      A HashSet could contain multiple Person objects with the same name.

    • C.

      All Person objects will have the same hash code because the hashCode method is not overridden.

    • D.

      If a HashSet contains more than one Person object with name="Fred", then removing another Person, also with name="Fred", will remove them all.

    Correct Answer
    B. A HashSet could contain multiple Person objects with the same name.
    Explanation
    The equals() method in the Person class is overridden to compare the names of two Person objects. However, the hashCode() method is not overridden. As a result, if two Person objects have the same name, they may have different hash codes. Therefore, a HashSet could contain multiple Person objects with the same name, as the HashSet uses the hashCode() method to determine uniqueness.

    Rate this question:

  • 27. 

    Given:   5. import java.util.*;   6. public class SortOf {   7. public static void main(String[] args) {   8. ArrayList<Integer> a = new ArrayList<Integer>();   9. a.add(1); a.add(5); a.add(3);   11. Collections.sort(a);   12. a.add(2);   13. Collections.reverse(a);   14. System.out.println(a);   15. }   16. }   What is the result?

    • A.

      [1, 2, 3, 5]

    • B.

      [2, 1, 3, 5]

    • C.

      [2, 5, 3, 1]

    • D.

      [5, 3, 2, 1]

    • E.

      [1, 3, 5, 2]

    • F.

      Compilation error

    • G.

      An exception is thrown at runtime

    Correct Answer
    C. [2, 5, 3, 1]
    Explanation
    The given code first creates an ArrayList named 'a' and adds the integers 1, 5, and 3 to it. Then, it sorts the ArrayList using the Collections.sort() method, which rearranges the elements in ascending order. After that, it adds the integer 2 to the ArrayList. Finally, it reverses the ArrayList using the Collections.reverse() method, which reverses the order of the elements. Therefore, the resulting ArrayList is [2, 5, 3, 1].

    Rate this question:

  • 28. 

    Given:   12. import java.util.*;   13. public class Explorer2 {   14. public static void main(String[] args) {   15. TreeSet<Integer> s = new TreeSet<Integer>();   16. TreeSet<Integer> subs = new TreeSet<Integer>();   17. for(int i = 606; i < 613; i++)   18. if(i%2 == 0) s.add(i);   19. subs = (TreeSet)s.subSet(608, true, 611, true);   20. s.add(629);   21. System.out.println(s + " " + subs);   22. }   23. }   What is the result?

    • A.

      Compilation fails.

    • B.

      An exception is thrown at runtime.

    • C.

      [608, 610, 612, 629] [608, 610]

    • D.

      [608, 610, 612, 629] [608, 610, 629]

    • E.

      [606, 608, 610, 612, 629] [608, 610]

    • F.

      [606, 608, 610, 612, 629] [608, 610, 629]

    Correct Answer
    E. [606, 608, 610, 612, 629] [608, 610]
    Explanation
    The code creates two TreeSet objects, "s" and "subs", and adds even numbers between 606 and 612 (inclusive) to "s". Then, it creates a subset of "s" called "subs" using the subSet() method with the range [608, 611]. After that, it adds the number 629 to "s". Finally, it prints out the elements of both "s" and "subs". The output is [606, 608, 610, 612, 629] for "s" and [608, 610] for "subs".

    Rate this question:

  • 29. 

    Given:   1. public class Drink implements Comparable {   2. public String name;   3. public int compareTo(Object o) {   4. return 0;   5. }   6. }   and:   20. Drink one = new Drink();   21. Drink two = new Drink(); 22. one.name= "Coffee";   23. two.name= "Tea"; 24. TreeSet set = new TreeSet();   25. set.add(one);   26. set.add(two);   A programmer iterates over the TreeSet and prints the name of each Drink object. What is the result?

    • A.

      Tea

    • B.

      Coffee

    • C.

      Coffee Tea

    • D.

      Compilation fails.

    • E.

      The code runs with no output.

    • F.

      An exception is thrown at runtime.

    Correct Answer
    B. Coffee
    Explanation
    The code creates two Drink objects, "one" with the name "Coffee" and "two" with the name "Tea". These objects are then added to a TreeSet. Since TreeSet stores elements in sorted order, the objects will be sorted based on their natural ordering. In this case, the Drink class implements the Comparable interface, so the compareTo method is used for sorting. However, the compareTo method is not implemented properly and always returns 0. Therefore, the TreeSet will treat both objects as equal and only store one of them. When iterating over the TreeSet, only the name of the first object, which is "Coffee", will be printed.

    Rate this question:

  • 30. 

    Given:   84. try {   85. ResourceConnection con = resourceFactory.getConnection();   86. Results r = con.query("GET INFO FROM CUSTOMER");   87. info = r.getData(); 88. con.close();   89. } catch (ResourceException re) {   90. errorLog.write(re.getMessage());   91. }   92. return info;   Which statement is true if a ResourceException is thrown on line 86?

    • A.

      Line 92 will not execute.

    • B.

      The connection will not be retrieved in line 85.

    • C.

      The resource connection will not be closed on line 88.

    • D.

      The enclosing method will throw an exception to its caller.

    Correct Answer
    C. The resource connection will not be closed on line 88.
    Explanation
    If a ResourceException is thrown on line 86, the code execution will jump to the catch block on line 89. This means that line 88, which is responsible for closing the resource connection, will not be executed. As a result, the resource connection will not be properly closed, potentially leading to resource leaks or other issues.

    Rate this question:

  • 31. 

    Which method will release the lock on the thread?

    • A.

      Notify()

    • B.

      Sleep()

    • C.

      Wait()

    • D.

      NotifyAll()

    Correct Answer
    C. Wait()
    Explanation
    The wait() method is used to release the lock on the thread. When a thread calls the wait() method, it enters a waiting state and releases the lock it holds. This allows other threads to acquire the lock and continue their execution. The thread will remain in a waiting state until it is notified by another thread using the notify() or notifyAll() method. This makes wait() the correct answer as it explicitly releases the lock on the thread.

    Rate this question:

  • 32. 

    Can abstract class have static method?

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    An abstract class can have static methods. Static methods belong to the class itself and can be called without creating an instance of the class. However, an abstract class cannot be instantiated, meaning we cannot create objects of an abstract class. Hence, we cannot directly call the static method of an abstract class.

    Rate this question:

  • 33. 

    If u want to create a user defined annotation -

    Correct Answer
    @interface
    Explanation
    The given correct answer is "@interface". This is the correct syntax to create a user-defined annotation in Java. The @interface keyword is used to define a new annotation type. Annotations are used to provide metadata about a program, and by creating a user-defined annotation, we can add our own custom metadata to our code.

    Rate this question:

  • 34. 

    CAn garbage collector be forced?

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The garbage collector in programming languages like Java cannot be forced or explicitly triggered by the programmer. It is an automatic process that runs in the background and is responsible for reclaiming memory that is no longer in use. The garbage collector decides when to run based on certain conditions like available memory and CPU usage. Therefore, the statement "garbage collector can be forced" is false.

    Rate this question:

  • 35. 

    Given: 11. public static void main(String[] args) { 12. for (int i = 0; i <= 10; i++) { 13. if (i > 6) break; 14. } 15. System.out.println(i); 16. } What is the result?

    • A.

      6

    • B.

      7

    • C.

      10

    • D.

      11

    • E.

      Compilation error

    • F.

      An exception is thrown at runtime.

    Correct Answer
    E. Compilation error
    Explanation
    The given code will result in a compilation error. This is because the variable "i" is declared within the scope of the for loop in line 12. Therefore, it is not accessible outside of the loop in line 15 where it is being printed. Hence, a compilation error will occur.

    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 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 25, 2011
    Quiz Created by
    Lakshmi312308
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.