Java Interview Questions and Answers

I have summarized some common java interview questions and answers as showed below, you may take a look if you are preparing an interview:

1, what’s  the difference for Error and Exception?

Error specifies system-level errors that you can’t handle it.
Exception specifies program-level exception that you can catch it.

2.  if A class is declared as final type, what this mean?

final keyword specifies the class can not be inherited.

3. What do you need to do to run a class with a main() method in a package?

Example: Say, you have a class named “Pet” in a project folder “c:\myProject” and package na, will you be able to compile and run it as it is?


public class Pet {
  public static void main(String[] args) {
     System.out.println("I am found in the classpath");

To run c:\myProject> java

1.  Set the operating system CLASSPATH environment variable to have the project folder “c:\myProject”. [Show in the above diagram as the System –classpath class loader]
2.  Set the operating system CLASSPATH environment variable to have a jar file “c:/myProject/client.jar”, whic
has the Pet.class file in it. [Shown in the above diagram as the System –classpath class loader].
3.  Run it with –cp or –classpath option as shown below

c:\>java –cp c:/myProject
c:\>java -classpath c:/myProject/client.jar

Important: Two objects loaded by different class  loaders are never equal even if they carry the same values, which mean a class is uniquely identified in  the context of the associated class loader. This applies to  singletons too, where  each class loader will have its own singleton. [Refer Q51 in Java section for singleton design pattern]

4. Give a few reasons for using Java?
Java is a fun language. Let’slook at some of the reasons:   Built-in support for multi-threading, socket communication, and memory management (automatic garbage collection). Object Oriented (OO). Better portability than other languages across operating systems.   Supports Web based applications (Applet, Servlet, and JSP), distributed applications (sockets, RMI, EJB etc) and network protocols (HTTP, JRMP etc) with the  help of extensive standardized APIs (Application Programming Interfaces).

5. How do you express an ‘is a’ relationship and a ‘is a’ relationship or explain inheritance and composition? What is the difference between composition and aggregation?
The ‘is a’ relationship is expressed with inheritance and ‘is a’ relationship is expressed with composition. Both inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for code reuse are class inheritance and object composition

6. does Java have keyword goto?

Java has take Goto as  the reserved words, so don’t use this word within your program

Оцените статью
Добавить комментарий

Your email address will not be published. Required fields are marked *