Java Interview Questions and Answers

Java is the most most populous object oriented programming language in the world.  The most commonly asked Core Java Interview Questions and Answers are explained briefly with suitable examples. The top interview questions and answers in the below articles covers all the basic concepts which will be asked for both freshers and experience job seekers.

1. Why is Java platform independent?

Java is platform independent because compiler converts the source code to byte code. The byte codes are machine(platform) independent and it can be run in any machine and thus Java code can be executed in any platform like Windows, Linux, Solaris etc. We can say, “compile once and run anywhere”. Though we require Java Virtual Machine(JVM) to compile and execute Java in any platform.

2. What is the difference between JDK and JVM?

  • Java Development Kit(JDK): JDK is program development kit for Java application and applets
  • Java Virtual Machine(JVM): JVM provide runtime environment to execute byte code.

3. What is Static Keyword?

Static keyword can be used for variable, method, block and nested class. It is used for memory management, as the same data is reserved for all.

4. Difference between Over-Riding and Over-Loading in Java?

Overloading occurs during compile time while overriding occurs during runtime. Overloading has two or more methods in same class but with different arguments whereas overriding has two different methods with same name and argument i.e. one is parent class and other is child class.

5. What is Constructor?

Constructor are used to initialize Objects. It can invoked when Object of the Class is created. Constructor name should be exactly the same as its class name. Java provides default contractor with no argument

6. Difference between Throw and Throws?

Throw keyword can throw exception explicitly (in any static block and method) whereas throws keyword declare exception.

7. What is Multi-Threading in Java?

Multithreading in Java is a process in which multiple processes(threads- lightweight sub process) can be executed simultaneously.

8. What is Object?

An Object is instance of a class. Memory is allocated when it is first created. Object have sub-object while class can have multiple sub-class.

9. What is Class?

Class defines methods(functions) and members(variables) that are included in object. Class is the blueprint used to create object. Memory is not allocated, when class is created. Class is user-defined which can be used to defined methods & functions and declare objects.

10. Difference between class and object?

  • Object is instance of class
  • Class does not stored any information while object stores it.
  • Class can have sub-classes but object cannot.

11. What are Java primitive Data types?

Variable in Java must be declared before using them. There are two types declaring variables : Reference(reference to object) type and Primitive type(directly contains value). Java supports eight primitive types. They are

  1. Char 16-bit
  2. Byte 8-bit
  3. Short 16-bit
  4. Int 32-bit
  5. Long 64-bit
  6. Float 32-bit
  7. Double 64-bit
  8. Boolean 1-bit

12. Does Java Supports pointers?

No, Java does not support pointers though Java has reference(stores an address). Pointers create lot of confusion for memory allocation and it has lot of bugs, so Java creators have decided to remove pointers( which is supported by C/C++).

13. What are local and instance variable?

Local variables are accessible only in the method or block they are declared whereas instance variable are accessible in all methods. Local variable can be created inside the method and destroy when the local variable exit the method. Instance variables are declared in class but outside the method, block. When object is created by new keyword, instance variable are created and instance variable are destroyed when object is destroyed.

14. What is the difference between path and class path?

Path is the environmental variable used by the operating system to find executable files(.exe). Classpath is the environmental variable used by the Java Compiler(JVM) to find class files(.class).

15. What is autoboxing and unboxing?

Autoboxing and unboxing was introduced in JDK 5. When primitive data types are converted into it equivalent wrapper type automatically is called autoboxing and reverse operation of autoboxing(i.e. wrapper types to primitive types) is called unboxing.

16. Is constructor overriding possible in java?

No, constructor cannot be overridden, though we can overload constructor.

17. What are the data types supported by Java?

In Java, there are two types of Data types. They are: primitive data types(short, int, byte, long, float etc.) and non-primitive datatypes(class, interface, arrays).

18. What is Abstract Class?

Abstract class in Java cannot be initialized i.e. We cannot create instance of abstract class. Abstract class must have abstract method.

19. Difference between Abstraction and Encapsulation?

Abstraction is the process of hiding implementation whereas encapsulation means hiding data. Abstraction can be achieved by using interface and abstract class, encapsulation by using private.

20. What is inheritance?

In inheritance, one object(or class) inherits(acquires) all the properties and behavior of another object(or class). Inheritance is possible by implementing, extends and implement keywords. E.g. SubClass extends ParentClass.

21. What is Enum in Java?

In Java, enum is a keyword, used to defined fixed number of constants. Enum values are final and cannot be changed once it values is created. E.g. enum WeekDays{ Mon, Tue, Wed, Thur, Fri, Sat, Sun}

22. What is classloaders and what are the different types of class loader?

In Java, Classloaders loads Java Classes into the Java Virtual Machine(JVM) at runtime dynamically, There are types of class loaders: Bootstrap class loaders, Extension class loaders and system class loaders

23. What is the use of super keyword?

Super keyword is used to provide reference to the base class. It is used to access method of base class and also immediate parent class instant variable.

24. What is this keyword?

This keyword is used to access method if current class. It is used to refer current class instance variable. It is also used as reference variable to current class object.

25. Difference between break and continue?

Break statement is used for termination of loop(further execution will stops in the loop) whereas continue statement stops current loop and proceeds with next iteration in the loop.

26. What are marker interface?

Empty interface with no methods and field in Java is called as Marker Interface. Example for marker interface are Searilizable interface, Cloneable interface, Remote interface etc.

27. What is the difference between inner class and nested class?

There are two types of nested class: static nested class and non-static nested class. A nested class declared as static is called as static nested class and non-static nested class are known as inner class.

28. What are access modifier?

Java access modifiers specify(set access) access for classes, variables, methods and constructors. They are four types of access modifiers in Java: private, protected, publc and package(default).

29. Can main() method overload?

Yes, main() method can be overload in class by overloading the main method

30. What is static block and static variable?

  • Static variables are declared by static keyword and it can be used common for all objects. Static Variable gets memory only once during class loading.
  • Static blocks used to initialized static data(static variable) and it is executed before main method during class loading.

31. What is downcasting?

When is superclass is casted to subclass, it is called as down casting.

32. Is null a keyword?

The null value is not a keyword.

33. What is Thread in Java?

Thread is a light weight sub process, it helps to perform different task concurrently. Thread can be implementing by using Runnable Interface(java.lang.Runnable) and by extending thread class(java.lang.Thread).

34. What are the life cycle of thread?

The life cycle of thread are: New, Runnable, Blocked, Waiting, Timed_Waiting and Terminated

35. Why Interface cannot be declared as final?

When we use final, method cannot be overridden, thus if interface in java declared as final, then it can be extended which could defy the point of using interface. So, interface cannot be declared as final.

36. What is package?

Packages are used to group similar(related) classes and interfaces. They also allow name space management and access of those package. They are two types of packages: build-in package and user-defined package.

37. What is the use of garbage collection?

Garbage collection are used for memory managements. When an object is stored in heap memory and no longer used, it will automatically cleaned(memory deallocation) by Garbage collector and thus used by subsequent new objects.

38. What are Applets?

Applets are small interactive internet-based Java programs, which can be run in web browser. Applets are downloaded by the users machine and it can be embed into HTML easily. JVM is required to run Applets.

39. What is Daemon Thread?

Daemon Threads runs in background even when the thread is finished and does not prevent JVM from exiting. Example for Daemon thread is garbage collection.

40. What is the return type of a main method in java?

The main() method in java does not have a return type. It has void type though.