Java Interview QnA by Talent Battle
Java Interview QnA by Talent Battle
Asked Java
Interview
Questions
w i t h a n s w e r s
Basic Level
Answer:
There are the following differences between the Java platform and
other platforms.
Page no: 1
Get free mentorship
from experts
4. What gives Java its 'write once and run anywhere' nature?
Answer:
The bytecode. Java compiler converts the Java programs into the class file (Byte
Code) which is the intermediate language between source code and machine code.
This bytecode is not platform specific and can be executed on any computer.
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.
Page no: 2
Follow us on Instagram
Page no: 3
Join WhatsApp Group for
Placement
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.
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.
Page no: 4
Get free mentorship
from experts
Answer:
We are already aware of the (==) equals operator. That we have used this to
compare the equality of the values. But when we talk about the terms of
object-oriented programming, we deal with the values in the form of
objects. And this object may contain multiple types of data. So using the
(==) operator does not work in this case. So we need to go with the
.equals() method.
Both [(==) and .equals()] primary functionalities are to compare the values,
but the secondary functionality is different.
Page no: 5
Follow us on Instagram
Intermediate Level
1. Accessing data members of parent class when the member names of the
class and its child subclasses are same.For example - We have used
mathematical functions in the java program like - max(), min(), sqrt(), pow(),
etc. And if we notice that, then we will find that we call it directly with the
class name. Like - Math.max(), Math.min(), etc. So that is a static method. And
Similarly static variables we have used like (length) for the array to get the
length. So that is the static method.
2. Static classes - A class in the java program cannot be static except if it is the
inner class. If it is an inner static class, then it exactly works like other static
members of the class.
3. To call the default and parameterized constructor of the parent class inside
the child class.
4. Accessing the parent class methods when the child classes have overridden
them.
Page no: 6
Join WhatsApp Group for
Placement
Page no: 7
Get free mentorship
from experts
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.
Page no: 8
Follow us on Instagram
24. What are the restrictions that are applied to the Java
static methods?
Answer:
Two main restrictions are applied to the static methods.
The static method cannot use non-static data member or call the non-static
method directly.
this and super cannot be used in static context as they are non-static.
Page no: 10
Get free mentorship
from experts
this is a final variable. Therefore, this cannot be assigned to any new value
whereas the current class object might not be final and can be changed.
this can be used in the synchronized block.
Page no: 11
Follow us on Instagram
Since the compile-time errors are better than runtime errors, Java renders
compile-time error if you inherit 2 classes. So whether you have the same
method or different, there will be a compile time error.
Page no: 13
Get free mentorship
from experts
Page no: 14
Follow us on Instagram
Place
d in A Rohit Borse
ccent
ure
Shrinija
Kalluri 6.5 LP
Placed in
Oracle A
9 LPA Page no: 15
Join WhatsApp Group for
Placement
Mutability: A String is immutable, whereas both the StringBuilder and StringBuffer are
mutable.
Efficiency: It is quite slow to work with a String. However, StringBuilder is the fastest in
performing operations. The speed of a StringBuffer is more than a String and less than a
StringBuilder. (For example appending a character is fastest in StringBuilder and very
slow in String because a new memory is required for the new String with appended
character.)
Variable types: Static and final variables can only be declared in the case of interfaces,
whereas abstract classes can also have non-static and non-final variables.
Data member accessibility: By default, the class data members of interfaces are of the
public- type. Conversely, the class members for an abstract class can be protected or
private also.
Page no: 16
Get free mentorship
from experts
Implementation: For a HashSet, the hash table is utilized for storing the
elements in an unordered manner. However, TreeSet makes use of the red-
black tree to store the elements in a sorted manner.
Methods: hashCode() and equals() are the methods utilized by HashSet for
making comparisons between the objects. Conversely, compareTo() and
compare() methods are utilized by TreeSet to facilitate object comparisons.
Objects type: Heterogeneous and null objects can be stored with the help of
HashSet. In the case of a TreeSet, runtime exception occurs while inserting
heterogeneous objects or null objects
45. Why is the character array preferred over string for storing
confidential information?
Answer:
In Java, a string is basically immutable i.e. it cannot be modified. After its
declaration, it continues to stay in the string pool as long as it is not removed in
the form of garbage. In other words, a string resides in the heap section of the
memory for an unregulated and unspecified time interval after string value
processing is executed.
Multiple-inheritance is not possible in Java. Classes can only extend from one
superclass. In cases where multiple functionalities are required, for example -
to read and write information into the file, the pattern of composition is
preferred. The writer, as well as reader functionalities, can be made use of by
considering them as the private members.
Page no: 19
Get free mentorship
from experts
Consider the University as a class that has some departments in it. So the
university will be the container object. And departments in it will contain objects.
Now in this case, if the container object destroys then the contained objects will
also get destroyed automatically. So here we can say that there is a strong
association between the objects. So this Strong Association is called Composition.
Now consider one more example. Suppose we have a class department and there
are several professors' objects there in the department. Now if the department
class is destroyed then the professor's object will become free to bind with other
objects. Because container objects (Department) only hold the references of
contained objects (Professor’s). So here is the weak association between the
objects. And this weak association is called Aggregation.
Page no: 20
Follow us on Instagram
Moreover, exhaustion of the heap memory takes place if objects are created
in such a manner that they remain in the scope and consume memory. The
developer should make sure to dereference the object after its work is
accomplished. Although the garbage collector endeavors its level best to
reclaim memory as much as possible, memory limits can still be exceeded.
Talent Battle is
associated with TCS iON
for content partnership &
providing internships to
students across India.
Kalluri
Shrinija
Oracle
Placed in
9 LPA
Page no: 21
Join WhatsApp Group for
Placement
53. What is the best way to inject dependency? Also, state the reason.
Answer:
There is no boundation for using a particular dependency injection. But the
recommended approach is -
Setters are mostly recommended for optional dependencies injection, and
constructor arguments are recommended for mandatory ones. This is
because constructor injection enables the injection of values into immutable
fields and enables reading them more easily.
Page no: 22
Get free mentorship
from experts
54. How we can set the spring bean scope. And what supported
scopes does it have?
Answer:
A scope can be set by an annotation such as the @Scope annotation or
the "scope" attribute in an XML configuration file. Spring Bean supports
the following five scopes:
Singleton
Prototype
Request
Session
Global-session
Get TCS iON Internship / TCS NQT paid exam for free
Certificate of Internship from TCS iON
Our Placement Reports
97.6% 4.91 / 5
Selection Ratio Overall Rating
of Complete Masterclass Students out of 5
Our Team
Industry Mentors
@talentbattle.in
@talentbattle_2023
@talentbattle_2024
@talentbattle_2025
@talentbattle_2026
WhatsApp Group
Free Mentorship
Talent Battle Facebook
Talent Battle YouTube
Talent Battle LinkedIn
https://talentbattle.in/