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;

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

Correct Answer : 5
Explanation: We have first stored all the key values in map like (1,a)(5,b), (30,c) ,(70,d), (15,e). We are using TreeMap, which help us sort the map values based on
keys and not on values. However, we have overridden the TreeMap compare() method, which sort the values in reverse order. Hence, keys would be sorted like (70,30,15,5,1) .
So when we iterate on map it will generate values as 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.


Correct Answer : 3
Explanation: As we know, java does not support multiple inheritance. Hence, code will not compile, because you are trying to extends two classes in GrandChild class.




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

Correct Answer : 5
Explanation:


Related 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();


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.



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



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.Map;
import java.util.stream.Collectors;

class Welcome {

public enum Courses {
HADOOP, SPARK, JAVA
}

String name;
Courses course;

public Welcome(String na, Courses cs) {
name = na;
course = cs;
}

public String getName() {
return name;
}

public Courses getCourses() {
return course;
}

public static void main(String[] args) {
List couList = Arrays.asList (
new Welcome ("Jayesh", Welcome.Courses.HADOOP),
new Welcome ("Imtiaz", Welcome.Courses.HADOOP),
new Welcome ("Gautam", Welcome.Courses.SPARK));
Map> userMap = couList.stream ()
.collect(Collectors.groupingBy (Welcome ::getCourses,Collectors.mapping(Welcome::getName, Collectors.toList())));
System.out.println(userMap);
}
}

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

2. It will print {SPARK=[Gautam], HADOOP=[Jayesh, Imtiaz]}

3. It will print {Gautam =[ SPARK], Jayesh=[ HADOOP, HADOOP]}

4. It will print {Gautam =[ SPARK], Jayesh=[ HADOOP], Imtiaz=[HADOOP]}



Question : You have been given below code, what will be the behavior of the code ?
package com.hadoopexam;

public class Welcome {
CompanyName companyName;

enum CompanyName {
HADOOPEXAM, QUICKTECHIE, TRAINING4EXAM
};

public Welcome(CompanyName pType) {
companyName = pType;
}

public static void main(String[] args) {
CompanyName cName = new CompanyName();
Welcome wel = new Welcome(CompanyName.HADOOPEXAM);
}
}
 : You have been given below code, what will be the behavior of the code ?
1. It will compile and run perfectly

2. It will compile but will run with Error

3. It will run perfectly and print the HADOOPEXAM

4. It will give compile time error.



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

package com.hadoopexam;

public enum Welcome {

private int totalProducts; // n1
HadoopExam(30), QuickTechie(5), Training4Exam(50); // n2

private Welcome(int totalProducts) {
this.totalProducts = totalProducts;
}

public int getTotalProducts() {
return totalProducts;
}
}
 : You have been given below code, what behavior you are expecting.
1. It will compile successfully

2. It will compile, but can not run successfully.

3. This is enum definition is not correct, it will through compile time errors.'

4. It will compile and run successfully.