85+ Top Core Java Interview Questions and Answers

Posted in /   /  

85+ Top Core Java Interview Questions and Answers

Vinay Khatri
Last updated on June 27, 2022

    Also known as Java SE (Standard Edition), Core Java is an edition of Java. The core Java is basic Java. In simple words, core Java is a part of the Java programming language and primarily has its application in the development of general-purpose applications.

    Want to learn Core Java in a simple way? You can buy the course here.

    Thus, in interviews, you can expect many core Java interview questions. If you are planning to appear for a Java interview and looking for the resource, you have landed on the right page.

    Here in this article, we have curated a list of some frequently asked core Java interview questions that may help you ace your upcoming Java interview.

    50 Top Core Java Interview Questions and Answers

    To make it easier for you, we have categorized the set of core Java interview questions into three levels, namely Basic, Intermediate, and Advanced. Let us begin with the basic level.

    Basic Java Interview Questions/Java interview questions for freshers

    1. What is Java?

    Answer: Java is a general-purpose, high-level, object-oriented programming language that works on the principle “write once and run anywhere.” James Gosling developed it in 1991 at Sun Microsystems, and later, Oracle Corporation later bought Sun Microsystems.

    Read more about the Java programming language here .

    2. What is Core Java?

    Answer: When Oracle bought Java, they gave their own definition to it and released the Java Standard Edition, which is commonly known as core Java and is the parent of all the Java versions. It deals with the basic type and Object of Java Programming.

    3. What is the latest version of Java?

    Answer: Java SE 17 is the latest version of Java that comes with Long Term Support (LTS). The next Java version's tentative release dates are

    Java Versions Release Date
    Java SE 18 March 2022
    Java SE 19 September 2022
    Java SE 20 March 2023
    Java SE 21 (LTS) September 2023

    4. What is JVM?

    Answer: JVM is an acronym for Java Virtual Machine . JVM is one of the essential parts of the Java Runtime Environment (JRE). It is the one that calls the Main method of Java code.

    5. How does bytecode run in Java?

    Answer: Bytecode is a compiled format of a Java program. Classloader loads the bytecode into a java runtime environment where the JIT compiler converts bytecode into machine-dependent code.

    6. What is a thread in Java?

    Answer: The flow of execution is referred to as a thread. The main function is present in every java program. Is also a thread. Java Virtual Machine (JVM) is responsible for creating this main thread. The user can create their own thread by extending the thread class.

    7. Why do we declare Main as Static?

    Answer: Main is the entry point of the program; hence it is declared as static. For calling the static method, we never need an object of a class.

    8. What is the super keyword in Java, and where to use it?

    Answer: If superclass and subclass both have the same method as well as the variable name, we use the super keyword to access superclass variables. The super refers to the superclass, and the super keyword can access the superclass variables when the super class has no constructor.

    // super class or Base class 
    class Car{ 
        // Attribute or variable 
        int maxSpeed = 130; 
    }
    
    // sub class or derived class 
    class BMW extends Car{ 
        //attribute of BMW class
        int maxSpeed = 200;
    
        // BMW class Method 
        void show_detail() 
        { 
            //access the variable of superclass using superkeyword
            System.out.println("Super Class Max Speed:"+ super.maxSpeed); 
            System.out.println("\nCurrent Class Max Speed:"+ maxSpeed); 
        } 
    
    } 
    class TechGeekBuzz 
    {
        public static void main(String[] args)
          {
             // object of child, sub or base class
             BMW bmw = new BMW(); 
    
             // call the show_detail of bmw
             bmw.show_detail();
           }
    }

    9. What will happen when a subclass has only parameterized constructor, and a superclass does not have a matching constructor?

    Answer: A default constructor of the superclass will get invoked when a subclass has only parameterized constructor, and the superclass does not have any matching constructor. If the superclass has parameterized constructor and the subclass wants to use that constructor, then we must write the “super” keyword to pass parameters in the superclass constructor. The super keyword must be written in the very first line in the constructor of the subclass.

    10. Explain Public Static Void Main (String args[]).

    Answer: Access modifier is also known as access specifiers. Access modifiers in Java have clarified the classes that can access a given class and its fields, constructors, and methods.

    • Public: A type of access modifier. We can declare a class, method, and variable as public. When we declare a class, method, or variable as public, they are accessible from any class.
    • Static : It is a keyword in Java that identifies a class as static. We can access a static class without creating the instance of the same.
    • Void : Void is a return type of method that will not return any value.
    • Main : It refers to a method that is searched by JVM as the starting point for an application with a particular signature only.
    • String args[]: It is a parameter that is passed to the main method.

    11. What are Constructors in Java?

    Answer: In Java, constructors are similar to methods. A constructor is called when an instance of an object is created. The name of the constructor is the same as the name of the class having it. There is no return type of a constructor, not even void. If we don’t create an explicit constructor in our class, the Java compiler creates it by default.

    12. What do you understand by the wrapper class?

    Answer: A wrapper class is a class that converts Java primitive data types into reference-type objects. It “wraps” the primitive data type into an object of that class, and therefore, we refer to it as a wrapper class.

    13. Explain the local variable and instance variable.

    Answer: Local variables are defined inside methods and constructors. On the other hand, instance variables are those variables that are present within a class but outside any method.

    14. Why is Java a statically typed language?

    Answer: Java is a statically typed language because the type of the variable is known at the compile time.

    15. What is Java Runtime Environment(JRE)?

    Answer: JRE is the abbreviation of Java Runtime Environment. It is an environment in which bytecode runs. In other words, a JRE is a software package that has everything to run a Java program.

    Intermediate Core Java Interview Questions

    16. What is a package in Java?

    Answer: Packages in Java is a group of similar kind of classes, subclasses, and interfaces. There are two categories of packages: built-in and user-defined packages. The built-in packages are java, lang, awt, Java X, swing, net, io, util, SQL, etc. To create a user-defined package in Java, we use the package keyword.

    Example:

    package mypack;  
    public class Simple
    {  
    public static void main(String args[])
    {  
    System.out.println("Welcome to package");  
    }
    }

    17. What is the Java platform?

    Answer: Java is a programming language and a platform too. A Java platform is an environment for running Java applications.

    18. What are the components of Java?

    Answer: The three main components of Java are:

    1. JVM or Java Virtual Machine is required to run a Java application.
    2. JDK stands for Java Development Kit. It is a software development environment that helps to develop Java applications and applets.
    3. JRE or Java Runtime Environment is a set of software that we can use to create Java applications. JRE combines JVM and JDK.

    19. Name all Java platforms.

    Answer: Following are the four Java platforms:

    1. Standard Edition (J2SE)
    2. Enterprise Edition (J2EE)
    3. Micro Edition
    4. JavaFX

    20. What are the components that a Java platform consists of?

    Answer: All Java platforms have two components, Java Virtual Machine (JVM) and Application Programming Interface (API).

    21. What is a Java Virtual Machine (JVM)?

    Answer: A Java Virtual Machine (JVM) is an abstract or virtual machine that allows the system to run Java programs. It is the one that calls the main method present in Java programs. In addition, it is a part of the Java Runtime Environment (JRE).

    22. What is an API?

    Answer: API stands for Application Programming Interface , which is a software component collection, that a Java developer uses to develop other software components and applications.

    23. What is Java EE?

    Answer: Java EE stands for Java Enterprise Edition. Also, we often refer to Java EE as Advance Java. This Java platform is built on top of the Java Standard Edition platform. We can use Java EE for creating enterprise-level applications because it provides an API and runtime environment for developing and running large-scale applications.

    24. What is Java ME?

    Answer: Java ME or Micro Edition is a platform of Java for creating micro native applications for small devices like mobile phones. This platform uses Java Standard Edition API and libraries to develop applications.

    25. What is JavaFX?

    Answer: JavaFX is a Java platform for building and developing Internet-based applications that use a lightweight API.

    26. Give some features of Core Java.

    Answer:

    • It is simple. Basically, it follows a similar syntax based on the C++ language .
    • It is an object-oriented programming platform, which follows all the concepts of OOP’s.
    • Core Java is platform-independent.
    • It provides more security as compared to other programming languages.
    • It has robust memory management.
    • Core Java provides high-quality application development.
    • It supports multithreading, which means it can handle many tasks at once.

    Read more Features of Java .

    27. Name all the OOPS concepts present in Java.

    Answer:

    • Abstraction
    • Encapsulation
    • Polymorphism
    • Inheritance
    • Association
    • Aggregation
    • Composition

    28. Is Java a pure object-oriented programming language?

    Answer: No, Java is not a pure object-oriented programming language because it has primitive data types, which are not objects, and to be a pure object-oriented programming language, everything should be an object in the programming language.

    29. Is Java Virtual Machine platform independent?

    Answer: No, it is not. That’s why we have different JVMs for different platforms.

    30. What is abstraction?

    Answer: Abstraction is a process of hiding the main source code from the user and only showing the functionality. It is the common practice of each object-oriented programming language.

    31. What is the difference between JVM and JRE?

    Answer: Java Runtime Environment (JRE) is a collection of JVM and java binaries. Basically, JRE is the implementation of Java Virtual Machine.

    32. What is a Classloader?

    Answer: A Java Classloader is an inbuilt program in JDK whose primary function is to load the byte code program into memory when a program accesses any class.

    33. Name all the different types of Classloader in Java.

    Answer:

    1. Bootstrap Classloader
    2. Extension Classloader
    3. System Classloader

    34. What is a class in Java?

    Answer: Everything we code in Java is inside a class block. Here, the class is a blueprint of an object and a collection of different attributes and methods.

    35. What is a constructor?

    Answer: A constructor is a method in the class that shares the same name as the class, and it gets invoked automatically at the moment the object of that class gets created. Basically, the primary use of a constructor of a class is to initialize values to the variables.

    36. Name all the types of constructors we use in Java?

    Answer: Java constructors are of two types:

    1. Default constructor
    2. Parameterized constructor

    37. What is an object?

    Answer: When we create an instance of the class, it is known as its object. Basically, at the moment we create the instance or object of a class, that class comes into existence, and with the help of that object or instance, we can access the class attributes and methods.

    38. Explain inheritance.

    Answer: Inheritance is one of the important OOPs concepts that increase code reusability in a programming language. With the help of inheritance, we can use the properties of one class in another class.

    39. Is String a datatype in Java?

    Answer: The string is not a primitive data type in Java. It’s an object under the string.lang.string class. We can use all the built-in methods of a string class on the string object.

    Example

    import java.io.*;
    import java.lang.*;
    
    class Test {
    public static void main(String[] args)
       {
        // Declare a string 
        String s = "TechGeekBuzz";
    
        System.out.println("String = " + s);
       }
    }

    40. What is the difference between Array and ArrayList?

    Answer:

    Array ArrayList
    An array is a fixed-length data structure. Example: String[] name = new String[2] It’s a variable length collection class. Example: ArrayList name = new ArrayList
    We need to specify the index to put an object in an array. Example: name[1] = “cat” ArrayList does not require indexing. Example: name.add(“cat”)
    It is not type parameterized as ArrayList. It is type parameterized.

    41. Explain public and private access specifiers.

    Answer: An access specifier in Java restricts the scope of a class, constructor, variable, method, or data member.

    • Public access specifier: It is specified using the public keyword. Public members are visible for the package having them as well as other packages.
    • Private access specifier: It is specified using the private keyword. Private members can only be seen in the class having them. They are not for the other classes in the same package as well as classes outside of packages.

    42. What is the Java superclass for all classes?

    Answer: java.lang.object is the root class for all Java classes.

    Advanced Java Interview Questions

    43. Explain method overloading and overriding.

    Answer:

    Method Overloading - Method overloading is one of the important features of Java that allows a class to have more than one method with the same name but different arguments.

    Code Example:

    class Adder
    {
    Static int add(int a, int b)
    {
    return a+b;
    }
    Static double add( double a, double b)
    {
    return a+b;
    }
    public static void main(String args[])
    {
    System.out.println(Adder.add(11,11));
    System.out.println(Adder.add(12.3,12.6));
    }
    }

    Method Overriding - It is defined as the definition of the superclass is used by a subclass using the same method signature.

    Code Example:

    class Car
    {
    void run()
    {
    System.out.println(“car is running”);
    }
    Class Audi extends Car
    {
    void run()
    {
    System.out.prinltn(“Audi is running safely with 100km”);
    }
    public static void main( String args[])
    {
    Car b=new Audi();
    b.run();
    }
    }

    44. What is an interface in Java?

    Answer: Java doesn't support multiple inheritance. The workaround for this in Java is an interface.

    45. Name all the types of memory that are allocated by Java Virtual Machine.

    Answer:

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

    46. What is the JIT compiler?

    Answer: JIT or Just-in-Time compiler is a part of Java Runtime Environment, and it optimizes the performance of Java-based applications at run time.

    47. How is Java platform-independent?

    Answer: Java is platform-independent because its bytecode can run on any system irrespective of the underlying operating system and hardware configuration. However, the necessary condition to run Java code is the availability of a JVM.

    48. What makes Java code write once and run anywhere?

    Answer: Java source code for one machine can not run on any other machine. However, when the code gets compiled, it gets converted into byte code (class file), which is a middle code format between source code and machine code, and this byte code is platform-independent.

    49. What is an abstract class in Java?

    Answer: In Java, we can create an abstract class using the abstract keyword before the class name. Also, an abstract class should have at least one abstract method.

    50. What is heap memory?

    Answer: JRE uses heap memory to allocate memory to objects and JRE classes. A Java object is always created in the heap memory space.

    51. What is an abstract method?

    Answer: An abstract method is a special method in the abstract class that only has implementation and does not contain any declaration or body.

    Example

    abstract class Car 
    {    
        //abstract method 
        public abstract void showDetails(); 
    
        public void WheelDetails()
        {    System.out.println("Car has 4 wheels"); } 
    }

    52. Does Java have virtual functions?

    Answer: Yes, apparently, all the Java methods are virtual by default.

    53. Does Java provide default values for the local variables?

    Answer: No, the coder has to initialize the values explicitly.

    54. What are static methods and variables?

    Answer: To make any variable or method static in Java, we use the static keyword, and when we make any variable static, the value of that variable remains the same for all the methods of the class. It is like we have fixed the value of that variable or method. We usually use static methods and variables for classes where we want all of their objects to share the common variable and method.

    55. What is the difference between an object-oriented programming language and an object-based programming language?

    Answer: An object-oriented programming language supports the OOPs concept like inheritance, polymorphism, and so on. In comparison, an object-based programming language does not support OOPs concepts. In object-oriented programming, objects are not pre-defined, but in object-based programming, objects are pre-defined. Java, C++, and Python are examples of object-oriented programming languages, whereas JavaScript and VBScript are instances of object-based programming languages.

    56. Why is the main method static in Java?

    Answer: We do not need an object to call a static method. Though we could make the main() method non-static, in that case, JVM has to create an object to call the main() method which leads to extra memory allocation.

    57. What does this keyword do in Java?

    Answer: We use this keyword to refer to the current object of the class. However, it can also refer to the current properties of the class, such as instances, methods, variables, and constructors. The use of this is ideal when we create trees algorithm and programs as it is used to separate the parent class from the child class.

    58. Can this keyword refer to the static method?

    Answer: Yes, it can, but it does not make any difference because the static value remains the same for all the objects.

    59. What are pointers, and does Java has pointers?

    Answer: A pointer is a user-defined variable that points to the memory address of another variable. In simple terms, it is a variable that stores the memory location of some other variable. Java does not have pointers.

    60. What is a garbage collector?

    Answer: In Java, the interpreter takes care of memory allocation and deallocation of objects in the heap memory. When an object is of no use, the interpreter deallocates the object with the help of a garbage collector. This process is called garbage collection.

    61. What are exceptions?

    Answer: An exception is an unwanted error that occurs during the execution of a program. Exceptions normally occur during the runtime, and to counter these exceptions, we use exception handling.

    62. Explain serialization.

    Answer: Serialization is the process of converting a file into a byte stream. The primary purpose of carrying out serialization is for security purposes.

    63. Where does the volatile variable is stored?

    Answer: A volatile variable is stored in the main memory.

    64. What are the final variables in Java?

    Answer: Final variables are those variables that, once assigned, couldn’t be updated by the user. If there is a final variable that is not yet assigned, then the user can only assign it once with the help of the class constructor.

    65. Please explain the final class.

    Answer: Once we declare a class final, any other class in a program cannot inherit it.

    66. What is dynamic polymorphism?

    Answer: Dynamic polymorphism or runtime polymorphism is a process to call the overridden methods at the run time instead of the compile time.

    67. What is the difference between static binding and dynamic binding?

    Answer: In static binding, the object binds with the method at the compile time, but in dynamic binding, the object binds with methods at the run time.

    68. When is ParseInt() method used?

    Answer: Sometimes, in Java, we need to convert a string into an integer type. The ParseInt() method parses a string argument and returns an integer of the specified radix. A radix is a base in the mathematical number system.

    69. What is the purpose of a volatile variable?

    Answer: The value of the volatile variable is always read from the main memory and not from the thread’s cache memory. The use of a volatile variable is mainly during synchronization. It is applicable only for variables.

    70. What do you understand by exception handling?

    Answer: Exception handling is a mechanism to handle runtime errors. We perform exception handling to maintain the normal flow of the application.

    71. Explain enumeration.

    Answer: It is an interface containing methods for accessing the underlying data structure from which the enumeration is obtained. It allows sequential access to all the elements stored in a collection.

    72. What is Applet? Which class does Applet extend?

    Answer: An applet is a Java program that runs in the browser. It can be a fully-functional Java application as it has the entire Java API. A Java Applet extends the java.applet.Applet class.

    73. Differentiate between Throw and Throws.

    Answer:

    Throw Throws
    As per the word itself, the Throw keyword is used to explicitly throw an exception. It is used to declare an exception.
    A checked exception cannot be propagated using throw only. A checked exception can be propagated using throws.
    It is followed by an instance. It is followed by a class.
    This is not allowed to throw multiple exceptions. You can declare multiple exceptions using the throws keyword.

    74. Explain garbage collection in Java.

    Answer: Java uses garbage collection to free memory. Garbage collection is the process of destructing useless objects in order to free memory. Java automatically performs garbage collection.

    75. Explain a multi-threaded program.

    Answer: A multi-threaded program is a combination of 2 or 3 parts that can run concurrently. These parts are referred to in the thread. Every thread has its own separate paths.

    Code Example:

    public class MultipleThreads implements Runnable
    {
    public static void main (String[] args)
    {
    Runnable r = new runnable ();
    Thread t=new thread ();
    t.start ();//User thread starts here
    Addition add=new addition ();
    }
    public void run()
    {
    go();
    } //User thread ends here
    }

    76. Differentiate between Swing and AWT components.

    Answer:

    AWT Swing
    It is a heavy component. It contains lightweight components.
    AWT doesn't support a pluggable look and feel. It supports a pluggable look and feel.
    AWT programs are not portable. Swing programs are portable.
    It is not based on MVC. It follows MVC.
    AWT is an old framework for creating GUIs. It is a new framework for creating GUIs.

    77. What is downcasting?

    Answer: When a subclass refers to an object of a parent class, then it is known as downcasting.

    Code Example:

    class Animal { }  
    class Dog3 extends Animal
    {  
    static void method(Animal a)
    {  
    if(a instanceof Dog3)
    {
    Dog3 d=(Dog3)a; //downcasting  
    System.out.println("ok downcasting performed");  
       }
    }
    public static void main (String [] args) 
    {
    Animal a=new Dog3();  
    Dog3.method(a);  
    }
    }

    78. What is an interface?

    Answer: An interface is a collection of static constants and abstract methods. The interface is implemented by class thereby, inheriting the abstract methods of the interface. Here, abstract methods mean only method signature.

    79. Why is multiple inheritances not supported in Java?

    Answer: Java does not support multiple inheritances through the class to avoid errors. For multiple inheritances, Java has interfaces.

    80. What is object cloning?

    Answer: Object cloning is a way through which an exact copy of an object can be created. We can use the clone() method of an object class to clone an object.

    81. Can private methods be overridden?

    Answer: A private method can’t be overridden. If you create a similar method with the same return type and same method arguments in the child class, then it will abstract the superclass method. This is known as method hiding. In the same way, you cannot override a private method in a subclass because it’s not accessible there. What you can do is that you can create another private method with the same name in the child class.

    82. What are the use cases? Explain.

    Answer: Use cases describe situations that a program might encounter and what behavior the program should exhibit in that circumstances. These are a part of the analysis of the program.

    83. What is a priority queue?

    Answer: Linked lists and queues are related to each other as the class has been enhanced to implement the queue interface, and queues can be handled using a linked list. The purpose of a queue is “Priority-in, Priority-out”.

    84. What are Ternary Operators? Explain with an example.

    Answer: The Java ternary operator functions as a simplified Java if statement. The ternary operator consists of a condition that evaluates to either true or false and one value that is returned if the condition is true, and another value is returned when the condition is false.

    85. How do you differentiate inner and nested classes?

    Answer: When a class is defined within the scope of another class, then it becomes an inner class. If the access modifier of the inner class is static, then it becomes a nested class.

    86. What is the difference between Stack and Queue?

    Answer:

    Stack Queue
    In the stack, insertion and removal of objects take place from the same end. Insertion and removal of objects take place from different ends in the queue.
    Stack uses only one pointer. It points to the top of the stack. Queue uses two different pointers for the front and rear ends.
    A stack follows LIFO (Last In First Out). A queue follows FIFO (First In First Out).
    Stack operations are called push and pop. Queue operations are called enqueue and dequeue.

    87. Describe different states of a thread.

    Answer: The different states of a thread are:

    • New
    • Runnable
    • Running
    • Blocked
    • Terminated

    88. Which type of exceptions is caught at the compile time?

    Answer: If code within a method throws a checked exception, then the method must either handle the exception or the code must specify the exception using the “Throws” keyword. Therefore, these are the checked exceptions that are checked at the compile time.

    Conclusion

    In the above article, we have provided the commonly asked Core Java interview questions. We often refer to Core Java as basic Java. Most of the questions above are based on the OOP’s concepts because that's what Java is famous for. You can go through this set of Core Java interview questions to polish your knowledge of Java and recollect concepts.

    Also, you can share other frequently asked Core Java interview questions apart from the ones mentioned above in the comments section.

    If you want to get familiar with more Core Java interview questions, you can buy the course here.

    People are also reading:

    Leave a Comment on this Post

    0 Comments