0% found this document useful (0 votes)
11 views

Java Interview questions and answers

Interview questions

Uploaded by

Chaitanya Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java Interview questions and answers

Interview questions

Uploaded by

Chaitanya Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

1.

What are the core OOPs concepts or main principles of object-oriented


programming?

The core concepts of OOPs are


Abstraction: Protecting data of a class from being accessed by members of another class
Encapsulation: Hiding data of class from other classes
Inheritance: Using code written in a class inside other classes
Polymorphism: Using various methods with same name

2. What is Encapsulation in Java?

The process of binding data and corresponding methods (behavior) together into a
single unit is called encapsulation.
3. How to achieve or implement encapsulation in Java?

There are two points thereby we can achieve or implement encapsulation in Java
program.
1. Declaring instance variable of class as private so that it cannot be accessed directly by
anyone from outside the class.
2. Provide the public setter and getter methods in the class to set/modify the value of variable.
4. What is Data hiding in Java? How to achieve it programmatically?

Data hiding is a technique by which we prevent to access data members (variables)


directly from outside the class so that we can achieve security on data.
By declaring data members (variables) as private, we can achieve or implement data hiding.
5. What is tightly encapsulated class in Java?

If each variable is declared as private in the class, it is called tightly encapsulated class
in Java.
6. What would happen if we do not use Encapsulation in a program?

If we don’t use encapsulation in a program, fields will not be private and could be
accessed by anyone from outside the class.
7. What is getter and setter method in Java?

A method that is used to retrieve/get the value of a variable or return the value of the
private member variable is called getter method.
A method that is used for updating or setting the value of a variable is called setter method.
8. Explain inheritance.

The technique of creating a new class by using an existing class functionality is called
inheritance. In other words, inheritance is a process where a child class acquires all the
properties and behaviors of parent class. The existing class is called parent class and the new
class is called child class.
9. What is Is-A relationship in Java?
Is-A relationship represents inheritance. It is implemented using “extends” keyword. It
is used for code reusability. All the classes extends java.lang.object by default. This is a very
good example of Is-A relationship.
10. Which class in Java is super class of every other class?

Object class is the superclass of every other class in Java.


11. How is Inheritance implemented/achieved in Java?

Inheritance can be implemented or achieved by using two keywords:


extends: extends is a keyword that is used for developing the inheritance between two classes
and two interfaces.
implements: implements keyword is used for developing the inheritance between a class and
interface.
12. What are the uses of inheritance?

We use inheritance in java for the following reasons:


a. We can reuse the code from the base class.
b. Using inheritance, we can increase features of class or method by overriding.
c. Inheritance is used to use the existing features of the class.
d. It is used to achieve runtime polymorphism i.e method overriding.

13. What are the advantages of Inheritance?

The advantages of inheritance are as follows:


1. One of the main advantages is that we can minimize the length of the duplicate code in an
application by putting the common code in superclass and sharing amongst several
subclasses.
2. Due to reducing the length of the code, redundancy of the application is also reduced.
14. Why Java does not support multiple inheritance through class?

Multiple inheritance means that one class extends two superclasses or base classes but
in Java, one class cannot extend more than one class simultaneously. At most, one class can
extend only one class.
Therefore, to reduce ambiguity, complexity, and confusion, Java does not support multiple
inheritance through class.
For more detail, go to this tutorial: Types of Inheritance in Java
15. How does multiple inheritance implement in Java?

Multiple inheritance can be implemented in Java by using the interface. A class cannot
extend more than one class but a class can implement more than one interface.
16. What are the uses of super keyword in Java?

super keyword is a reference variable that refers to an immediate superclass object. It is


used for the following purposes:
a. To refer immediate parent class instance variable.
b. To call immediate parent class constructor.
c. To invoke immediate superclass method.
17. Why do we use this keyword?

this keyword is a reference variable that refers to the current class object. It holds the
reference to current class object or same class object.
There are six usages of Java this keyword. They are as follows:
1. this reference can be used to refer to the current class instance variable.
2. this keyword is used to call the non-static method of the current class.
3. this() can be used to invoke the current class constructor.
4. this keyword can be used as a parameter in the method call.
5. The keyword “this” can be used as a parameter in the constructor call.
6. It can also be used to return the object of the current class from the method.
18. Is it possible to use this() and super() both in same constructor?

No, Java does not allow using both super() and this() together in same constructor. As
per Java specification, super() or this() must be the first statement in a constructor.
19. What are the differences between super and this keyword?

Go to this tutorial: Difference between super and this keyword


20. What is method overloading in Java?

When a class has more than one method having the same name but different in
parameters, it is called method overloading in Java.
21. How method overloading is implemented in Java?

Here is the list of rules by which method overloading can be implemented in Java. They
are:
i. The method name must be the same.
ii. Parameters must be different as
a. Data types of parameters
b. Number of parameters
c. Sequence of data type of parameters
iii. Access specifiers can be anything or different.
iv. Return type can be anything or different.
v. Exception thrown can be anything.
22. What are the features of method overloading?

The features of method overloading are as follows:


i. The call to overloaded method is bonded at compile time.
ii. The concept of method overloading is also known as compile-time polymorphism in
java.
iii. Method overloading is generally done in the same class. But it can also be done in the
subclass. We will have to make a relationship between the parent class and child class
by using extends keyword for it.
iv. Method overloading in Java cannot be done by changing the return type of the method
because there may occur ambiguity. But the overloaded methods can change the
return type.
v. The private methods can be overloaded in Java.
vi. The final methods can be overloaded in Java.
vii. The main method can also be overloaded in Java.
viii. Both static and instance methods can be overloaded in Java.

23. When to use method overloading in Java?

Method overloading is used for the following purpose. They are:


i. Method overloading is used when we need to perform same task with different
parameters.
ii. Method overloading is done to reuse the same method name.
iii. It is used to achieve the compile-time polymorphism in Java.

24. Why method overloading is not possible by changing return type of method?

In Java, Method overloading cannot be done when the return type, method name, and
argument list are the same because there may occur ambiguity.

25. Can we overload main() method in Java?

Yes, we can overload the main() method in Java. A class can have any number of
main() methods but the execution always starts from public static void main(String[] args)
only.

26. What is type conversion in Java?

The process of converting a value from one data type to another is known as type
conversion.

27. What is implicit conversion or implicit casting?

When two data types are compatible with each other, Java compiler performs the
conversion automatically or implicitly. This is called implicit casting.

28. What is widening?

The process of converting lower data type into higher data type is called widening in
java.

29. What is explicit type casting or narrowing conversion?

The conversion of a higher data type into a lower data type is called narrowing
conversion.

30. What is class casting in Java?

The process of converting a class type into another class type having relationship
between them through inheritance is called class casting.

31. What is upcasting and downcasting in Java?


When the reference variable of super class refers to the object of sub class, it is known
as upcasting.
When subclass reference refers to super class object, it is called downcasting.
32. What is method overriding in Java?

When the method of superclass is overridden in the subclass to provide more specific
implementation, it is called method overriding.

33. Why we need method overriding in Java program?

We need method overriding due to mainly three reasons. They are:


i. To add a new feature or properties.
ii. To override or change the existing functionality.
iii. To inherit the existing functionality.

34. What are the features of method overriding?

The features of method overriding are as follows:


 Method overriding technique supports the runtime polymorphism.
 It allows a subclass to provide its own implementation of the method which is already
provided by the superclass.
 Only the instance method can be overridden in Java.
 An instance variable can never be overridden in Java.
 The overriding method can not be more restrictive than the overridden method of the
superclass.
 Overriding concept is not applicable for private, final, static, and main method in
Java.
 Method overriding can be done by changing the covariant return type only.
 Overriding method cannot throw any checked exception

35. How do we implement method overriding in Java?

To implement method overriding in a program, we need to follow the following rules.


They are:
o Subclass method name must be the same as superclass method name.
o The parameters of subclass method must be the same as superclass method
parameters. i.e. the method signature must be the same or matched.
o Must be Is-A relationship (Inheritance).
o Subclass method’s access modifier must be the same or higher than superclass method
access modifier.

36. Why can’t private method be overridden?

We cannot override private method because when superclass method is private that will
not be visible to subclass, whatever methods we writing in the subclass will be treated as a
new method but not an overridden method.
37. Can we override static method in Java?

No, we cannot override a static method. We cannot also override a static method with
an instance method because static method is bound with class whereas an instance method is
bound with an object.

38. Can we stop method overriding in Java?

Yes, we can stop method overriding by declaring method as final.

39. What is the use of method overriding?

Method overriding is used to achieve runtime polymorphism. It is used to change the


existing functionality of the superclass method.

40. What are the differences between method overloading and method overriding?

Overloading vs Overriding in Tabular Form

A list of differences between overloading and overriding in Java is given below for quick
revision in tabular form.
SN Property Overloading Overriding

Must be different (at


1 Argument type Must be the same (including order).
least order).

2 Method signatures Must be different. Must be the same.

Must be the same until Java 1.4 version only.


3 Return type Same or different. Java 1.5 onwards, Covariant return type is
allowed.

Generally performed in Performed in two classes through Inheritance


4 Class
the same class. (Is-A relationship).

Private/Static/Final
5 Can be overloaded. Cannot be overridden.
method

Subclass method’s access modifier must be


6 Access modifiers Anything or different. same or higher than superclass method access
modifier.

If child class method throws any checked


exception compulsory parent class method
should throw the same exception is its parent
7 Throws clause Anything
otherwise we will get compile-time error but
there is no restriction for an unchecked
exception.

Always take care by


Always take care by JVM based on runtime
8 Method resolution java compiler based on
object.
reference type.

Also known as
compile-time
Also known as runtime polymorphism,
9 Polymorphism polymorphism, static
dynamic polymorphism, or late binding.
polymorphism, or early
binding.

10 Performance Better Less

41. Is it allowed to override an overloaded method?

Yes, we can override an overloaded method in Java.

42. What is Abstraction in OOPs?

Abstraction is a technique by which we can hide the data that is not required to a user. It
hides all unwanted data so that users can work only with the required data.
43. What is the difference between abstraction and encapsulation?
Abstraction is used to hide the implementation details whereas, encapsulation binds
code and data into a single unit.

44. How to achieve or implement abstraction in Java?

There are two ways by which we can achieve abstraction in java. They are:
o Abstract class (0 to 100%)
o Interface (100%)

45. What is abstract class in Java?

A class that is declared with an abstract keyword is called abstract class. An abstract
class has to be extended and its abstract methods must be implemented by a child class.
46. What is abstract method in Java?

A method which is declared with abstract modifier and has no implementation (means
no body) is called an abstract method. It does not contain any body.
47. Can an abstract class be defined without any abstract methods?

Yes, it is possible. It is basically done to avoid instance creation of the class.


48. Can there be any abstract method without abstract class in Java?

No, if there is any abstract method in a class then class must be declared with abstract
keyword.
49. Is it allowed to declare a method abstract as well as final?

No, because abstract method needs to be overridden by child class whereas, a final
method cannot be overridden. Therefore, a method can be either abstract or final in Java.
50. Can we create an object of abstract class in Java?

No, we cannot create an instance of abstract class in Java. This is because abstract class
is not a concrete class. If you try to instantiate, you will get compile time error.
51. Can we create a reference for an abstract class?

Yes, we can create a reference for an abstract class only when the object being provided
the implementation for the abstract class.
52. When to use abstract class and abstract method in Java?

An abstract method is generally used when the same method has to perform different
tasks depending on the object calling it.
An abstract class is used when we need to share the same method to all non-abstract
subclasses with their own specific implementations.
53. What is interface in Java?

An interface in Java is a mechanism that is used to achieve complete abstraction. It is


basically a kind of class but can have only abstract methods declaration and constants as
members.
54. Is it allowed to declare an interface method as static?

Yes, from Java 8 onwards, we can declare static and default methods in an interface.
Prior to Java 8, it was not allowed.
55. Can an interface be final?

No, because its implementation is provided by another class. A final method cannot be
overridden. Therefore, an interface method cannot be declared as final.
56. What is marker interface?

An interface that has no variable and method is known as marker interface. For
example, serializable, cloneable, remote, etc.
57. What is the difference between abstract class and interface?

58. What is the difference between class and interface?

59. What is the difference between abstract class and class?

a. We cannot create an instance of abstract class whereas, we can create an instance of


class.
b. In abstract class, methods have no body. But in class, methods have body.
60. Why abstract class has constructor even though you cannot create object?

We cannot create an object of abstract class but we can create an object of subclass of
abstract class. When we create an object of subclass of an abstract class, it calls the
constructor of subclass.
This subclass constructor has super in the first line that calls constructor of an abstract class.
Thus, the constructors of an abstract class are used from constructor of its subclass.
If the abstract class doesn’t have constructor, a class that extends that abstract class will not
get compiled.
61. Does Java allow us to declare private and protected modifiers for variables in an
interface?

No, all the variables in an interface are by default public.


62. What is polymorphism in Java?

Polymorphism is a concept by which we can perform a single task in different ways.


There are two types of polymorphism in java. They are: Static polymorphism and Dynamic
polymorphism.
63. Explain static and dynamic polymorphism with example.

A polymorphism that exhibited during compilation is called static polymorphism in


java. In the static polymorphism, the behavior of a method is decided at compile-time.
Therefore, this type of polymorphism is also called compile-time polymorphism.
Compile-time polymorphism can be achieved/implemented by method overloading in java.
Another example of static polymorphism is constructor overloading and method hiding.
A polymorphism that is exhibited at runtime is called dynamic polymorphism in java. In
dynamic polymorphism, the behavior of a method is decided at runtime, therefore, it is called
runtime polymorphism.
Dynamic polymorphism can be achieved/implemented in java using method overriding. An
example of runtime polymorphism is method overriding.
64. What is binding in Java?

The connecting (linking) between a method call and method body/definition is called
binding in java.
65. What is the difference between static binding and dynamic binding?

The difference between static and dynamic binding are as follows:


o Static binding occurs at compile-time while dynamic binding occurs at runtime.
o In static binding, actual object is not used whereas, actual object is used in the
dynamic binding.
o Static binding is resolved at compile time whereas, dynamic binding is resolved at
runtime.
o Static binding is also called late binding because it happens during compilation
whereas, dynamic binding is called early binding because it happens during runtime.
o An example of static binding is method overloading whereas, example of dynamic
binding is method overriding.
o Private, static, and final methods show static binding because they cannot be
overridden whereas, except private, static, and final methods, other methods show
dynamic binding because they can be overridden.

Core Java Interview Questions based on Final Keyword

66. What is final in Java?

Final is a keyword that is used to restrict the user in Java programming. It is a non-
access modifier that can be applied to a variable, method, or class.
67. What is the final keyword in Java?

Java Final keyword has three different uses:


o To create a constant.
o To prevent inheritance.
o To prevent the method from being overridden.
68. Is it allow to change the value of final variable in Java?

No, Java does not allow to change the value of final variable. Once the value is defined,
it cannot be changed.
69. Can a class be declared as final in Java?

Yes, a class can be declared as final in Java. Once a class is declared final, it cannot be
extended.
70. What is blank final variable? Can we initialize the blank final variable?

A variable that is declared as final and not initialized at a time of declaration is known
as a blank final variable.
Yes, if it is non-static then we can declare in the constructor. If it is static blank final variable,
it can be initialized only in the static block.
71. Can we change state of object to which a final reference is pointing?

Yes, we can change the state of an object to which a final reference variable is pointing
but we cannot re-assign a new object to this final reference variable.
72. Can we declare a method as final in Java?

Yes, we can declare a method as final but a final method cannot be overridden by a
child class.
73. How can inheritance be prohibited in Java?

If a class is declared as final, it cannot be extended. This will prevent the inheritance of
that class in Java.
74. Is it allowed to mark main method as final?

Yes, the main method can be marked as final.


75. What is the use of final class in Java?

Three important uses of a final class in Java are as follows:


o To prevent inheritance because the final class cannot be extended.
o To create an immutable class like the predefined String class. We cannot make a class
immutable without making it final.
o A final class is very useful when we want high security in any application.
76. Which class is the most common predefined final class object in Java?

String class

Core Java Interview Questions based on Static


77. What is static in Java?

Static is a keyword that is used for memory management mainly. Static means single
copy storage for variables or methods.
78. Can a class be declared as static?

No, a class cannot be marked as static but an inner class can be static.
79. What is the use of static keyword in Java?

The purpose of using static keyword in Java programming is that we can access the
data, method, or block of the class without any object creation.
80. Is it allowed to change the value of static variable in Java?

Yes, we can change the value of the static variable by using a constructor, or static
block but not inside a static method.
81. What is the difference between static variable and instance variable?

The difference between static variable (class variable) and instance variable are as
follows:
 A static variable is also known as class variable whereas, instance variable is also
known as non-static variable.
 Class variables can be accessed inside the static block, instance block, static method,
instance method, and method of inner class whereas instance variable can be accessed
inside the instance block, instance method, and method of inner class.
 Class variable is always resolved during compile time whereas, instance variable is
resolved during the runtime.
 They are not serialized in Java whereas, instance variables are serialized in Java.

82. Can we use this or super keyword in static method in Java?

In entire core java, this and super keyword is not allowed inside the static method or
static area.
83. Can static method be overloaded in Java?

Yes, a static method can be overloaded in Java but not override it.
84. In Java, why do we use static variable?

If we have a common property for all objects of a class, we use a class-level variable
i.e. a static variable. This variable is loaded in memory only once at the time of class loading.
So, it saves memory.
85. What is the difference between static method and instance method?

The difference between static method (class method) and instance method are as
follows:
 A static method is also known as class method whereas instance method is also
known as non-static method.
 The only static variable can be accessed inside static method whereas, in the instance
method, both static and instance variables can be accessed.
 We do not need to create the object of the class for accessing static method whereas,
in the case of an instance method, we need to create the object for access.
 Class method cannot be overridden whereas, an instance method can be overridden.

86. Why a static method cannot be overridden in Java?

No, we cannot override static method because a static method is resolved at compile
time by Java compiler whereas method overriding is resolved at runtime by JVM because
objects are only available at runtime.
You can declare static methods with the same signature in subclass, but it is not considered as
overriding. It is considered a method hiding.
87. What is the use of static block in Java?

The purpose of using a static block is to write that logic inside static block that is
executed during the class loading. It is also used for changing the default value of static
variable.

Core Java Interview Questions based on Constructor

88. What is the use of constructor in Java?

The constructor is mainly used in a program to assign the default value of instance
variables.
89. What is the use private constructor in Java?

Private constructor is used to preventing other classes from accessing objects of a class.
It can be also used in single tone classes where the object of the class cannot be created
outside the class.
90. What is the difference between constructor and method?

The difference between constructor and method is as follows:


o Constructor is used to initialize the state of an object whereas, method is used to
expose the behavior of an object.
o Constructor has no return type even void also where method has both void and return
type.
o If we don’t define any constructor in the class, Java Compiler provides a default
constructor for that class. Whereas, method is not provided by the compiler in any
case.
o Constructor name must be same as name of the class whereas, method name may or
may not be the same name as the class name.
o The purpose of a constructor is to create an object of a class whereas, the purpose of a
method is to execute the functionality of the application.
o Constructor can not be inherited by subclass whereas method can be inherited in
subclass.
91. Can a constructor be final?

Not, a constructor cannot be final.


92. What is constructor overloading in Java?

Constructor overloading is a technique in Java in which a class can have more than one
constructor that differ in the parameters list.
93. What is constructor chaining in Java?

Constructor chaining is a technique of calling one constructor from another constructor


using this and super keyword. The keyword “this” is used to call a constructor from another
constructor within the same class whereas, keyword “super” is used to call parent (super)
class constructor from child (subclass) class constructor. It occurs through inheritance.
94. Why do we need default constructor in a class?

Default constructor is a no-argument constructor that is automatically generated by


JVM if we do not define any constructor in a class. We need at least one constructor to create
an object. Therefore, Java provides a default constructor.
95. Can we inherit a constructor?

No, Java does not support the inheritance of constructor.

Core Java Interview Questions based on Method

96. Why do we need or use method in Java?

The purpose of using methods in the Java program is to write the logic of the
application which performs some specific task.
97. What is main method in Java?

A main() method is an entry point to start the execution of a program. Every Java
application has at least one class and at least one main method.
98. Why do we need to declare main method as static in Java?

The main method is declared as static. It is called by JVM when we run a class. JVM
does not know how to create an object of a class. It needs a standard way to start the
execution of a program.
Therefore, the main method is declared as static so that JVM can call it using the class name
which is passed on the command line.
99. Can we have more than one main() method in class?
Yes, a class can have any number of main() methods but the execution always starts
from public static void main(String[ ] args) only.
100. What is the difference between argument and parameter in Java?

A parameter is a variable in the definition of a method whereas an argument is an actual


value of this variable that is passed to the method’s parameter.

Core Java Interview Questions based on Access Modifiers

101. What is the use of access modifiers in Java?

Access modifiers is used to restrict the visibility/accessibility of classes, fields,


methods, or constructors.
102. Which access modifiers can be used with a class?

Public and default access modifiers.


103. What is the difference between public and private in Java?

The difference between public and private access modifiers is as follows:


o Public can be applied to a class but private cannot be applied to class.
o Public members can be accessed from anywhere outside class whereas, private
members can be accessed only within the class.
o Public members of a class can be inherited to any subclass whereas, private members
cannot be inherited to subclass.
104. Can we reduce the visibility of inherited or overridden method?

No, we can reduce the visibility of overridden method.


105. What will happen if we make a constructor of class private?

If we make any constructor as a private, we cannot create the object of that class from
another class and also cannot create the subclass of that class.
106. Can we create an object of class if we make a constructor protected?

Yes, if we make constructor as protected then we can create the subclass of that class
within the same package but not outside the package.
107. Which modifiers are applicable to the outer class?

Public, default, final, abstract, and strictfp are five modifiers that can be applied to outer
class.
108. Which modifiers cannot be applied to the method?
Transient and volatile are two modifiers that cannot be applied to the method.
109. Which modifiers are not applicable to variables?

Abstract, synchronized, native, and strictfp are not applicable to variables.


110. Which modifier is applicable to local variable?

The only applicable modifier with local variable is final.


111. Which modifiers are applicable to the constructor?

Only access modifiers are applicable to the constructor. Non-access modifiers cannot be
applied.
112. What are modifiers applicable to the outer interface?

Public, default, abstract, and strictfp are such modifiers that are applicable for outer
interface.
113. What are non-access modifiers in Java?

Abstract, final, native, static, strictfp, synchronized, transient, and volatile are non-
access modifiers in Java.
Core Java Interview Questions based on Class, Object, Data types, and Variables

114. What are the types of primitive data types in Java?

Primitive data types are those data types whose variables can store only one value at a
time. Java defines eight primitive data types: boolean, char, byte, short, int, long, char, float,
and double.
115. What are the non-primitive data types in Java?

Non-primitive data types are created by programmers. These data types are used to
store a group of values or several values. Class, object, string, array, and interface are five
non-primitive data types in Java.
116. What is the default value of the local variable?

Java does not initialize local variable with any default value. Therefore, local variable
will be null by default.
117. What is the difference between an object and object reference?

An object is an instance of a class whereas object reference is a pointer to the object.


There can be many references to the same object.
118. What are the default values of primitive data types?
For boolean data type, it is false. For byte, short, int, and long, it is 0. For float and
double, it is 0.0. For char, the default value is ‘u000’.

You might also like