Java Core Mock Test 4

40 Questions | Total Attempts: 59

SettingsSettingsSettings
Core Java Quizzes & Trivia

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.

  • 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

  • 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

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

  • 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

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

  • 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

  • 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

  • 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

  • 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

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

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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

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

  • 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

  • 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

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

  • 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

Back to Top Back to top