Java is a popular programming language and recruiters are constantly looking for individuals with java programming skills. Through this blog, you’ll learn the top java interview questions that you must learn in 2021.
Top Java Interview Questions:
Features of java
Question: What are the key features of java?
Answer:
- Java is an object-oriented programming language.
- Java is platform-independent and runs on JVM (Java Virtual machine).
- It is simple with just 50 keywords.
- Java is an interpreted and high-performance language.
- It is a dynamic programming language.
Java OOPs
Any java interview OOPs concepts (Object Oriented Programming) is mandatory. No interviewer wants to know the syntax but to know if you are clear with concepts.
So, let’s read this article and understand what basic types of java interview questions ask. Mentioned below are some of the OOPs concepts in Java.
1) Class:
- a) What is Java Class?
Ans: Class is where the objects are created, hence it is called the collection of objects that share common properties.
- b) How do you create a Java class?
Ans: Java class is created using the keyword “class.”
2) Object:
- a) What is a Java object?
Ans: Object is the instance of a class.
Example: A cat is an object because it has states like color, breed, etc., and behavior like meowing, eating, etc.,
- b) How to create an object?
Ans: Method 1: Using default constructor
Ex: Cat cat = new Cat();
Method 2: Using parameterized constructor
Ex: Cat cat = new Cat(white);
3) Inheritance:
- a) What is inheritance?
Ans: When a class acquires all the properties of parent and behavior of parent class. Ex: class A{} //parent class
class B extends A //child class
- b) What is the use of inheritance?
Ans: It provides code reusability.
- c) Which type of inheritance is not supported by java and why?
Ans: Multiple inheritances are not supported in java and the reason for this is it creates ambiguity. Consider an example, there are two classes A And B that have the same method. Now, there is a class C that covers both A and B. When that method is called in class C, it creates ambiguity as to which method to call.
4) Abstraction:
- a) What is abstraction?
Ans: Hiding internal details and showing only relevant functionality to the user.
- b) How to achieve abstraction?
Ans: In Java, abstract classes and interfaces are used to achieve abstraction.
The main difference between abstract classes and interfaces is abstract classes can have both abstract methods and normal methods, whereas interfaces can have only abstract methods.
- c) How can you achieve 100% abstraction?
Ans: 100% abstraction can be achieved using abstract classes.
5) Polymorphism:
- a) What is polymorphism?
Ans: One task performed in different ways.
Ex: A man can be a father, son, husband, etc., but the role he is paying depends on who is calling him.
- b) How many types of polymorphisms?
Ans: Two types of polymorphisms: Run-time polymorphism and Compile-time polymorphism.
- c) How do we achieve polymorphism?
Ans: In Java, we use method overloading and method overriding to achieve polymorphism.
- d) What is method overloading?
Ans: A class that has two or more methods that have the same name but different parameters. It is an example of static or compiles time polymorphism as it happens during compile time. Ex: class Example{
public void func()
{System.out.println(“this is method 1”);}
public void func(String args[])
{System.out.println(“this is method 2”);}
- e) What is the use of method overloading?
Ans: Having few methods with the same name increases the readability of the code.
- f) How can we overload a process in Java?
Ans: Methods in Java can be overloaded either by changing the number of arguments or by changing the data type.
- g) Can we overload a method by changing the return type of the method?
Ans: No, method overworking is not possible by changing the return type.
Java
- h) Can we overload the main() method in Java?
Ans: Yes, the main() method can be overloaded. You can have some main() methods in a class. But JVM calls the main() method first that receives the string array as its arguments.
- g) What is method overriding?
Ans: Child class taking the same method as declared IN parent class, is known as method overriding.
Ex: class A{
void func()
}
Class B extends A{
void func()
}
6) Encapsulation:
- a) What is encapsulation?
Ans: Wrapping internal details is called encapsulation
Ex: A capsule, wrapped with different medicines.
- b) Explain encapsulation?
Ans: Keeping the properties of a class restricted to that class only.
Ex: Properties of class A should remain in this class itself. no other class can access it.
- c) How is encapsulation achieved?
Ans: Using private keywords.
- d) List some examples of encapsulation?
Ans: Java class is an example of encapsulation.
- e) What is fully encapsulated in java?
Ans: Java bean is a fully encapsulated class because all the data members are private here.
Also Read : How to Setup an IT Department
Static keyword:
Many people get confused in interviews when asked about concepts behind static keywords. So, let’s answer some basic questions asked during the Java interview.
1) Why is the static keyword used?
Ans: Static keyword is used to define class-level things. If anything declared as static it is initialized only once in the memory.
Ex: class A{
Static Age;
Age= 20;
}
This variable “Age” is now the same for all classes.
2) Is the main method compulsory in Java?
Ans: This depends on the version of Java you are using. Before JDK5, the main() method was not mandatory.
3) Why was the main method not mandatory before JDK5?
Ans: Since static blocks are executed first before anything. The entire code can be written in a static block and it would still run fine.
4) How to print “HI” without using the main method?
Ans: Using static blocks. But after the JDK5 version of Java, this is not possible and you get a runtime error saying “main method not found in the class.”
5) What are static variables?
Ans: Class level variables are called static variables.
6) Why isn’t the static variable initialized in the main() method?
Ans: Static keyword is used to create variables at the class level. Hence it doesn’t make sense to create it outside the class in the main() method.
7) What is a basic example of a static method?
Ans: main() method is the basic example of a static method.
8) Why do we declare methods static?
Ans: To use some function of a class without creating an instance of that class. Then we declare the method as static.
This brings us to the end of the blog on top of java interview questions. We hope that you are now better equipped to attend the upcoming interview sessions. Happy Learning!