1.
Under which of the following scenarios a checked exception is thrown? (Choose 2)
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.
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?
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".
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?
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.
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
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.
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?
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.
6.
Which of the following code snippets show valid inheritance? (Choose 3)
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?
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".
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?
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)
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.
10.
Which of the following are correct for Set interface?(Choose 2)
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.
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)
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.
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?
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?
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.
14.
Which of the following options are true? (Choose 3)
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.
15.
Which of the following interfaces is used to get the number of columns, names of columns and its types in a table?
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.
16.
Which of the following are interfaces in JDBC API?(choose 3)
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.
17.
Which of the following options define an entrySet in the Map interface?(Choose 2)
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.
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?
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.
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?
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.
20.
Which of the following are true about inheritance?(Choose 3)
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.
21.
Which declare a compilable Abstract Class?
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.
22.
What of the following is the default Scroll type for a ResultSet object?
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.
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?
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.
24.
Which of the following statements is true about NavigableSet interface?
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?
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.
26.
Which of the following is the process of creating a new class from an existing class?
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.
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?
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.
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?
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".
29.
Under which of the following scenarios a checked exception is thrown? (Choose 2)
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.
30.
What type of inheritance does Java have?
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.
31.
Any class that implements the Runnable interface
has to provide the implementation for the following methods
public void start();
public void run();
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.
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?
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?
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.
34.
Is a program required to catch all exceptions that might happen?
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.
35.
4. What type of exception is thrown by parseInt() if it gets illegal data?
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.
36.
8. What happens in a method if an exception is thrown in a try{} block and there is NO MATCHING catch{} block?
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.
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?
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.
38.
Can an abstract method be defined in a non-abstract class?
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.
39.
Can an abstract class define both abstract methods and non-abstract methods?
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.
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();
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.