Premium

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



Question : You have been given below code, what would be the behavior of the code

package com.hadoopexam;
class Welcome {
public static void call1() throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException();
}

public static void call2() throws ArithmeticException {
throw new ArithmeticException();
}

public static void main(String[] args) {
try {
call1();
call2();
} catch (IndexOutOfBoundsException || ArithmeticException ex) {
System.out.println(ex);
}
}
}
 : You have been given below code, what would be the behavior of the code
1. It will give compile time error.

2. It will run and print IndexOutOfBoundsException

3. Access Mostly Uused Products by 50000+ Subscribers

4. It will run and print Exception.


Correct Answer : Get Lastest Questions and Answer :
Explanation: There is no too much mind should be used. It is just a syntax error. When you are catching multiple exception, in single catch block you
should have used | (single pipe) an dnot ||. Otherwise entire code is fine.




Question : You have been given following exception hierarchy.

HParentException
HChild1Exception extends HParentException
HChild2Exception extends HParentException

Which of the below given multi catch statement would be correct?
 : You have been given following exception hierarchy.
1. catch (HParentException | HChild1Exception exception)

2. catch (HChild2Exception | HParentException exception)

3. Access Mostly Uused Products by 50000+ Subscribers

4. catch (HChild1Exception ex1 | HChild2Exception ex2)


Correct Answer : Get Lastest Questions and Answer :
Explanation: As we know in multi catch statement , we should not have both Parent and Child exception. We can have peer exception.




Question : You have been given following exception hierarchy.

HParentException
HChild1Exception extends HParentException
HChild2Exception extends HParentException

Below is the given code?

try {
HChild2Exception ch2 = new HChild2Exception ();
throw (Exception) ch2;
}
catch (HChild2Exception anfe) {
System.out.println("HChild2Exception ");
}
catch (HChild1Exception ae) {
System.out.println("HChild1Exception ");
}
catch (Exception e) {
System.out.println("Exception");
}

What is the expected output?

 : You have been given following exception hierarchy.
1. HChild2Exception

2. HChild1Exception

3. Access Mostly Uused Products by 50000+ Subscribers

4. Cannot be predicted


Correct Answer : Get Lastest Questions and Answer :
Explanation: As we are aware java has a feature of dynamic binding. That means at run time actual instance will be used. Here the actual instance
of exception is HChild2Exception. Event we are casting it with Exception, it will use only actual exception. Hence , it will print HChild2Exception


Related Questions


Question : package com.hadoopexam;

class Parent {}

class FirstChild extends Parent {}

class SecondChild extends Parent {}

class Welcome {
public static void main(String[] args) {
Parent[] array = new FirstChild[3]; //n1
array[0] = new FirstChild(); //n2
array[2] = new SecondChild(); //n3
System.out.println(array.length); //n4
}
}
 : package com.hadoopexam;
1. It will throws an ArrayStoreException, when program is run.

2. It will throw compile time exception at line n1 and n2 both.

3. It will throw compile time exception at line n3.

4. It will run successfully and print 3

5. It will run successfully and print 2



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

package com.hadoopexam;

import java.util.Arrays;
import java.util.List;

class Welcome {
public static void main(String[] args) {
List values = Arrays.asList(5,5,5);
values.stream().map(n -> n * 2).peek(System.out::print).count();
}
}
 : You have been given below code, what is the expected output?
1. It will compile time error

2. It will compile perfectly but give RuntimeError

3. It will compile and run perfectly, but will not produce any output.

4. It will compile and run perfectly, but will produce 555 as output.

5. It will compile and run perfectly, but will produce 101010 as output.



Question : Please select the correct statement for the given method.
package com.hadoopexam;

class Welcome {
int counter;

Welcome() {
Welcome(10);
}

Welcome(int r) {
counter = r;
}

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

public static void main(String[] args) {
Welcome wel = new Welcome();
wel.printValue();
}
}
 : Please select the correct statement for the given method.
1. It will compile and run perfectly with output as Counter Value =10.

2. It will compile perfectly, but give runtime Exception.

3. It will not even compile, because method Welcome(int ) is not available.

4. It will compile and run perfectly with output as Counter Value =0.

5. : As you know, whenever you do constructor chaining i.e. Same class constructor should be called by another constructor. You should have use this(10)
instead of Welcome(10). As it is trying to look for Welcome(10) method.


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

package com.hadoopexam;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Welcome {
public static void main(String[] args) {
Map map = new HashMap<>();
map.put(1, "a");
map.put(5, "b");
map.put(30, "c");
map.put(70, "d");
map.put(15, "e");
Map treeMap = new TreeMap(
new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
treeMap.putAll(map);
for (Map.Entry entry : treeMap.entrySet()) {
System.out.print(entry.getValue() + " ");
}
}
}
 : You have been given below code, what is the behavior of the code?
1. It will give compile time error

2. It will compile nut produce , runtime error

3. It will compile and run perfectly and produce a,b,c,d,e

4. It will compile and run perfectly and produce e,d,c,b,a

5. It will compile and run perfectly and produce d c e b a


Question : What is the behavior of the following code?
package com.hadoopexam;

class Parent { }
class Child1 extends Parent { }
class Child2 extends Parent { }
class GrandChild extends Child1, Child2 { }


 : What is the behavior of the following code?
1. It will compile and run perfectly.

2. It will compile perfectly but give NullPointerException when run

3. It will not compile correctly.

4. It will not compile and there will be an exception e.g. ClassNotFoundException.



Question : Which two reasons should you use interfaces instead of abstract classes?
A. You expect that classes that implement your interfaces have many common methods or fields,
or require access modifiers other than public.
B. You expect that unrelated classes would implement your interfaces.
C. You want to share code among several closely related classes.
D. You want to declare non-static on non-final fields.
E. You want to take advantage of multiple inheritance of type

 : Which two reasons should you use interfaces instead of abstract classes?
1. A,B
2. B,C
3. C,D
4. D,E
5. A,E