Core Java Interview Questions With Answers

1) What do you understand by Java?

Ans)1. Java is an object-oriented computer language.
2. It is a high-level programming language developed by James Gosling in Sun Microsystem in 1995.
3. Java is a fast, secure and reliable language used for many games, devices and applications.


2) What is difference between JDK,JRE and JVM?

Ans) JVM

  • JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the runtime environment in which java bytecode can be executed. It is a specification.
  • JVMs are available for many hardware and software platforms (so JVM is platform dependent).
  • JRE
  • JRE stands for Java Runtime Environment. It is the implementation of JVM.
  • JDK
  • JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.

  • 3) Outline the major Java features?

    Ans)Features of java are:

  • Object-Oriented – java is based on object-oriented programming where the class and methods describe about the state and behavior of object.
  • Portable – Java program gets converted into Java Byte Codes that can be executed on any platform without any dependency.
  • Platform independent – java works on “write once and run anywhere” as it supports multiple platforms like Windows, Linux, Mac, Sun Solaris, etc.
  • Robust – Java has a strong memory management as there is no pointer allocations. It has automatic garbage collection that prohibits memory leaks.
  • Interpreted – java compiler converts the codes into Java Byte Codes which are then interpreted and executed by Java Interpreter.

  • 4)What do you mean by Object?

    Ans) An object consists of methods and class which depict its state and perform operations. A java program contains a lot of objects instructing each other their jobs. This concept is a part of core java.


    5) What is class in Java?

    Ans) Java encapsulates the codes in various classes which define new data types. These new data types are used to create objects.


    6) Define Inheritance?

    Ans)Java includes the feature of inheritance which an object-oriented programming concept. Inheritance lets a derived class to inherit the methods of a base class.


    7)Explain method overloading?

    Ans) When a Java program contains more than one methods with the same name but different properties, then it is called method overloading.


    8) Compare Overloading and Overriding?

    Ans)Overloading refers to the case of having two methods of same name but different properties, but overriding occurs when there are two methods of same name and properties, but one is in child class and one is in parent class.


    9)Explain the creation of a thread-safe singleton in Java using double-checks locking

    Ans) Singleton is created with double checked locking as before Java 5 acts as an broker and it’s been possible to have multiple instances of Singleton when multiple threads creates an instance of Singleton at the same time. Java 5 made it easy to create thread-safe Singleton using Enum. Using a volatile variable is essential for the same.


    10) How many types of memory areas are allocated by JVM?

    Ans) Many types:

  • Class(Method) Area
  • Heap
  • Stack
  • Program Counter Register
  • Native Method Stack

  • 11)What is JIT compiler?

    Ans) Just-In-Time(JIT) compiler:It is used to improve the performance. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation.Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.


    12) What is platform?

    Ans)A platform is basically the hardware or software environment in which a program runs. There are two types of platforms software-based and hardware-based. Java provides software-based platform.


    13)Why Java is platform independent?

    Ans)Platform independent practically means “write once run anywhere”. Java is called so because of its byte codes which can run on any system irrespective of its underlying operating system..


    14)Why java is not 100% Object-oriented?

    Ans) Java is not 100% Object-oriented because it makes use of eight primitive datatypes such as boolean, byte, char, int, float, double, long, short which are not objects.


    15) What are wrapper classes?

    Ans) Wrapper classes converts the java primitives into the reference types (objects). Every primitive data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive data type into an object of that class. Refer to the below image which displays different primitive type, wrapper class and constructor argument. .


    16) What are constructors in Java?

    Ans) In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created. There are two types of constructors:
    1. Default constructor
    2. Parameterized constructor


    17) What is the main difference between Java platform and other platforms?

    Ans) The Java platform differs from most other platforms in the sense that it's a software-based platform that runs on top of other hardware-based platforms.It has two components:
    1.Runtime Environment
    2.API(Application Programming Interface)


    18) What gives Java its 'write once and run anywhere' nature?

    Ans) The bytecode. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform.


    19) What is classloader?

    Ans) The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.


    20) What is difference between object oriented programming language and object based programming language?

    Ans)Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.


    21) What is constructor?

    Ans) Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.


    22) What is the purpose of default constructor?

    Ans)The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class


    23)Does constructor return any value?

    Ans)yes, that is current instance (You cannot use return type yet it returns a value)


    24)Is constructor inherited?

    Ans) No, constructor is not inherited.


    25)What is static variable?

    Ans)

  • static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
  • static variable gets memory only once in class area at the time of class loading.

  • 26) What is static method?

    Ans)

  • A static method belongs to the class rather than object of a class.
  • A static method can be invoked without the need for creating an instance of a class.
  • static method can access static data member and can change the value of it.

  • 27)Why main method is static?

    Ans)because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation


    28) What is static block?

    Ans)1.Is used to initialize the static data member.
    2.It is excuted before main method at the time of classloading.


    29) Can we execute a program without main() method?

    Ans)Yes, one of the way is static block


    30)What if the static modifier is removed from the signature of the main method?

    Ans) Program compiles. But at runtime throws an error "NoSuchMethodError".


    31)What is this in java?

    Ans) It is a keyword that that refers to the current object


    32) Which class is the superclass for every class?

    Ans) Object class.


    33) Why multiple inheritance is not supported in java?

    Ans) To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class


    34) What is composition?

    Ans) Holding the reference of the other class within some other class is known as composition.


    35) Why Java does not support pointers?

    Ans) Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.


    36) What is super in java?

    Ans) It is a keyword that refers to the immediate parent class object.


    37)Can you use this() and super() both in a constructor?

    Ans)No. Because super() or this() must be the first statement.


    38)What is method overloading?

    Ans) If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program


    39) What is object cloning?

    Ans) The object cloning is used to create the exact copy of an object


    40)What is method overriding:

    Ans) If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the method


    41)Can we overload main() method?

    Ans)Yes, You can have many main() methods in a class by overloading the main method.


    42)Can we override static method?

    Ans) No, you can't override the static method because they are the part of class not object.


    43)What is covariant return type?

    Ans) Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type.


    44) What is final variable?

    Ans)If you make any variable as final, you cannot change the value of final variable(It will be constant)


    45) What is final method?

    Ans) Final methods can't be overriden.


    46)Can you declare the main method as final?

    Ans)Yes, such as, public static final void main(String[] args){}..


    47)What is the difference between static binding and dynamic binding?

    Ans) In case of static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at runtime


    48)What is abstraction?

    Ans) Abstraction is a process of hiding the implementation details and showing only functionality to the user.


    49)Can you use abstract and final both with a method?

    Ans) No, because abstract method needs to be overridden whereas you can't override final method.


    50)Can an Interface be final?

    Ans) No, because its implementation is provided by another class.