DAY - 2________________________________________________________JavaITan’s
CORE JAVA INTERVIEW QUESTION’S
DAY - 2
---------*********---------
TEJAS PATIL
JAVA DEVELOPER
PURSUING PG - CDAC, PUNE
1
DAY - 2________________________________________________________JavaITan’s
CONTENTS
1. What is instanceof operator ? .......................................................................................... 4
2. Why copy constructor is not implicitly provided in java? ............................................ 4
3. How many types of inheritance allowed in java ? ......................................................... 4
4. Why “Multiple Inheritance” is not supported in Java? ................................................ 5
5. Explain accessibility modifiers in java........................................................................... 5
6. What is super keyword ?.................................................................................................. 5
7. What is @Override ? what does it do ? .......................................................................... 5
8. What are Overriding rules in java? ................................................................................ 5
9. What is Object class ? ...................................................................................................... 6
10. What is “ClassCastException” ? ................................................................................. 6
11. Where can we apply final keyword in java ? ............................................................. 6
12. Can a class be defined a both final and abstract ........................................................ 6
13. Explain “[Link]”. ..................................................................................... 7
14. Explain “public static void main”. .............................................................................. 7
15. Can we use “this” with static member ? ..................................................................... 7
16. Explain static block . .................................................................................................... 7
17. What is non-static block ? ............................................................................................ 8
18. Can static member function access non-static data ? ................................................ 8
19. Can non-static member function access static data ? ................................................ 8
20. Explain constructor invocation in case of inheritance............................................... 8
21. In case of inheritance , if parent class does not have no-arg constructor, what
happens ? how do u solve the problem ? ............................................................................... 8
22. In case of an array which variable is created automatically ? can u modify it ? if
no , why ? .................................................................................................................................. 8
23. What is class Class in java ?......................................................................................... 9
24. What does “forName() “ function do ? ...................................................................... 9
2
DAY - 2________________________________________________________JavaITan’s
25. When does class get loaded ? ..................................................................................... 10
26. What is bridge method in java ?................................................................................ 10
27. Why compiler synthesizes bridge method in case of overriding ?.......................... 11
28. what are the scenarios in which we have early binding in java ? ........................... 11
3
DAY - 2________________________________________________________JavaITan’s
1. What is instanceof operator ?
Answer:- instanceof operator checks “is-a” relationship. It returns Boolean.
2. Why copy constructor is not implicitly provided in java?
Answer:- in C++, when we say
MyClass m1;
MyClass m2=m1;
It means “m2” object is initialized with “m1”.
i.e. for “m2” copy constructor is called, which is provided by compiler implicitly.
In java above code is written as
MyClass m1=new MyClass();
MyClass m2=m1;
It means that “m2” reference is initialized using “m1” reference, i.e. “m2” refers to
the same object “m1” refers to.
However in java we can write following statement:
MyClass m3=new MyClass(m1);
For the above statement programmer can provide “copy constructor” explicitly.
3. How many types of inheritance allowed in java ?
Answer:- On the basis of class, there can be three types of inheritance: single,
multilevel and hierarchical in java.
In java programming, multiple and hybrid inheritance is supported through interface
only. We will learn about interfaces later.
Multiple inheritance is not supported in java in case of class.
4
DAY - 2________________________________________________________JavaITan’s
4. Why “Multiple Inheritance” is not supported in Java?
Answer:- Multiple inheritance permits you to derive a subclass from more than one
direct superclass. This poses a problem if two direct parent classes have conflicting
implementations from Grand Parent. Now which one to follow in the child class? i.e.
a famous “Diamond Problem”!. This problem ideally can be solved by declaring a
Grand Parent class as “virtual base” class, just to ensure that only one copy of Grand
Parent in child class object. But these all add complications to the language and hence
Multiple Inheritance is not allowed in java.
5. Explain accessibility modifiers in java.
Accessibility Same class Sub class in Different Sub class in Different
modifiers same class in same different class in
package package package different
package
Private Yes No No No no
<default> Yes Yes Yes No no
Protected Yes Yes Yes Yes no
Public Yes Yes Yes Yes yes
6. What is super keyword ?
Answer:- super keyword is used to access parent class member ( member functions as
well as constructors)
7. What is @Override ? what does it do ?
Answer:- @Override is an annotation which instructs compiler that we are overriding
a particular method and then compiler will ensure that u follow all the rules while
overriding especially method name, parameters are correct etc.
8. What are Overriding rules in java?
Answer:- there are 4 rules for overriding in java.
a) arguments must be same otherwise it becomes "overloading".
b) returntype of overriding can be co-variant.
c) overriding method must be having same or more accessibility as compare to
overridden method.
5
DAY - 2________________________________________________________JavaITan’s
d) overriding and checked-exception rule :
i) overriding method may not throw any checked exception.
ii) overriding method can throw same checked exception or its sub-type declared by
overridden method.
iii) overriding method can not throw checked exception not thrown by overridden
method.
9. What is Object class ?
Answer:- Object class is the parent class of all the classes in java.
[Link] is “ClassCastException” ?
Answer:- when u try to cast the classes out of hierarchy, u get ClassCastException.
[Link] can we apply final keyword in java ?
Answer:- final keyword can be applied to
a) instance member
b) class variable
c) local variable
d) member function
e) class
final member must be initialized and once initialized it can not be modified. It is also
known as “compile-time constant”.
final member function can not be overridden.
final class can not be inherited.
[Link] a class be defined a both final and abstract
Answer:- No, because final and abstract are contradictory. i.e. abstract encourages
inheritance whereas final stops inheritance.
6
DAY - 2________________________________________________________JavaITan’s
[Link] “[Link]”.
Answer:-
class System
{
public static PrintStream out;
public static InputStream in;
}
class PrintStream
{
public void print(){}
public void println(){}
}
println() is a not static method of PrintStream class, hence it has to be invoked
using the reference of PrintStream.
out is a reference of PrintStream. i.e. [Link]()
out is a public static member of System class .
that’s why “[Link]()”
[Link] “public static void main”.
Answer:- public - so that main can be accessed from anywhere.
static - so that jvm need not create the object of class in which main
function is defined.
void - main is not expected to return anything.
String args[] - in java main should be always ready to accept command line
arguments.
[Link] we use “this” with static member ?
Answer:- No. “this” and “static” both are contradictory. i.e. this is a reference to
invoking object and static is not at all related to object.
[Link] static block .
Answer:- static block
a) is used to access static members.
b) is called as soon as class gets loaded.
c) u can define more than one static block inside the class. They will get
executed in the order in which they are defined.
7
DAY - 2________________________________________________________JavaITan’s
[Link] is non-static block ?
Answer:- NON-STATIC BLOCK:is used for - if we have many constructor inside a
class and those constructors need to have some common statements.
instead of repeate those statements in each constructor,we place those statements in
non-static block.
e.g counter which is incremented in each constructor , to keep a track of number of
objects created.
[Link] static member function access non-static data ?
Answer:- No. because static member function can be called without creating object.
When object is not created, non-static member is not allocated memory.
[Link] non-static member function access static data ?
Answer:- Yes. Because in order to invoke non-static member function u need to
create object and by that time static members are already allocated memory.
[Link] constructor invocation in case of inheritance.
Answer:- when u instantiate child class, child class constructor by default invokes
“no-arg” constructor of parent class. If parent class does not have “no-arg”
constructor, then child class must invoke one of the available constructor of parent
class explicitly ( using “super”)
[Link] case of inheritance , if parent class does not have no-arg
constructor, what happens ? how do u solve the problem ?
Answer:- in such case, when u try to instantiate child class u get compile-time error.
Solution:- If parent class does not have “no-arg”
constructor, then child class must invoke one of the available constructor of parent
class explicitly ( using “super”)
[Link] case of an array which variable is created automatically ? can u
modify it ? if no , why ?
Answer:- in java whenever u create array, u get “length” variable by default which is
nothing but size of array. No u can’t modify it because it is final.
8
DAY - 2________________________________________________________JavaITan’s
[Link] is class Class in java ?
Answer:- In java whenever any class gets loaded, it is represented by an instance of class
Class.
[Link] is one of the most important class in java but oftenly overlooked by
java developers.
Every time JVM creates an object, it also creates a [Link] object that describes the
type of the object.
All instances of the same class share the same Class object and you can obtain the
Class object by calling the getClass() method of the object. This method is inherited from
[Link]
Suppose you create two instances of class called Person e.g.
Person A = new Person();
Person B = new Person();
if([Link]() == [Link]())
{
[Link]("A and B are instances of same class");
}
Else
{
[Link]("A and B are instances of different class");
}
In this case it will print "A and B are instances of same class" because they are both
instance of clas Person.
[Link] does “forName() “ function do ?
Answer:- Returns the Class object associated with the class or interface with the
given string name.
9
DAY - 2________________________________________________________JavaITan’s
[Link] does class get loaded ?
Answer:- a) implicitly
java MyClass
or
emp e=new emp(); when u create first
object of that class
or
[Link]();
whichever is first……..
b) explicitly
[Link]("classname");
[Link] is bridge method in java ?
Answer:- When u override parent class method with a co-variant return type, compiler
synthesizes (implicitly provides) additional method in child class with a matching signature
of overridden method. This method invokes the method which we are overriding. This
synthesized method is known as “Bridge Method”.
10
DAY - 2________________________________________________________JavaITan’s
[Link] compiler synthesizes bridge method in case of overriding ?
Answer:- suppose we have overridden a parent class method with co-variant return
type e.g.
class base{
public Object disp(){
[Link]("base disp");
return null;
class sub extends base{
public String disp(){
[Link]("sub disp");
return null;
Even though the above overriding is correct according to compiler, for JVM this is
not exactly an overriding.
Actually return types are also part of method signature for JVM. So for JVM these two
methods [public Object disp() And public String disp() ] are overloaded. To solve this
problem, compiler adds a bridge method [ public volatile Object disp() ] in Derived class
[sub] which calls the method which we are overriding.
28. what are the scenarios in which we have early binding in java ?
Answer:- in case of private or static or final methods.
11
DAY - 2________________________________________________________JavaITan’s
THANK YOU
Do Follow on Instagram for More Information……!
12