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
- 220.127.116.11 What are the access specifiers that can be applied to a top level class?
- 18.104.22.168 What is the implicit return type of the constructor?
- 22.214.171.124 Can objects be stored in arrays?
- 126.96.36.199 A public class with the name “MyClass” has been declared inside a java file. Can I save the file with the name “AnotherClass.java”?
- 188.8.131.52 How to access the variables and methods in a class without creating a object for the class?
- 184.108.40.206 Can a static method access instance variables?
- 220.127.116.11 Can a static method be overloaded?
- 18.104.22.168 Can a static method access non-static method?
- 22.214.171.124 What are the default values of instance variables and local variables?
- 126.96.36.199 What is the use of final in java?
- 188.8.131.52 How to create strings in Java?
- 184.108.40.206 Difference between String, StringBuffer and StringBuilder?
- 220.127.116.11 Difference between == and equals() in the case of strings?
- 18.104.22.168 What is the use of inheritance?
- 22.214.171.124 Is multiple inheritance supported by Java?
- 126.96.36.199 Use of super in java?
- 188.8.131.52 What is method overriding?
- 184.108.40.206 What is dynamic method dispatch?
- 220.127.116.11 What is the use of abstract in java?
- 18.104.22.168 Can an abstract class be instantiated?
- 22.214.171.124 What happens when a class is declared as final?
- 126.96.36.199 What happens when a method is declared as final?
- 188.8.131.52 Base class for all the classes in java?
- 184.108.40.206 Interfaces can be extended. True or false?
- 220.127.116.11 What is a package?
- 18.104.22.168 How to import a class in a package?
- 22.214.171.124 How to declare a package?
- 126.96.36.199 What is an Exception?
- 188.8.131.52 Difference between exception and error in java?
- 184.108.40.206 How does java handle exceptions?
- 220.127.116.11 Is a catch block mandatory after a try block?
- 18.104.22.168 Is a finally block guaranteed to execute always?
- 22.214.171.124 Difference between checked exceptions and unchecked exceptions in java?
- 126.96.36.199 Use of throws in Java?
- 188.8.131.52 What happens when there are multiple catch blocks?
- 184.108.40.206 What happens when there are nested try blocks?
- 220.127.116.11 How to create own exceptions?
- 18.104.22.168 What is the use of multithreading?
- 22.214.171.124 What are the states in the thread life cycle?
- 126.96.36.199 How to create threads in Java?
- 188.8.131.52 How to synchronize thread access on shared resources?
- 184.108.40.206 What are daemon threads?
- 220.127.116.11 How to manage multiple threads at a time?
- 18.104.22.168 What is an applet?
- 22.214.171.124 Differences between standalone java program and an applet?
- 126.96.36.199 Types of applets?
- 188.8.131.52 How java provides security?
- 184.108.40.206 How to create applets in java?
- 220.127.116.11 What are the applet life cycle methods?
- 18.104.22.168 What is an event?
- 22.214.171.124 What is an event source?
- 126.96.36.199 What is an event listener?
- 188.8.131.52 What are adapter classes?
- 184.108.40.206 What are inner classes?
- 220.127.116.11 What are anonymous classes?
- 18.104.22.168 What is the motto of java?
- 22.214.171.124 What are the difference between AWT and Swing?
- 126.96.36.199 Differences between interface and abstract class?
- 188.8.131.52 What if the main method is declared as private?
- 184.108.40.206 Can a class contain multiple main methods?
- 220.127.116.11 Can I import same package/class twice? Will the JVM load the package twice at runtime?
- 18.104.22.168 Does importing a package imports the sub packages as well?
- 22.214.171.124 What is the default value of an object reference declared as an instance variable?
- 126.96.36.199 Objects are passed by value or by reference?
- 188.8.131.52 Does Java provide any construct to find out the size of an object?
- 184.108.40.206 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?
- 220.127.116.11 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.