Top Java Interview Q/A
Top Java Interview Q/A
IDKARTIK.TECH
AV A
P J
T O
V IEW
TER
IN
Q / A
T O P 2 5 0 0 + J A V A IN T E R V I E W T E C H N I CA L
QUESTION ANSWER
IDKARTIK
Top 2500+ Java Interview Technical Question Answer
Top
2500+
Java
Interview
Technical
Question
Answer
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
RISFRUTZZ
This Is a Sample e-book for the reader Containing only the Basic Questions which I think gives an
idea of the quality of content and the main course E-book will released on 7th of May, 2020.
All rights reserved. This book or any portion thereof may not be reproduced or used in any
manner whatsoever without the express written permission of the Author IDKartik except for the
use of brief quotations in a book review or scholarly journal.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Author side:
“This book is dedicated to all those who make the daily sacrifices, Especially those who’ve made
sacrifice, to ensure our freedom & security.”
‘‘... I am just now beginning to discover the difficulty of expressing one’s ideas on paper. As long as it
consists solely of description it is pretty easy; but where reasoning comes into play, to make a proper
connection, a clearness & a moderate fluency, is to me, as I have said, a difficulty of which I had no
idea ...’’
- IDKartik (idkartik.tech)
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Q6. What are the different types of memory areas allocated by JVM?
Ans. In java, JVM allocates memory to different processes, methods and objects. Some of
the memory areas allocated by JVM are:
1. Class Loader: It is a component of JVM used to load class files.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
2. Class (Method) Area: It stores per-class structures such as the runtime constant pool, field
and method data, and the code for methods.
3. Heap: Heap is created a runtime and it contains the runtime data area in which objects
are allocated.
4. Stack: Stack stores local variables and partial results at runtime. It also helps in method
invocation and return value. Each thread creates a private JVM stack at the time of thread
creation.
5. Program Counter Register: This memory area contains the address of the Java virtual
machine instruction that is currently being executed.
6. Native Method Stack: This area is reserved for all the native methods used in the
application.
Q9. Why people say that Java is 'write once and run anywhere' language?
Ans. You can write Java code on Windows and compile it in Windows platform. The class
and jar files that you get from Windows platform can run as it is on Unix environment. So it
is a truly platform independent language.
Behind all this portability is Java byte code. Byte code generated by Java compiler can be
interpreted by any JVM. So it becomes much easier to write programs in Java and expect
those to run on any platform.
Java compiler javac compiles java code and JVM java runs that code.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Q18. Does the order of public and static declaration matter in main method?
Ans. No it doesn't matter but void should always come before main().
Q19. Can a source file contain more than one Class declaration?
Ans. Yes. A single source file can contain any number of Class declarations but only one of
the class can be declared as public.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Q27. Do you think ‘main’ used for main method is a keyword in Java?
Ans. No, main is just a name of method. There can be multiple methods with same name
main in a class file. It is not a keyword in Java.
Q28. Can we write main method as public void static instead of public static void?
Ans. No, you cannot write it like this. Any method has to first specify the modifiers and then
the return value. The order of modifiers can change.
We can write static public void main() instead of public static void main().
Q29. In Java, if we do not specify any value for local variables, then what will be the default
value of the local variables?
Ans. Java does not initialize local variables with any default value. So these variables will be
just null by default.
Q30. What is the difference between byte and char data types in Java?
Ans. Both byte and char are numeric data types in Java. They are used to represent numbers
in a specific range.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Major difference between them is that a byte can store raw binary data where
as a char stores characters or text data.
Usage of char is E.g. char ch = ‘x’;
Byte values range from -128 to 127.
A byte is made of 8 bits. But a char is made of 16 bits. So it is equivalent to 2 bytes.
Q31. What is the difference between Object Oriented Programming language and Object
Based Programming language?
Ans. Object Oriented Programming languages like Java and C++ follow concepts of OOPS
like- Encapsulation, Abstraction, Polymorphism and Inheritance etc.
Object Based Programming languages follow some features of OOPS but they do not
provide support for Polymorphism and Inheritance. Egg. JavaScript, VBScript etc.
Object Based Programming languages provide support for Objects and you can build
objects from constructor. They languages also support Encapsulation. These are also known
as Prototype-oriented languages.
Q32. In Java what is the default value of an object reference defined as an instance variable
in an Object?
Ans. All the instance variable object references in Java are null.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
We need at least one constructor to create an object, that’s why Java provides a default
constructor.
When we have overloaded constructor, then Java assumes that we want some custom
treatment in our code. Due to which it does not provide default constructor. But it needs
default constructor as per the specification. So it gives error.
If we set a method as abstract it means that it has no body and it should be implemented in
a child class. But the constructor is called implicitly when the new keyword is used.
Therefore it needs a body.
If we set a method as static it means that it belongs to the class, but not a particular object.
The constructor is always called to initialize an object. Therefore, there is no use of marking
constructor static.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
The issue with Multiple Inheritance is that both the parent classes may have different
implementation for the same method. So they have different ways of doing the same thing.
Now which implementation should the child class choose?
This leads to ambiguity in Multiple Inheritance. This is the main reason for Java not
supporting Multiple Inheritance in implementation.
Lets say you have a class TV and another class AtomBomb. Both have method switchOn()
but only TV has switchOff() method. If your class inherits from both these classes then you
have an issue that you can switchOn() both parents, but switchOff will only switchOff() TV .
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
E.g. A Library has students. If a Library is destroyed, Students still exist. So Library and
Student are related by Aggregation. A Library has Books. If Library is destroyed, the Books
are also destroyed. Books of a Library cannot exist without the Library. So Book and Library
are related by Composition.
The absence of pointers helps Java in managing memory and garbage collection effectively.
Also it provides developers with convenience of not getting worried about memory
allocation and deallocation.
In case an object reference points to null object, and we try to access a method or member
variable on it, then we get NullPointerException.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
By using ‘super’ we can call a method of parent class from the method of a child class.
We can also call the constructor of a parent class from the constructor of a child class by
using ‘super’ keyword.
One disadvantage of cloning is that the return type is an Object. It has to be explicitly cast to
actual type.
This variable is loaded in memory only once at the time of class loading. So it saves memory,
since it is not defined per object in Java.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
If we want tighter control on scope, then variables should be created at the object creation
level.
Also defining static variables is not a good practice because they go against the principles of
Object Oriented Programming.
Also a static method can access and modify static data members. This also helps in keeping
the behavior as well as state at the class level.
As such it is a known as convention to mark main method static in Java. But if we remove
the static, then there will be ambiguity. Java process may not know which method of a class
to call to start the program.
So this convention helps in Java process to identify the starting code for a program in class
that is passed as an argument to java process.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Sometimes, we can also replace static block with a static method of class.
Q53. What happens when static modifier is not mentioned in the signature of main
method?
Ans. As per Java specification, main method has to be marked as static. It needs only one
argument that is an array of String.
A program can compile with a non-static method. But on execution it will give
NoSuchMethodError.
Q54. What is the difference between static method and instance method in Java?
Ans. Often, there is a need to define a behaviour for a class that is not dependent on
member variables of an object. Such behaviour is captured in a static method. If there is a
behaviour dependent upon the member variables of an object, then we do not mark it
static, it remains as instance method.
To call as static method, we do not need to create an object. We just call it with class name.
But to call an instance method, we need to create/get an object first.
Instance member variables cannot be accessed by a static method. But an instance method
can call both instance variables and static variables.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Q60. Why it is not possible to do method overloading by changing return type of method in
java?
Ans. If we change the return type of overloaded methods then it will lead to ambiguous
behaviour. How will clients know which method will return what type. Due to this different
return type are not allowed in overloaded methods.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Q66. What is the difference between method overloading and method overriding in Java?
Ans. Differences between method overloading and overriding are:
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Let say class B is child of class A. There is a get() method in class A as well as class B. get()
method of class A can return an instance of A, and get() method of class B return an
instance of B. Here class B overrides get() method, but the return type is different.
Before Java 5, any method that overrides the method of parent class would have same
return type.
From Java 5 onwards, a child class can override a method of parent class and the child class
method can return an object that is child of object return by parent class method.
So at compile time it is not known which method will be called at run time.
E.g. Person p = new Person(); p.walk(); // Java compiler resolves this binding at compile
time.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
An abstract class has to be extended in Java and its abstract methods have to be
implemented by a child class. Also Java does not allow new instance of Abstract class.
Q75. Is it allowed to mark a method abstract method without marking the class abstract?
Ans. No. Java specification says that if there is at least one abstract method in a class, the
class has to be marked abstract.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
An abstract method has to be overridden by a child class. And a final method cannot be
overridden. Therefore a method can be either abstract or final in Java.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
• interface contains methods that must be abstract; abstract class may contain
concrete methods.
• interface contains variables that must be static and final; abstract class may contain
non-final and final variables.
• members in an interface are public by default, abstract class may contain non-public
members. interface is used to "implements"; whereas abstract class is used to
"extends".
• interface can be used to achieve multiple inheritance; abstract class can be used as a
single inheritance.
• interface can "extends" another interface, abstract class can "extends" another class
and "implements" multiple interfaces.
• interface is absolutely abstract; abstract class can be invoked if a main() exists.
• interface is more flexible than abstract class because one class can only "extends"
one super class, but "implements" multiple interfaces.
• If given a choice, use interface instead of abstract class.
Q94. What happens if a subclass has inherited a abstract class but has not provided
implementation for all the abstract methods of the super class?
Ans. Then the subclass also becomes abstract. This will be enforced by the compiler.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Q95. What happens if a class has implemented an interface but has not provided
implementation for a method in a interface?
Ans. Its the same as the earlier answer. The class has to be marked as abstract. This will be
enforced by the compiler.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Q102. Why is an Interface able to extend more than one Interface but a Class can't extend
more than one Class?
Ans. Basically Java doesn't allow multiple inheritance, so a Class is restricted to extend only
one Class. But an Interface is a pure abstraction model and doesn't have inheritance
hierarchy like classes (do remember that the base class of all classes is Object). So an
Interface is allowed to extend more than one Interface.
Annotations are more powerful than a Marker interface. They allow programmers to pass
more sophisticated information to classes that "consume" it.
• public : Public class is visible in other packages, field is visible everywhere (class must
be public too)
• private : Private variables or methods may be used only by an instance of the same
class that declares the variable or method, A private feature may only be accessed by
the class that owns the feature.
• protected : Is available to all classes in the same package and also available to all
subclasses of the class that owns the protected feature. This access is provided even
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
to subclasses that reside in a different package from the class that owns the
protected feature.
• default :What you get by default without any access modifier (ie, public private or
protected).It means that it is visible to all within a particular package.
Q108. Does Java allow us to use private and protected modifiers for variables in interfaces?
Ans. No. Always all variables declared inside an interface are of public access.
Q111. How can you change the value of a final variable in Java?
Ans. Java does not allow changing the value of a final variable. Once the value is set, it
cannot be changed.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
A blank final static variable can be initialized in the static block of class.
• Inner classes are not reusable hence defeats one of the Encapsulation feature of
Java.
• Highly confusing and difficult syntax which leads poor code maintainability.
Q121. What is the difference between int and Interger? You can have question like why we
need Wrapper Class(Integer )?
Ans. int is primitive type and Integer is Wrapper Class; if you are using Collection objects like
Hashtable etc. you can't use int as a key. you have to use object so you can use Integer class.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Q123. When you serialize an object, what happens to the object references included in the
object?
Ans. The serialization mechanism generates an object graph for serialization. Thus it
determines whether the included object references are serializable or not. This is a recursive
process. Thus when an object is serialized, all the included objects are also serialized along
with the original obect.
Q124. What is the difference between declaring a variable and defining a variable?
Ans. In declaration we just mention the type of the variable and it's name. We do not
initialize it.
But defining means declaration + initialization.
A Package also provides access protection for classes and interfaces. A package also helps in
removing naming collision.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
It also contains wrapper classes like- Integer, Boolean, Character etc. It provides Math class
for mathematical operations.
Q129. Can you import same package or class twice in your class?
Ans. If we import same package multiple times in a class, compiler includes it only once. So
neither JVM nor Compiler gives any error/warning on including a package multiple times.
If you have two classes with same name, then you may get name collision on importing the
class erroneously.
Q131. What is the difference between import static com.test.Fooclass and import
com.test.Fooclass?
Ans. First import is a static import and the second import is normal import of a class. First
import allows us to import static members of class.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
It helps is following the local conventions of a country, native or region. These conventions
can be for formatting the dates, money, numbers etc.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Deserialization is to convert a stream of bytes back into a copy of the original object.
IDKartik – idkartik.tech
Top 2500+ Java Interview Technical Question Answer
Hey Reader,
IDKartik – idkartik.tech