1.Which JVM parameter is used to control stack size of a thread?
-Xss parameter is used to control stack size of Thread in Java.
2. What is the difference between forking a process and spawning a thread?
In Forking the new process will run the same code as parent process but in different memory
space, but when you spawn a new thread in existing process, it just creates another
independent path of execution but share same memory space.
3. What is Law of Demeter violation in java? Why it matters?
Answer: Law of Demeter suggests you "talk to friends and not stranger", It matters because it is
used to reduce coupling between classes.
4..How do you prevent for creating anotherinstance of Singleton using serialization?
Answer: This question is from Design-Patterns
In Serialization: You can prevent this by using readResolve() method.
5.What is difference between CyclicBarriar and CountdownLatch in Java ?
The following are two main differences
1) CyclicBarrier is resulable, CountDownLatch is not.
2) CountDownLatch is advanceable but CyclicBarrier is not.
6. What is busy spin? Why should you use it?
Busy spin is one of the technique to wait for events without releasing CPU. It's often done to
avoid losing data in CPU cached.
7. Do you know about Liskov Substitution Principle?
According to Liskov Substitution Principle, Subtypes must be substitutable for super type i.e.
methods or functions which uses super class type must be able to work with object of sub class
without any issue".
8. How will you take thread dump in Java/ How will you analyze Thread dump in unix and
windows/ How you do you analyze deadlockin your program ?
Answer: A Java thread dump is a snapshot what every thread in the JVM is doing at a particular
point in time. In UNIX you can use kill -3 and then thread dump will print on log. On windows
you can use "CTRL+Break".
9.Why does the JVM crash with a core dump or a Dr.Watson error?
Answer: core dump is a memory map of a running process. This can happen due to one of the
following reasons:
1) The OS on which your JVM is running might require a patch or service pack.
2) On Linux/Unix, it is easy to crash JVM crash by sending it a Signal to the running
process.
3) Typical crashes in native code happen by dereferencing pointers to wrong memory
areas (like Nullpointer) or illegal opcodes.
10. What is a Lambda Expression ? What's its use ?
Ans. It is introduced in java 8. Its an anonymous method without any declaration. Lambda
Expression are useful to write shorthand Code and hence saves the effort of writing lengthy
Code.
11. What is PermGen or Permanent Generation ?
Answer: The Permanent generation contains metadata required by the JVM to describe the
classes and methods used in the application. The permanent generation is populated by the
JVM at runtime based on classes in use by the application.
12. When do you use Visitor design pattern?
The visitor pattern is a solution of problem where you need to add operation on a class
hierarchy but without touching them. This pattern uses double dispatch to add another level of
indirection.
13. What algo does fork-join frameworkin Java uses?
The fork/join framework uses a work-stealing algorithm.
14.Explain re-entrant and idempotent methods/functions?
Ans) A method in stack is re-entrant allowing multiple concurrent invocations that do not
interfere with each other.
Idempotent methods are methods, which are written in such a way that repeated calls to the
same method with the same arguments yield same results.
15. How to ensure that instance is never garbage collected.
using singleton kind of pattern. There's a static reference to a singleton, so it won't be eligible
for garbage collection until the classloader is eligible for garbage collection.
16. How do you find memory usage from Java program?
Answer: Runtime.freeMemory() return amount of free memory in bytes,
Runtime.totalMemory() returns total memory in bytes and Runtime.maxMemory() returns
maximum memory in bytes.
17. Difference between Predicate, Supplier and Consumer methods in java ?
Ans. Predicate represents an anonymous function that accepts one argument and produces a
result.
Supplier represents an anonymous function that accepts no argument and produces a result.
Consumer represents an anonymous function that accepts an argument and produces no result
Q18. What is the use of Optional ?
Ans. Introduced in java 8. Optional is a good way to protect application from runtime
nullPointerException in case the the absent value has been represented as null.
19. What is Producer Consumer Design Pattern in java? How do we solve the Produce
consumer problem in Java?
Answer: Producer Consumer Design pattern reduces coupling between Producer and Consumer
by separating Identification of work with Execution of Work.
There are two ways to solve this problem in Java, One by using wait and notify method and
other by using BlockingQueue in Java
20. What do the expression 1.0 / 0.0 will return? will it throw Exception? any compile time
error?
Answer: The simple answer to this question is that it will not throw ArithmeticExcpetion and
return Double.INFINITY.
21. How much time does it take to retrieve an element if stored in HashMap, Binary tree, and
a Linked list.?
Answer: This question is from collections
In HashMap it takes O(1) time, in the binary tree it takes O(logN) where N is a number of nodes
in the tree and in linked list it takes O(n) time where n is a number of element in the list
22.What is the load Factor in Java? What is default size and load factor of HashMap
Answer :This question is from collections
Load Factor - "Till what load, hashmap can allow elements to put in it before its capacity is
automatically increased".
Default, initial capacity of the HashMap is 16 and Load factor is 0.75f (i.e 75% of current map
size).
23. What is Deadly Diamond of Death problem in Java?
Answer: The problem of multiple inheritance is called Deadly Diamond of Death that is why java
does not support it.
24.What is lazy and early loading of Singleton class and how will you implement it?
Answer : This question is from Design-Patterns
As there are many ways to implement Singleton like using double checked locking or Singleton
class with static final instance initialized during class loading. Former is called lazy loading while
later is called early .
25. Write Java program to create a deadlockin Java and fix it ?
Answer: The following program shows deadlock.In order to fix it just provide the order access
like either in first method below call integer class first and then string class as in second method
or second way is in method2 call string class first and then integer class as in method1.
public class DeadLockDemo { /* * This method request two locks, first String and then Integer
*/
public void method1() {
synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
synchronized (Integer.class) {
System.out.println("Aquired lock on Integer.class object");
}
}
}
public void method2() {
synchronized (Integer.class) {
System.out.println("Aquired lock on Integer.class object");
synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
}
}
}
}
26. Write the code for double checked locking in Singleton design pattern?
Answer :
public class DoubleCheckedLockingSingleton{
private volatile DoubleCheckedLockingSingleton INSTANCE;
private DoubleCheckedLockingSingleton(){}
public DoubleCheckedLockingSingleton getInstance(){
if(INSTANCE == null){
synchronized(DoubleCheckedLockingSingleton.class){
//double checking Singleton instance
if(INSTANCE == null){
INSTANCE = new DoubleCheckedLockingSingleton();
}
}
}
return INSTANCE;
}
}

25 java tough interview questions

  • 1.
    1.Which JVM parameteris used to control stack size of a thread? -Xss parameter is used to control stack size of Thread in Java. 2. What is the difference between forking a process and spawning a thread? In Forking the new process will run the same code as parent process but in different memory space, but when you spawn a new thread in existing process, it just creates another independent path of execution but share same memory space. 3. What is Law of Demeter violation in java? Why it matters? Answer: Law of Demeter suggests you "talk to friends and not stranger", It matters because it is used to reduce coupling between classes. 4..How do you prevent for creating anotherinstance of Singleton using serialization? Answer: This question is from Design-Patterns In Serialization: You can prevent this by using readResolve() method. 5.What is difference between CyclicBarriar and CountdownLatch in Java ? The following are two main differences 1) CyclicBarrier is resulable, CountDownLatch is not. 2) CountDownLatch is advanceable but CyclicBarrier is not. 6. What is busy spin? Why should you use it? Busy spin is one of the technique to wait for events without releasing CPU. It's often done to avoid losing data in CPU cached. 7. Do you know about Liskov Substitution Principle? According to Liskov Substitution Principle, Subtypes must be substitutable for super type i.e. methods or functions which uses super class type must be able to work with object of sub class without any issue". 8. How will you take thread dump in Java/ How will you analyze Thread dump in unix and windows/ How you do you analyze deadlockin your program ? Answer: A Java thread dump is a snapshot what every thread in the JVM is doing at a particular point in time. In UNIX you can use kill -3 and then thread dump will print on log. On windows you can use "CTRL+Break". 9.Why does the JVM crash with a core dump or a Dr.Watson error? Answer: core dump is a memory map of a running process. This can happen due to one of the following reasons:
  • 2.
    1) The OSon which your JVM is running might require a patch or service pack. 2) On Linux/Unix, it is easy to crash JVM crash by sending it a Signal to the running process. 3) Typical crashes in native code happen by dereferencing pointers to wrong memory areas (like Nullpointer) or illegal opcodes. 10. What is a Lambda Expression ? What's its use ? Ans. It is introduced in java 8. Its an anonymous method without any declaration. Lambda Expression are useful to write shorthand Code and hence saves the effort of writing lengthy Code. 11. What is PermGen or Permanent Generation ? Answer: The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application. 12. When do you use Visitor design pattern? The visitor pattern is a solution of problem where you need to add operation on a class hierarchy but without touching them. This pattern uses double dispatch to add another level of indirection. 13. What algo does fork-join frameworkin Java uses? The fork/join framework uses a work-stealing algorithm. 14.Explain re-entrant and idempotent methods/functions? Ans) A method in stack is re-entrant allowing multiple concurrent invocations that do not interfere with each other. Idempotent methods are methods, which are written in such a way that repeated calls to the same method with the same arguments yield same results. 15. How to ensure that instance is never garbage collected. using singleton kind of pattern. There's a static reference to a singleton, so it won't be eligible for garbage collection until the classloader is eligible for garbage collection. 16. How do you find memory usage from Java program? Answer: Runtime.freeMemory() return amount of free memory in bytes, Runtime.totalMemory() returns total memory in bytes and Runtime.maxMemory() returns maximum memory in bytes. 17. Difference between Predicate, Supplier and Consumer methods in java ?
  • 3.
    Ans. Predicate representsan anonymous function that accepts one argument and produces a result. Supplier represents an anonymous function that accepts no argument and produces a result. Consumer represents an anonymous function that accepts an argument and produces no result Q18. What is the use of Optional ? Ans. Introduced in java 8. Optional is a good way to protect application from runtime nullPointerException in case the the absent value has been represented as null. 19. What is Producer Consumer Design Pattern in java? How do we solve the Produce consumer problem in Java? Answer: Producer Consumer Design pattern reduces coupling between Producer and Consumer by separating Identification of work with Execution of Work. There are two ways to solve this problem in Java, One by using wait and notify method and other by using BlockingQueue in Java 20. What do the expression 1.0 / 0.0 will return? will it throw Exception? any compile time error? Answer: The simple answer to this question is that it will not throw ArithmeticExcpetion and return Double.INFINITY. 21. How much time does it take to retrieve an element if stored in HashMap, Binary tree, and a Linked list.? Answer: This question is from collections In HashMap it takes O(1) time, in the binary tree it takes O(logN) where N is a number of nodes in the tree and in linked list it takes O(n) time where n is a number of element in the list 22.What is the load Factor in Java? What is default size and load factor of HashMap Answer :This question is from collections Load Factor - "Till what load, hashmap can allow elements to put in it before its capacity is automatically increased". Default, initial capacity of the HashMap is 16 and Load factor is 0.75f (i.e 75% of current map size). 23. What is Deadly Diamond of Death problem in Java? Answer: The problem of multiple inheritance is called Deadly Diamond of Death that is why java does not support it.
  • 4.
    24.What is lazyand early loading of Singleton class and how will you implement it? Answer : This question is from Design-Patterns As there are many ways to implement Singleton like using double checked locking or Singleton class with static final instance initialized during class loading. Former is called lazy loading while later is called early . 25. Write Java program to create a deadlockin Java and fix it ? Answer: The following program shows deadlock.In order to fix it just provide the order access like either in first method below call integer class first and then string class as in second method or second way is in method2 call string class first and then integer class as in method1. public class DeadLockDemo { /* * This method request two locks, first String and then Integer */ public void method1() { synchronized (String.class) { System.out.println("Aquired lock on String.class object"); synchronized (Integer.class) { System.out.println("Aquired lock on Integer.class object"); } } } public void method2() { synchronized (Integer.class) { System.out.println("Aquired lock on Integer.class object"); synchronized (String.class) { System.out.println("Aquired lock on String.class object"); } } } } 26. Write the code for double checked locking in Singleton design pattern? Answer : public class DoubleCheckedLockingSingleton{ private volatile DoubleCheckedLockingSingleton INSTANCE;
  • 5.
    private DoubleCheckedLockingSingleton(){} public DoubleCheckedLockingSingletongetInstance(){ if(INSTANCE == null){ synchronized(DoubleCheckedLockingSingleton.class){ //double checking Singleton instance if(INSTANCE == null){ INSTANCE = new DoubleCheckedLockingSingleton(); } } } return INSTANCE; } }