Java Core Mock Test 4

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 Aarthi_madhavan
A
Aarthi_madhavan
Community Contributor
Quizzes Created: 1 | Total Attempts: 91
Questions: 40 | Attempts: 91

SettingsSettingsSettings
Java Core Mock Test 4 - Quiz


Questions and Answers
  • 1. 

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

    • A.

      A. Given username and password is checked with database and found invalid

    • B.

      B. 5th element of an array is accessed, whose size is 3

    • C.

      C. length() method is called on a String object, that is assigned to null

    • D.

      D. A file that actually does not exist, is opened for reading

    • E.

      E. An attempt to connect to the database is made but failed.

    Correct Answer(s)
    D. D. A file that actually does not exist, is opened for reading
    E. E. An attempt to connect to the database is made but failed.
    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.

    Rate this question:

  • 2. 

    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?

    • A.

      A. true

    • B.

      B. Welcome Planet

    • C.

      C. Compile-time error

    • D.

      D. false

    • E.

      Run time error

    Correct Answer
    E. Run time error
    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".

    Rate this question:

  • 3. 

    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?

    • A.

      A. Compile-time error

    • B.

      B. false false

    • C.

      C.true false

    • D.

      D.true true

    • E.

      E.false true

    Correct Answer
    A. A. Compile-time error
    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.

    Rate this question:

  • 4. 

    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

    • A.

      A. Line 1: public void m1() throws IOException Line 2: public void m1() throws FileNotFoundException Line 3: public void m1() throws Exception

    • B.

      B. Line 1: public void m1() throws Error Line 2: public void m1() throws Exception Line 3: public void m1() throws Throwable

    • C.

      C. Line 1: public void m1() throws NoClassDefFoundError Line 2: public void m1() throws Error Line 3: public void m1()

    • D.

      D. Line 1: public void m1() throws NullPointerException Line 2: public void m1() throws RuntimeException Line 3: public void m1() e. Line 1: public void m1()

    • E.

      E. Line 1: public void m1() Line 2: public void m1() Line 3: public void m1()

    Correct Answer(s)
    A. A. Line 1: public void m1() throws IOException Line 2: public void m1() throws FileNotFoundException Line 3: public void m1() throws Exception
    D. D. Line 1: public void m1() throws NullPointerException Line 2: public void m1() throws RuntimeException Line 3: public void m1() e. Line 1: public void m1()
    E. E. Line 1: public void m1() Line 2: public void m1() Line 3: public void 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.

    Rate this question:

  • 5. 

    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?

    • A.

      A.Cloneable interface

    • B.

      B. Writable interface Writable interface

    • C.

      C. Readable interface

    • D.

      D. Serializable interface

    • E.

      E. ObjectSerializable interface

    Correct Answer
    D. D. Serializable interface
    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.

    Rate this question:

  • 6. 

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

    • A.

      A. class A { int v; public String sayHello() { return "Hello"; }public class B extends A { public int sayHello(int a) { return 3 + a; } } }

    • B.

      B. class A { int v; public String sayHello() { return "Hello"; } } class B { A a; public String sayHello() { return "Hello from B"; } }

    • C.

      C. class A { int v; final String sayHello() { return "Hello"; } } class B extends A { public int sayHello(int a) { return 3 + a; } }

    • D.

      D. class A { int a; public String methodA(String s) { String var = "My App" + s; return var; } } class B extends A { public String methodA(String s) { String bar = "Bar" + s; return bar; } }

    • E.

      E. interface MyInterface { public void myMethod(String s); } class A implements MyInterface { public void myMethod(String s) { // Some Implementation } }

    Correct Answer(s)
    A. A. class A { int v; public String sayHello() { return "Hello"; }public class B extends A { public int sayHello(int a) { return 3 + a; } } }
    D. D. class A { int a; public String methodA(String s) { String var = "My App" + s; return var; } } class B extends A { public String methodA(String s) { String bar = "Bar" + s; return bar; } }
    E. E. interface MyInterface { public void myMethod(String s); } class A implements MyInterface { public void myMethod(String s) { // Some Implementation } }
  • 7. 

    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?

    • A.

      A. Runtime Error

    • B.

      B. Inside showMethod. followed by caught: b.java.lang.IllegalAccessException: exception

    • C.

      C. Compiles successfully, nothing is printed

    • D.

      D. Compilation Error

    Correct Answer
    D. D. Compilation Error
    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".

    Rate this question:

  • 8. 

    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?

    • A.

      A. No special precautions need be taken

    • B.

      B. Any method calling X must use try-catch, specifically catching FileNotFoundException

    • C.

      C. The method X must be declared as throwing FileNotFoundException

    • D.

      D. The method X must be declared as throwing IOException or Exception 26 Consider the following code: class A

    • E.

      E. Can contain Nulls

    Correct Answer
    D. D. The method X must be declared as throwing IOException or Exception 26 Consider the following code: class A
  • 9. 

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

    • A.

      A. Deqeue

    • B.

      B. Queue

    • C.

      C. Stack

    • D.

      D. NavigableSet

    • E.

      E. NavigableMap

    Correct Answer(s)
    A. A. Deqeue
    D. D. NavigableSet
    E. E. NavigableMap
    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.

    Rate this question:

  • 10. 

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

    • A.

      A. the elements are ordered

    • B.

      B. the elements are sorted

    • C.

      C. cannot contain NULLs

    • D.

      D. the elements are NOT ordered

    Correct Answer
    D. D. the elements are NOT ordered
    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.

    Rate this question:

  • 11. 

    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)

    • A.

      A. throw new Error();

    • B.

      B. throw new Throwable();

    • C.

      C. throw new ExceptionOneOne();

    • D.

      D. throw new Exception();

    • E.

      E. throw new ExceptionOneTwo();

    Correct Answer(s)
    C. C. throw new ExceptionOneOne();
    D. D. throw new Exception();
    E. E. throw new ExceptionOneTwo();
    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.

    Rate this question:

  • 12. 

    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?

    • A.

      A. public class Employee implements Info extends Data { public void load(){ /*do something*/ } public void Info.load(){ /*do something*/ } }

    • B.

      B. public class Employee extends Info implements Data { public void load() { /*do something*/ } }

    • C.

      C. public class Employee implements Info extends Data { public void Data.load(){ /*do something*/ } public void load(){ /*do something*/ } }

    • D.

      D. public class Employee implements Info extends Data { public void load() { /*do something*/ } }

    • E.

      E. public class Employee extends Info implements Data public void load(){ /*do something*/ } public void Info.load(){ /*do something*/ } } 10\

    Correct Answer
    B. B. public class Employee extends Info implements Data { public void load() { /*do something*/ } }
  • 13. 

    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?

    • A.

      A. 10 10

    • B.

      B. Compilation Error. Duplicate identifer ID found

    • C.

      C. 20 20

    • D.

      D. 10 20

    • E.

      E. 20

    Correct Answer
    C. C. 20 20
    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.

    Rate this question:

  • 14. 

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

    • A.

      A. Subclasses of Exceptions can be caught using try-catch

    • B.

      B. Error type objects can be handled only by JVM

    • C.

      C. Error is the subclass of Exception

    • D.

      D. Subclasses of Error are unchecked

    • E.

      E. Subclasses of Throwable can be caught using try-catch

    Correct Answer(s)
    A. A. Subclasses of Exceptions can be caught using try-catch
    D. D. Subclasses of Error are unchecked
    E. E. Subclasses of Throwable can be caught using try-catch
    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.

    Rate this question:

  • 15. 

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

    • A.

      A. DBMetaData

    • B.

      B. MetaData

    • C.

      C. DatabaseMetaData

    • D.

      D. ResultSetMetaData

    • E.

      E. SchemaMetaData

    Correct Answer
    D. D. ResultSetMetaData
    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.

    Rate this question:

  • 16. 

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

    • A.

      A. DriverManager

    • B.

      B. CallableStatement

    • C.

      C. Connection

    • D.

      D. Statement

    • E.

      E. SQLWarning

    Correct Answer(s)
    B. B. CallableStatement
    C. C. Connection
    D. D. Statement
    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.

    Rate this question:

  • 17. 

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

    • A.

      A. the Set of key-value pairs contained in the Map

    • B.

      B. The Collection of values contained in the Map

    • C.

      C. It is an inner interface inside Map interface

    • D.

      D. the Set of keys contained in the Map

    Correct Answer(s)
    A. A. the Set of key-value pairs contained in the Map
    C. C. It is an inner interface inside Map interface
    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.

    Rate this question:

  • 18. 

    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?

    • A.

      A. Line 1 : throw new FileNotFoundException(); Line 2 : throw new IOException(); Line 3 : throw new Exception();

    • B.

      B. Line 1 : throw new Exception(); Line 2 : throw new IOException(); Line 3 : throw new FileNotFoundException();

    • C.

      C. The code is wrong. Exceptions should be caught in reversed hierarchy order.

    • D.

      D. Line 1 : throw new IOException(); Line 2 : throw new IOException(); Line 3 : throw new IOException();

    • E.

      E. Line 1 : throw new IOException(); Line 2 : throw new FileNotFoundException(); Line 3 : throw new Exception(); 43 Consider the following program: class A extends Thread { public void run() {System.out.print("A");} } class B

    Correct Answer
    B. B. Line 1 : throw new Exception(); Line 2 : throw new IOException(); Line 3 : throw new FileNotFoundException();
    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.

    Rate this question:

  • 19. 

    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?

    • A.

      A. runtime error

    • B.

      B. Catchable caught null pointer exception null pointer exception

    • C.

      C. null pointer exception null pointer exception Catchable caught

    • D.

      D. compile-time error

    • E.

      E. null pointer exception Catchable caught null pointer exception

    Correct Answer
    E. E. null pointer exception Catchable caught null pointer exception
    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.

    Rate this question:

  • 20. 

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

    • A.

      A. Inheritance is a kind of Encapsulation

    • B.

      B. Inheritance enables adding new features and functionality to an existing class without modifying the existing class

    • C.

      C. In an inheritance hierarchy, a subclass can also act as a super class

    • D.

      D. Inheritance does not allow sharing data and methods among multiple classes

    • E.

      E. In an inheritance hierarchy, a superclass can also act as a sub class

    Correct Answer(s)
    B. B. Inheritance enables adding new features and functionality to an existing class without modifying the existing class
    C. C. In an inheritance hierarchy, a subclass can also act as a super class
    E. E. In an inheritance hierarchy, a superclass can also act as a sub class
    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.

    Rate this question:

  • 21. 

    Which declare a compilable Abstract Class?

    • A.

      Public abstract class Canine{ public Bark speak(); }

    • B.

      Public abstract class Canine{ public Bark speak(); {} }

    • C.

      Public abstract Canine {public abstract Bark speak(); }

    • D.

      Public class canine abstract { public abstract Bark speak); }

    Correct Answer
    B. Public abstract class Canine{ public Bark speak(); {} }
    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.

    Rate this question:

  • 22. 

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

    • A.

      A. ResultSet.TYPE_SCROLL_SENSITIVE

    • B.

      B. ResultSet.TYPE_SCROLLABLE

    • C.

      C. ResultSet.TYPE_SCROLL_INSENSITIVE

    • D.

      D. ResultSet.TYPE_FORWARD_ONLY

    • E.

      E. ResultSet.TYPE_SCROLL_BIDIRECTIONAL

    Correct Answer
    D. D. ResultSet.TYPE_FORWARD_ONLY
    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.

    Rate this question:

  • 23. 

    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?

    • A.

      A. Compiles successfully, nothing is printed

    • B.

      B. Compilation Error

    • C.

      C. Runtime Error

    • D.

      D. Inside showMethod. followed by caught: java.lang.IllegalAccessException: exception

    Correct Answer
    B. B. Compilation Error
    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.

    Rate this question:

  • 24. 

    Which of the following statements is true about NavigableSet interface?

    • A.

      A. a SortedSet extended with navigation methods for Maps.

    • B.

      B. a SortedSet extended with navigation methods for Lists.

    • C.

      C. a new class implementation of Set which can navigate the ResultSet object

    • D.

      D. a SortedSet extended with navigation methods reporting closest matches for given search targets.

    Correct Answer
    C. C. a new class implementation of Set which can navigate the ResultSet object
  • 25. 

    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?

    • A.

      A. No compilation error but throws RuntimeException on running the code

    • B.

      B. Compilation error at line 10

    • C.

      C. Compilation error at line 14

    • D.

      D. Compilation error at line 5

    • E.

      E. Compilation error at line 6

    Correct Answer
    C. C. Compilation error at line 14
    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.

    Rate this question:

  • 26. 

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

    • A.

      A. Polymorphism

    • B.

      B. Inheritance

    • C.

      C. Abstraction

    • D.

      D. Reusability

    Correct Answer
    D. D. Reusability
    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.

    Rate this question:

  • 27. 

    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?

    • A.

      A. Compiles successfully, nothing is printed

    • B.

      B. Compilation Error

    • C.

      C. Runtime Error

    • D.

      D. Inside showMethod. followed by caught: java.lang.IllegalAccessException: exception

    Correct Answer
    C. C. Runtime Error
    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.

    Rate this question:

  • 28. 

    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?

    • A.

      A. 10 10

    • B.

      B. 10 20

    • C.

      C. Compile time error. Incompatible Type conversion.

    • D.

      D. 20 10

    • E.

      E. 20 20

    Correct Answer
    A. A. 10 10
    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".

    Rate this question:

  • 29. 

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

    • A.

      A. 5th element of an array is accessed, whose size is 3

    • B.

      B. A file that actually does not exist, is opened for reading

    • C.

      C. An attempt to connect to the database is made but failed.

    • D.

      D. length() method is called on a String object, that is assigned to null

    • E.

      E. Given username and password is checked with database and found invalid

    Correct Answer(s)
    B. B. A file that actually does not exist, is opened for reading
    C. C. An attempt to connect to the database is made but failed.
    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.

    Rate this question:

  • 30. 

    What type of inheritance does Java have?

    • A.

      A. single inheritance

    • B.

      B. double inheritance

    • C.

      C. multiple inheritance

    • D.

      D. class inheritance

    Correct Answer
    A. A. single inheritance
    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.

    Rate this question:

  • 31. 

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

    • A.

      A.true

    • B.

      B.false

    Correct Answer
    B. B.false
    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.

    Rate this question:

  • 32. 

    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?

    • A.

      1. The thread that has been waiting the longest.

    • B.

      2. The thread that was the last one to to exit the monitor.

    • C.

      You can never be sure which thread will get to run first.

    • D.

      4. The the first thread that called the wait() method

    Correct Answer
    C. You can never be sure which thread will get to run first.
  • 33. 

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

    • A.

      A.Errors

    • B.

      B.Exceptions

    • C.

      C.Both Errors and Exceptions

    • D.

      Neither

    Correct Answer
    B. B.Exceptions
    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.

    Rate this question:

  • 34. 

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

    • A.

      A.No. You can write a program to catch just the exceptions you want.

    • B.

      B. No. But if a program catches one type of exception it must catch all other types as well.

    • C.

      C. Yes. If a program is not written to catch all exceptions it will not compile.

    • D.

      D. Yes. A program can not do I/O unless it catches all exceptions.

    Correct Answer
    A. A.No. You can write a program to catch just the exceptions you want.
    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.

    Rate this question:

  • 35. 

    4. What type of exception is thrown by parseInt() if it gets illegal data?

    • A.

      A.ArithmeticException

    • B.

      B. RunTimeException

    • C.

      C. NumberFormatException

    • D.

      D. NumberError

    Correct Answer
    C. C. NumberFormatException
    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.

    Rate this question:

  • 36. 

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

    • A.

      A. This is not legal, so the program will not compile.

    • B.

      B. The method throws the exception to its caller, exactly if there were no try{} block.

    • C.

      C. The program halts immediately.

    • D.

      D. The program ignores the exception.

    Correct Answer
    B. B. The method throws the exception to its caller, exactly if there were no try{} 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.

    Rate this question:

  • 37. 

    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?

    • A.

      A. The type of the object.

    • B.

      B. The type of the reference variable.

    Correct Answer
    A. A. The type of the object.
    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.

    Rate this question:

  • 38. 

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

    • A.

      No—if a class defines an abstract method the class itself must be abstract.

    • B.

      No—only classes are abstract, not methods.

    • C.

      Yes—a method can be declared abstract in any parent as long as the child classes also declare it abstract.

    • D.

      Yes—there is no restriction on where abstract methods can be defined.

    Correct Answer
    A. No—if a class defines an abstract method the class itself must be abstract.
    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.

    Rate this question:

  • 39. 

    Can an abstract class define both abstract methods and non-abstract methods?

    • A.

      No—it must have all one or the other.

    • B.

      No—it must have all abstract methods.

    • C.

      Yes—but the child classes do not inherit the abstract methods.

    • D.

      Yes—the child classes inherit both.

    Correct Answer
    D. Yes—the child classes inherit both.
    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.

    Rate this question:

  • 40. 

    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();

    • A.

      A.valentine

    • B.

      B.birthday

    • C.

      C.holiday

    • D.

      D.card

    Correct Answer
    D. D.card
    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.

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

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.