Blog about Java, Programming, Spring, Hibernate, Interview Questions,Tech News,Programming News

java interview question with company name, for freshers,for experience, java, interview material,spring questions,hibernate questions,Latest Tech News,Latest Programming News,

Subscribe Us

LightBlog
Responsive Ads Here

Translate in Your Own Language | अपनी खुद की भाषा में अनुवाद करें

Monday, March 8, 2021

Top 20 Java Interview Questions For Freshers

 

Top 20 Java Interview Questions For Freshers


java interview question for fresher

1) What is Java?

Java is the high-level, object-oriented, robust, secure programming language, platform-independent, high performance, Multithreaded, and portable programming language. It was developed by James Gosling in June 1991. It can also be known as the platform as it provides its own JRE and API.


2) What is Class and object in java?

 Class:-A Class is like an object constructor, or a "blueprint" for creating objects.

 Object is the real-time entity having some state and behaviour. In Java, Object is an instance of the class having the instance variables as the state of the object and the methods as the behaviour of the object. The object of a class can be created by using the new keyword.


3) What is the constructor?

The constructor can be defined as the special type of method that is used to initialise the state of an object. It is invoked when the class is instantiated, and the memory is allocated for the object. Every time, an object is created using the new keyword, the default constructor of the class is called. The name of the constructor must be similar to the class name. The constructor must not have an explicit return type.


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

4) What is the purpose of a default constructor?

Imp java interview question for fresher

The purpose of the default constructor is to assign the default value to the objects. The java compiler creates a default constructor implicitly if there is no constructor in the class.


5) What is the default value of the local variables?

The local variables are not initialized to any default value, neither primitives nor object references.


6) What are the various access specifiers in Java?

Imp java interview question for fresher

In Java, access specifiers are the keywords which are used to define the access scope of the method, class, or a variable. In Java, there are four access specifiers given below.


  • Public The classes, methods, or variables which are defined as public, can be accessed by any class or method.
  • Protected Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.
  • Default Default are accessible within the package only. By default, all the classes, methods, and variables are of default scope.
  • Private The private class, methods, or variables defined as private can be accessed within the class only.

Access Modifierwithin classwithin packageoutside package by subclass onlyoutside package
PrivateYNNN
DefaultYYNN
ProtectedYYYN
PublicYYYY


7) What is Java OOPs Concepts.

The programming paradigm where everything is represented as an object is known as a truly object-oriented programming language.

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

9) what is Inheritance in java?

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Types of inheritance in Java

  • Single:-When a class inherits another class, it is known as a single inheritance.(For eg:-We have 2 Classes A,B.When class B inherits Class A)
  • Multilevel:-When there is a chain of inheritance, it is known as multilevel inheritance.
  • Hierarchical:-When two or more classes inherits a single class, it is known as hierarchical inheritance.
  • Multiple
  • Hybrid

10) Why multiple inheritance is not supported in java?

Imp java interview question for fresher

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.

Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.

Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error.

11) what is Polymorphism in java?

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. ... Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.

There are two types of polymorphism in Java: 

  • compile-time polymorphism 
  • runtime polymorphism.

 We can perform polymorphism in java by 

  • method overloading 
  • method overriding.


12) what is method overloading in java?

If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.

Advantage of method overloading

Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java

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

13) what is method overriding in java?

Imp java interview question for fresher

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.

In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.

Usage of Java Method Overriding

Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
Method overriding is used for runtime polymorphism

Rules for Java Method Overriding

The method must have the same name as in the parent class
The method must have the same parameter as in the parent class.

13) what is  Encapsulation in java?

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class.

We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.

The Java Bean class is the example of a fully encapsulated class.

14) what is  Abstraction in java?

Data Abstraction is the property by virtue of which only the essential details are displayed to the user.The trivial or the non-essentials units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components.

In java, abstraction is achieved by interfaces and abstract classes. We can achieve 100% abstraction using interfaces.

15) what is  Interfaces in java

      100% Abstraction is Achieved using interface
An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types.

Why we use of interface?

It is used to achieve total abstraction.It is used to achieve total abstraction. Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance . It is also used to achieve loose coupling. Interfaces are used to implement abstraction.

You use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are useful for the following: Capturing similarities among unrelated classes without artificially forcing a class relationship.

16) what is  Abstract Classes in java?

100% Abstraction is not Achieved  using Abstract Classes
     because it has both methods Abstract and Non-Abstract Methods
A class that is declared using “abstract” keyword is known as abstract class. It can have abstract methods(methods without body) as well as concrete methods (regular methods with body). ... An abstract class can not be instantiated, which means you are not allowed to create an object of it.

17) What is the difference between class and abstract class?

A class that is declared using “abstract” keyword is known as abstract class. It can have abstract methods(methods without body) as well as concrete methods (regular methods with body). ... An abstract class can not be instantiated, which means you are not allowed to create an object of it.


18)what is the Difference between final, finally and finalize.

final:-
  • Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final method can't be overridden and final variable value can't be changed.
  • Final is a keyword.

finally:-
  • Finally is used to place important code, it will be executed whether exception is handled or not.
  • Finally is a block.

finalize:-
  • Finalize is used to perform clean up processing just before object is garbage collected.
  • Finalize is a method.

19) What is a singleton class? Give an example

Imp java interview question for fresher

In Java, a singleton class can have only one instance and thus all its variables and methods belong to one instance. The concept of a singleton class is used for the situations while there is a requirement to limit the no of objects for a class.

The good example of singleton usage scenario is while there is a limit of having an only single connection to a database due to some limitations of the driver or any licensing issues.

20) In java why strings are known as Immutable?

Imp java interview question for fresher

String objects are known immutable in java once the value is assigned to a string, it cannot be changed. If it is changed, a new object is created.

No comments: