Basic Java Questions

0
798


Every interview for a Java position, always starts with Core Java questions first. This ensures that the candidate has a strong understanding of Java basics. Core Java basic questions cover many of the topics including the OOPS concepts, constructors, cloning, serialization, Strings, threading concepts etc.

[symple_highlight color=”gray”]

Question 1: What is the difference between overloading and overriding?

[symple_highlight]

Overloading and overriding are the ways to provide the polymorphism in Java.

Overloading is reusing the same method names and different arguments in the same or in subclass.
Overloaded methods MUST change argument list.
Overloaded methods CAN change return type.
Overloaded methods CAN change the Access Modifiers.
Overloaded methods CAN declare new or broader checked exceptions.
Overloaded methods calls are decided based on the reference type. i.e By the compiler – at compile time itself.

Overriding methods MUST NOT change argument list.
Overriding methods MUST NOT change return type but can be a subtype of the return type declared.
Overriding methods CAN NOT narrow down the Access Modifiers but can make it broader.
Overriding methods CAN NOT throw new or broader checked exceptions but can narrow them down.
Overriding methods CAN throw Runtime exception regardless of overridden method declarations.
Static or final methods CAN NOT be overridden.
If a method can not be inherited, it cant be overridden.

Overriding methods calls are decided based on the reference type. i.e By the compiler – at compile time itself.

[symple_highlight color=”gray”]

Question 2: Can a constructor be synchronized?

[symple_highlight]

No, a constructor can not be synchronized and produces compile time error if we try to make it synchronized. Actually constructors do not need to be synchronized because they cant be called on pre-existing objects so there is no way for 2 threads to call the same constructor on the same object at the same time.

Constructors provide object initialization which will make a separate, unique object and not related to any other object so there is no sense as well of making the constructor synchronized.

[symple_highlight color=”gray”]

Question 3: What is HAS-A and IS-A relationship?

[symple_highlight]

HAS-A relationship is based on usage rather than inheritance. “Class A HAS-A B” if code in class A has a reference to an instance of class B.

IS-A relationship is based on class inheritance or interface implementation.
I.e This thing is a type of that thing. For ex: A Car extends Vehicle means “Car IS-A Vehicle”.
If expression (Dog instanceOf Animal) is true, then class Dog IS-A Animal, even if Dog doesn’t directly extends Animal.

Ex: A Dog IS-A Animal
A Dog HAS-A Collar

public class Animal { }
public class Horse extends Animal {
private Collar myCollar;
}

public class Collar { …}

[symple_highlight color=”gray”]

Question 4: What is the difference between Transient and Volatile keyword?

[symple_highlight]

Volatile keyword provides another way of synchronization. Whenever any object/thread tries to access volatile variable, it will get the latest copy of that. Its content will never be cached for any object/thread. We should use it for the state that is truly independent of everything else in your code.

Transient keyword is used in classes which are supposed to be serialized and we want to restrict any variables to be serialized. So the transient variables’ state does not get captured.

[symple_highlight color=”gray”]

Question 5: What is a marker or tag interface?

[symple_highlight]

A marker or tag interface is the one which has no methods or fields. It helps to mark the implementing class for its special behavior so that JVM is aware about it. In Java we have these marker interfaces available:
– java.lang.Cloneable
– java.io.Serializable
– java.util. EventListner
– java.util.RandomAccess

[symple_highlight color=”gray”]

Question 6: What is the difference between cloning and copying the object?

[symple_highlight]

The original and the copy of objects, refer to the same object and any change in either one of them affects the other as well.

Student first = new Student (“Shalini”, “MCA”);
Student copyFirst = original;
copyFirst.changeSubject(“MBA”); // This would also change object first

But when we talk about cloning we get two separate objects and the change in one would not impact the other one.

[symple_highlight color=”gray”]

Question 7: Where to implement equals() and hashCode()?

[symple_highlight]

The Collection classes that implement interfaces ‘set’ or ‘map’, expect the object or the key object to override equals() method. If a class overrides equals(), it must override hashCode() method as well.
Object1.equals(object2) implies object.hasCode() == object2.hasCode()

So if 2 objects are equal then they must have the same hashcode. However, vice-a-verse is not true.

[symple_highlight color=”gray”]

Question 8: What is the difference between throw and throws?

[symple_highlight]

We use ‘throws’ in method declarations where the content of the method may throw an exception. In this way the responsibility to handle the exception occurred in method is passed to the caller method. Whereas, ‘throw’ keyword can be used in method bodies to explicitly throw an exception.

[symple_highlight color=”gray”]

Question 9: What is the difference between coupling and cohesion?

[symple_highlight]

Coupling is the degree to which one class knows about the another class. If the only knowledge that class A has about Class B is ‘what class B has exposed through its interface’, then A and B are loosely coupled and that is actually good.

Cohesion is about how a single class is designed. This is used to indicate the degree to which a class has a single, well focused purpose. More focus, better Cohesion.

So classes which are loosely coupled and have high cohesion are considered good for code.

[symple_highlight color=”gray”]

Question 10: What does it mean if a method or variable is static?

[symple_highlight]

In Java, static variables and methods can be accessed with out having an instance of the class.
Static variables get the same default value as instance variable get. A static method can not access a non-static variable or non-static method. Also, static methods can’t be overridden.

Popular Core Java CODING INTERVIEW QUESTIONS

Finding out if two String are Anagrams

Find out if a number is an Armstrong number or not

Previous articleJAR/WAR and EAR
Next articleAll About Constructors
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.

NO COMMENTS

LEAVE A REPLY