Premium

Oracle Advacned Java Advanced Certification Questions and Answers (Dumps and Practice Questions)



Question : You have been given following code snippet. Based on the below code, select the correct option which can have overridden method in child class.

package com.hadoopexam;

import java.nio.file.FileAlreadyExistsException;

class Parent {
private T test;

void call(T t) throws FileSystemException{
test = t;
System.out.println("Testing Method 1");
}
}
 : You have been given following code snippet. Based on the below code, select the correct option which can have overridden method in child class.
1. void call(T t) throws FileAlreadyExistsException{}

2. void call(T t) throws Exception{}

3. Access Mostly Uused Products by 50000+ Subscribers

4. None of the above


Correct Answer : Get Lastest Questions and Answer :
Explanation: As we know we can have more specific exception child class. Hence, option A is correct. Because FileAlreadyExistsException is a child
class of FileSystemException. So that your derived class can have more specific throws clause.




Question : You have been given following code. When you run it what is the expected output.

package com.hadoopexam;
class Welcome {
public static void call() {
try {
throw new ArrayIndexOutOfBoundsException();
} catch (ArrayIndexOutOfBoundsException boundException) {
RuntimeException runtimeException = new RuntimeException(boundException);
runtimeException.initCause(boundException);
throw runtimeException;
}
}

public static void main(String[] args) {
try {
call();
} catch (Exception exception) {
System.out.println(exception.getClass());
}
}
}
 : You have been given following code. When you run it what is the expected output.
1. class java.lang.RuntimeException

2. class java.lang.IllegalStateException

3. Access Mostly Uused Products by 50000+ Subscribers

4. class java.lang.ArrayIndexOutOfBoundsException


Correct Answer : Get Lastest Questions and Answer :
Explanation: Throwable java.lang.Throwable.initCause(Throwable cause)

Initializes the cause of this throwable to the specified value. (The cause is the throwable that caused this throwable to get thrown.)
This method can be called at most once. It is generally called from within the constructor, or immediately after creating the throwable. If this throwable
was created with Throwable(Throwable) or Throwable(String, Throwable), this method cannot be called even once.
An example of using this method on a legacy throwable type without other support for setting the cause is:
try {
lowLevelOp();
} catch (LowLevelException le) {
throw (HighLevelException)
new HighLevelException().initCause(le); // Legacy constructor
}
Parameters:
cause the cause (which is saved for later retrieval by the getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
Returns:
a reference to this Throwable instance.
Throws:
IllegalArgumentException - if cause is this throwable. (A throwable cannot be its own cause.)
IllegalStateException - if this throwable was created with Throwable(Throwable) or Throwable(String, Throwable), or this method has already been called on this throwable.
In the expression new RuntimeException(boundException)the exception object boundException is already
chained to the RuntimeException object. The method initCause() cannot be
called on an exception object that already has an exception object chained during
the constructor call. Hence, the call re.initCause(oob); results in initCause()
throwing an IllegalStateException.





Question : You have been given below exception, select the correct behavior
package com.hadoopexam;

class Welcome {
public static void call() {
try {
throw new IndexOutOfBoundsException();
} catch (IndexOutOfBoundsException ioob) {
throw new Exception(ioob);
}
}

public static void main(String[] args) {
try {
call();
} catch (Exception e) {
System.out.println(e.getCause());
}
}
}
 : You have been given below exception, select the correct behavior
1. Code will give compile time error.

2. Code will run and print nothing.

3. Access Mostly Uused Products by 50000+ Subscribers

4. Code will run and print IllegalStateException


Correct Answer : Get Lastest Questions and Answer :
Explanation: As you know, when you throw a checked exception, It should be added in the throws clause or you have to catch it again.
Here throw new Exception(ioob) is a checked exception hence, you need to have it in throws clause of call method.
public static void call() throws Exception



Related Questions


Question : You have been given below code, what is the expected behavior?

package com.hadoopexam;

import java.util.Locale;

class Welcome {
public static void main(String[] args) {
Locale locale = new Locale("quick", "Hadoop");
System.out.println(locale);
}
}



 : You have been given below code, what is the expected behavior?
1. It will give compile time error.

2. It will give runtime error.

3. It will compile and run but no output.

4. It will give NullPointer Exception at run time

5. It will produce output as "quick_HADOOP"



Question : You have been given below code, what is the behavior expected?
package com.hadoopexam;
class ParentException extends Exception {}

class ChildException extends ParentException {}

public class Welcome {
public void subscribeForCourse(String courseName, int fee) throws ParentException, ChildException {
if (courseName.length() < 6) {
throw new ParentException();
} else if (fee >= 60) {
throw new ChildException();
} else {
System.out.println("You are allowed for this course");
}
}

public static void main(String[] args) throws ParentException {
Welcome t = new Welcome();
t.subscribeForCourse("Hadoop", 60);
}
}

 : You have been given below code, what is the behavior expected?
1. It will print : You are allowed for this course

2. It will print : Hadoop

3. It will throw Exception in thread "main" com.hadoopexam.ChildException

4. It will give compile time error.

5. It will throw Exception in thread "main" com.hadoopexam.ParentException



Question : You have been given below code, what is the expected behavior when executed?

package com.hadoopexam;

import java.util.Locale;

class Welcome {
public static void main(String[] args) {
Locale l1 = new Locale("en");
Locale l2 = new Locale("en", "in");
Locale l3 = new Locale("th", "TH", "TH");
Locale l4 = new Locale(l3);
System.out.println(l1 + " " + l2 + " " + l3 + " " + l4);
}
}
 : You have been given below code, what is the expected behavior when executed?
1. It will give compile time error.

2. It will give run time error.

3. It will compile and run with output as "en en_IN th_TH_TH_#u-nu-thai th_TH_TH_#u-nu-thai"

4. It will compile and run with output as "en en_IN th_TH_TH_#u-nu-thai th_TH_TH_#u-nu-IN"



Question : What will be the output when you run below program?

class Welcome {
int counter1;

void Welcome() {
counter1 = 10;
}

void printValue () {
System.out.println("Counter Value = " + counter1);
}

public static void main(String[] args) {
Welcome wel = new Welcome();
wel.printValue();
}
}
 : What will be the output when you run below program?
1. It will be having a compile time error. Because counter1 is not properly initialized.

2. It will compile and will print "Counter Value = 0"

3. It will compile and will print "Counter Value = 10"

4. It will give runtime error as it will not get value initialized for counter. And Null primitive is not correct.



Question : You have been given below code, what is the expected behavior?

package com.hadoopexam;

class Welcome {
int distance;
Welcome(int x) {
this.distance = x;
}

public void increSpeed(int time) {
int timeTravel = time;
class Car {
int value = 0;
public void speed() {
value = distance / timeTravel;
System.out.println("Velocity with new speed " + value + "kmph");
}
}
new Car().speed();
}

public static void main(String[] args) {
Welcome wel = new Welcome (100);
wel.increSpeed(60);
}
}


 : You have been given below code, what is the expected behavior?
1. It will give compile time error.

2. It will give run time error

3. It will print "Velocity with new speed 1kmph"

4. It will print "Velocity with new speed 1.4kmph"

5. It will give NullPointerException
Correct Answer : Exp : It is a Question of mixing Inner classes as well access outer class variable. As we know distance variable is accessible from inner class.
While creating constructor of Welcome class , we assigned distance=100. In Inner class we are acceding defined value of timeTravel=60. Hence, value= 100/60 , as we know both
are int value hence result would be int value. Which is 1. So it will print Velocity with new speed 1kmph


Question : You have been given below code. Please select the correct options, which applies to this code.

package com.hadoopexam;

class Parent {
public void printValue() {
System.out.println("It is a Parent Class");
}
}

abstract class Child extends Parent { // n1
public static void main(String[] args) {
Parent obj = new Parent();
obj.printValue (); // n2
}
}
 : You have been given below code, what is the expected behavior?
1. You cannot have an abstract class which can extend non abstract class. Hence, it will give compile time error.

2. Parent class printValue() method is not accessible from a child class.

3. Program will compile successfully and also run perfectly with "It is a Parent Class".

4. There will be a RuntimeError. As child class is an abstract class.



Question : You have been given below code, what is the expected behavior?

package com.hadoopexam;

import java.util.function.IntFunction;
import java.util.function.IntUnaryOperator;
import java.util.stream.IntStream;

class Welcome {
public static void main(String[] args) {
IntStream stream = IntStream.of(1, 2, 3);
IntFunction inFu= x -> y -> x*y;//line n1
IntStream newStream = stream.map(inFu.apply(10));// line n2
newStream.forEach(System.out::print);
}
}
 : You have been given below code, what is the expected behavior?
1. This code will compile and run perfectly and produce 102030

2. This code will compile, but give run time error.

3. This code will compile and run with the 302010 as output.

4. This code will be executed successfully by changing n1 line with this "IntFunction inFu = x -> y -> x*y;"