Java Interview Questions and Answers for 3 year experience
How to identify if a timezone is eligible for Daylight Saving?
a) useDaylightTime() of Time class
b) useDaylightTime() of Date class
c) useDaylightTime() of TimeZone class
d) useDaylightTime() of DateTime class
What does SAM stand for in context of Functional Interface?
Single Ambivalue Method
Single Abstract Method
Simple Active Markup
Simple Abstract Markup
Find the Output
public class TestClass { public static void main(String[] args) { TestClass tc= new TestClass(); System.out.println(tc.test()); } public int test(){ try{ return 1; }catch(Exception e){ return 2; }finally{ return 3; } } }
1
2
3
Compilation error
Find the Output
public class A1 { static final String sample="simple"; String output=sample+"sample"; void a(){ if("simplesample"==output){ System.out.println("sample==output"); } else { System.out.println("sample!=output"); }} public static void main(String[] s) { A1 a=new A1(); a.a(); } }
sample==output
sample!=output
Runtime exception
compilation fails
What is it called where the child object gets killed if the parent object is killed?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
Which design pattern provides a single class which provides simplified methods required by the client and delegates call to those methods?
Adapter pattern
Builder pattern
Facade pattern
Prototype pattern
Which of the following is not a marker interface?
Serializable
Cloneable
Remote
Reader
Example of functional interfaces in Java 8
java.util.concurrent.Callable
java.lang.Runnable
All
Which of the following is true about FunctionalInterfaces?
1. It is an annotation
2. All Interfaces having 1 public method should be mandatorily marked as Functional Interface
3. A Functional Interface cannot have default static methods
1, 2, and 3 are required
Only 1
1 and 2 are required
None of these
If we try to add Enum constants to a TreeSet, what sorting order will it use?
a) Sorted in the order of declaration of Enums
b) Sorted in alphabetical order of Enums
c) Sorted based on order() method
d) Sorted in descending order of names of Enums
What will be the output of below
Object obj = null; Optional.of(Obj)
Compile Time Error: Null
Runtime Exception
null
“”
Which of these interfaces are functional interfaces?
1.
public interface Adder{ int add(int a, int b); }
2.
public interface SmartAdder extends Adder{ int add(double a, double b); }
3.
public interface Nothing{ }
Only 1
Both 1 and 2
All
None
What does the Liskov substitution principle specify?
parent class can be substituted by the child class
child class can be substituted by the parent class
parent class cannot be substituted by the child class
No classes can be replaced by each other
What will be the output of the below Program
public class StreamTest { public static void main(String args[]) { int myArray[] = { 1, 5, 8 }; IntStream stream = Arrays.stream(myArray); System.out.println(stream.sum()); System.out.println(stream.reduce(1, (a,b) -> a+b)); } }
Compilation Error: A stream once closed cannot be reused
14, 14
Runtime Exception: A stream once closed cannot be reused
14,15
Which of the following are not valid lambda expressions?
() -> {}
() -> Raoul
() -> {return Mario;}
(Integer i) -> return Alan + i;
Lambda expressions are __________ scoped
Semantically
Lexically
Binary
None
In, R apply(T t) is a method of
Function
Process
Predicate
None
T get() is a method of which functional interface
Function
Supplier
Consumer
None
Deque and Queue are derived from
AbstractList
Collection
AbstractCollection
List
What does the expression float a = 35 / 0 return?
1. 0
2. Not a Number
3. Infinity
4. Run time exception
Find Output
public class TestClass { public static void main(String[] args) { TestClass tc= new TestClass(); tc.display(5); } public void display(Integer i){ System.out.println("Inside Integer"); } public void display(long i){ System.out.println("Inside long"); } }
Compilation error
No Output
Inside Integer
Inside long
Which of these keywords is used to upper bound a wildcard?
stop
bound
extends
implements
For an interface to be used in Lamda, the following must be true
1. It must be having exactly one public abstract method
2. It must have @FunctionalInterface
3. It must have at least one default static method
Only 1
1, 2, and 3 are required
1 and 2 are required
None of these
What is Predicate in?
method
class
Interface
Framework
When does method overloading is determined?
At run time
At compile time
At coding time
At execution time
What is used to inject mock fields into the tested object automatically?
@InjectMocks
@Inject
@InjectMockObject
@Mock
What will be the output of the following Java program?
class string_class { public static void main(String args[]) { String obj = "I LIKE JAVA"; System.out.println(obj.size()); } }
9
10
11
None of these
Which version of the java added the Flushable interface?
java SE 7
java SE 8
java SE 6
java SE 5
Find Output
public class MyRun { public static void main (String[] args){ String name = "India"; Runnable r1 = () -> System.out.println(name); String name1 = name.toUpperCase(); name1 = name.toUpperCase(); Runnable r2 = () -> System.out.println(name1); r1.run(); } }
India
INDIA
Compilation Error
Others
What should be the execution order, if a class has a method, static block, instance block, and constructor, as shown below?
public class First_C { public void myMethod() { System.out.println("Method"); } { System.out.println(" Instance Block"); } public void First_C() { System.out.println("Constructor "); } static { System.out.println("static block"); } public static void main(String[] args) { First_C c = new First_C(); c.First_C(); c.myMethod(); } }
a. Instance block, method, static block, and constructor
b. Method, constructor, instance block, and static block
c. Static block, method, instance block, and constructor
d. Static block, instance block, constructor, and method
How to let units know that they need to be run using PowerMock?
@PowerMock
@RunWith(PowerMock)
@RunWith(Junits)
@RunWith(PowerMockRunner.class)
What will be the output of the following Java code?
import java.util.*; class Bitset { public static void main(String args[]) { BitSet obj = new BitSet(5); for (int i = 0; i < 5; ++i) obj.set(i); obj.clear(2); System.out.print(obj); } }
{0, 1, 3, 4}
{0, 1, 2, 4}
{0, 1, 2, 3, 4}
{0, 0, 0, 3, 4}
Which of these classes is not related to input and output stream in terms of functionality?
Writer
File
InputStream
Reader
Which of the following are not valid lambda expressions?
1. () -> {}
2. () -> “Raoul”
3. () -> {return “Mario”;}
4. (Integer i) -> return “Alan” + i;
5. (String s) -> {“Iron Man”;}
1,2
4,5
All are valid
None are valid
How to get prints of shared object memory maps or heap memory maps for a given process?
jmap
memorymap
memorypath
jhat
This pattern can be used for attaching additional responsibilities to an object dynamically.It provides a flexible alternative to subclasses for extending functionality.
Decorator
Composite
Adapter
Chain of responsibility
What is it called where an object has its own lifecycle and child object cannot belong to another parent object?
Aggregation
Composition
Encapsulation
Association
Find Output
List<Integer> list = Stream.of(2,3,4) .forEach(System.out::println) .collect(Collectors.toList()); System.out.println(list.size());
Compilation error
3
Runtime Exception
2
39.
Given,
What is the initial quantity of the ArrayList list?
ArrayList list = new ArrayList();
1. 5
2. 10
3. 0
4. 100
Identify the terminal operation
long count = menu.stream() .filter(d -> d.getCalories() > 300) .distinct() .limit(3)
limit
both distinct and limit
filter
None
Which method can be used to check null on an Optional variable in ?
isPresent()
isNullable()
isPresentable()
isNotNull()
Deque and Queue are derived from:
AbstractList
Collection
AbstractCollection
List
What will be the output of below Program
public class OptionalTest { public static void main(String[] args) { Optional<String> opt = Optional.ofNullable(null); System.out.println(opt.isPresent()); System.out.println(Optional.empty().equals(opt)); } }
NullPointerException
true,true
false, true
false,false
Which method is called if the main is not static?
a) static block whichever is on top of the class
b) Throws NoSuchMethodError
c) Calls main method even its not static
d) Runs all static methods and skip main method
What is -Xms and -Xmx while starting jvm?
Initial; Maximum memory
Maximum; Initial memory
Maximum memory
Initial memory
Which of the following pattern refers to creating duplicate object while keeping performance in mind?
Builder Pattern
Bridge Pattern
Prototype Pattern
Filter Pattern
Which right shift operator preserves the sign of the value?
a) <<
b) >>
c) <<=
d) >>=
What is the difference between AutoCloseable and Closeable?
Closeable is an interface and AutoCloseable is a concrete class
Closeable throws IOException; AutoCloseable throws Exception
Closeable is a concept; AutoCloseable is an implementation
Closeable throws Exception; AutoCloseable throws IOException
What are equivalent method reference for the following lambda expression?
BiPredicate<List<String>, String> contains = (list, element) -> list.contains(element);
BiPredicate<List<String>, String> contains = List::contains();
BiPredicate<List<String>, String> contains = List::contains;
BiPredicate<List<String>, String> contains = List.contains();
BiPredicate<List<String>, String> contains = List.contains;
It is a good practise to not throw which exception in close() method of autocloseable?
IOException
CustomException
InterruptedException
CloseException