Core Java Interview Questions

By | June 29, 2019

Java Core is an edition of Java also known as Java SE (Standard Edition). The core java is the basic Java and in interviews, most of the Core java Interview questions for a java developer come from here. Here in this article we have mentioned the frequent questions ever asked in the Java Interview. With the increment in the flow of interview questions, difficulty raise too.

Core Java Interview Questions

Now here we have listed a lot of interview questions which can be asked in the interview by the interviewer

Question: What is Java?

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

Read more about Java Programming Languages here.

Question: What is Core Java?

Answer: When Oracle bought Java, they give their own definition to it and release a Java Standard Edition which commonly known as Java. The core Java deals with the basic type and Object of Java Programming. 

Question: What is JVM?

Answer: JVM is an acronym of Java Virtual Machine. JVM is one of the parts of the Java Runtime Environment (JRE). It is the one which calls the Main method of Java Code.

Question: How does a bytecode run in JAVA?

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

Question: What is Thread in Java?

Answer: The flow of execution is referred to as a thread. The main which is used in every java program is also a thread. This main thread is created by JVM. The user can create their own thread by extending the thread class.

Question: Why 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.

Question: What is 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, to identify superclass variables & methods in the subclass, a super keyword is used. It is always written in the subclass only when the method name is the same. It is used to class superclass constructor explicitly.

Question: When a subclass has only parameterized constructor and superclass does not have a matching constructor, What will happen?

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 superclass has parameterized constructor and subclass want to use that constructor then we must write “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.

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

Answer:

Access modifier is also known as Access specifiers. Access modifiers in Java are those clarify the classes which can access a given class and its fields, constructors and methods.

  • Public: A type of access modifier. It is used to specify who can access this method. This means that this method can be accessed by any class.
  • Static: It is a keyword in java which identifies it can be accessed without creating the instance of a Class.
  • Void: Void is a return type of a method that will not return any value.
  • Main: It is referred to as a method that is searched by JVM as a starting point for an application with a particular signature only.
  • String args[]: It is a parameter that is passed to the main method.

Question: What are Constructors in Java?

Answer: In Java, Constructors are the block of codes that are similar to a method. A constructor is called when an instance of an object is created. The name of the constructor is kept the same as the name of the class. There is no return type of a constructor, not even void. If we don’t create an explicit constructor in our class, java compiler supplies it by default.

Question: What do you understand by a Wrapper class?

Answer: A wrapper class is a class that converts java primitives into reference type objects. They “wrap” the primitive data type into an object of that class, therefore they are referred to as wrapper class.

Question: Explain local variable and instance variable?

Answer: Local variables are defined inside methods and constructors. Instance variables are those variables that are present within a class but outside any method.

Question: Why JAVA is a statically typed language?

Answer: Java is referred to as statically typed language as the type of the variable is known at the compile time.

Question: What is Java Runtime Environment(JRE)?

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

Question: What is Package in JAVA?

Answer: Packages in Java is a group of similar kind of classes, subclasses, interfaces. There is two categories of packages: Built-in and user-defined packages. The built-in packages are java, lang, awt, javax, swing, net, io, util, sql etc.

A keyword “package” is used to create a package in Java.

Example:

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

Question: What is the Java language programming platform?

Answer: Java is a programming language and a platform too. A Java platform is an environment for Java programming language on which its application runs.

Question: What are the components of JVM?

Answer: The three main components of Java are JVM, JDK, JRE.

  • JVM is known as Java Virtual Machine. It is required to run a Java application.
  • JDK is known as the Java Development Kit. It is a software development environment that is used for developing Java applications and applets.
  • JRE or Java Runtime Environment is a set of software that is used to create Java applications. JRE is that which combines JVM and JDK Together.

Question: Name all Java Programming language platforms.

Answer:

  • Java Platform Standard Edition
  • Java Platform, Enterprise Edition
  • Java Platform, Micro Edition
  • JavaFX

Question: What are the components that a Java Platform consists?

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

Question: What is Java Virtual Machine?

Answer: It is a program that is designed for specific hardware and software platform so it could run the application based on Java Technology. 

Question: 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. 

Question: What is Java EE?

Answer: Java EE where EE stands for Java Enterprise Edition which is also known as Advance Java, this Java platform is made up on Java Standard Edition platform. This platform is mainly used to create enterprise-level applications because this provides an API and runtime environment for developing and running large-scale applications. 

Question: What is Java ME?

Answer: Java ME or Micro Edition, this platform of Java is used to create micro native applications for small devices like Mobile Phones. This platform uses Java Standard Edition API and libraries to develop applications. 

Question: What is JavaFX?

Answer: This Java platform is used to build and develop Internet-based applications that use lightweight API. 

Question: Give some features of Core Java.

Answer:

  • It is simple basically it follows the similar syntax based on C++ language
  • It is an Object-oriented programming platform, which follows all the concepts of OOP’s.
  • It is platform-independent.
  • It provides security as compared to other programming languages
  • It has a strong Memory management
  • The provides high-quality application development
  • It supports multithreaded which means it can handle many tasks at once.

Read more Features of Java

Question: Name all the OOPS concepts present in Java.

Answer:

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

Question: 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. 

Question: Is Java Virtual machine platform independent?

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

Question: What is abstraction?

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

Question: 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.

Question: What is a Classloader?

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

Question: Name all the different types of Classloader in Java.

Answer:

  • Bootstrap Classloader
  • Extension Classloader
  • System Classloader

Question: What is a class in Java?

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

Question: What is Object?

Answer: An object in Java is a combination of data and procedures and it has states and behaviors.

Question: What is a constructor?

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

Question: Name all the types of the constructor we used in JAVA?

Answer: In Java constructor are of two types:

  • Default constructor
  • Parameterized constructor

Question: What is an Object?

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

Question: What is inheritance?

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

Question: Is String a datatype in JAVA?

Answer: The string is not a primitive data type, while it’s an object of string.lang.string class that gets created when a string is created in Java. We can use all the built-in methods of a string class on the string object.

Question: What is the difference between Array and ArrayList?

Answer:

Array ArrayList
An array is a fixed-length data structure.

Ex:

String[] name = new String[2]

It’s a variable length collection class.

Ex:

ArrayList name = new ArrayList

We need to specify the index to put an object in an array.

Ex:

name[1] = “cat”

No indexing required.

Ex:

name.add(“cat”)

Not type parameterized as ArrayList. It is type parameterized.

Question: Explain Public and Private access specifiers.

Answer: An access specifiers in java is used to restrict the scope of a class, constructor, variable, method or data member.

  • Public Specifiers: They are specified using the public keyword. The public members are visible in the same package as well as the outside package that is for other packages.
  • Private Specifiers: They are specified using the private keyword. Private members can be seen in the same class. They are not for the other classes in the same package as well as classes in the outside of packages.

Question: What is the Java superclass for all classes?

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

Question: Explain Method Overloading and Overriding.

Answer: Method Overloading:

Method overloading is one of the important features of Java which allows a class to have more than one method with the same name and their argument list is different. Method overloading keeps the method name the same.. It depends on a number of parameters and their data types.

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:

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

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();
}
}

Question: What is an interface in Java?

Answer: Java in-built does not support multiple inheritances so in order to overcome this problem we use interface concept in java. 

Question: 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

Question: What is the JIT compiler?

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

Question: Why Java is said to be Platform Independent?

Answer: Java is called platform independent because it’s bytecode can run on any system irrespective to the underlying operating system. But the necessary condition is the presence of JVM.

Question: What makes Java code write once and run anywhere?

Answer: Though Java source code could not run anywhere 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. 

Question: What is an abstract class in Java?

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

Question: What is Heap Memory?

Answer: Heap memory is a space that is used by java runtime to allocate memory to Objects and JRE classes. Whenever an object is created, it’s always created in the Heap space.

Question: What is an abstract method?

Answer: An abstract method is a special method in the abstract class which have the only implantation and does not contain any body declaration. 

Question: Does Java have virtual functions?

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

Question: Does Java provide default values for the local variables?

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

Question: What are the static methods and variables?

Answer: To make any variable and method static 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, its like that we have fixed that variable value. We commonly use static methods and variables for those classes in which we want, all its objects of share the common variable and method. 

Question: What is the difference between an object-oriented programming language and an object-based programming language?

Answer: The Object-oriented programming language support the OOPs concept like Inheritance, polymorphism etc, whereas the object-based programming language does not have OOP’s concept

In Object-Oriented programming, language object are not pre-defined but in Object-based programming language objects are pre-defined

Java, C++, and python are examples of Object-oriented programming language whereas JavaScript and VBScript are Object-Based programming languages.

Question: Why the main method is static?

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

Question: What does this keyword do in Java?

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

Question: 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. 

Question: What are pointers and does Java has pointers?

Answer: A pointer is a user-defined variable that is used to point the memory address of another variable and Java does not pointers.

Question: What is a garbage collector?

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

Question: What are the exceptions?

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

Question: What is Serialization?

Answer: When a file is get converted into byte stream is known as serialization, the serialization is done for the security purpose.

Question: Where does the volatile variable stores?

Answer: In the main memory

Question: What is a final variable in Java?

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

Question: What do you mean by the final class?

Answer: Once we declare a class final it could not be inherited by any other class.

Question: What is dynamic polymorphism?

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

Question: What is the difference between static binding and dynamic binding?

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

Question: When is ParseInt() Method used?

Answer: Sometimes in Java, we need to convert a number represented as a string into an integer type. ParseInt() method is used to execute such type of tasks to convert String into Integer.

Question: What is the purpose of a Volatile Variable?

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

Question: What do you understand by Exception Handling?

Answer: Exception Handling is a mechanism to handle runtime error. It is done to maintain the normal flow of the application.  

Question: 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.

Question: 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. The class which is used for Applet extends is Java.applet.Applet.class.

Question: 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 cannot 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 throws keyword.

Question: Explain Garbage Collection in JAVA.

Answer: Java uses garbage collection to free memory. It is done by removing those objects which are not referenced in the program anymore.

Question: Explain the Multi-threaded Program.

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

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
}

Question: Differentiate between Swing and AWT components.

Answer:

AWT Swing
It is a heavy-weighted component. It contains light-weighted components.
Pluggable look and feel are not supported in AWT. 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.
It is an old framework for creating GUIs. It is a new framework for creating GUIs.

Question: What is Downcasting?

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

Ex.

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);  
}
}

Question: 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, nobody.

Question: Why multiple inheritances are not supported in Java?

Answer: Java does not support multiple inheritances through the class to avoid errors. For multiple inheritances, the interface can be used.  

Question: What is Object Cloning?

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

Question: Can Private Methods be Overridden?

Answer: A private method can’t be overridden. If you’ll create a similar method with the same return type and same method arguments in 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.

Question: What are the use cases? Explain.

Answer: Use cases describe a situation that a program might encounter and what behavior the program should exhibit in that circumstance. It is a part of the analysis of the program.

Question: What is a priority queue?

Answer: Linked list and queue 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”.

Question: What are Ternary Operators? Explain with 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.

Question: How do you differentiate inner and nested class?

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.

Question: What is the difference between Stack and Queue?

Answer:

Stack Queue
In the stack, objects are inserted and removed from the same end. Objects are inserted and removed from the different ends in the queue.
There is only one pointer used in Stack. It points to the top of the stack. In the queue, two different pointers are used for the front and rear end.
Stack follows LIFO (Last in First Out) Queue follows FIFO (First In )
Stack operations are called push and pop. Queue operations are called enqueue and dequeue.

Question: Describe different states of Thread.

Answer: The different states of thread are:

  • New
  • Runnable
  • Running
  • Blocked
  • Terminated

Question: Which type of Exceptions is caught at 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, this is the Checked exceptions that checked at compile time.

Conclusion

In the above article, we have provided the top Core Java interview questions and the core Java is also known as basic Java. Most of the question in the above article based on the OOP’s concepts because that’s what Java is famous for.

You may be also interested in:

Leave a Reply

Your email address will not be published. Required fields are marked *