Premium

Oracle Java Programming Certification Questions and Answers (Dumps and Practice Questions)



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
  : Which three are advantages of the Java exception mechanism?
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?


  : Which three are advantages of the Java exception mechanism?
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?

  :  Given the code fragment:
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?
  : Given:
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.



Related Questions


Question : Given:

public static void main(String args[]){

String ta = "A ";
ta = ta.concat("B ");
String tb = "C ";
ta= ta.concat(tb);
ta.replace('C', 'D');
ta=ta.concat(tb);
System.out.println(ta);

}

What is the result?

 :   Given:
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?
 :  Given:
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() {}



Question : Given the code fragment:
What is the result?
 :  Given the code fragment:
1. Match 1
2. Match 2
3. No Match
4. A NullPointerException is thrown at runtime.





Question : Given the following two classes:
How should you write methods in the ElectricAccount
class at line n1 so that the member variable
bill is always equal to the value of the member
variable kwh multiplied by the member variable rate?

Any amount of electricity used by a customer
(represented by an instance of the customer class)
must contribute to the customer's bill
(represented by the member variable bill)
through the method useElectricity method.
An instance of the customer class should never be able to tamper
with or decrease the value of the member variable bill.
 :  Given the following two classes:
1. A
2. B
3. C
4. D



Question :
 :
1. A,B
2. B,C
3. A,C
4. D,E

4. A,E




Question : Given:

Car c1 = new Car("Auto");
Car c2 = new Car("4W" , 150 , "Manual");
System.out.println(c1.type + " " + c1.maxSpeed + " " + c1.trans);
System.out.println(c2.type + " " + c2.maxSpeed + " " + c2.trans);

What is the result?

 :  Given:
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