TOP 30 Core Java Interview Questions

6030 0

Released in 1995, Java is a popular computing platform and a programming language as well. With each day, there is an increases in the number of apps that are created using Java. Moreover, many apps and apps require Java to be installed in order to function. The prevalence of Java today is undeniable. From laptops to cell phones to game consoles to datacenters to supercomputers, Java is everywhere.

Java is the prefered language of use by the IT sector due to its simplicity and the ease with which it can be used. The language’s wide usage means that there is always a demand for professionals who are familiar with Java. The questions compiled in this article will help you to ace any interview that tests your core Java knowledge. Experienced professionals can refer to these questions to add on or revise their existing knowledge.

  1. What do you know about the principle concepts of Object Oriented Programming?

There are four essential concepts of OOPs. They are:  

a) Abstraction

b) Inheritance

c)Polymorphism

d) Encapsulation

Tip: You can easily remember this as A-PIE.

2. What do you understand about the term Abstraction?
Abstraction is the term given to the process of representing all the essential features. This does mean including any additional or background information.

3. What do you know about Encapsulation?
It is a method for hiding the behaviour and properties of an object and permitting only limited outside access. This technique stops other objects from directly accessing or altering the methods or properties of the encapsulated object.

4. How is Encapsulation and Abstraction different?
In Abstraction, the focus is on the interface of an object, ie, the object’s outside view. While in Encapsulation, the basic idea is to hide the inside view from the client.
Abstraction provides solution to the design side and Encapsulation relates to the implementation.

Encapsulation is essentially the deliverables of Abstraction. In Encapsulation, there is a very little about grouping the abstraction to fulfill the developer’s requirements.

5. What do you understand by Inheritance?
It is the method through which the objects of one class get the properties of another class. Thus inherited class is known as superclass and the class that does the inheriting is known a as subclass.
Inheritance can be done with the help of keyword extends. There are mainly two reasons as to why you should use inheritance. They are:
a) For using polymorphism
b) For promoting code reuse

6. What do you understand by Polymorphism?
Polymorphism can be depicted as “one interface, many implementations.” This technique allows entities like an object, function or a variable to have multiple forms.

7. How is Polymorphism implemented in Java?
Polymorphism is achieved in Java through methods such as Inheritance, Overriding and Overloading. In Java, Polymorphism is manifested in the form of multiple methods with the same name.
There are some cases where multiple methods can possess the same name, but have various formal argument lists (overloaded methods). There can also be cases where multiple methods can have the same name, same formal argument list and same return type.

8. Briefly discuss the different kinds of Polymorphism.
There are two kinds of Polymorphism. They are: Run time polymorphism and compile time polymorphism. While Run time polymorphism is carried out with the help of interface and inheritance, Compile time polymorphism is method overloading.
From a programming perspective, in java polymorphism manifests in three different ways:
a)Method overriding through inheritance
b) Method overloading
c) Method overriding through the Java interface

9. What do you understand by the term dynamic method dispatch or runtime polymorphism as it is better known?
In Runtime polymorphism or dynamic method dispatch, the call to an overridden method is resolved at runtime rather than at compile-time. With this method, an overridden method is invoked with the help of a superclass reference variable. The type of the method is determined by considering the object which is being alluded to by the reference variable.

10. In Java, what is known as Dynamic Binding?
The concept, Binding also known as late binding, essentially refers to the linkage of a procedure call to the code that is to be executed in response to the call. Dynamic binding also means that the code related to a given procedure decision is unknown till runtime when the decision is taken. This concept is related to polymorphism and inheritance.

11. What do you understand by method overloading?
In method Overloading, you can more than two methods with the same name but with different arguments in the same class. One of the advantage of using method overloading is it enables you to administer methods that is consistent with the same semantic operation and is different from the argument type or number.or type.

Some important things you should keep in mind:
a) Ensure that overloaded methods change the argument list.
b) The return type could be changed by the overloaded methods
c) The access modifier can also be changed by the overloaded methods.
d) Broader or new checked exceptions can be declared by overloaded methods.
e) There is a chance that a given method could be overloaded in the same class or in a subclass.

12. What do you understand by the term method overriding?
Method overriding happens when a method declared by the subclass has the same argument type as that of a method that one of its superclass has declared. One key advantage of overriding is its capability to explain behaviour which is particular to a specific type of subclass.
What you should remember is that it is not possible for the overriding method to have a more prohibitive access modifier than the overridden method. Those methods that are marked final or static cannot be overridden.

13. Differentiate between method overriding and method overloading?
In Overloaded method, the arguments should change, the return type, access, and exceptions can change. With regard to invocation, the reference type will decide the version of overloaded method that is used. This takes place during the compile time. exceptions Arguments – Must Change
While in Overridden method, the arguments should not change; the exceptions can be removed or reduced and should not throw broader or new checked exceptions; it is not possible for the return type to change other than for covariant returns’ the access can me made a little less restrictive and with regard to invocation, the method to be selected will be chosen by the object type and this takes place during the runtime.

14. Is it possible to override overloaded methods?
Yes. Overloaded method can be overridden by derived classes. Polymorphism can still take place. The method classes will not be bound by the compiler as it is overloaded. The main reason is that there is a probability that it can be overridden now or in the future.

15. Can the main method be overridden?
No. It cannot be overridden because the main method is a static method. In java, a static method cannot be overridden.

16. How do you call a superclass type of an Overridden method?
In order to call a superclass method that was overridden in a subclass, you can either use superclass instance to directly invoke the method or utilize the super prefix in the subclass. When you see from a subclass view, the super prefix offers an explicit reference to the superclass’ method implementation.
Eg: // From subclass
super.overriddenMethod();

17. What do you understand by the term Super?
The keyword, Super, is for accessing the member variables or method from the super class. In case, a method hides in its superclass one of the member variables, the super keyword can be used to refer to the hidden variable. Similarly, the keyword super can also be used for the method to invoke the overridden method if one of the methods in its superclass is overridden by a method.
Remember: It is possible to go back by only a level. If the keyword, super() is used in the constructor, it should be the initial code and it cannot be accessed any this.xxx variables or methods for computing its parameters.

18. How can you stop a method from being overridden?
A method can be prevented from being overridden in a subclass by using the final modifier for declaring the method. This means” this is the final implementation of this method”, the end of its inheritance hierarchy.
public final void exampleMethod() {
// Method statements
}

19. What do you understand by the term Interface?
The description of the set of methods that implementing classes that conform must have is called as Interface.
Remember:
a) An Interface cannot be marked as final
b) The variables of an interface must be static
c) Only another interfaces can be extended by an interface and not anything else

20. In Java, what do you understand by the term ‘abstract class’?
A class that cannot be instantiated but can only be extended is termed as an abstract class. An abstract class provided the generalised form which is similar across all of its subclasses, thus each sub classes are required to complete the details. Partial abstraction can be achieved with the help of abstract classes.

21. Differentiate between Interface and Abstract class?
a) An interface can have only abstract methods while an abstract class can have both non-abstract and abstract methods.
b) Static methods can be there in an abstract class but it cannot be there in an interface.
c) Constructors can be there in an abstract class, but it cannot be there in an interface.

22. What are the access modifiers which could be applied to the inner classes?
public , abstract, private ,protected,final.

23. In Java, what do you know about constructor?
An instance is created with the help of a constructor. When an instance of a class is created, constructors are invoked. A class name and constructor name should be the same and also it does not have a return type.

24. Is it possible to inherit the constructors?
No, constructors cannot be inherited.

25. Is it okay to mark constructors as fina?
No. Constructor should not be declared as final.

26. What do you understand by parameterized and default constructors?
Parameterized constructors are those constructors with arguments. While Default constructors are those constructors with no argument. A default constructor is created automatically by the compiler when you do not declare any constructor in a class.

27. Can another constructor be invoked by a constructor?
Yes. One constructor can call another one in the same class using the keyword this.
For instance, this() calls the default constructor. Remember that this() should be the initial statement in calling the constructor.

28. In Java, is multiple inheritances supported? If not, why?
No. It is not supported in Java so as to decrease the complexity and to make the language simple.

29. Can the constructor of a parent class be called by a constructor?
Yes, it happens actually by default. The parent class constructor can always be called by a child class constructor using super keyword.
For instance: super(), this keyword is used for calling super class default constructor.

30. In Java, is there a copy constructor?
No. java does not have a copy constructor. However it is possible to copy value from an object to another like the C++ copy constructor.
Some of the ways in which the values of an object can be copied in java are:
Using clone() method of the object class
Using constructor
Giving the value of an object into another

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA

*

About Vibhuthi Viswanathan

Vibhuthi is a an avid follower of the latest trends in the world of Technology. Her writing aims to engage and educate the readers on all things Tech. When she is not twirling with words and pauses at SpringPeople, she binge reads popular literature.

Posts by Vibhuthi Viswanathan