Top Java Interview Questions For 2022

 



Q1) Why is Java a platform independent language?

Java language was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems.The only condition to run that byte code is for the machine to have a runtime environment (JRE) installed in it.


Q2)What are the differences between C++ and Java?

The differences between C++ and Java are given in the following table.

Java C++
Java is platform-independent. C++ is platform-dependent.
Java is mainly used for application programming. It is widely used in window, web-based, enterprise and mobile applications. C++ is mainly used for system programming.
Java doesn't support multiple inheritance through class. It can be achieved by interfaces in java. C++ supports multiple inheritance.
Java doesn't support operator overloading. C++ supports operator overloading.
Java supports pointer internally. However, you can't write the pointer program in java. It means java has restricted pointer support in Java. C++ supports pointers. You can write pointer program in C++.
Java supports call by value only. There is no call by reference in java. C++ supports both call by value and call by reference.

Q3)What do you understand by an instance variable and a local variable?

Instance variables are those variables that are accessible by all the methods in the class. They are declared outside the methods and inside the class. These variables describe the properties of an object and remain bound to it at any cost.
All the objects of the class will have their copy of the variables for utilization. If any modification is done on these variables, then only that instance will be impacted by it, and all other class instances continue to remain unaffected.

Example:
class Athlete {
public String athleteName;
public double athleteSpeed;
public int athleteAge;
}

Local variables are those variables present within a block, function, or constructor and can be accessed only inside them. The utilization of the variable is restricted to the block scope. Whenever a local variable is declared inside a method, the other class methods don’t have any knowledge about the local variable.

Example:
public void athlete() {
String athleteName;
double athleteSpeed;
int athleteAge;
}

Q4)How to reverse a string in Java?

Code: Using built in reverse() method of the StringBuilder class
import java.lang.*;
import java.io.*;
import java.util.*;
// Class of ReverseString
class ReverseString {
    public static void main(String[] args)
    {
        String input = "AJ Blogs";
        StringBuilder input1 = new StringBuilder();
        // append a string into StringBuilder input1
        input1.append(input);
        // reverse StringBuilder input1
        input1.reverse();
        // print reversed String
        System.out.println(input1);
    }
}

Q5)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 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, sub class or derived class.
Parent Class:
The class whose properties and functionalities are used(inherited) by another class is known as parent class, super class or Base class.

Q6)Pointers are used in C/ C++. Why does Java not make use of pointers?

Pointers are quite complicated and unsafe to use by beginner programmers. Java focuses on code simplicity, and the usage of pointers can make it challenging. Pointer utilization can also cause potential errors. Moreover, security is also compromised if pointers are used because the users can directly access memory with the help of pointers.

Thus, a certain level of abstraction is furnished by not including pointers in Java. Moreover, the usage of pointers can make the procedure of garbage collection quite slow and erroneous. Java makes use of references as these cannot be manipulated, unlike pointers.

Q7)How many types of constructors are used in Java?

Based on the parameters passed in the constructors, there are two types of constructors in Java.

  • Default Constructor: default constructor is the one which does not accept any value. The default constructor is mainly used to initialize the instance variable with the default values. It can also be used for performing some useful task on object creation. A default constructor is invoked implicitly by the compiler if there is no constructor defined in the class.
  • Parameterized Constructor: The parameterized constructor is the one which can initialize the instance variables with the given values. In other words, we can say that the constructors which can accept the arguments are called parameterized constructors.

Q8)What are the differences between the constructors and methods?

There are many differences between constructors and methods. They are given below.

Java Constructor Java Method
A constructor is used to initialize the state of an object. A method is used to expose the behavior of an object.
A constructor must not have a return type. A method must have a return type.
The constructor is invoked implicitly. The method is invoked explicitly.
The Java compiler provides a default constructor if you don't have any constructor in a class. The method is not provided by the compiler in any case.
The constructor name must be same as the class name. The method name may or may not be same as class name.

Q9)What is this keyword in Java?

The this keyword is a reference variable that refers to the current object. There are the various uses of this keyword in Java. It can be used to refer to current class properties such as instance methods, variable, constructors, etc. It can also be passed as an argument into the methods or constructors. It can also be returned from the method as the current class instance.


Q10)What are the main uses of this keyword?

There are the following uses of this keyword.

  • this can be used to refer to the current class instance variable.
  • this can be used to invoke current class method (implicitly)
  • this() can be used to invoke the current class constructor.
  • this can be passed as an argument in the method call.
  • this can be passed as an argument in the constructor call.
  • this can be used to return the current class instance from the method.

Q11)What is super in java?

The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever you create the instance of the subclass, an instance of the parent class is created implicitly which is referred by super reference variable. The super() is called in the class constructor implicitly by the compiler if there is no super or this.

class Animal{
    Animal(){
        System.out.println("animal is created");
     }
 }
class Dog extends Animal{
    Dog(){
        System.out.println("dog is created");
    }
}
class TestSuper4{
    public static void main(String args[]){
        Dog d=new Dog();
    }
}

Q12)What are the main uses of the super keyword?

There are the following uses of super keyword.

  • super can be used to refer to the immediate parent class instance variable.
  • super can be used to invoke the immediate parent class method.
  • super() can be used to invoke immediate parent class constructor.

Q13)What is method overloading?

Method overloading is the polymorphism technique which allows us to create multiple methods with the same name but different signature. We can achieve method overloading in two ways.

  • By Changing the number of arguments
  • By Changing the data type of arguments

Method overloading increases the readability of the program. Method overloading is performed to figure out the program quickly.


    Q14)What is method overriding?

    If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to implement the interface methods.

    • The method must have the same name as in the parent class.
    • The method must have the same signature as in the parent class.
    • Two classes must have an IS-A relationship between them.

    Q15)Difference between method Overloading and Overriding.


    Method Overloading Method Overriding
    Method overloading increases the readability of the program. Method overriding provides the specific implementation of the method that is already provided by its superclass.
    Method overloading occurs within the class. Method overriding occurs in two classes that have IS-A relationship between them.
    In this case, the parameters must be different. In this case, the parameters must be the same.

    More Questions Coming Soon...

AJ Blogs

Hello everyone, My name Arth and I like to write about what I learn. Follow My Website - https://sites.google.com/view/aj-blogs/home

Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

AJ Facebook
Checkout Our Facebook Page
AJ Blogs
Checkout Our Instagram Page