Advanced Java Interview Questions- Part 5

Advanced Java Interview Questions- Part 5Preparing for a Java interview can be a game-changer for landing high-paying jobs like Senior Java Developer, Java Software Architect, Java Solutions Architect, Java Backend Engineer, and Java Full Stack Developer. As Java allows creating scalable, reliable programs, it is a go-to tool for all. To help you learn and practice for the Java interview, we have compiled 120-plus questions. All these Top Java questions for Interviews have answers to help you prepare before your next interview.

This guide is informative not only for those who are preparing for interviews but also for those who want to brush up on their knowledge.

So, let’s dive in and learn start learning and practicing!

Answer:

The enableEvents() method enables an event for a particular object. Usually, this method is enabled when a listener is added to an object for a specific event. It is used by the objects whose primary responsibility is to handle events by overriding their event-dispatch methods.

Answer:

The File class encapsulates the files & directories of a local file system. The RandomAccessFile class provides the methods to access data contained in any part of a file directly. The Java.io.RandomAccessFile class provides random access to file with both writing & reading operations.

Answer:

Following are the list of restriction placed on method overriding:.
The overridden methods must have the same argument list, return type & name.
The overriding method must not limit the access of a method it overrides.
The overriding method must not throw an exception that may not be thrown by an overridden method; else, you will see the compile-time exception.

Answer:

A thread can enter the waiting state in the following ways:

Blocking on I/O;

Invoking its sleep() method;

Unsuccessfully attempting to attain an object’s lock.

Answer:

The ResourceBundle class in Java is a group of related sub-classes that have the same base name. It allows storing locale-specific resources that a program can load to tailor its appearance to a specific locale wherein it is running.

Answer:

Numeric promotion refers to converting a smaller numeric type to the larger numeric type so that integer & floating-point operations can occur. In numerical promotion, char, byte, & short values get converted into int values. Further, the int values also get converted to long values, if necessary. Thereon long & float values get converted to double values, as required.

Answer:

Scrollbar ScrollPane
It is a component. It is a container.
It does not handle its own events. It handles its events & performs its own scrolling.
Scrollbar cannot have a ScrollPane. ScrollPane can have a Scrollbar.

Answer:

A class with public access specifier is accessible across the package.  A non-public class refers to a class with a protected access specifier or without a public access specifier that cannot be accessed across the package.

Answer:

The prefix first performs the increment operation & then returns the value of an increment operation. In contrast, the postfix first returns the current value & then performs the increment operation.

Answer:

A Java package is a naming context for interfaces & classes. It is used to create a separate namespace for groups of interfaces & classes. The Java package also helps  organize related classes & interfaces in a single API unit and control access to those classes and interfaces.

Answer:

Synchronized methods help to control access to the object. A thread executes a synchronized method only after acquiring the lock for the method’s object or class. When a method needs to be synchronized in Java, you need to add the keyword synchronized. Synchronization does not allow invocation of that synchronized method for the same object until the thread is done with the object.

The synchronized statement is just like synchronized methods. It can execute after a thread acquires the lock for an object or class referenced in the synchronized statement. The synchronized statement contains a synchronized block that has objects and methods which are yet to be synchronized.

Answer:

When a Java programmer doesn’t use the layout managers, it will be challenging to determine how the GUI will be displayed over multiple windowing systems & find a common positioning or sizing that can work within the constraints imposed by each windowing system.

Answer:

There are mainly four OOP concepts, as follows:

  • Abstraction– It represents the critical properties of an object that differentiates from another object. Thus, abstraction crisply defines conceptual boundaries related to the prospective viewer.
  • Encapsulation– It is a mechanism that binds the code and the data it manipulates. It also keeps them safe from misuse & external intervention.
  • Inheritance– It helps an object to inherits the methods & properties of another object.
  • Polymorphism– This OOP concept refers to the ability of an object to take on several forms.

Answer:

The final keyword in Java is useful in many contexts like variable, method, or a class. It is a non-access modifier, which means if you make a class or variable final, you cannot change them, but if you try to change the final class or variable, then the compiler will throw a compilation error.

Answer:

Static keywords are used with class, block, variable, & method. The static members belong to a class instead of a specific instance, which means if you make a member static, you can access it without an object.

Answer:

Yes, an abstract class can have an abstract method without a body & it can have implementation methods. When you use the abstract class to implement an interface: some of the interface’s methods can become completely missing, but the compiler does not complain.

Answer:

There are two types of typecasting; Upcasting & Downcasting:

  • Upcasting means typecasting of a child object to a parent object. It can be done implicitly and provides the flexibility to access the parent class’s members, however, it is impossible to access all the child class’s members using this feature, but we can access some specified members of a child class.
  • Downcasting means typecasting of a parent object to a child object, and it cannot be implicitly.

Answer:

The following are the advantages of classes & interfaces in a Java package:

  • Java packages help to determine a category of a file.
  • Java Packages provides a way to hide classes, thus preventing other packages from accessing classes meant for internal use.
  • Packages allow programmers to separate design from coding.
  • Java package provides reusability of code.
  • It removes naming collision.

Answer:

Following are some considerable differences between super() and this() methods in Java:

Sr. No. super() method this() method
Definition super() refers to an immediate parent class instance. this() refers to a current class instance.
Invoke super() keyword helps to invoke the immediate parent class method. this() keyword can be used to invoke the current class method.
Constructor super() keyword acts as an immediate parent class constructor & should be the first line in the child class constructor. this() keyword acts as a current class constructor & it can be used in parameterized constructors.
Override The super keyword helps to invoke a superclass version of an overridden method. this() keyword is used to invoke a current version of an overridden method.

Answer:

Java compiler automatically creates a default constructor when no constructor is present in the Java class. Following is the purpose behind a default constructor:

  • Create the object
  • Call the superclass constructor ()
  • Initialize all the instance variables of the class object.