Core Java Interview Questions Part I

An object is resurrected by making other object refer to the dying object in finalize method. Will this object be ever garbage collected?

Resurrection can happen in 'finalize' method which will prevent GC to reclaim the object memory. However this could be done only once. Next time GC will not invoke 'finalize' method before garbage collection.

What is the rule regarding overriding methods throwing exceptions?

Overriding methods cannot throw more generic exception than base method.

Learn Java in Detail

What are the different types of inner classes?
  • An 'inner class' is part of the implementation of its enclosing class (or classes). As such, it has access to the private members of any enclosing class. Top-level nested classes are declared with 'static' keyword.

  • Top level inner classes can be accessed / instantiated without an instance of the outer class. Can access only static members of outer class. Cannot access instance variables or methods of the enclosing class.

  • Non-static inner classes which are declared without 'static' keyword cannot exist without enclosing class. Can access all the features (even private) of the enclosing outer class.

  • Local classes are defined inside a block (could be a method, a constructor, a local block, a static initializer or an instance initializer). Cannot be specified with 'static' modifier.

  • A class cannot have non-static inner interface. All inner classes except anonymous can be 'abstract' or 'final'.

Can a class implement two interfaces with same variable names?

If both the interface have same variable and the variable is not declared in implementing class, the compiler will throw an ambiguous error.

Can a class implement two interfaces with same variable names?

If both the interface have same variable and the variable is not declared in implementing class, the compiler will throw an ambiguous error.

Member variables are resolved at compile time or runtime?

Member variables are resolved at compile time.

Write the nearest equivalent of size operator in C. (Hint: Use Runtime class)

[Note: Since GC can't be enforced in java the result is not always predictable.]

Can we override variables?

Yes, we can override variables but variables when overridden shadows the super class variable. Print S1 S1 Print S1 S2 Print S1 now S1 Print S1.gets() now S2.

Which one will throw an arithmetic exception: a)int i = 100/0; b)float f = 100.00/0.0

b. [float f = 100.00/0.0. Float division by zero returns NAN (not a number) instead of exception.]

If an overridden method calls super class method which accesses class member variable, which variable will be used for base class or super class?

Methods access variables only in the context of the class they belong to will be used for base class or super class. If subclass calls super class method, it will access super class variable. Display in S1 S1 Display in S2 S2 Print S1 S1 Print S2 S1

Map implements collection. True or false?

False, as map does not implement collection.

Can a class implement two interfaces which has got methods with same name and signatures?

Yes, a class can implement two interfaces which has got methods with same name and signatures.

Is this statement correct: char ch = 'd'; if(ch < 32.00){ }

Yes, the mentioned statement is correct.

Can an interface be final?

Interface cannot be declared 'final' as they are implicitly 'abstract'.

Does the 'finalize' method in subclass invoke 'finalize' method in super class?

'Finalize' is not implicitly chained. A 'finalize' method in sub-class should call 'finalize' in super class explicitly as its last action for proper functioning. Compilers does not enforce this check.

Can we have static method in interface?

All methods in an interface are implicitly 'public', 'abstract' but never 'static'.

What is the use of volatile variable?

Volatile can be applied only to variables, not for 'static' or 'final'. Declaring a variable volatile indicates that it might be modified asynchronously, so that thread will get correct value and used in multi processor environment.

Can an interface have variables? Can these variables be transient?

All variables in an interface are implicitly static , public and final. They cannot be transient or volatile. A class can shadow the interface variable with its variable while implementing.

What is the use of transient variable? Can a transiant variable be static?

Transient variables are not stored as object's persistence state and is not serialized for security. Transient variables may not be final or static. Compilers do not give any errors as static variables and anyways they are not serialized.

Learn More in our Video Tutorial