Question : Which three are advantages of the Java exception mechanism? A. Improves the program structure because the error handling code is separated from the normal program function B. Provides a set of standard exceptions that covers all the possible errors C. Improves the program structure because the programmer can choose where to handle exceptions D. Improves the program structure because exceptions must be handled in the method in which they occurred E. Allows the creation of new exceptions that are tailored to the particular program being created 1. A,B,C 2. B,D,E 3. A,C,D 4. B,C,E
Exp : A program can use exceptions to indicate that an error occurred. To throw an exception, use the throw statement and provide it with an exception object - a descendant of Throwable - to provide information about the specific error that occurred. A method that throws an uncaught, checked exception must include a throws clause in its declaration.
A program can catch exceptions by using a combination of the try, catch, and finally blocks.
The try block identifies a block of code in which an exception can occur. The catch block identifies a block of code, known as an exception handler, that can handle a particular type of exception. The finally block identifies a block of code that is guaranteed to execute, and is the right place to close files, recover resources, and otherwise clean up after the code enclosed in the try block. The try statement should contain at least one catch block or a finally block and may have multiple catch blocks.
The class of the exception object indicates the type of exception thrown. The exception object can contain further information about the error, including an error message. With exception chaining, an exception can point to the exception that caused it, which can in turn point to the exception that caused it, and so on. Advantage 1: Separating Error-Handling Code from "Regular" Code Advantage 2: Propagating Errors Up the Call Stack Advantage 3: Grouping and Differentiating Error Types
Question : Given the code fragment: What is the result?
1. A B C Work done 2. A B C D Work done 3. A Work done 4. Compilation fails
Correct Answer : 3 break leaves a loop, continue jumps to the next iteration. Hence, in first iteration it will print "A" and then "Work done" and reaches to break statement. Which will leave the loop. And no further iterations.
Question : Given the code fragment: Which code fragment, when inserted at line 3, enables the code to print 10:20?
1. int[] array n= new int[2]; 2. int[] array; array = int[2]; 3. int array = new int[2]; 4. int array [2] ;
Correct Answer : 3 Explanation: We need to insert two values in array hence, size of array to be 2. Perfect syntax for array declaration with size is as below. int array = new int[2];
You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).
For primitive types:
int[] myIntArray = new int[3]; int[] myIntArray = {1,2,3}; int[] myIntArray = new int[]{1,2,3}; For classes, for example String, it's the same:
String[] myStringArray = new String[3]; String[] myStringArray = {"a","b","c"}; String[] myStringArray = new String[]{"a","b","c"};
Question : Given:
What is the result? 1. 3 4 5 6
2. 3 4 3 6 3. 5 4 5 6
4. 3 6 4 6
Correct Answer :3
Explanation: here variable "i" is a static variable. Hence all the objects will be pointing to same variable. So i is overridden by x2 object with value 5. And variable j is non-static, each object will have their own copy. Hence, will not be shared among objects.
1. A B C D 2. A C D 3. A B C C 4. A B D 5. A B D C
Question : Given: Which option enables the code to compile? 1. Replace the code fragment at line n1 with : class Book implements Readable { 2. At line n2 insert : public abstract void setBookMark(); 3. Replace the code fragment at line n3 with : abstract class EBook extends Book { 4. At line n4 insert: public void setBookMark() {}
1. 4W 100 Auto 4W 150 Manual 2. Null 0 Auto 4W 150 Manual 3. Compilation fails only at line n1 4. Compilation fails only at line n2 5. Compilation fails at both line n1 and line n2