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 Aarthi_madhavan
A
Aarthi_madhavan
Community Contributor
Quizzes Created: 1 | Total Attempts: 96
| Attempts: 96 | Questions: 40
Please wait...
Question 1 / 40
0 %
0/100
Score 0/100
1. 8. What determines what method is run in the following:
Card crd = new BirthDay("Lucinda", 42);

crd.greeting();
The type of the object or the type of the reference variable?

Explanation

The method that is run in the given code is determined by the type of the object. In this case, the object is of type BirthDay, so the method greeting() from the BirthDay class will be executed. The type of the reference variable (Card) does not determine which method is run, it only determines what methods can be called on the object.

Submit
Please wait...
About This Quiz
Java Core Mock Test 4 - Quiz

This 'java core mock test 4' assesses knowledge on Java core concepts such as exception handling, interfaces, abstract classes, and inheritance. It is designed to test understanding of code snippets, error identification, and object serialization, essential for developers and programmers.

Personalize your quiz and earn a certificate with your name on it!
2. 4. What type of exception is thrown by parseInt() if it gets illegal data?

Explanation

The parseInt() method in Java throws a NumberFormatException if it receives illegal data. This exception is specifically designed to be thrown when a string cannot be parsed into a valid integer. Therefore, option c. NumberFormatException is the correct answer.

Submit
3. In order for the following code to be correct, what must be the type of the reference variable card?
_________ card;

card = new Valentine( "Joe", 14 ) ;
card.greeting();

card = new Holiday( "Bob" ) ; 
card.greeting();

card = new Birthday( "Emily", 12 ) ; 
card.greeting();

Explanation

The code does not specify the type of the reference variable "card". It is declared as a generic "card" variable. This means that it can refer to objects of any type. This allows the code to assign different types of greeting cards to the "card" variable and call the "greeting()" method on each of them. Therefore, the correct answer is d.card.

Submit
4. What type of inheritance does Java have?

Explanation

Java has single inheritance, which means that a class can only inherit from one superclass. This is in contrast to multiple inheritance, where a class can inherit from multiple superclasses.

Submit
5. 1. From which problems is it possible for a program to recover?

Explanation

A program is able to recover from exceptions. Exceptions are unexpected events that occur during program execution, such as dividing by zero or accessing an invalid memory location. When an exception occurs, the program can catch and handle it, allowing it to continue executing without terminating abruptly. By using exception handling mechanisms, programmers can anticipate and handle these exceptional situations, ensuring that the program can recover and continue running smoothly.

Submit
6. Consider the following code: public class ThrowsException { static void throwMethod() { System.out.println("Inside throwMethod."); throw new IllegalAccessException("exception"); } public static void main(String args[]) { try { throwMethod(); } catch (IllegalAccessException e) { System.out.println("Caught " + e); } } } Which of the following gives the output for the above given code?

Explanation

The code will compile successfully without any errors. However, when the code is executed, it will throw a runtime exception of type IllegalAccessException. The catch block in the main method will catch this exception and print "Caught: java.lang.IllegalAccessException: exception". Therefore, the correct answer is not b. Compilation Error.

Submit
7.
Any class that implements the Runnable interface
   has to provide the implementation for the following methods
    public void start();
    public void run();

Explanation

Any class that implements the Runnable interface does not have to provide the implementation for the methods public void start() and public void run(). The Runnable interface only requires the implementation of a single method, which is the public void run() method. The public void start() method is part of the Thread class, not the Runnable interface.

Submit
8. Is a program required to catch all exceptions that might happen?

Explanation

A program is not required to catch all exceptions that might happen. It is possible to write a program to catch only specific exceptions that the programmer wants to handle. This allows for more control and flexibility in handling different types of exceptions in different ways.

Submit
9. Consider the following scenario: Mr.Vijay is working for a Software Company. He needs to save and reload objects from a Java application. He needs to write a module to accomplish the same. Which of the following options can be used to accomplish the above requirement?

Explanation

The Serializable interface can be used to accomplish the requirement of saving and reloading objects from a Java application. This interface allows objects to be converted into a byte stream, which can then be saved to a file or sent over a network. The objects can later be deserialized, or converted back into their original form, allowing them to be reloaded and used in the application.

Submit
10.
 A number of threads of the same priority  have relinquished the lock
    on a monitor and are in a waiting state after having called the wait()
    method of the object. A new thread enters the monitor and calls the
    notifyAll() method of the meonitor. Which of these threads will be the
    first one to resume? 

Explanation

not-available-via-ai

Submit
11. Can an abstract method be defined in a non-abstract class?

Explanation

The correct answer is that if a class defines an abstract method, the class itself must be abstract. This is because an abstract method is a method that has no implementation and is meant to be overridden by its subclasses. Therefore, it only makes sense for the class containing the abstract method to also be abstract, as it cannot be instantiated on its own.

Submit
12. Which of the following interfaces are newly added to the collection framework in JDK 1.6? (Choose 3)

Explanation

In JDK 1.6, the newly added interfaces to the collection framework are Deque, NavigableSet, and NavigableMap. Deque is a double-ended queue that allows insertion and removal of elements from both ends. NavigableSet is a set that provides navigation methods like finding the closest element or getting a subset of elements. NavigableMap is a map that provides navigation methods based on the keys. Queue and Stack are not newly added interfaces in JDK 1.6.

Submit
13. Which of the following interfaces is used to get the number of columns, names of columns and its types in a table?

Explanation

The correct answer is d. ResultSetMetaData. ResultSetMetaData is an interface in Java that is used to retrieve metadata about the columns in a ResultSet object. It provides methods to get the number of columns, names of columns, and the types of columns in a table. This information can be useful for dynamically processing the results of a database query.

Submit
14. 4 Consider the following code: class ExceptionOne extends Exception { } class ExceptionOneOne extends ExceptionOne { } class ExceptionOneTwo extends ExceptionOne { } class TestExp { public static void main(String args[]) { throwExceptions(); } public static void throwExceptions() throws Exception { // Insert Code } } Which of the following code snippets when substituted to the commented line (// Insert Code) in the above program will make the program to compile and run properly? (Choose 3)

Explanation

The code snippet in the commented line should throw an exception that is declared in the method signature. In this case, the method signature declares that it throws an Exception. Therefore, options c, d, and e are correct. Option c throws an instance of ExceptionOneOne, which is a subclass of ExceptionOne. Option d throws a generic Exception. Option e throws an instance of ExceptionOneTwo, which is also a subclass of ExceptionOne. These options will allow the program to compile and run properly.

Submit
15. Under which of the following scenarios a checked exception is thrown? (Choose 2)

Explanation

In scenario b, when a file that does not exist is opened for reading, a checked exception is thrown. This is because the file not being found is an exceptional situation that the code needs to handle.

In scenario c, when an attempt to connect to the database is made but fails, a checked exception is thrown. This is because the failure to establish a database connection is an exceptional situation that the code needs to handle.

In scenarios a, d, and e, no checked exceptions are thrown. In scenario a, accessing the 5th element of an array with a size of 3 would result in an IndexOutOfBoundsException, which is an unchecked exception. In scenario d, calling the length() method on a null String object would result in a NullPointerException, which is also an unchecked exception. In scenario e, the invalid username and password would typically result in an authentication exception, which is an unchecked exception.

Submit
16. Consider the following code: public class ThrowsException { static void throwMethod() { System.out.println("Inside throwMethod."); throw new IllegalAccessException("exception"); } public static void main(String args[]) { try { throwMethod(); } catch (IllegalAccessException e) { System.out.println("Caught " + e); } } } Which of the following gives the output for the above given code?

Explanation

The given code will compile successfully without any errors. The method `throwMethod()` throws an `IllegalAccessException`, and this exception is caught in the `catch` block in the `main()` method. Therefore, the code will run without any compilation errors and will print "Inside throwMethod." followed by "Caught java.lang.IllegalAccessException: exception".

Submit
17. Consider the following code: interface InterfaceFirst { int ID = 10; void show(); } interface InterfaceSecond extends InterfaceFirst { int ID = 20; void show(); } class Implementation implements InterfaceSecond { public void show() { System.out.println("ID:" + ID); } } public class TestImplementation { public static void main(String args[]) { InterfaceSecond i2 = new Implementation(); i2.show(); InterfaceFirst i1 = i2; i1.show(); } } Which of the following will be the output for the above code snippet?

Explanation

The code snippet defines two interfaces, InterfaceFirst and InterfaceSecond. InterfaceSecond extends InterfaceFirst and overrides the ID variable with a value of 20. The Implementation class implements InterfaceSecond and overrides the show() method to print the value of ID. In the main method, an object of Implementation is created and assigned to InterfaceSecond and InterfaceFirst variables. When the show() method is called on both variables, it will print the value of ID, which is 20 in both cases. Therefore, the output will be 20 20.

Submit
18. Under which of the following scenarios a checked exception is thrown? (Choose 2)

Explanation

In scenario d, when a file that does not exist is opened for reading, a checked exception is thrown because the file cannot be found. This is because opening a file is an operation that can potentially fail and the programmer needs to handle this exception.

In scenario e, when an attempt to connect to the database is made but fails, a checked exception is thrown. This is because connecting to a database is an operation that can potentially fail due to various reasons such as incorrect credentials or network issues. The programmer needs to handle this exception to handle the failure gracefully.

Submit
19. 25 Consider the following scenario: Here is part of the hierarchy of exceptions that may be thrown during file IO operations: Exception +-IOException +-File Not Found Exception You have a method X that is supposed to open a file by name and read data from it. Given that X does not have any try-catch statements, which of the following option is true?

Explanation

not-available-via-ai

Submit
20. 9 Consider the following code: interface Data { public void load(); } abstract class Info { public abstract void load(); } Which of the following implementation correctly uses the Data interface and Info class?

Explanation

not-available-via-ai

Submit
21. Which of the following statements is true about NavigableSet interface?

Explanation

not-available-via-ai

Submit
22. Consider the following code snippet: interface InterfaceOne { int ID = 10; int getAccess(); } interface InterfaceTwo { int ID = 20; int getAccess(); } class InterfaceImpl implements InterfaceOne, InterfaceTwo { public int getAccess() { if (this instanceof InterfaceOne) { return InterfaceOne.ID; } else { return InterfaceTwo.ID; } } } public class Code { public static void main(String args[]) { InterfaceOne i1 = new InterfaceImpl(); System.out.println(i1.getAccess()); InterfaceTwo i2 = (InterfaceTwo) i1; System.out.println(i2.getAccess()); } } Which of the following will be the output for the above code snippet?

Explanation

The output for the above code snippet will be "10 10". This is because when the method getAccess() is called on the object i1, it checks if the object is an instance of InterfaceOne. Since i1 is an instance of InterfaceImpl which implements InterfaceOne, the condition is true and it returns the value of InterfaceOne.ID, which is 10. Similarly, when the method getAccess() is called on the object i2, which is a typecasted version of i1 to InterfaceTwo, it also returns the value of InterfaceOne.ID, which is 10. Therefore, the output is "10 10".

Submit
23. Can an abstract class define both abstract methods and non-abstract methods?

Explanation

An abstract class can define both abstract methods and non-abstract methods. Child classes that inherit from the abstract class will also inherit both types of methods. This allows the abstract class to provide a combination of concrete implementations and placeholders for implementation in the child classes.

Submit
24. Consider the following code snippet: abstract class BaseTest extends Object implements Runnable { public void run() { } } class AdvancedTest extends BaseTest { } public class TestIt { public boolean checkTest( Object obj ) { return ( obj instanceof BaseTest ) & ( obj instanceof Runnable ); } public static void main(String args[]) { System.out.println(new TestIt().checkTest(new AdvancedTest())); System.out.println(new TestIt().checkTest(new Thread())); } } Which of the following option will be the output for the above code snippet?

Explanation

The code snippet defines an abstract class `BaseTest` that implements the `Runnable` interface. It also defines a class `AdvancedTest` that extends `BaseTest`. The `TestIt` class has a method `checkTest` which takes an `Object` parameter and checks if it is an instance of `BaseTest` and `Runnable`.

In the `main` method, it calls `checkTest` twice with different objects. The first call passes an instance of `AdvancedTest`, which is a subclass of `BaseTest`, so it will return `true` for the first condition and `false` for the second condition.

However, the second call passes an instance of `Thread`, which is not related to `BaseTest`, so it will cause a compile-time error as `Thread` is not compatible with the `instanceof` check for `BaseTest`. Therefore, the output will be a compile-time error.

Submit
25. Consider the following program: import java.io.*; public class SteppedTryCatch { public static void main(String[] args) { try { try { try { // Line 1 } catch(Exception e3) { System.out.println("Exception 1"); // Line 2 } } catch(IOException e2) { System.out.println("Exception 2"); // Line 3 } } catch(FileNotFoundException e1) { System.out.println("Exception 3"); } } } You need to make the above program to print the output as Exception 1 Exception 2 Exception 3 Which of the following when substituted in place of commented lines (// Line 1, Line 2 and Line 3) produce the desired output?

Explanation

The correct answer is b. Line 1 : throw new Exception(); Line 2 : throw new IOException(); Line 3 : throw new FileNotFoundException(). This is because the desired output is Exception 1 Exception 2 Exception 3. In order to achieve this, we need to throw exceptions in the reverse order of their hierarchy. FileNotFoundException is a subclass of IOException, which is a subclass of Exception. Therefore, we need to throw Exception first, followed by IOException, and then FileNotFoundException.

Submit
26. Which declare a compilable Abstract Class?

Explanation

The correct answer is the third option: public abstract class Canine{ public Bark speak(); {} }. This option declares a compilable abstract class because it has the keyword "abstract" before the class name "Canine" and it has an abstract method "speak()" with the return type "Bark". Additionally, it includes an empty implementation block "{}" which is allowed for abstract classes.

Submit
27. Which of the following options are true? (Choose 3)

Explanation

a. Subclasses of Exceptions can be caught using try-catch: This is true because try-catch blocks are used to catch and handle exceptions, and exceptions are represented by subclasses of the Exception class.

d. Subclasses of Error are unchecked: This is true because unchecked exceptions are exceptions that are not required to be caught or declared in a method's throws clause, and errors are a type of unchecked exception.

e. Subclasses of Throwable can be caught using try-catch: This is true because Throwable is the superclass of both exceptions and errors, so any subclass of Throwable, including exceptions and errors, can be caught using try-catch.

Submit
28. Which of the following options define an entrySet in the Map interface?(Choose 2)

Explanation

The correct answer is a. the Set of key-value pairs contained in the Map. The entrySet in the Map interface represents a Set of key-value pairs contained in the Map. Each entry in the Set is represented by a Map.Entry object, which contains both the key and the corresponding value. This allows for easy iteration and manipulation of the key-value pairs in the Map.

The correct answer is c. It is an inner interface inside Map interface. The entrySet is an inner interface inside the Map interface. It provides a way to access and manipulate the key-value pairs in the Map. By using the entrySet, you can iterate over each entry in the Map and perform operations on the key-value pairs.

Submit
29. Which of the following are true about inheritance?(Choose 3)

Explanation

Inheritance allows for the addition of new features and functionality to an existing class without modifying the existing class. This is achieved by creating a subclass that inherits the properties and methods of a superclass. In an inheritance hierarchy, a subclass can also act as a superclass, meaning that it can be further extended by other subclasses. Similarly, a superclass can also act as a subclass, allowing it to inherit properties and methods from a higher-level superclass.

Submit
30. Consider the following program: 1. class CheckedException extends RuntimeException { } 2. class UncheckedException extends Exception { } 3. public class Check { 4. public static void main(String args[]) { 5. generateException1(); 6. generateException2(); 7. } 8. 9. private static void generateException1() { 10. throw new CheckedException(); 11. } 12. 13. private static void generateException2() { 14. throw new UncheckedException(); 15. } 16. } Which of the following is true regarding the above given program?

Explanation

The program will compile without any errors until line 14. At line 14, a compilation error will occur because the method generateException2() is declared to throw an UncheckedException, which is a checked exception. However, RuntimeExceptions and its subclasses, such as CheckedException, are unchecked exceptions. Therefore, throwing an UncheckedException in this method will result in a compilation error.

Submit
31. 1 Consider the following code snippet: interface Equalizer { boolean equals(Object o1, Object o2); } public class EqualIt { String name; public EqualIt(String name) { this.name = name; } public void TestIt() { System.out.println( new Equalizer() { public boolean equals(Object o1, Object o2) { return o1.equals(o2); } }.equals(this, this) ); } public static void main(String[] args) { new EqualIt("Welcome Planet").TestIt(); } } Which of the following will be the output of the above code snippet?

Explanation

The code snippet creates an anonymous class implementing the Equalizer interface. In the TestIt() method, the equals() method of the anonymous class is called with "this" as both arguments. However, the equals() method is using the equals() method of Object class to compare the two objects. Since the Object class does not have a specific implementation for the equals() method, it uses reference equality by default. Therefore, when the equals() method is called with the same object as both arguments, it leads to a recursive call and eventually results in a StackOverflowError at runtime. Hence, the output of the code snippet will be "Run time error".

Submit
32. Consider the following program: class CatchableException extends Throwable { } class ThrowableException extends CatchableException { } public class ThrowCatchable { public static void main(String args[]) { try { tryThrowing(); } catch(CatchableException c) { System.out.println("Catchable caught"); } finally { tryCatching(); } } static void tryThrowing() throws CatchableException { try { tryCatching(); throw new ThrowableException(); } catch(NullPointerException re) { throw re; } } static void tryCatching() { System.out.println(null + " pointer exception"); } } What will be the output of the above program?

Explanation

The program will output "null pointer exception Catchable caught null pointer exception". This is because the tryThrowing() method throws a ThrowableException, which is a subclass of CatchableException. This exception is caught in the catch block and the message "Catchable caught" is printed. Then, the tryCatching() method is called in the finally block, which prints "null pointer exception". Finally, the program goes back to the catch block and prints "null pointer exception" again.

Submit
33. Which of the following is the process of creating a new class from an existing class?

Explanation

The process of creating a new class from an existing class is known as reusability. Reusability allows developers to save time and effort by utilizing existing code and creating new classes with similar functionalities. It promotes code efficiency and reduces redundancy, as developers can leverage the functionality of existing classes without having to rewrite the code from scratch. This helps in maintaining code consistency and improves overall productivity.

Submit
34. 8. What happens in a method if an exception is thrown in a try{} block and there is NO MATCHING catch{} block?

Explanation

If an exception is thrown in a try{} block and there is no matching catch{} block, the method will throw the exception to its caller. This means that the exception will not be caught and handled within the method itself, but will instead be passed up the call stack to the method that called it. This behavior is similar to what would happen if there were no try{} block at all in the method.

Submit
35. What of the following is the default Scroll type for a ResultSet object?

Explanation

The default scroll type for a ResultSet object is ResultSet.TYPE_FORWARD_ONLY. This means that the ResultSet can only be scrolled forward and cannot be scrolled backwards or in any other direction.

Submit
36. Which of the following are interfaces in JDBC API?(choose 3)

Explanation

The correct answers are b. CallableStatement, c. Connection, and d. Statement. These are interfaces in the JDBC API. CallableStatement is used to execute stored procedures in a database, Connection is used to establish a connection with a database, and Statement is used to execute SQL statements. DriverManager and SQLWarning are not interfaces in the JDBC API.

Submit
37. Which of the following are correct for Set interface?(Choose 2)

Explanation

The correct answer is d. the elements are NOT ordered. The Set interface in Java does not guarantee any specific order of elements. The elements in a Set can be stored in any order and may change their position over time. However, the Set interface does ensure that duplicate elements are not allowed.

Submit
38. Which of the following code snippets show valid inheritance? (Choose 3)

Explanation

not-available-via-ai

Submit
39. Consider the following code: public class ThrowsException { static void throwMethod() { System.out.println("Inside throwMethod."); throw new IllegalAccessException("exception"); } public static void main(String args[]) { try { throwMethod(); } catch (IllegalAccessException e) { System.out.println("Caught " + e); } } } Which of the following gives the output for the above given code?

Explanation

The code compiles successfully and when the throwMethod() is called, it throws an IllegalAccessException. This exception is caught in the catch block and the message "Caught: java.lang.IllegalAccessException: exception" is printed. Therefore, the output for the above code is a runtime error.

Submit
40. 10 Consider the following program: interface I { void m1() throws Exception; } class A implements I { // Line 1 { System.out.println("A: m1"); } } class B implements I { // Line 2 { System.out.println("B: m1"); } } class C implements I { // Line 3 { System.out.println("C: m1"); } } public class UseABC { public static void main(String args[]) throws Exception { I i[] = { new A(), new B(), new C() }; for(I c : i) c.m1(); } } Which of the following set of code snippets when replaced to the commented Lines (Line 1, Line 2 and Line 3) will make the program compile properly and produce the following output? (Choose 3) A: m1 B: m1 C: m1

Explanation

The correct answer is a. Line 1: public void m1() throws IOException Line 2: public void m1() throws FileNotFoundException Line 3: public void m1() throws Exception.

This set of code snippets will make the program compile properly and produce the expected output because the interface I declares that the method m1() throws an Exception. The classes A, B, and C, which implement the interface I, need to adhere to this declaration. In this set of code snippets, each class correctly implements the m1() method and throws the necessary exceptions as declared in the interface.

Submit
View My Results

Quiz Review Timeline (Updated): Dec 7, 2023 +

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

  • Current Version
  • Dec 07, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 18, 2011
    Quiz Created by
    Aarthi_madhavan
Cancel
  • All
    All (40)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
8. What determines what method is run in the following: ...
4. What type of exception is thrown by parseInt() if it gets illegal...
In order for the following code to be correct, what must be the type...
What type of inheritance does Java have?
1. From which problems is it possible for a program to recover?
Consider the following code: public class ThrowsException { static...
Any class that implements the Runnable interface...
Is a program required to catch all exceptions that might happen?
Consider the following scenario: Mr.Vijay is working for a Software...
A number of threads of the same priority have relinquished the lock...
Can an abstract method be defined in a non-abstract class?
Which of the following interfaces are newly added to the collection...
Which of the following interfaces is used to get the number of...
4...
Under which of the following scenarios a checked exception is thrown?...
Consider the following code: public class ThrowsException { static...
Consider the following code: interface InterfaceFirst { int ID = 10;...
Under which of the following scenarios a checked exception is thrown?...
25...
9...
Which of the following statements is true about NavigableSet...
Consider the following code snippet: interface InterfaceOne { int ID =...
Can an abstract class define both abstract methods and non-abstract...
Consider the following code snippet:...
Consider the following program: import java.io.*; public class...
Which declare a compilable Abstract Class?
Which of the following options are true? (Choose 3)
Which of the following options define an entrySet in the Map...
Which of the following are true about inheritance?(Choose 3)
Consider the following program: 1. class CheckedException extends...
1...
Consider the following program:...
Which of the following is the process of creating a new class from an...
8. What happens in a method if an exception is thrown in a try{} block...
What of the following is the default Scroll type for a ResultSet...
Which of the following are interfaces in JDBC API?(choose 3)
Which of the following are correct for Set interface?(Choose 2)
Which of the following code snippets show valid inheritance? (Choose...
Consider the following code: public class ThrowsException { static...
10...
Alert!

Advertisement