Question : You have been given below code, what is the behavior you can expect by looking at the code?
public class Welcome { public Welcome() { System.out.println("In Welcome constructor "); }
public void hello() { enum Company { HADOOPEXAM, QUICKTECHIE, TRAINING4EXAM } } }
1. This code will compile perfectly.
2. This code will compile cleanly without any compiler warnings or errors, and when used, will generate a runtime exception
3. This code will compile cleanly without any compiler warnings or errors, and when used, will run without any problems
4. This code will generate compile time error.
Correct Answer : 4 Explanation: This code will not compile because, enum can only be defined in a top-level class or interface and not within a method.
Question : You have been given following Parent child Interface and classes.
public class GrandChild implements Child2, Child1 { public static void main(String[] args) { new GrandChild().call(); } }
What happens, when you run above GrandChild application?
1. There will be a compile time error. Because you try to implement multiple inheritance.
2. There will be a compile time error. Because there is a confusion which call() method should be called.
3. There will be a no compile time error, and it will print "Child1..." when executed.
4. There will be a no compile time error, and it will print "Child2..." when executed.
Correct Answer : 3 Explanation: As we are extending two interfaces. Child1 and Child2. Both are having same method name call(). However, Child2 interface is having static call() method. And child class cannot override the static methods of a parent interface. Hence, if you want to use call() method of Child2() class then , you should have called as Child2.call() . But the Child1 is having an interface, which has defined a default call() method, which can be overridden.
Question : You have been given following code, with the LambdaFunction. package com.hadoopexam;
class Welcome { @FunctionalInterface interface HadoopFunction { int call(int j);
boolean equals(java.lang.Object arg0); }
public static void main(String[] args) { HadoopFunction hadoopFunction = val -> val * new Integer(6); // n1 System.out.println(hadoopFunction.call(2)); } }
1. There will be a compile time error. Because there is no lambda function defined which can take Integer as an argument.
2. There will be a compile time error. Because there is no lambda function defined which can take two Integer values as argument2.
3. There will be a compile time error. Because there is no lambda function defined for equals method.
4. The program will run and compile perfectly, because auto unboxing will be applied and it will print 12.
Correct Answer : 4 Explanation: It is valid to define an interface inside the class. As equals method is having the same signature as parent class of all the classes Objects equals() method, hence it is not considered as abstract method of the functional interface. It is acceptable to leave the parameter type when there is only one parameter and the parameter and return type are inferred from the LambdaFunction abstract method declaration int call(int j). Since the lambda function is passed with the value 2, the returned value is 2 * new Integer(6), and hence 12 is printed in console. There will be Auto Unboxing for Integer(6) to 6 as primitive values.