Java Interview Question and Answers Part 2
Java Interview Question and Answers
How
Java enabled High Performance?
Java uses Just-In-Time compiler
to enable high performance. Just-In-Time compiler is a program that turns Java
bytecode, which is a program that contains instructions that must be
interpreted into instructions that can be sent directly to the processor.
What kind of variables a class
can consist of?
A class consists of Local
variable, instance variables and class variables.
What is a Local Variable?
Variables defined inside methods,
constructors or blocks are called local variables. The variable will be
declared and initialized within the method and it will be destroyed when the
method has completed.
What is a Instance Variable?
Instance variables are variables
within a class but outside any method. These variables are instantiated when
the class is loaded.
What is a Class Variable?
These are variables declared with
in a class, outside any method, with the static keyword.
What is Singleton class?
Singleton class control object
creation, limiting the number to one but allowing the flexibility to create
more objects if the situation changes.
List the three steps for
creating an Object for a class?
An Object is first declared, then
instantiated and then it is initialized.
When a byte datatype is used?
This data type is used to save
space in large arrays, mainly in place of integers, since a byte is four times
smaller than an int.
What is a static variable?
Class variables also known as
static variables are declared with the static keyword in a class, but outside a
method, constructor or a block.
What is an Exception?
An exception is a problem that
arises during the execution of a program. Exceptions are caught by handlers
positioned along the thread's method invocation stack.
Explain Runtime Exceptions?
It is an exception that occurs
that probably could have been avoided by the programmer. As opposed to checked
exceptions, runtime exceptions are ignored at the time of compilation.
When super keyword is used?
If the method overrides one of its
superclass's methods, overridden method can be invoked through the use of the
keyword super. It can be also used to refer to a hidden field.
What are the OOP’s concepts?
Object-oriented programming
contains many significant features, such as encapsulation, inheritance,
polymorphism and abstraction.
Define Inheritance?
It is the process where one
object acquires the properties of another. With the use of inheritance the
information is made manageable in a hierarchical order.
What is Polymorphism?
Polymorphism is the ability of an
object to take on many forms. The most common use of polymorphism in OOP occurs
when a parent class reference is used to refer to a child class object.
What is Abstraction?
It refers to the ability to make
a class abstract in OOP. It helps to reduce the complexity and also improves
the maintainability of the system.
What is Encapsulation?
It is the technique of making the
fields in a class private and providing access to the fields via public
methods. If a field is declared private, it cannot be accessed by anyone
outside the class, thereby hiding the fields within the class. Therefore
encapsulation is also referred to as data hiding.
What is the primary benefit of
Encapsulation?
The main benefit of encapsulation
is the ability to modify our implemented code without breaking the code of
others who use our code. With this Encapsulation gives maintainability,
flexibility and extensibility to our code.
What is Abstract class?
These classes cannot be instantiated
and are either partially implemented or not at all implemented. This class
contains one or more abstract methods which are simply method declarations
without a body.
When Abstract methods are
used?
If you want a class to contain a particular
method but you want the actual implementation of that method to be determined
by child classes, you can declare the method in the parent class as abstract.
What is the purpose of garbage
collection in Java, and when is it used ?
The purpose of garbage collection
is to identify and discard those objects that are no longer needed by the
application, in order for the resources to be reclaimed and reused.
What does System.gc() and
Runtime.gc() methods do ?
These methods can be used as a
hint to the JVM, in order to start a garbage collection. However, this it is up
to the Java Virtual Machine (JVM) to start the garbage collection immediately
or later in time.
When is the finalize() called
? What is the purpose of finalization ?
The finalize method is called by
the garbage collector, just before releasing the object’s memory. It is
normally advised to release resources held by the object inside the finalize
method.
If an object reference is set
to null, will the Garbage Collector immediately free the memory held by that
object ?
No, the object will be available
for garbage collection in the next cycle of the garbage collector.
When does an Object becomes
eligible for Garbage collection in Java ?
A Java object is subject to
garbage collection when it becomes unreachable to the program in which it is
currently used.
What is the difference between
Exception and Error in java ?
Exception and Error classes are
both subclasses of the Throwable class. The Exception class is used for
exceptional conditions that a user’s program should catch. The Error class
defines exceptions that are not excepted to be caught by the user program.
What will happen to the
Exception object after exception handling?
The Exception object will be
garbage collected in the next garbage collection.
How does finally block differ
from finalize() method ?
A finally block will be executed whether or
not an exception is thrown and is used to release those resources held by the
application. Finalize is a protected method of the Object class, which is
called by the Java Virtual Machine (JVM) just before an object is garbage
collected.
Thanking You !!
Java Interview Question and Answers Part 2
Reviewed by Unknown
on
October 08, 2018
Rating:

No comments: