JAVA INTERVIEW QUESTIONS AND ANSWERS
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.
Why Java is platform independent?
Java
is called platform independent because of its byte codes which can run on any
system irrespective of its underlying operating system.
What are constructors
in Java?
In Java, constructor refers to a block of code
which is used to initialize an object. It must have the same name as that of
the class. Also, it has no return type and it is automatically called when an
object is created.
Why pointers are not
used in Java?
Java doesn’t use pointers because they are unsafe
and increases the complexity of the program. Since, Java is known for its
simplicity of code, adding the concept of pointers will be contradicting.
Moreover, since JVM is responsible for implicit memory allocation, thus in
order to avoid direct access to memory by the user, pointers are
discouraged in Java.
What are access
modifiers in Java?
In Java, access modifiers are special keywords
which are used to restrict the access of a class, constructor, data member and
method in another class. Java supports four types of access modifiers:
- Default
- Private
- Protected
- Public
What is an object in
Java and how is it created?
An object is a real-world entity that has a state
and behavior. An object has three characteristics:
- State
- Behavior
- Identity
An object is created using the ‘new’ keyword. For
example:
ClassName obj = new ClassName();
What is constructor
chaining in Java?
In Java, constructor chaining is the process of
calling one constructor from another with respect to the current object.
Constructor chaining is possible only through legacy where a subclass
constructor is responsible for invoking the superclass’ constructor first.
There could be any number of classes in the constructor chain. Constructor
chaining can be achieved in two ways:
- Within the same class using this()
- From base class using super()
Can we execute a program without main() method?
Ans) Yes, one of the ways to execute the program
without the main method is using static block.
What is JIT compiler?
Just-In-Time(JIT) compiler: It is used to
improve the performance. JIT compiles parts of the bytecode that have similar
functionality at the same time, and hence reduces the amount of time needed for
compilation. Here the term “compiler” refers to a translator from the
instruction set of a Java virtual machine (JVM) to the instruction set of a
specific CPU.
What is the platform?
A platform is the hardware or software
environment in which a piece of software is executed. There are two types of
platforms, software-based and hardware-based. Java provides the software-based
platform.
What is an object?
The
Object is the real-time entity having some state and behavior. In Java, Object
is an instance of the class having the instance variables as the state of the object
and the methods as the behavior of the object. The object of a class can be
created by using the new keyword.
Does constructor return any value?
yes, The constructor implicitly returns the
current instance of the class (You can't use an explicit return type with the
constructor).
Can you make a constructor final?
No, the constructor can't be final.
What is the static variable?
The static variable is used to refer to the
common property of all objects (that is not unique for each object), e.g., The
company name of employees, college name of students, etc. Static variable gets
memory only once in the class area at the time of class loading. Using a static
variable makes your program more memory efficient (it saves memory). Static
variable belongs to the class rather than the object.
What is the static method?
- A static method belongs to the class rather than the object.
- There is no need to create the object to call the static
methods.
- A static method can access and change the value of the static
variable.
Why is the main method static?
Because the object is not required to call the
static method. If we make the main method non-static, JVM will have to create
its object first and then call main() method which will lead to the extra
memory allocation.
Can we override the static methods?
No, we can't override static methods.
Can we make the abstract methods static in Java?
In Java, if we make the abstract methods static,
It will become the part of the class, and we can directly call it which is
unnecessary. Calling an undefined method is completely useless therefore it is
not allowed.
What is super in java?
The super
keyword in Java is a reference variable that is used to refer to the immediate
parent class object. Whenever you create the instance of the subclass, an
instance of the parent class is created implicitly which is referred by super
reference variable. The super() is called in the class constructor implicitly
by the compiler if there is no super or this.
Can you use this() and super() both in a constructor?
No, because this() and super() must be the first
statement in the class constructor.
What is method overloading?
Method overloading is the polymorphism technique
which allows us to create multiple methods with the same name but different
signature. We can achieve method overloading in two ways.
- Changing the number of arguments
- Changing the return type
Method overloading increases the readability of
the program. Method overloading is performed to figure out the program quickly.
Can we overload the main() method?
Yes, we can have any number of main methods in a
Java program by using method overloading.
Can we override the static method?
No,
you can't override the static method because they are the part of the class,
not the object.
80) Why can we not override static method?
It
is because the static method is the part of the class, and it is bound with
class whereas instance method is bound with the object, and static gets memory
in class area, and instance gets memory in a heap.
What is the final variable?
In Java, the final variable is used to restrict
the user from updating it. If we initialize the final variable, we can't change
its value. In other words, we can say that the final variable once assigned to
a value, can never be changed after that. The final variable which is not
assigned to any value can only be assigned through the class constructor.
What is the final class?
If we make any class final, we can't inherit it
into any of the subclasses.
Can we declare the main method as final?
Yes, We can declare the main method as public
static final void main(String[] args){}.
Can we declare a constructor as final?
The constructor can never be declared as final
because it is never inherited. Constructors are not ordinary methods;
therefore, there is no sense to declare constructors as final. However, if you
try to do so, The compiler will throw an error.
Java is a platform-independent language.
Why?
Java programming language does not depend on any
particular hardware or software because it is compiled by the compiler and then
converted into byte code. Byte code is platform-independent and can run on
multiple systems. The only requirement is Java needs a runtime environment,
i.e., JRE, which is a set of tools used for developing Java applications.
Explain
Typecasting
The concept of assigning a variable of one data
type to a variable of another data type. It is not possible for the Boolean
data type.
It is of two types:
- Implicit
- Explicit
What do you mean by an
object?
An object consists of methods and classes that
depict its state and perform operations. A Java program contains a lot of
objects instructing each other their jobs. This concept is part of core Java.
What is a class in
Java?
Java encapsulates codes in various classes that
define new data types. These new data types are used to create objects.
What is abstraction in Java?
Abstraction refers to the quality of dealing with
ideas rather than events. It basically deals with hiding the details and
showing the essential things to the user. Thus you can say that abstraction in
Java is the process of hiding the implementation details from the user and
revealing only the functionality to them.
What is the role of JDBC DriverManager
class?
The DriverManager class manages the
registered drivers. It can be used to register and unregister drivers. It
provides factory method that returns the instance of Connection.
What is JDBC Connection interface?
The Connection interface maintains a session with
the database. It can be used for transaction management. It provides factory
methods that returns the instance of Statement, PreparedStatement,
CallableStatement and DatabaseMetaData.
What is Spring?
Wikipedia defines the Spring framework as “an
application framework and inversion of control container for the Java platform.
The framework’s core features can be used by any Java application, but there
are extensions for building web applications on top of the Java EE platform.”
Spring is essentially a lightweight, integrated framework that can be used for
developing enterprise applications in java.
0 Comments