Premium

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



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.


Correct Answer : 2
Explanation: In this code we will have default constructor. As void printValue() is not a valid constructor but a valid method. As constructor will never have a
return type. And as we know, primitive int by default will be initialized with 0.




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.


Correct Answer : 3
Explanation: Abstract class can extend a non abstract class. Ans abstract class can have a main() method, which is static.




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;"


Correct Answer : 4
Explanation: : java.util.function.IntFunction

@FunctionalInterface
Represents a function that accepts an int-valued argument and produces a result. This is the int-consuming primitive specialization for Function.
This is a functional interface whose functional method is apply(int).
Parameters:
the type of the result of the function

IntUnaryOperator java.util.function.IntFunction.apply(int value)

Applies this function to the given argument.
Parameters:
value the function argument
Returns:
the function result


IntStream java.util.stream.IntStream.map(IntUnaryOperator mapper)

Returns a stream consisting of the results of applying the given function to the elements of this stream.
This is an intermediate operation.
Parameters:
mapper a non-interfering, stateless function to apply to each element
Returns:
the new stream



Related Questions


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




Question : You have been given below code, please select the correct option for it.


package com.hadoopexam;

class Welcome {
int counter;
int counter1;

Welcome() {
this(10);
}

Welcome(int c) {
counter = c;
counter1 = c;

}

public String toString() {
return "counter vale is: " + counter+counter1;
}

public static void main(String[] args) {
System.out.println(new Welcome());
}
}


 : You have been given below code, please select the correct option for it.
1. It will give compile time error, saying method Welcome(int) is not defined.

2. It will give compile time error. As counter is defined.

3. It will give runtime error, by throwing NullPointerException.

4. The code will run perfectly and print "counter value is: 1010"

5. The code will run perfectly and print "counter value is: 20"



Question : You have been given below code

package com.hadoopexam;

class Welcome {
private K key;
private V value;

public Welcome(K key, V value) {
this.key = key;
this.value = value;
}

public static Welcome createPair(T value) {
return new Welcome(value, value);
}

public K getKey() {
return key;
}

public V getValue() {
return value;
}

}
Which of the belwo option is invalid?

 : You have been given below code
1. Welcome score = new Welcome("Amit",100);

2. Welcome welcomePair = Welcome. createPair("HadoopExam.com");

3. Welcome anotherPair = new Welcome<>(97, 32);

4. Welcome myGrade = new Welcome<>("Amit", "B+");

5. None of the above



Question : What is the behavior is expected from below code ?

package com.hadoopexam;
class Welcome {
int counter;

Welcome() {
this(10);
}

Welcome(int c) {
counter=c;
}

String toString() {
return "Value of counter is" + counter;
}

public static void main(String[] args) {
System.out.println(new Welcome());
}
}

 : What is the behavior is expected from below code ?
1. It will compile and run perfectly and product output as "Value of counter is 10"

2. It will compile and run perfectly and product output as "Value of counter is 0"

3. Code will not compile as no valid Welcome(int) method is defined.

4. Code will not compile because, you are trying to restrict the visibility of toString() method.

5. As we know, every class by default extends the Object class. Which has public toString() method. Hence, you cannot restrict the access modifiers
for the overridden method.