Here is a collection of frequently asked java questions in technical interviews. All Java questions are provided with appropriate answers. If you find any answer is not correct, do comment in the comments section (scroll down).
Note: Click the question or click on the arrow to see the answer for a question.
- 1 Question and Answers
- 188.8.131.52 What are the access specifiers that can be applied to a top level class?
- 184.108.40.206 What is the implicit return type of the constructor?
- 220.127.116.11 Can objects be stored in arrays?
- 18.104.22.168 A public class with the name “MyClass” has been declared inside a java file. Can I save the file with the name “AnotherClass.java”?
- 22.214.171.124 How to access the variables and methods in a class without creating a object for the class?
- 126.96.36.199 Can a static method access instance variables?
- 188.8.131.52 Can a static method be overloaded?
- 184.108.40.206 Can a static method access non-static method?
- 220.127.116.11 What are the default values of instance variables and local variables?
- 18.104.22.168 What is the use of final in java?
- 22.214.171.124 How to create strings in Java?
- 126.96.36.199 Difference between String, StringBuffer and StringBuilder?
- 188.8.131.52 Difference between == and equals() in the case of strings?
- 184.108.40.206 What is the use of inheritance?
- 220.127.116.11 Is multiple inheritance supported by Java?
- 18.104.22.168 Use of super in java?
- 22.214.171.124 What is method overriding?
- 126.96.36.199 What is dynamic method dispatch?
- 188.8.131.52 What is the use of abstract in java?
- 184.108.40.206 Can an abstract class be instantiated?
- 220.127.116.11 What happens when a class is declared as final?
- 18.104.22.168 What happens when a method is declared as final?
- 22.214.171.124 Base class for all the classes in java?
- 126.96.36.199 Interfaces can be extended. True or false?
- 188.8.131.52 What is a package?
- 184.108.40.206 How to import a class in a package?
- 220.127.116.11 How to declare a package?
- 18.104.22.168 What is an Exception?
- 22.214.171.124 Difference between exception and error in java?
- 126.96.36.199 How does java handle exceptions?
- 188.8.131.52 Is a catch block mandatory after a try block?
- 184.108.40.206 Is a finally block guaranteed to execute always?
- 220.127.116.11 Difference between checked exceptions and unchecked exceptions in java?
- 18.104.22.168 Use of throws in Java?
- 22.214.171.124 What happens when there are multiple catch blocks?
- 126.96.36.199 What happens when there are nested try blocks?
- 188.8.131.52 How to create own exceptions?
- 184.108.40.206 What is the use of multithreading?
- 220.127.116.11 What are the states in the thread life cycle?
- 18.104.22.168 How to create threads in Java?
- 22.214.171.124 How to synchronize thread access on shared resources?
- 126.96.36.199 What are daemon threads?
- 188.8.131.52 How to manage multiple threads at a time?
- 184.108.40.206 What is an applet?
- 220.127.116.11 Differences between standalone java program and an applet?
- 18.104.22.168 Types of applets?
- 22.214.171.124 How java provides security?
- 126.96.36.199 How to create applets in java?
- 188.8.131.52 What are the applet life cycle methods?
- 184.108.40.206 What is an event?
- 220.127.116.11 What is an event source?
- 18.104.22.168 What is an event listener?
- 22.214.171.124 What are adapter classes?
- 126.96.36.199 What are inner classes?
- 188.8.131.52 What are anonymous classes?
- 184.108.40.206 What is the motto of java?
- 220.127.116.11 What are the difference between AWT and Swing?
- 18.104.22.168 Differences between interface and abstract class?
- 22.214.171.124 What if the main method is declared as private?
- 126.96.36.199 Can a class contain multiple main methods?
- 188.8.131.52 Can I import same package/class twice? Will the JVM load the package twice at runtime?
- 184.108.40.206 Does importing a package imports the sub packages as well?
- 220.127.116.11 What is the default value of an object reference declared as an instance variable?
- 18.104.22.168 Objects are passed by value or by reference?
- 22.214.171.124 Does Java provide any construct to find out the size of an object?
- 126.96.36.199 If my class already extends from some other class what should I do if I want an instance of my class to be thrown as an exception object?
- 188.8.131.52 Can there be an abstract class with no abstract methods in it?
Question and Answers
String s1 = new String("hello");
String s2 = new String("hello");
If we compare s1 and s2 as s1==s2, it evaluates to false as s1 and s2 reference separate memory locations (objects). To compare the string content we have to write s1.equals(s2). It evaluates to true.
In the above example mypack is the name of our package.