Premium

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



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

package com.hadoopexam;


class ParentException extends IndexOutOfBoundsException {
}

class ParentClass {
void call() throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException();
}
}

class ChildClass extends ParentClass {
public void call() throws ParentException { // n1
throw new ParentException();
}
}

public class Welcome {
public static void main(String[] args) {
try {
ParentClass parentClass = new ChildClass();
parentClass.call();
} catch (Exception e) {
System.out.println(e);
}
}
}

 : You have been given below code, what is the behavior of the code?
1. It will print com.hadoopexam.ParentException

2. It will print IndexOutOfBoundsException

3. Access Mostly Uused Products by 50000+ Subscribers

4. It will print IllegalStateException

5. It will not compile.


Correct Answer : Get Lastest Questions and Answer :
Explanation: It will compile successfully. Because child class methods can have more restrictive exceptions.
ParentException is a child exception of IndexOutOfBoundsException. However, as we know as per dynamic binding it will use the call method from child class.
Hence, it will print com.hadoopexam.ParentException




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

package com.hadoopexam;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Welcome implements Runnable {
String str;

public Welcome(String s) {
this.str = s;
}

public void run() {
System.out.println(str.concat(" : Welcome to HadoopExam Learning Resoueces"));
}

public static void main(String[] args) throws InterruptedException,
ExecutionException {
ExecutorService es = Executors.newFixedThreadPool(2);
Future f1 = es.submit(new CourseCreator("Start Creating Course"));
Future f2 = es.submit(new Welcome("Start Watching Course"));
String str1 = (String) f1.get();
String str2 = (String) f2.get();// line n1
System.out.println(str1 + " : " + str2);
}
}

class CourseCreator implements Callable {
String author;

public CourseCreator(String s) {
this.author = s;
}

public String call() throws Exception {
return author.concat(" : by HadoopExam");
}
}

 : You have been given below code, what is the expected behavior?
1. Start Watching Course : Welcome to HadoopExam Learning Resoueces
Start Creating Course : by HadoopExam : null


2. Start Watching Course : Welcome to HadoopExam Learning Resoueces
Start Creating Course : by HadoopExam : Start Watching Course


3. Access Mostly Uused Products by 50000+ Subscribers

4. An Exception thrown at run time


Correct Answer : Get Lastest Questions and Answer :
Explanation: The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread.
A Runnable, however, does not return a result and cannot throw a checked exception.




Question : You have been given below code, in their respective file.

public interface IParent {
public abstract void draw();
}

class Parent implements IParent{
public void draw () { }
}

public abstract class Child1 extends Parent { }


public class Child2 extends Parent {
protected void draw (int color) { }
}


public class GrandChild extends Parent implements IParent {
public void resize() {
}
}

Which is the correct statement?

 : You have been given below code, in their respective file.
1. Child1 does not compile.

2. Child2 does not compile.

3. Access Mostly Uused Products by 50000+ Subscribers

4. IParent does not compile.

5. All classes compile successfully.


Correct Answer : Get Lastest Questions and Answer :
Explanation: There is no issue with overriding and new method introduced in child classes. Code compiles successfully.

What if you try changing the throws clause? There are many ways to change the throws clause in the
overriding method, including the following:
a. Listing more general checked exceptions to throw.
b. Listing more checked exceptions in addition to the given checked exception(s) in the base method.

. An overriding method cannot declare more checked exceptions in the throws clause than the list of exceptions declared in the throws clause of the base method.
Why? The callers of the base method see only the list of the exceptions given in the throws clause of that method and will declare or handle these checked exceptions in their
Code (and not more than that).
. An overriding method can declare more specific exceptions than the exception(s) listed in the throws clause of the base method; in other words, you can
The overriding method
. Should have the same argument list types (or compatible types) as the base version.
. Should have the same return type.
. But from Java 5 onwards, the return type can be a subclass-covariant return types (which you'll learn shortly).
. Should not have a more restrictive access modifier than the base version.
. But it may have a less restrictive access modifier.
. Should not throw new or broader checked exceptions.
. But it may throw fewer or narrower checked exceptions, or any unchecked exception.
. The names should exactly match.
Remember that you cannot override a method if you do not inherit it. Private methods cannot be overridden because they are not inherited.



Related Questions


Question : What would be the output when you run below code ?

package com.hadoopexam;

class Welcome {
public void print(Integer i) {
System.out.println("Integer value");
}

public void print(int i) {
System.out.println("primitive int value ");
}

public void print(long i) {
System.out.println("primitive long value");
}

public static void main(String args[]) {
Welcome wel = new Welcome();
wel.print(10);
}
}
 : What would be the output when you run below code ?
1. It will print "Integer value".

2. It will print "primitive int value "

3. It will print "primitive long value"

4. It will give Runtime Error. Method Ambiguity found.



Question : You have been given below code, what is the expected behavior when executed with -ea option

package com.hadoopexam;

public class Welcome {
public static void main(String[] args) {
int val1 = 10;
int val2 = -1;
assert (val2 >= 1) : "Invalid value have been provided";
int result = val1 / val2;
System.out.println(result);
}
}

 : You have been given below code, what is the expected behavior when executed with -ea option
1. It will run and produce output as -10

2. It will run and produce output as 0

3. It will run and produce output as -1

4. It will run and throw AssertionError: Invalid value have been provided



Question : You have been given below two class in separate files. What is the correct replacement for the XXXXX?

package com.hadoopexam;

public class Welcome {
protected void print() {
System.out.println("Parent Class");
}
}

package com.hadoopexam;

public class Company extends Welcome {
XXXXX void display() {
System.out.println("Child Class");
}
}

 : You have been given below two class in separate files. What is the correct replacement for the XXXXX?
1. You can use only abstract,

2. You can use public or protected

3. You can only use public

4. You can only use protected.



Question : You have been given below code,

package com.hadoopexam;

abstract class Welcome {
Welcome() {
System.out.println("Welcome Constructor");
}

protected void message() {
System.out.println("Hello and welcome to HadoopExam.com");
}
}


package com.hadoopexam;

class Child1 extends Welcome {
int weight;

Child1(int weight) {
//n1
this.weight = weight;
}

public void message() {
System.out.println("Welcome to Child 1 Class");
}
}

class GrandChild1 extends Child1 {
int age, height;

GrandChild1(int x, int y) {
//n2
age = x;
height = y;
}

public void message() {
System.out.println("Welcome to grand child class");
}
}

Which of the following statement are correct, so that code will compile without error?



 : You have been given below code,
1. Replace n1 with this()

2. Replace n1 with super(x)

3. Replace n2 with super(x)

4. Replace n2 with super(x,y)



Question : Which of the following method(s) from Object class can be overridden? (Select all that apply.)
A. finalize()
B. clone()
C. getClass()
D. notify()
E. wait()

 : Which of the following method(s) from Object class can be overridden? (Select all that apply.)
1. A,B
2. B,C
3. C,D
4. D,E
5. A,E


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

package com.hadoopexam;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

class Welcome extends RecursiveAction { // line n1
static final int MAX_INDEX_COUNT = 3;
int startIndex, lastIndex;
int[] data;

public Welcome(int[] data, int start, int end) {
this.data = data;
this.startIndex = start;
this.lastIndex = end;
}

protected void compute() {
int sum = 0;
if (lastIndex - startIndex <= MAX_INDEX_COUNT) {
for (int i = startIndex; i < lastIndex; i++) {
sum += data[i];
}
System.out.println(sum);
} else {
new Welcome(data, startIndex + MAX_INDEX_COUNT, lastIndex).fork();
new Welcome(data, startIndex, Math.min(lastIndex, startIndex + MAX_INDEX_COUNT)).compute();
}
}

public static void main(String[] args) {
ForkJoinPool fjPool = new ForkJoinPool();
int data[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
fjPool.invoke(new Welcome(data, 0, data.length));
}
}
 : You have been given below code, what is the expected behavior?
1. Code will give compile time error

2. Code will run and print 55

3. Code will run and print any one value between 0 to 55

4. Code will run and print 4 values, whose total will be 55