Core Java Interview Questions- Part 11

Core Java Interview Questions- Part 11Are you preparing for a Java interview? You’ve come to the right place. Java is a language that’s used by millions of developers worldwide. It’s known for being fast, reliable, and easy to learn. Companies use Java to create web apps, desktop tools, and even cloud-based software.
If you want to get a job as a Java developer, you’ll likely face an interview full of technical questions. These questions help employers check if you truly understand how Java works. Some of them might be simple, like explaining the difference between a class and an object. Others might be harder, like writing code to solve a problem.
This page gives you a collection of common Java interview questions and answers. They cover basic and advanced topics in a clear way. You’ll get to learn more about Java’s features and how to use them. Practicing these questions can boost your confidence and prepare you for real interviews.
Keep practicing and stay focused. With the right preparation, you can succeed and land the Java job you’ve been working toward.

Answer:

There are three types of variables in Java, which are as follows:

  • Local Variable:It is declared inside the method body. As the local variable is only used within a method, it is not visible to all the other class methods.
  • Instance Variable:This variable is declared outside the method body but inside the class. The value of an instance variable is not shared amongst other instances.
  • Static Variable: A static variable is declared as static. You can only have a single copy of the static variable that is shared amongst all the instances.

Answer:

The key difference between a break statement & continue statement is that the former is used to immediately terminate a loop. In contrast the latter terminates the current iteration & continues the control to the next iteration of a loop.

Answer:

Following are the ways to overload a method in Java:

  • Change the number of parameters;
  • Change the data type of parameters and;
  • Change the order of data type of parameters.

Answer:

Constructor overloading is a technique of having two or more constructors with different parameter lists. Constructors are arranged in a particular manner that each one performs a task differently. Under constructor overloading, the constructors get differentiated by a compiler, the number of parameters in the list & their types.

Answer:

Constructor Chaining is a process wherein a Constructor calls another Constructor of a superclass or the same class. Constructors calling from another constructor must be the first step or first line. The super() keyword is used to call the superclass Constructor & this() keyword is used to call the Constructor same class’s Constructor. Constructor Chaining can only be achieved through inheritance.

Answer:

A Copy Constructor in Java creates an object using another object of the same class. It is useful when we want to copy a complex object with several fields or a deep copy of an existing object.

Answer:

A String pool is a separate storage area in the heap memory. When declaring a string, an object of type String is created in the stack; at the same time, an instance with the value of a String is created in a heap.

Answer:

Suspending thread means to put a thread running to a waiting state. Resuming thread method is used to resume the suspended thread. The stopping thread method stops the execution of the thread completely

Answer:

The enums are type-safe implies that an enum has its namespace; thus, you cannot assign any other value except the one specified in enum constants.

Answer:

Following are the major differences between abstract class and interface:

  • An abstract class contains at least one abstract method, but it can contain endless number of concrete methods. In contrast, an interface can only contain abstract methods.
  • The abstract class can have private, public, default, or protected variables & constants. The interface only has constants & no variables.
  • The interface supports multiple inheritances, whereas the abstract class does not support the same.
  • When an abstract class is extended, it is mandatory to implement all the abstract methods, but it becomes mandatory to implement all of its methods when an interface is implemented.

Answer:

Yes, abstract class can have constructor, and infact it always has one. If you don’t define explicitly it will have default constructor. The constructor is used for inheritance. When you extend a class, you need constructor in that class.

Answer:

Dynamic dispatch in Java is a mechanism that can resolve a call to an overridden method at runtime instead of compile-time.

Answer:

Runtime polymorphism in Java is a process in which a call to an overridden method can be resolved at runtime rather than at compile-time. In the process, the reference variable of a superclass calls an overridden method. The method’s determination to be called is based on the object being referred to by the reference variable.

Answer:

Dynamic binding implies that the code associated with a given procedure call is not known until the runtime.

Answer:

Yes, the derived classes can override the overloaded methods. The compiler does not bind the method calls as it is overloaded & might be overridden now or shortly.