Java Interview Questions

  • 1
    What is Java?

    Java is the high-level, object-oriented, robust, secure programming language, platform-independent, high performance, Multithreaded, and portable programming language. It was developed by James Gosling in June 1991. It can also be known as the platform as it provides its own JRE and API.

  • 2
    Why is Java a platform independent language?

    Java language was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems.

    The only condition to run that byte code is for the machine to have a runtime environment (JRE) installed in it

  • 3
    Why is Java not a pure object oriented language?

    Java supports primitive data types - byte, boolean, char, short, int, float, long, and double and hence it is not a pure object oriented language.

  • 4
    Can java be said to be the complete object-oriented programming language?

    It is not wrong if we claim that java is the complete object-oriented programming language. Because Everything in Java is under the classes. And we can access that by creating the objects.

    But also if we say that java is not a completely object-oriented programming language because it has the support of primitive data types like int, float, char, boolean, double, etc.

    Now for the question: Is java a completely object-oriented programming language? We can say that - Java is not a pure object-oriented programming language, because it has direct access to primitive data types. And these primitive data types don't directly belong to the Integer classes.

  • 5
    What is a Servlet?

    The servlet is a Java programming language class used to process client requests and generate dynamic web content.

    Servlets are mostly used to process or store data submitted by an HTML form, provide dynamic content and manage state information that does not exist in the stateless HTTP protocol.

  • 6
    What is JDBC?

    JDBC is an abstraction layer that allows users to choose between databases. JDBC enables developers to write database applications in Java without having to concern themselves with the underlying details of a particular database.

  • 7
    What is a ClassLoader?

    A classloader in Java is a subsystem of Java Virtual Machine, dedicated to loading class files when a program is executed; ClassLoader is the first to load the executable file.

    Java has Bootstrap, Extension, and Application classloaders.

    Also Read: What is Bootstrap and How to Embed Bootstrap into Angular?

  • 8
    Why Java is platform independent?

    Java is called platform independent because of its byte codes which can run on any system irrespective of its underlying operating system.

  • 9
    What are constructors in Java?

    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:

    • Default Constructor: In Java, a default constructor is the one which does not take any inputs. In other words, default constructors are the no argument constructors which will be created by default in case you no other constructor is defined by the user. Its main purpose is to initialize the instance variables with the default values. Also, it is majorly used for object creation. 
    • Parameterized Constructor: The parameterized constructor in Java, is the constructor which is capable of initializing the instance variables with the provided values. In other words, the constructors which take the arguments are called parameterized constructors.
  • 10
     What is singleton class in Java and how can we make a class singleton?

    Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.

  • 11
     What is the importance of reflection in Java?

    Reflection is a runtime API for inspecting and changing the behavior of methods, classes, and interfaces. Java Reflection is a powerful tool that can be really beneficial. Java Reflection allows you to analyze classes, interfaces, fields, and methods during runtime without knowing what they are called at compile time. Reflection can also be used to create new objects, call methods, and get/set field values. External, user-defined classes can be used by creating instances of extensibility objects with their fully-qualified names. Debuggers can also use reflection to examine private members of classes.

  • 12
    Contiguous memory locations are usually used for storing actual values in an array but not in ArrayList. Explain.

    An array generally contains elements of the primitive data types such as int, float, etc. In such cases, the array directly stores these elements at contiguous memory locations. While an ArrayList does not contain primitive data types. An arrayList contains the reference of the objects at different memory locations instead of the object itself. That is why the objects are not stored at contiguous memory locations.

  • 13
    What are the features of JAVA?

    Features of Java are as follows:

    • OOP concepts
    1. Object-oriented
    2. Inheritance
    3. Encapsulation
    4. Polymorphism
    5. Abstraction
    • Platform independent: A single program works on different platforms without any modification.
    • High Performance: JIT (Just In Time compiler) enables high performance in Java. JIT converts the bytecode into machine language and then JVM starts the execution.
    • Multi-threaded: A flow of execution is known as a Thread. JVM creates a thread which is called the main thread. The user can create multiple threads by extending the thread class or by implementing the Runnable interface.
  • 14
    How does Java enable high performance?

    Java uses Just In Time compiler to enable high performance. It is used to convert the instructions into bytecodes.

  • 15
    What do you mean by Constructor?

    Constructor can be explained in detail with enlisted points:

    • When a new object is created in a program a constructor gets invoked corresponding to the class.
    • The constructor is a method which has the same name as the class name.
    • If a user doesn’t create a constructor implicitly a default constructor will be created.
    • The constructor can be overloaded.
    • If the user created a constructor with a parameter then he should create another constructor explicitly without a parameter.
  • 16
    What are the default values assigned to variables and instances in java?

    • There are no default values assigned to the variables in java. We need to initialize the value before using it. Otherwise, it will throw a compilation error of (Variable might not be initialized). 
    • But for instance, if we create the object, then the default value will be initialized by the default constructor depending on the data type. 
    • If it is a reference, then it will be assigned to null. 
    • If it is numeric, then it will assign to 0.
    • If it is a boolean, then it will be assigned to false. Etc.
  • 17
    What are the Memory Allocations available in Java?

    Java has five significant types of memory allocations.

    • Class Memory
    • Heap Memory
    • Stack Memory
    • Program Counter-Memory
    • Native Method Stack Memory
  • 18
    What are the differences between Heap and Stack Memory in Java?

    Stack memory in data structures is the amount of memory allocated to each individual programme. It is a fixed memory space. Heap memory, in contrast, is the portion that was not assigned to the Java code but will be available for use by the Java code when it is required, which is generally during the program's runtime.

  • 19
    What is the default value stored in Local Variables?

    Neither the Local Variables nor any primitives and Object references have any default value stored in them.

  • 20
    What is an Association?

    An Association can be defined as a relationship that has no ownership over another. For example, a person can be associated with multiple banks, and a bank can be related to various people, but no one can own the other.

  • 21
    What do you mean by aggregation?

    The term aggregation refers to the relationship between two classes best described as a “whole/part” and “has-a” relationship. This kind is the most specialized version of an association relationship. It contains the reference to another class and is said to have ownership of that class.

  • 22
    Define Copy Constructor in Java

    A Copy Constructor in Java is a constructor that initializes an object through another object of the same class.

  • 23
    How is the creation of a String using new() different from that of a literal?

    When we create a string using new(), a new object is created. Whereas, if we create a string using the string literal syntax, it may return an already existing object with the same name.

  • 24
    Explain the term “Double Brace Initialization” in Java?

    Double Brace Initialization is a Java term that refers to the combination of two independent processes. There are two braces used in this. The first brace creates an anonymous inner class. The second brace is an initialization block. When these both are used together, it is known as Double Brace Initialization. The inner class has a reference to the enclosing outer class, generally using the ‘this’ pointer. It is used to do both creation and initialization in a single statement. It is generally used to initialize collections. It reduces the code and also makes it more readable.

  • 25
     Why is it said that the length() method of String class doesn’t return accurate results?

    The length() method of String class doesn’t return accurate results because

    it simply takes into account the number of characters within in the String. In other words, code points outside of the BMP (Basic Multilingual Plane), that is, code points having a value of U+10000 or above, will be ignored.

    The reason for this is historical. One of Java’s original goals was to consider all text as Unicode; yet, Unicode did not define code points outside of the BMP at the time. It was too late to modify char by the time Unicode specified such code points.

  • 26
    Why pointers are not used in Java?

    Java doesn’t use pointers because they are unsafe and increases the complexity of the program. Since, Java is known for its simplicity of code, adding the concept of pointers will be contradicting. Moreover, since JVM is responsible for implicit memory allocation, thus in order to avoid direct access to memory by the user, pointers are discouraged in Java.

  • 27
    What is JIT compiler in Java?

    JIT stands for Just-In-Time compiler in Java. It is a program that helps in converting the Java bytecode into instructions that are sent directly to the processor. By default, the JIT compiler is enabled in Java and is activated whenever a Java method is invoked. The JIT compiler then compiles the bytecode of the invoked method into native machine code, compiling it “just in time” to execute. Once the method has been compiled, the JVM summons the compiled code of that method directly rather than interpreting it. This is why it is often responsible for the performance optimization of Java applications at the run time.

  • 28
    Is delete, next, main, exit or null keyword in java?

    No, these keywords do not exist in Java. Delete, Next, Exit are the operations performed in the Java program, Main is the predefined method, and Null is the default String type.

    With this we are done with the first section that is Basic Java Interview Question, Now, lets move on to our next section of Intermediate Java Interview Questions.

  • 29
    What is the difference between JDK, JRE, and JVM?

    JVM has a Just in Time (JIT) compiler tool that converts all the Java source code into the low-level compatible machine language. Therefore, it runs faster than the regular application.

    JRE has class libraries and other JVM supporting files. But it doesn’t have any tool for java development such as compiler or debugger.

    JDK has tools that are required to write Java Programs and uses JRE to execute them. It has a compiler, Java application launcher, and an applet viewer.

  • 30
    What is a JIT compiler?

    JIT compiler refers to Just in Time compiler. It is the simplest way of executing the computer code that takes in compilation during the execution of a program rather than before performance. It commonly uses bytecode translation to machine code. It is then executed directly.

  • 31
    How to write a basic Hello World program in Java?

    class E3docs
    {
      public static void main(String args[ ]) {
        System.out.println("Hello World");
      }
    }
    
  • 32
    How does Java handle multithreading?

    Java has built-in support for multithreading and provides several ways to create and manage threads. Here are some key points on how Java handles multithreading:

    • Creating threads: In Java, you can create threads either by extending the Thread class or by implementing the Runnable interface.

    • Thread states: Java threads can be in one of six states: New, Runnable, Blocked, Waiting, Timed Waiting, or Terminated.

    • Synchronization: Java provides synchronized blocks and methods to allow safe access to shared resources. This prevents race conditions and other issues that can arise when multiple threads access the same resource simultaneously.

    • Thread communication: Java provides mechanisms like wait(), notify(), and notifyAll() methods to facilitate communication between threads. These methods help to avoid busy waiting and can be used to coordinate the execution of threads.
    • Thread pools: Java provides an Executor framework that allows you to create and manage thread pools. A thread pool is a group of pre-created threads that can be used to execute tasks concurrently.

    • Thread priorities: Java threads have a priority level, which can be used to control the order in which threads are executed. Higher priority threads are executed before lower priority threads.

  • 33
    Define variables in Java and explain with example?

    Variables in Java can be defined as a basic storage unit of a program. It is a storage unit that holds the value during the program execution. Always the variable is assigned with a datatype.

    For Example:

    int a = 10; 
  • 34
    What is Typecasting?

    Typecasting in Java is done explicitly by the programmer; this is done to convert one data type into another data type. 

    Widening (automatically) - conversion of a smaller data type to a larger data type size.  

    byte -> short -> char -> int -> long -> float -> double
    

    Narrowing (manually) - converting a larger type to a smaller size type

    double -> float -> long -> int -> char -> short -> byte
    
  • 35
    What are primitive data types?

    Primitive data types in Java are the major constituents of data manipulation. These are the most basic data types that are available in Java. The primitive data types include int, char, byte, float, double, long, short, and boolean.

  • 36
    What are non-primitive data types?

    The non-primitive data types are something that is different from primitive data types, and these non-primitive data types include String, arrays, and structures.

  • 37
    What is the Unicode system?

    Unicode is a Universal International Standard Character Encoding which is an adequate resource for representing most of the languages written worldwide. 

  • 38
    What is a Servlet?

    The servlet is a Java programming language class used to process client requests and generate dynamic web content.

    Servlets are mostly used to process or store data submitted by an HTML form, provide dynamic content and manage state information that does not exist in the stateless HTTP protocol.

  • 39
    Explain the Meaning of Static in Java?

    • Static stands for one per class, not one for every object no matter how various instances of a class might occur. This entails that you can use them deprived of generating an instance of a class.
    • Static methods are indirectly final since overriding is done grounded on the type of the object and static approaches are involved in a class, not an object.
    • A static technique in a superclass can be followed by an alternative static method in a subclass if the unique method was not acknowledged as final.
    • Though, you can’t overrule a static technique with a non-static technique. In other words, you can’t modify a static technique into an illustration method of a subclass
  • 40
     Why is Java Perceived to be Platform-Independent?

    This is because platform-independent is the term that means “write once run anywhere”. Java is referred to because of its bytecodes that have the capacity to run on any system or device whatsoever irrespective of the underlying operating system.

  • 41
    Is it Possible to Override a private or a Static Method in Java?

    No, there is no provision to override a private or static method in Java. However, you can use method hiding approach in extraordinary cases.

  • 42
    What do you Mean by Association?

    Association refers to a relationship where all the objects of the class have got their own lifecycle and there is no owner as such. These relationships or associations as we call them can be one to one or one to many or many to one or many to many.

  • 43
    What is an abstraction in Java?

    Objects are the building blocks of Object-Oriented Programming. An object contains some properties and methods. We can hide them from the outer world through access modifiers. We can only provide access to the other programs’ required functions and properties. This is the general procedure to implement abstraction in OOPS.

  • 44
     How is Abstraction achieved in Java?

    Abstraction is achieved in Java by the use of abstract classes and abstract methods.

  • 45
    What is API in Java?

    Java application programming interface (API) is a list of all classes that are part of the Java development kit (JDK). It includes all Java packages, classes, and interfaces, along with their methods, fields, and constructors. These pre-written classes provide a tremendous amount of functionality to a programmer.

  • 46
    How to create a package in Java?

    To make a bundle, you pick a name for the bundle (naming shows are talked about in the following area) and put a bundle articulation with that name at the head of each source record that contains the sorts (classes, interfaces, lists, and explanation types) that you need to remember for the bundle.

  • 47
    What is a method in Java?

    A method is a block of code that only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.

  • 48
    What is a string in Java?

    The string is a sequence of characters, for e.g. “Hello” is a string of 5 characters. In java, the string is an immutable object which means it is constant and cannot be changed once it has been created.

  • 49
    What is bytecode in Java

    Bytecode is the compiled format for Java programs. Once a Java program has been converted to bytecode, it can be transferred across a network and executed by Java Virtual Machine (JVM). Bytecode files generally have a .class extension.

  • 50
    What is DAO in Java?

    Dao is a simple java class that contains JDBC logic. The Java Data Access Object (Java DAO) is an important component in business applications. Business applications almost always need access to data from relational or object databases and the Java platform offers many techniques for accessing this data.