10X Sale
kh logo
All Courses

Introduction

Java is a high-level programming language used for creating web applications. It is widely used in a variety of other applications, from mobile applications to desktop software and enterprise applications. It is also commonly used in the development of Android mobile apps. Java's popularity is due to its reliability, scalability, security features, and the large community of developers and extensive libraries available for the language. Our set of Java interview questions and answers will help you prepare your interview from beginner to advanced-level. The questions are from various topics like Java syntax and data types, exception handling, multithreading, Java frameworks and libraries, best practices, and coding conventions. With these Java interview questions, you can appear for the interview more confidently.

Java Interview Questions and Answers
Beginner

1. Is Java fully object-oriented?

Java supports programming in the Object-Oriented paradigm, but it is not fully object-oriented. Java has a set of primitive data types - byte, short, char, int, long, float, double, boolean. Any variable of this type is not an object. That’s why Java is not purely an object-oriented. 

2. What is the purpose of a wrapper class?

This is a frequently asked question in Java interview questions and answers.

The wrapper classes wrap the primitive data types to introduce them as objects. The primitive values are not objects, and the developer needs to write many boilerplate codes to convert them to each other and use them in collections. To overcome these problems, Java introduced wrapper classes. These classes provide with polymorphic APIs for data type conversions and the utility methods like hashCode() and equals(). These make the values very useful members in the object-oriented environment. 

3. What is polymorphism?

Polymorphism is a property of the object-oriented programming paradigm, which denotes that an object or a method can have different forms in different contexts. We can define a method in a class with different implementations based on its arguments in Java. In this way, when the client code calls the method using the same interface with a different set of parameters, internally, it decides which implementation needs to be invoked. Let’s take an example: 

class AreaCalculator { 
  double calculate(Circle c)
     { return 3.14*c.getRadius()*c.getRadius(); 
     } 
double calculate(Square s) 
     {  return s.getLength()*s.getLength();
     } 

//Client code 
AreaCalculator ac = new AreaCalculator(); 
ac.calculate(new Circle(10)); 
ac.calculate(new Square(5)); 

As you see, in the AreaCalculator class, there are separate implementations for calculate(), but from the client’s point of view, the interface is the same. 

4. In how many ways an object can be created in Java?

There are several ways we can create an object in java. 

1. A new object can be created using the new operator on the class calling one of 

its constructors. MyClass o = new MyClass(); 

2. We can create an object using the Java reflection API - Class.newInstance() if the class has a default constructor. If the class has multiple constructors that take parameters, we can get the corresponding constructor using Class.getConstructor() method and invoke that to create a new object. MyClass o = MyClass.class.newInstance(); MyClass o = MyClass.class 

.getConstructor(int.class) .newInstance(10);  

3. We can invoke clone method on an object to create a duplicate object. 

MyClass o = new MyClass(); MyClass b = (MyClass)o.clone(); 

4. If a state of that object is available in a serialized form, we can deserialize it to 

create a new object having the same state. ObjectInputStream is = new ObjectInputStream(anIStream); MyClass o = (MyClass) is.readObject(); 

5. What is runtime polymorphism?

Polymorphism or static polymorphism decides which method needs to be invoked at the time of compilation and bind the method invocation with the call. However, there are some situations where the static binding doesn’t work. As we know, a parent class reference can point to a parent object as well as a child object. Now, if there is a method which exists in both the parent class and the child class with the same signature and we invoke the method from parent class reference, the compiler cannot decide which method to bind with the call. This will depend on which type of object the reference is pointing to at the time of running. If the reference is pointing to a parent object, then the method in the parent class will be invoked. If the pointed object is an instance of the Child class, then the child-class implementation is invoked. That’s why this is called dynamic binding or runtime polymorphism and it is said that the child class method has overridden the parent class method. Let’s take an example of this: class Animal { 

public void makeSound() { 
System.out.println(“My sound varies based on my type”); } } class Dog extends Animal { 
@Override public void makeSound() { 
System.out.println(“I bark”); 
} } Animal a = new Animal(); a.makeSound(); a = new Dog(); a.makeSound(); 

If we run the code snippet, we’ll find that a.makeSound() printing different messages based on the object-type pointed by reference. Please note that the Override annotation is required in the child class to notify the compiler that this method is overriding the parent implementation. 

Want to Know More?
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Description

Java is a high-level programming language used for creating web applications. It runs on many different operating systems, including android. It is still one of the most in-demand programming languages, and there is a high demand for skilled Java developers in various industries, such as finance, healthcare, and e-commerce. To upskill your career, enroll in our Java Course and be a part of this trending career path.

Prepare better with the best java interview questions and answers, and walk away with top interview tips. These java interview questions and answers will boost your core interview skills and help you perform better.

We have compiled a set of frequently asked java interview questions to help you crack your interview with conviction. These java interview questions for experienced as well as freshers roles will enable you to face the toughest of interviews confidently. Prepare yourself well with these Java interview questions and open new doors to your dream job. To learn more about different software programming, join us in our software programming course to upskill yourself.

Recommended Courses

Learners Enrolled For
CTA
Got more questions? We've got answers.
Book Your Free Counselling Session Today.