Premium

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



Question : You have been given below code, what will be printed once executed?

package com.hadoopexam;

import java.util.Map;
import java.util.TreeMap;

class Welcome {

public static void main(String[] args) {
Map user = new TreeMap<>();
user.put (1007, "Akash");
user.put (1002, "Kamlesh");
user.put (1001, "Bilash");
user.put (1003, "Bimal");
System.out.println (user);
}
}
 : You have been given below code, what will be printed once executed?
1. {1001=Bimal, 1002=Kamlesh, 1003=Bimal, 1007=Akash}

2. {1007=Akash, 1001=Bimal, 1003=Bimal, 1002=Kamlesh }

3. {1001=Bimal, 1002=Kamlesh, 1007=Akash}

4. {1007=Akash, 1001=Bimal, 1002=Kamlesh }

5. TreeMap is always sorted by Keys. Map can not have duplicate keys, but it can have duplicate values.


java.util.TreeMap.TreeMap()

Constructs a new, empty tree map, using the natural ordering of its keys. All keys inserted into the map must implement the Comparable interface. Furthermore, all such keys
must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException for any keys k1 and k2 in the map. If the user attempts to put a key into the map that
violates this constraint (for example, the user attempts to put a string key into a map whose keys are integers), the put(Object key, Object value) call
will throw a ClassCastException.


Correct Answer : 1
Explanation:




Question : You have been given below code, what behavior are you expecting?

package com.hadoopexam;

interface ParentInterface {
default void call() {
System.out.println("Why are you not working?");
}
}

@FunctionalInterface
interface ChildInterface extends ParentInterface {
@Override
abstract void call();
}

class Welcome {
public static void main(String[] args) {
ChildInterface cInterface = () -> System.out.println("Welcome to HadoopExam Learning Resources");
cInterface.call();
}
}
 : You have been given below code, what behavior are you expecting?
1. There will be a compile time error, as child interface can not override parent interface abstract method.

2. It will given compile time error, because ChildInterface is not a correctr functional interface.

3. This program compile and run perfectly and prints Why are you not working?.

4. This program compile and run perfectly and prints Welcome to HadoopExam Learning Resources.



Correct Answer : 4
Explanation: A default method of a parent interface can be overridden by a child interface and child interface can mark is abstract. And given method
call to call() method will call lambda expression. Hence, it will print respective output.




Question : You have been given below code, what is the behavior you are expecting?

package com.hadoopexam;


class Welcome {

int id;
String name;

public Welcome(int id, String name) {
this.id = id;
this.name = name;
}

public boolean equals(Object obj) { // line n1
boolean output = false;
Welcome b = (Welcome) obj;
if (this.name.equals(b.name))
output = true;
return output;
}

public static void main(String[] args) {
Welcome wel1 = new Welcome(9199, "Hadoop User");
Welcome wel2 = new Welcome(2099, "Hadoop User");
System.out.println(wel1.equals(wel2)); // line n2
}

}


 : You have been given below code, what is the behavior you are expecting?
1. It will compile time error, as you have not overridden the hashCode() method.

2. It will give compile time error, you have to replace System.out.println(wel1.equals((Object)wel2));

3. It will compile and run, and print true

4. It will compile and run and print false.

Correct Answer : 3
Explanation: Given code is perfectly fine. To compare two objects you need to override equals() method. hashCode() method is required only when you want to
store these objects in HashMap, HashSet which uses hash values to find the correct address of the stored objects in collection.
Here, we are using course name to check whether two objects are same or not. As we can see both are using same name hence, they are equal and it will print true.



Related Questions


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

package com.hadoopexam;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class Welcome {
int courseId;
int coursePrice;

public Welcome(int courseId, int coursePrice) {
this.courseId = courseId;
this.coursePrice = coursePrice;
}

public String toString() {
return courseId + ":" + coursePrice;
}

public static void main(String[] args) {

List courses = Arrays.asList(new Welcome(1, 10), new Welcome(2, 30), new Welcome(3, 40));
Welcome p = courses.stream().reduce(new Welcome(4, 0), (p1, p2) -> {
p1.coursePrice += p2.coursePrice;
return new Welcome(p1.courseId, p1.coursePrice);
});

//System.out.println(courses);

((Stream) courses.stream().parallel())
.reduce((p1, p2) -> p1.coursePrice > p2.coursePrice ? p1 : p2)
.ifPresent(System.out::println);
}
}

 : You have been given below code, what is the expected behavior?
1. 3 : 40

2. 4: 0

3. Access Mostly Uused Products by 50000+ Subscribers

4. 4 : 80
2 : 30
3 : 40
1 : 10

5. The program prints nothing.



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

package com.hadoopexam;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Welcome implements Comparator {
String courseName;
double coursePrice;

public Welcome() {
}

public Welcome(String courseName, double coursePrice) {
this.courseName = courseName;
this.coursePrice = coursePrice;
}

public int compare(Welcome b1, Welcome b2) {
return b1.courseName.compareTo(b2.courseName);
}

public String toString() {
return courseName + ":" + coursePrice;
}

public static void main(String[] args) {
List books = Arrays.asList(new Welcome("Java Training", 1), new Welcome("Hadoop Training", 2), new Welcome("Spark training", 3));
Collections.sort(books, new Welcome());
System.out.print(books);
}
}

 : You have been given below code, what is the expected behavior?
1. [ Java Training:1.0, Hadoop Training:2.0, Spark training:3.0]

2. [Hadoop Training:2.0, Java Training:1.0, Spark training:3.0]

3. Access Mostly Uused Products by 50000+ Subscribers

4. An Exceptionis thrown at run time.



Question : You have been given below code

package com.hadoopexam;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Welcome {

public static void main(String[] args) {
List listVal = Arrays.asList("HadoopExam", "QuickTechie", "Training4Exam", ".com");
System.out.println (
// line n1
);
}
}

Which code fragment, when inserted at line n1, enables the code to print the count of string
elements whose length is greater than 7?
 : You have been given below code
1. listVal.stream().filter(x -> x.length()>7).count()

2. listVal.stream().map(x -> x.length()>7).count()

3. Access Mostly Uused Products by 50000+ Subscribers

4. listVal.stream().filter(x -> x.length()>7).mapToInt(x -> x).count()


Question : You have been given below code, what is the expected behavior?
package com.hadoopexam;

import java.util.function.IntPredicate;
import java.util.stream.IntStream;

public class Welcome {
public static void main(String[] args) {
IntStream values = IntStream.of(-1, -2, -3, -4, 5, -6, -7);
IntPredicate positivePred = val -> val > 0;
if (values.anyMatch(positivePred)) {
int positiveVal = values.filter(positivePred).findAny().getAsInt(); //
System.out.println(positiveVal);
}
}
}
 : You have been given below code, what is the expected behavior?
1. Program will compile and run perfectly and print 5

2. Program will compile and run perfectly and print nothing

3. Access Mostly Uused Products by 50000+ Subscribers

4. Program will not compile



Question : You have been given below code, what is the behavior expected?
package com.hadoopexam;

import java.util.stream.Stream;

public class Welcome {
public static void main(String[] args) {
boolean value = Stream.of("Ha", "do", "op", "Ex", "am", ".c", "om")
.filter(str -> str.length() > 3) //n1
.peek(System.out::println)
.allMatch(str -> str.length() > 5);
System.out.println(value);
}
}
 : You have been given below code, what is the behavior expected?
1. Code will give compile time error.

2. Code will throw IllegalStateException.

3. Access Mostly Uused Products by 50000+ Subscribers

4. Code will run perfectly and print true

5. Code will run perfectly and print true



Question : You have been given following code, what is the behavior you are expecting?
package com.hadoopexam;

import java.util.*;

class Welcome {
public static void main(String[] args) {
List list = Arrays.asList("Hadoop ", "Exam ", "Quick ", "Techie ");
Collections.sort(list, (str1, str2) -> str2.compareTo(str1));
list.forEach(word -> System.out.print(word));
}
}
 : You have been given following code, what is the behavior you are expecting?
1. Code will give compile time error.

2. Code will give runtime error

3. Access Mostly Uused Products by 50000+ Subscribers

4. Code will run perfectly and print "Exam Hadoop Quick Techie"