Wednesday, 7 December 2016

Most Frequently used Java Questions

http://howtodoinjava.com/core-java/interviews-questions/real-java-interview-questions-asked-for-oracle-enterprise-manager-project/

1)  What is abstraction and encapsulation in java ?

2)  What is method overloading rules? Can we overload the super class method in sub class?                     Discussion happened with the example.

3)  What is method overriding rules?

4)  What is widening and narrowing in java? discussion happened with example?

5)  Can I have only try block in code?

6)  Threads : producer and consumer problem?

7)  Why wait(), notify() and notifyAll() are defined in Object class?

8)  Can we override wait() or notify() methods?

9)  Difference between wait(), sleep() and yield()?

10) Explain about join() method in thread class

11) Have you faced out of memory error? If yes how you fixed ? Tell different scenarios why it
      comes?

12) Database connection leakage?

13) To avoid this extra overhead every time, double checked locking principle is used what is that ??

14) What is Java Singleton Pattern ?? how are the different way to create a singleton class either by            thread safe or non thread safe ??
http://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-examples

15) Rules of Exceptions during Method Overriding & Overloading
http://www.javatpoint.com/exception-handling-with-method-overriding

16) How to get data from HashMap & HashSet?

17) I have two arrays which contains integer. Write a program to merge those two arrays and remove       duplicate elements? At the end, I need one array which should have unique elements?

18) Write a program on Singleton?

19) Write a program to sort an array and remove duplicates?

20) Write a program to fetch the data from a table using JDBC and result set?

21) What happens if i write return in try block? Will finally executes? What happens if write                      system.exit(), will finally block executes?

22) Different type of design patterns used before ?
http://www.vogella.com/tutorials/DesignPatterns/article.html
http://www.java67.com/2012/09/top-10-java-design-pattern-interview-question-answer.html

23)  Can you override a private or static method in Java?

24) What will happen if we put a key object in a HashMap which is already there?

25) If a method throws NullPointerException in the superclass, can we override it with a method               which throws RuntimeException?

26) Difference between compareTo and compare in java

27) How do you ensure that N thread can access N resources without deadlock?

28) What is the difference between StringBuffer and StringBuilder in Java?

29) Can you access a non-static variable in the static context?

30) is it possible to load a class by two ClassLoader?

31) is it possible for equals() to return false, even if contents of two Objects are same?

32) How does "has before" apply to volatile work?

33) What happens when an exception is thrown by a Thread?

34) Difference between System.exit() and System.halt() method?

35) difference between BLOB and CLOB

36) Why character array is better than String for Storing password in Java?

37) How to create a thread-safe singleton in Java using double-checked locking?

38) Write Java program to create a deadlock in Java and fix it ?

39) Why is String immutable in Java?

40) Why does Java not support operator overloading ?

41) Why is multiple inheritances not supported in Java ?

42) Why wait and notify is declared in Object class instead of Thread ?

43) What happens if your Serializable class contains a member which is not  serializable? How do              you fix it?

44) Why wait and notify  called from synchronized method in Java?

45) Why does Java not support operator overloading ?

46) Why is multiple inheritances not supported in Java ?

47) Can we overload a static method in Java?

48) Can we override a static method in Java?

49) Can we prevent overriding a method without using the final modifier?

50) Can we override a private method in Java?

51) What is covariant method overriding in Java?

52) Can we change the return type of method to subclass while overriding?

53) Can we change the argument list of an overriding method?

54) Can we override a method which throws runtime exception without throws clause?

55) How do you call superclass version of an overriding method in sub class?

56) Can we override a non-static method as static in Java?

57) Can we override the final method in Java?

58) Can we have a non-abstract method inside interface?

59) What is the default method of Java 8?

60) Can we make a class both final and abstract at the same time?

61) Can we overload or override the main method in Java?

62) What is the difference between Association, Aggregation, and Composition in OOP?

63) What problem is solved by Strategy pattern in Java?

64) Which OOP concept Decorator design Pattern is based upon?

65) What is the difference between Decorator, Proxy and Adapter pattern in Java?

66) Consider the following Java code snippet,

which is initializing two variables and both are not volatile, and two threads T1 and T2 are modifying these values as following, both are not synchronized
int x = 0;
boolean bExit = false;

Thread 1 (not synchronized)
x = 1;
bExit = true;

Thread 2 (not synchronized)
if (bExit == true)
System.out.println("x=" + x);
Now tell us, is it possible for Thread 2 to print “x=0”?

Answer: It's impossible for a list of tricky Java questions to not contain anything from multi-threading. This is the simplest one I can get. Answer of this question is Yes, It's possible that thread T2 may print x=0.Why? because without any instruction to compiler e.g. synchronized or volatile, bExit=true might come before x=1 in compiler reordering. Also, x=1 might not become visible in Thread 2, so Thread 2 will load x=0. Now, how do you fix it?

 When I asked this question to a couple of programmers they answer differently, one suggests to make both threads synchronized on a common mutex, another one said make both variable volatile. Both are correct, as it will prevent reordering and guarantee visibility.

But the best answer is you just need to make bExit as volatile, then Thread 2 can only print “x=1”. x does not need to be volatile because x cannot be reordered to come after bExit=true when bExit is volatile.

No comments:

Post a Comment

JSP interview questions and answers

Q1. What is JSP and why do we need it? JSP stands for JavaServer Pages. JSP is java server side technology to create dynamic web pages. J...