Java core interview questions

In this blog post I have created list of core Java interview questions.

JVM

  1. What is JVM?

    JVM - Java Virtual Machine - is an abstract machine which provides the runtime environment in which Java bytecode can be executed. Also it is a specification which specifies the working of Java Virtual Machine.

  2. What is JRE?

    JRE - Java Runtime Environment - is the implementation of JVM.

  3. What is JDK?

    JDK - Java Development Kit. It is a software development environment which is used to develop Java applications. It contains JRE + development tools.

    Read: How to install JDK 8 on Ubuntu.

  4. What types of memory areas are allocated by JVM?

    • Class Area: stores per-class structures such as the runtime constant pool, field, method data, and the code for methods.
    • Heap: contains objects.
    • Stack: stores frames. It holds local variables and partial results, and plays a part in method invocation and return. Each thread has a private JVM stack, created at the same time as the thread. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
    • Program Counter Register: contains the address of the Java virtual machine instruction currently being executed.
    • Native Method Stack: contains all the native methods used in the application.
  5. What is JIT compiler?

    JIT compiler - Just-In-Time compiler - compiles parts of the bytecode that are often executed to native CPU instructions, and hence make program faster.

  6. What is classloader?

    Classloader - subsystem of JVM which loads class files.

    Types:

    • Bootstrap classloader - loads the rt.jar file which contains all class files of Java Standard Edition.

    • Extension classloader - child classloader of Bootstrap classloader. It loads the jar files located inside $JAVA_HOME/jre/lib/ext directory.

    • Application ClassLoader - child classloader of Extension classloader. It loads the class files from the classpath.

Variables

  1. What is the default value of the local variables?

    The local variables are not initialized to any default value.

Classes

  1. What is access specifier?

    It is keyword that restricts access of the field, method or class.

    • public - can be accessed by any classes

    • protected - can be accessed by sub-class, classes from the same package and from the same class.

    • default - has not keyword - can be accessed from classes from the same package and from the same class.

    • private - can be accessed from the same class.

  2. What is the constructor?

    • special type of method that is used to initialize the state of the object;

    • is called when the object is created;

    • name must be similar to the class name;

    • must not have the explicit return type;

    • implicitly returns current instance of class;

    • can not be final;

    Types:

    • default constructor - constructor without parameters. Is invoked when no other constructor defined.

    • parametrized constructor - constructor with parameters.

  3. What are limitations of static methods?

    • static method can’t use non-static data or invoke non-static methods;

    • this and super can’t be used in static methods;

  4. What is class initializer?

    • it is static block of code that is used to initialize static data;
  5. What is instance initializer?

    • instance initializer is block of code that is used to initialize instance data;

    • it is executed after super constructor is executed before instance constructor is executed.

  6. What is order of object initialization?

    First of all class initialization take place. After that object is initialized.

    Read: Object initialization order.

  7. What is this?

    • this - contains final link to current object instance;

    • this - implicitly passed to all non-static methods;

  8. What is the Inheritance?

    • mechanism by which one class acquires all the properties and behavior of another class.

    • inheritance is described as IS-A relationship;

  9. What is aggregation?

    • relationship between two classes where one ob classes has field reference to another class;

    • aggregation is described as HAS-A relationship;

  10. What is enum?

    • special data type that enables for a variable to be a set of predefined constants;

    • can not be instantiated through constructor;

  11. What what is role of static import?

    • imports static members from classes, allowing them to be used without class qualification.

    • example: import static java.util.Arrays.asList;

30.12.2018