Java Interview Questions

Java Interview Questions

Edit
Crack Your Interview

Core Java Interview Questions and Answers 2022

We are sharing Top Core Java Interview Questions and their Answers. These are the questions frequently asked by the Interviewer. The Interviewer can ask Java questions from any java topic. So we are providing the frequently asked Core java interview questions and answers for experienced & fresher which should be on your to-do list before facing Core java interview questions in a technical round. We wish you crack your interview with the below java interview questions. The given Core java interview questions are asked most of the time. So happy learning and reading core java interview questions to crack your interview.

1.Is Java a pure object-oriented language? If not Why?

Java contains primitive data types e.g. int, float, long, byte, boolean, double, short, and char that is why it is not a pure object-oriented language.

2. What is the difference between Abstract Class and Interface?

                           Abstract Class                                                Interface
Abstract Class can have abstract as well as non-abstract methods. The interface has only abstract methods, Since Java 8, it can have default and static methods also.
Abstract Class can have Constructor inside. The interface does not have a constructor.
Abstract class doesn’t support multiple inheritance. Interface supports multiple inheritance.
A Java abstract class can have class members like private, protected, etc. Members of a Java interface are public by default.
An abstract class can provide the implementation of an interface. Interface can’t provide the implementation of an abstract class.
Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
Example:
public abstract class Ac{
public abstract void print();
}
Example:
public interface In{
void print();
}

3. 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.

4. What are the differences between Heap and Stack Memory in Java?

Stack is generally used to store the order of method execution and local variables. In contrast, Heap memory is used to store the objects. After storing, they use dynamic memory allocation and deallocation.

5. Will the program run if we write static public void main?

Yes, the program will successfully execute if written so. Because, in Java, there is no specific rule for the order of specifiers

6. What is JIT Compiler?

The Just-In-Time compiler is one of the integral parts of the Java Runtime Environment. It is mainly responsible for the performance optimization of Java-based applications at run time or execution time. In general, the main motto of the compiler is increasing the performance of an application for the end-user and the application developer.

7. What is enumeration in Java?

Enumeration means a list of named constants. In Java, enumeration defines a class type. An Enumeration can have constructors, methods, and instance variables. It is created using the enum keyword. Each enumeration constant is public, static, and final by default. Even though enumeration defines a class type and has constructors, you do not instantiate an enum using new. Enumeration variables are used and declared in much the same way as you do a primitive variable.

8. What is inheritance in Java?

The process by which one class acquires the properties(data members) and functionalities(methods) of another class is called inheritance. The aim of inheritance in java is to provide the reusability of code so that a class has to write only the unique features and the rest of the common properties and functionalities can be extended from another class.

Child Class:
The class that extends the features of another class is known as child class, subclass, or derived class.

Parent Class:
The class whose properties and functionalities are used(inherited) by another class is known as a parent class, superclass, or Base class.

9. What is an abstraction in Java?

                                                 Encapsulation in Java

 

 

                                                                                           Encapsulation 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 provide access only for required functions and properties to the other programs. This is the general procedure to implement abstraction in OOPS.

10. What is encapsulation in java?

The idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data members (variable) of other classes.

However, if we set up public getter and setter methods to update (for example void setName which is the String name, and read (e.g. String getName()) the private data fields then the outside class can access those private data fields via public methods.

11. How to sort an array in java?

Below is the way from which you can sort the array.

   import java. util.Arrays; 
   Arrays.sort(NameOfArray);

12. How to check the java version?

Execute java -version on a command prompt/terminal.

13. Can we Overload or Override the static method? 

Overriding: Overriding is related to run-time polymorphism. A subclass (or derived class) provides a specific implementation of a method in the superclass (or base class) at runtime.

Overloading: Overloading is related to compile-time (or static) polymorphism. This feature allows different methods to have the same name, but different signatures, especially the number of input parameters and type of input parameters.

Can we overload static methods?   The answer is ‘Yes’. We can have two or more static methods with the same name, but differences in input parameters

Can we Override static methods in java?  We can declare static methods with the same signature in a subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’. Static methods cannot be overridden because method overriding only occurs in the context of dynamic (i.e. runtime) lookup of methods. Static methods (by their name) are looked up statically (i.e. at compile-time).

14. What is the difference between equals() method and equality operator == in Java?

equals()  ==
This method is used for checking the equality of contents between two objects as per the specified business logic. This operator is used for comparing addresses (or references), i.e checking if both the objects are pointing to the same memory location.
This is a method defined in the Object class.  It is a binary operator in Java.

15What are the Memory Allocations available in Java?

  • Java has five significant types of memory allocations.
    1. Class Memory
    2. Heap Memory
    1. Stack Memory
    2. Program Counter-Memory
    3. Native Method Stack Memory

17. 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.

18.  What are the various access specifiers for Java classes?

In Java, access specifiers are the keywords used before a class name that defines the access scope. The types of access specifiers for classes are:

1. Public: Class, Method, Field is accessible from anywhere.

2. Protected: Method, Field can be accessed from the same class to which they belong or from the sub-classes,and from the class of same package, but not from outside.

3. Default: Method, Field, the class can be accessed only from the same package and not from outside of it’s native package.

4. Private: Method, Field can be accessed from the same class to which they belong.

19. What’s the purpose of Static methods and static variables?

When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keywords to make a method or variable shared for all objects.

20. What is the Final Keyword in Java? Give an example.

In java, a constant is declared using the keyword Final. Value can be assigned only once and after the assignment, the value of a constant can’t be changed.

In the below example, a constant with the name const_val is declared and assigned value:

Private Final int const_val=200

When a method is declared as final, it can NOT be overridden by the subclasses. This method is faster than any other method because they are resolved at the complied time.

When a class is declared as final, it cannot be subclassed. Example String, Integer, and other wrapper classes.

21. Can main() method in Java can return any data?

In java, main() method can’t return any data and hence, it’s always declared with a void return type. e.g. Public static void main.

22. Can we declare a class as Abstract without having any abstract method?

Yes, we can create an abstract class by using abstract keyword before class name even if it doesn’t have any abstract method. However, if a class has even one abstract method, it must be declared as abstract otherwise it will give an error.

23. Which is better in between Interfaces over abstract classes?

Interfaces are slower in performance as compared to abstract classes as extra indirections are required for interfaces. Another key factor for developers to take into consideration is that any class can extend only one abstract class while a class can implement many interfaces.

The use of interfaces also puts an extra burden on the developers as any time an interface is implemented in a class; the developer is forced to implement each and every method of the interface.

24. Can we declare the main method of our class as private?

In java, the main method must be public static in order to run any application correctly. If the main method is declared as private, the developer won’t get any compilation error however, it will not get executed and will give a runtime error.

25. How an object is serialized in java?

In java, to convert an object into a byte stream by serialization, an interface with the name Serializable is implemented by the class. All objects of a class implementing a Serializable interface get serialized and their state is saved in a byte stream.

26. When we should use serialization?

Serialization is used when data needs to be transmitted over the network. Using serialization, the object’s state is saved and converted into a byte stream. The byte stream is transferred over the network and the object is re-created at the destination.

27. When the constructor of a class is invoked?

The constructor of a class is invoked every time an object is created with the new keyword.

For e.g.: In the following class two objects are created using the new keyword and hence, the constructor is invoked two times.

public class constructor_ex {

    constructor_ex() {

        system.out.println("Inside constructor");
    }
    public static void main(String args[]) {

        constructor_ex e1 = new constructor_ex();

        constructor_ex e2 = new constructor_ex();
    }
}core-java-interview-questions