Core Java Interview Questions- Part 2

Core Java Interview Questions- Part 2Whether you are preparing for an entry-level programming career or an advanced-level career such as a full-stack developer, software developer, or database administrator, you must be proficient in Java.

Java is a popular programming language that is recognized and used worldwide by companies. So, to help you succeed in your career, we have created a collection of all the important Java questions. These Core Java questions are essential for both entry-level and advanced roles.

The core Java questions cover every important topic right from OOPs to Multithreading, Packages, Collections, and more. So, let’s start learning with the help of our carefully created list of the most popular Core Java interview questions.

Answer:

As ‘this’ is a final variable, it cannot be assigned to a new value while the current class object may not be final or susceptible to change. Also, the implementation of ‘this’ keyword can be used for a synchronized block.

Answer:

Inheritance is a mechanism that enables one object to acquire the behavior & properties of another object belonging to another class. It is used for method overriding & code reusability. Through inheritance, you can create new classes built upon the existing classes. When inheriting from the existing class, you can reuse the fields & methods of the parent class. Besides, you can also add new fields & methods to your current class. Inheritance represents the parent-child relationship or the IS-A relationship.

Answer:

Having reference to an instance of a class within the instance of some other class is called composition. In simple terms, the composition is a special case of aggregation that represents a robust relationship between two objects. The composition represents the Has-A relationship in classes. For instance, if a class contains students, then those students cannot exist without the class. Thus, it shows that there exists a composition between the class & students.

Answer:

A pointer is a variable that stores the memory address. Java doesn’t use pointers because they are complex to understand & are unsafe or unsecured.

Answer:

Following are the primary uses of the super keyword:

  • It can be used to refer immediate parent class instance variable.
  • It can be used to invoke immediate parent class method.
  • super () is useful to invoke the immediate parent class constructor.

Answer:

Method overloading allows a class to define multiple methods with the same name, but different arguments. That is, it allows you to define different methods that have the same name, but that respond to correspondingly different messages sent to an instance of the class.

Answer:

We cannot override a static method as the static method is bonded with static binding at compile-time, while method overriding is based on the dynamic binding at runtime.

Answer:

Yes, it is possible to modify the superclass method’s throws clause when overriding it in the subclass. However, there are some rules which we need to follow while overriding in exception handling, like:

  • If the superclass methods do not declare an exception, then the subclass overridden method cannot declare the checked exception, yet it can declare the unchecked exception.
  • If the superclass method declares an exception, then the subclass overridden method can also declare the same, subclass exception or no exception, but cannot declare parent exception.

Answer:

In Java, all the functions except private, static, or final are virtual functions by default.

Answer:

The main difference between the final method and abstract method is that, the final method is regarded as complete, while the abstract method is incomplete. The abstract method can only be used by overriding it; however, you cannot override the final method.

Answer:

It is the process of hiding certain details and showing only essential information to the user. It can be achieved with either abstract classes or interfaces.

Answer:

An Interface is a blueprint of a class that has abstract methods & static constants. It is a mechanism used to achieve multiple inheritance & full abstraction. The interface in Java represents the IS-A relationship.

Answer:

Following are the advantages of Encapsulation in Java:

  • The encapsulated codes are more flexible & easier to change as per the new specifications.
  • It allows you to modify the codebase without disrupting the function of the program
  • Encapsulation in Java prevents other classes from accessing private fields.
  • It protects the codes & data from external inheritance.
  • Encapsulation simplifies an application’s maintainability.
  • It also helps to achieve loose coupling in Java & reduces human error.

Answer:

Exception Handling is a process that allows you to handle runtime errors due to exceptions. It helps to maintain the normal flow of a Java program & keeps it protected from any disruption.

Answer:

As its name suggests, the Java BufferedInputStream class is used to read information from the stream. It uses the buffer mechanism internally to accelerate the performance & adds functionality to another input stream. Whereas, Java BufferedOutputStream class is useful for buffering an output stream. It increases efficiency by storing values to be written in a buffer than to writing the data directly.

Answer:

The Externalizable interface is deployed to write the state of an object into the byte stream in a compressed format. It means externalizing your serialization.

Answer:

A socket is one endpoint of a two-way communication link between two programs running on the network.

Answer:

Java strictfp keyword assures you to provide the same result on all the platforms if the operations are performed in the floating-point variable. The precision can differ from one platform to another; that is why Java has provided the strictfp keyword to get the same result on every platform. It enables us to have better control over floating-point arithmetic.

Answer:

JavaBean is a reusable component of software that can encapsulate many objects into one; thus, it enables us to access this object from multiple places.