Core Java Full Mock Test1

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Lakshmi312308
L
Lakshmi312308
Community Contributor
Quizzes Created: 1 | Total Attempts: 137
| Attempts: 137 | Questions: 35
Please wait...
Question 1 / 35
0 %
0/100
Score 0/100
1. If u want to create a user defined annotation -

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.

Submit
Please wait...
About This Quiz
Core Java Full Mock Test1 - Quiz

Core Java Full Mock Test1 evaluates understanding of Java fundamentals through code analysis and problem-solving scenarios. It covers object-oriented concepts, garbage collection, method overriding, and error handling, essential... see morefor Java developers. see less

2. Which statement is true?

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.

Submit
3. 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|?

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.

Submit
4. 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?

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

Submit
5. 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?

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.

Submit
6. 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?

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.

Submit
7. Which capability exists only in java.io.FileWriter?

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.

Submit
8. CAn garbage collector be forced?

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.

Submit
9. 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?  

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.

Submit
10. 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?

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

Submit
11. 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?

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.

Submit
12. 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?

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.

Submit
13. 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.)

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

Submit
14. 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?

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

Submit
15. 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?

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.

Submit
16. 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?

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.

Submit
17. 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?

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.

Submit
18. 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?

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

Submit
19. Can abstract class have static method?

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.

Submit
20. 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?

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

Submit
21. 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?

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.

Submit
22. 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?

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.

Submit
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?

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.

Submit
24. 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?

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

Submit
25. Which method will release the lock on the thread?

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.

Submit
26. 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?

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

Submit
27. 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?

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.

Submit
28. 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?

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.

Submit
29. 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?

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.

Submit
30. 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?

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

Submit
31. 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?

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.

Submit
32. 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?

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.

Submit
33. 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?

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.

Submit
34. 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.)

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.

Submit
35. 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?

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.

Submit
View My Results

Quiz Review Timeline (Updated): Apr 23, 2024 +

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

  • Current Version
  • Apr 23, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 25, 2011
    Quiz Created by
    Lakshmi312308
Cancel
  • All
    All (35)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
If u want to create a user defined annotation -
Which statement is true?
1. public class LineUp {...
Given:...
Given:...
Given:...
Which capability exists only in java.io.FileWriter?
CAn garbage collector be forced?
Given:...
Given:...
Given:...
Given:...
Given:...
Given:...
Given:...
Given:...
Given:...
Given:...
Can abstract class have static method?
12. public class Test {...
Given:...
Given:...
Given:...
Given:...
Which method will release the lock on the thread?
Given:...
Given:...
Given:...
Given:...
Given:...
Given:...
Given:...
Given:...
11. public class PingPong implements Runnable {...
Given:...
Alert!

Advertisement