Premium

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



Question : What is the behavior of the below code?

package com.hadoopexam;

class Welcome {
public Welcome() {
System.out.println("In Welcome");
}

public class Company {
public Company() {
System.out.println("In Company");
}
}
}

class TestColor {
public static void main(String[] args) {
Welcome.Company comp = new Welcome().Company(); // n1
}
}
 : What is the behavior of the below code?
1. It will compile without any error but give RuntimeError

2. It will also run without any error and print "In Company"

3. It will also run without any error and print "In Welcome"

4. It will not compile as it will give error at line n1.

5. If you want to create an instance of an inner class. Then you also have to create instance of an outer class. As below Welcome.
Company comp = new Welcome().new Company();

Correct Answer : 4
Explanation:




Question : You have been given below code, however you see there is some compilation error. How would you resolve this.

package com.hadoopexam;

class Welcome {

{
try {
doStuff();
} catch (ArithmeticException | NumberFormatException | Exception e) {
System.out.println(e1.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

void doStuff() throws ArithmeticException, NumberFormatException, Exception {
if (Math.random() > -1)
throw new Exception("There is an Error , using this code. Contact www.HadoopExam.com");
}

}


 : You have been given below code, however you see there is some compilation error. How would you resolve this.
1. We should remove following code segment
catch (Exception e) {
System.out.println(e.getMessage());
}

2. Replace this line catch (ArithmeticException | NumberFormatException | Exception e)
With catch (Exception | ArithmeticException | NumberFormatException e)

3. Replace this line catch (ArithmeticException | NumberFormatException | Exception e) With catch (ArithmeticException | NumberFormatException e)

4. There will be no compilation error.


Correct Answer : 3
Explanation: In a multi-catch block, you cannot combine catch handlers for two exceptions that share a base- and
derived-class relationship. Here, Exception is a base class for all the Exceptions. You can only combine catch handlers for exceptions that do not share the parent-child
relationship between them. Hence, to remove compilation error we should have removed Exception e





Question : You have been given below code, what would be the behavior of the given code? Both the classes are in their respective files.

package com.hadoopexam;

class Welcome {
private boolean isCompany;
protected int totalProductss;

public Welcome() {
isCompany = false;
totalProductss = 0;
}

public class Company {
public void print() {
System.out.println("isCompany: " + isCompany);
System.out.println("totalProductss: " + totalProductss);
}
}
}

package com.hadoopexam;

class Test {
public static void main(String[] args) {
Welcome.Company comp = new Welcome().new Company();
comp.print();
}
}

 : You have been given below code, what would be the behavior of the given code? Both the classes are in their respective files.
1. It will throw compile time error.

2. It will compile but throw RuntimeError

3. It will run and compile successfully and print below
isCompany: false
totalProductss: 0


4. Compiler error: an inner class cannot access private members of the outer class


Correct Answer : 3
Explanation: There is no restriction that inner class can not access private member of outer class. Inner classes can access any member variable from
outer class without considering outer class access modifiers.


Related Questions


Question : You have been given below code. What is the behavior of the code?

package com.hadoopexam;

import java.util.function.Function;

public class Welcome {
public static void main(String[] args) {
Function negate = (i -> -i), square = (i -> i * i), negateSquare = negate
.compose(square);
System.out.println(negateSquare.apply(13));
}
}
 : You have been given below code. What is the behavior of the code?
1. Code will be having a compile time error.

2. Code will produce the output as -169

3. Access Mostly Uused Products by 50000+ Subscribers

4. Code will produce output as 13



Question : You have been given a following method of Long class.
long parseLong(String s)
Now select the correct functional interface for which, you can pass method reference like.
Long::parseLong()


 : You have been given a following method of Long class.
1. BiPredicate

2. Function

3. Access Mostly Uused Products by 50000+ Subscribers

4. Predicate

5. Consumer




Question : You have been given below code, when you run this code. What would be the behavior?
package com.hadoopexam;

import java.util.function.BiFunction;

public class Welcome {
public static void main(String args[]) {
BiFunction stringCompare = (str1, str2) -> str1.equals(str2);
System.out.println(stringCompare.apply("HadoopExam", "HadoopExam"));
}
}
 : You have been given below code, when you run this code. What would be the behavior?
1. It will generate compile time error.

2. You cannot use predicate in Function interface. Hence, will generate RuntimeError

3. Access Mostly Uused Products by 50000+ Subscribers

4. Program runs perfectly and produce false.



Question : Which one of the following abstract methods does not take any argument but returns a value?


 : Which one of the following abstract methods does not take any argument but returns a value?
1. The accept() method in java.util.function.Consumer interface

2. The get() method in java.util.function.Supplier interface

3. Access Mostly Uused Products by 50000+ Subscribers

4. The apply() method in java.util.function.Function interface



Question : You have been given following code. What is the behavior you expect?

package com.hadoopexam;

import java.util.function.Predicate;

public class Welcome {
public static void main(String args[]) {
Predicate checkValue = "HadoopExam Learning Resource"::contains; //n1
checkString(checkValue, "Quick");
}

static void checkString(Predicate predicate, String str) {
System.out.println(predicate.test(str) ? "It has given String" : "It does not have given String"); //n2
}
}
 : You have been given following code. What is the behavior you expect?
1. There will be a compile time error at n1, because you cannot have a method reference with an Object.

2. There will be a compile time error at n2.

3. Access Mostly Uused Products by 50000+ Subscribers

4. Program will compile and run as well and print "It does not have given String"



Question : What would happen, if you use below code.

package com.hadoopexam;

import java.util.function.Predicate;

import java.util.function.ObjIntConsumer;

class Welcome {
public static void main(String[] args) {
ObjIntConsumer charAt = (str, i) -> str.charAt(i); // n1
System.out.println(charAt.accept("java", 2)); // n2
}
}
 : What would happen, if you use below code.
1. There will be a compile time error at line n1

2. There will be a compile time error at line n2

3. Access Mostly Uused Products by 50000+ Subscribers

4. There will be no error, it runs perfectly and print v