Java Interview Questions: Series 3
27. Why there are no pointers in
Java?
In Java there are references instead of pointers. These references
point to objects in memory. But there is no direct access to these
memory locations. JVM is free to move the objects within VM
memory.
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.
28. If there are no pointers in Java,
then why do we get
NullPointerException?
In Java, the pointer equivalent is Object reference. When we use a .
it points to object reference. So JVM uses pointers but
programmers only see object references.
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.
29. What is the purpose of ‘super’
keyword in java?
‘super’ keyword is used in the methods or constructor of a child
class. It refers to immediate parent class of an object.
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.
30. Is it possible to use this() and
super() both in same constructor?
No, Java does not allow using both super() and this() in same
constructor. As per Java specification, super() or this() must be the
first statement in a constructor.
31. What is the meaning of object
cloning in Java?
Object.clone() method is used for creating an exact copy of the
object in Java. It acts like a copy constructor. It creates and returns
a copy of the object, with the same class and with all the fields
having same values as of the original object.
One disadvantage of cloning is that the return type is an Object. It
has to be explicitly cast to actual type.
Static
32. In Java, why do we use static
variable?
Whenever we want to 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, since it is not defined per object in
Java.
33. Why it is not a good practice to
create static variables in Java?
Static variables are common to all the objects of a class. If a new
object is created, there is no need to test the value of static variable.
Any code that uses static variable can be in any state. It can be
within a new object or at a class level. So the scope of static
variable is open ended in a Java class.
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.
34. What is the purpose of static
method in Java?
Java provides the feature of static method to create behavior at the
class level. The static method is common to all the objects of a
class. We do not need to create any object of a class to call a static
method. So it provides convenience of not creating an object for
calling it.
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.
35. Why do we mark main method as
static in Java?
The main method in Java is marked as static, so that JVM can call it
to start the program. If main method is not static, then which
constructor will be called by Java process?
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.
36. In what scenario do we use a static
block?
At times, there is a class that has static member variables. These
variables need some complicated initialization. At this time static
block helps as a tool to initialize complex static member variable
initialization.
The static block is executed even before the execution of main.
Sometimes, we can also replace static block with a static method of
class.
37. Is it possible to execute a program
without defining a main() method?
No, with Java 7 onwards, you need a main() method to execute a
program. In earlier versions of Java, there was a workaround
available to use static blocks for execution. But now this gap has
been closed.
38. What happens when static
modifier is not mentioned in the
signature of main method?
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.
39. What is the difference between
static method and instance method in
Java?
Often, there is a need to define a behavior for a class that is not
dependent on member variables of an object. Such behavior is
captured in a static method. If there is a behavior 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.
40. What is the other name of Method
Overloading?
Method Overloading is also known as Static Polymorphism.
41. How will you implement method
overloading in Java?
In Java, a class can have multiple methods with same name but
different arguments. It is called Method Overloading. To implement
method overloading we have to create two methods with same name
in a class and do one/more of the following:
1. Different number of parameters
2. Different data type of parameters
3. Different sequence of data type of parameters
42. What kinds of argument
variations are allowed in Method
Overloading?
Method Overloading allows two methods with same name to differ
in:
1. Number of parameters
2. Data type of parameters
3. Sequence of data type of parameters
43. Why it is not possible to do
method overloading by changing
return type of method in java?
If we change the return type of overloaded methods then it will lead
to ambiguous behavior. How will clients know which method will
return what type. Due to this different return type are not allowed in
overloaded methods.
44. Is it allowed to overload main()
method in Java?
Yes, Java allows users to create many methods with same name
‘main’. But only public static void main(String[] args) method is
used for execution.
45. How do we implement method
overriding in Java?
To override a method, we just provide a new implementation of a
method with same name in subclass. So there will be at least two
implementations of the method with same name. One
implementation is in parent class. And another implementation is in
child class.
46. Are we allowed to override a static
method in Java?
No. Java does not allow overriding a static method. If you create a
static method with same name in subclass, then it is a new method,
not an overridden method.
47. Why Java does not allow
overriding a static method?
To override a method, you need an instance of a class. Static method
is not associated with any instance of the class. So the concept of
overriding does not apply here.
Therefore, Java does not allow overriding a static method.
48. Is it allowed to override an
overloaded method?
Yes. You can override an overloaded method in Java.
49. What is the difference between
method overloading and method
overriding in Java?
Differences between method overloading and overriding are:
1. Method overloading is static polymorphism. Method
overriding is runtime polymorphism.
2. Method overloading occurs within the same class. Method
overriding happens in two classes with hierarchy
relationship.
3. Parameters must be different in method overloading.
Parameters must be same in method overriding.
4. Method overloading is a compile time concept. Method
overriding is a runtime concept.
50. Does Java allow virtual functions?
Yes. All instance methods in Java are virtual functions by default.
Only class methods and private instance methods are not virtual
methods in Java.

Comments
Post a Comment