top of page

Core Java Interview questions

Can the main method be Overloaded?

Yes, It is possible to overload the main method. We can create as many overloaded main methods we want. However, JVM has a predefined calling method that JVM will only call the main method with the definition of -

public static void main(string[] args)


Is it possible to overload a function just by changing the return type?

No. The reason is that overloads in Java are only allowed for methods with different signatures.

The return type is not part of the method signature, hence cannot be used to distinguish overloads.


Can we mark constructors final?

A constructor cannot be marked as final because whenever a class is inherited, the constructors are not inherited. Hence, marking it final doesn't make sense. Java throws compilation error saying - modifier final not allowed here


Is it possible that the ‘finally’ block will not be executed? If yes then list the case.

Yes. It is possible that the ‘finally’ block will not be executed. The cases are-

  • Suppose we use System.exit() in the above statement.

  • If there are fatal errors like Stack overflow, Memory access error, etc

What will be output of the code snippet below?

final int i;
i = 20;
int j = i + 20;
i = j + 30;
System.out.println(i + " " + j);

The above code will generate a compile-time error at Line 4 saying - [error: variable i might already have been initialized]. It is because variable ‘i’ is the final variable. And final variables are allowed to be initialized only once, and that was already done on line no 2.


What part of memory - Stack or Heap - is cleaned in garbage collection process?

Heap


Which of the below generates a compile-time error? State the reason.

  1. int[] n1 = new int[0];

  2. boolean[] n2 = new boolean[-200];

  3. double[] n3 = new double[2241423798];

  4. char[] ch = new char[20];

3. Compile time error Int number too large -

2. Runtime error: As the syntax looks all fine, so no compile time error but during runtime JVM allocates memory and will incur negative value of array and will throw NegativeArraySizeException.

Rest all cases seem to work fine since it expects integer and they are in range


Explain Java Coding standards for interfaces?

1) Interface should start with uppercase letters

2) Interfaces names should be adjectives

Example : Runnable, Serializable, Marker, Cloneable


What does null mean in java?

When a reference variable doesn’t point to any value it is assigned null.

Example : Employee employee;

In the above example employee object is not instantiate so it is pointed no where


Can we have try block without catch block?

Each try block requires atleast one catch block or finally block. A try block without catch or finally will result in compiler error. We can skip either of catch or finally block but not both.


Explain a situation where finally block will not be executed?

Finally block will not be executed whenever jvm shutdowns. If we use system.exit(0) in try statement finally block if present will not be executed.


What if I write static public void instead of public static void?

The program compiles and runs correctly because the order of specifiers doesn't matter in Java.


Does constructor return any value?

No it doesn't return any value, but it has the class instance


What happens if there are multiple main methods inside one class in Java?

The program can't compile as the compiler says that the method has been already defined inside the class


What if the static modifier is removed from the signature of the main method?

Program compiles. However, at runtime, It throws an error "NoSuchMethodError"



https://youtube.com/shorts/sUhnSmdzCrw?feature=share

43 views0 comments

Recent Posts

See All

Data Visualization Techniques Using Python

Introduction Data visualization is a crucial part of the data analysis process. It helps in understanding the data better and uncovering patterns, trends, and insights. Python provides several librari

bottom of page