Java Interview Questions | Page 3

Question 21:

What is method overriding?

Declaring a method in subclass which is already present in superclass is known as method overriding. The main advantage of method overriding is that subclass provides its own implementation to an inherited method without changing the parent class implementation.
For example:-

Class BaseClass{
     public printText(){
           System.out.println(“Base Class”);
Class SubClass extends BaseClass{
      public printText(){
           System.out.println(“Sub Class”);
Question 22:

What are the points that need to kept on mind while method overriding?

a. Argument List:- The argument list of the overriding method must b same as that of the method in parent class. Argument list should be same in terms of data type and sequence as well.
b. Access Modifiers:- The access modifiers of the overriding method cannot be more restrictive than the same method of parent class.
For Example:-

Class BaseClass{
           public void printResult(){
Class SubClass{
           //Not allowed as access modifier more restricted that base class method
            private void printResult(){

It will give an exception as – Cannot reduce the visibility of the inherited method from BaseClass to SubClass.

Question 23:

What is a class?

A class is blueprint/prototype from which objects are created. It is a logical entity.
A class can have any number of methods and attributes.

class Mathematics
             int num1;
             int num2;
             public int sum()
                      return num1 + num2;
Question 24:

What is an object?

Any entity that has state and behaviour is known as an object. Objects are often used to model the real-world objects that you find in everyday life.

Question 25:

What is polymorphism?

Polymorphism is an ability of an object to take on many forms.


Polymorphism means to process objects differently based on their data types.In other words, one method with multiple implementations.
Polymorphism could be static or dynamic.

Question 26:

What is thread?

Thread is lightweight process.Both process and thread provide an execution environment but creating a new thread requires fewer resource than creating a new process.

Question 27:

When to make a custom checked Exception or custom unchecked Exception?

If an application can reasonably be expected to recover from an exception, make it a checked exception. If an application cannot do anything to recover from the exception, make it an unchecked exception.
In reality most applications will have to recover from pretty much all exceptions including NullPointerException, IllegalArgumentExceptions and many other unchecked exceptions. The action / transaction that failed will be aborted but the application has to stay alive and be ready to serve the next action / transaction. The only time it is normally legal to shut down an application is during startup. For instance, if a configuration file is missing and the application cannot do anything sensible without it, then it is legal to shut down the application.

Question 28:

Can a class have both Synchronized and non-synchronized methods?

Yes a class can have both synchronized and non-synchronized methods.

Question 29:

How many locks does an object have?

Each object has only one lock.

Question 30:

What is StackOverflowError?

The StackOverFlowError is an Error Object thrown by the Runtime System when it encounters that your application/code has ran out of the memory.
When a function call is invoked by a Java application, a stack frame is allocated on the call stack. The stack frame contains the parameters of the invoked method, its local parameters, and the return address of the method. The return address denotes the execution point from which, the program execution shall continue after the invoked method returns. If there is no space for a new stack frame then, the StackOverflowError is thrown by the Java Virtual Machine (JVM).
The most common case that can possibly exhaust a Java application’s stack is recursion. In recursion, a method invokes itself during its execution. Recursion is considered as a powerful general-purpose programming technique, but must be used with caution, in order for the StackOverflowError to be avoided.

Share interview questions


comments powered by Disqus


Social Media