Advanced Java Interview Questions- Part 4

Advanced Java Interview Questions- Part 4Getting ready for a Java programming job interview can be a big deal for your future, whether you’re new to coding or an experienced developer. Our Tough Java Interview questions can help your prepare for Interviews. Java is super popular because it’s used for business apps, websites, and Android apps, so interviewers will likely test your Java skills if you are applying for positions like mobile developer, software developer, etc.
This interview questions guide will give you confidence to answer advanced Java interview questions and stay prepared for tricky interview questions. If you’re just starting, basics like object-oriented programming, handling errors, and simple data structures are enough since these are commonly asked in beginner interviews.
But if you’re experienced, expect advanced Java questions on topics like Multithreading, Concurrency, Design Patterns, Memory Management, Streams, Lambda Expressions, Spring Framework, and Performance Optimization. Going through our interview questions will help you strengthen your Java knowledge and give clear answers, which is super important in interviews.

Answer:

The finally clause of a try-catch-finally statement executes code regardless of whether an exception is thrown or not.

Answer:

To let a class implement an interface, you must include an implements clause in the class declaration. A class can implement multiple interfaces, so in such case, the implements keyword is followed by a comma-separated list of the implemented interfaces.

Answer:

The methods of wait(), notify(), and notifyAll() provides an efficient way for threads to wait for a shared resource. When a thread executes the wait() method on an object, it enters the waiting state & only reenters the Ready state after some other thread invokes the object’s notify() or notifyAll() methods.

Answer:

The main difference between a non-static inner class & a static class is that the former may have object instances associated with instances of a class’s outer class. Yet, the latter does not have any object instances.

Answer:

An object’s lock is a mechanism used by multiple threads to attain synchronized access to an object. A thread can execute a synchronized method of an object once it acquires the object’s lock. All the classes & objects have locks. A class acquires a lock on the class’s Class object.

Answer:

Yes, we can cast an object reference to an interface reference when the object implements the referenced interface.

Answer:

No, a reachable object cannot get garbage collected as only unreachable objects may be garbage collected.

Answer:

Clipping refers to a process of confining paint operations to a limited shape or area. The clipping region is set to the repainted window on the area that requires repainting. When the AWT painting thread repaints a window, it sets the clipping region to that area, which requires repainting.

Answer:

An EventListener interface defines the methods that need to be implemented by an event handler for a particular event. On the other hand, an Event Adapter class provides the default implementation of an EventListener interface.

Answer:

A switch statement refers to a multiple branch statement that provides a way to dispatch execution to different code parts based on an expression’s value. When compiling a Java program, each case of a switch statement’s values needs to be evaluated as a value that can be promoted to an int value or not.

Answer:

The highest level class of an event delegation model is java.util.EventObject class as it contains all interfaces & classes that support all types of event handlings.

Answer:

All the elements of a GridBagLayout are organized as per the grid. However, the elements are of different sizes may occupy multiple columns or rows of the grid. Besides, the rows & columns can have different sizes.

Answer:

paint() method repaint() method
When some action is being performed on the window, the paint() method is called. The repaint() method is called when an update method and the paint() method is called.
The paint() method supports painting through a graphics object. The repaint() method is deployed to cause paint() to be invoked by the AWT painting thread.

Answer:

The Java File class creates objects that provide access to a local file system’s directories & files. It is an abstract representation of file & directory pathname, which can be either relative or absolute. The File class has various methods to work with directories & files, such as creating new files or directories, renaming & directories the files, listing a directory’s content, etc.

Answer:

It will break the waiting or sleeping state by throwing Interrupted Exception. An Interrupted Exception in Java is thrown when a thread is currently getting blocked. However, if the thread is not getting blocked, the Interrupted Exception will not be thrown.

Answer:

A cast is a way to explicitly inform the compiler that you intend to convert even if the data loss might occur or the cast may fail during run-time. To perform casting in Java, specify the type you are casting to in parentheses in front of the variable or value to be converted.

Answer:

A Choice gets displayed in a compact format that requires you to pull it down to see the list of available choices. You can select only one item from a Choice. It is an act of deciding or picking among two or more possibilities. The List can be displayed in a way that various List items are visible. It is an enumeration of a set of items that supports the selection of multiple List items.
A Choice implies an act of choosing or picking one between two or more possibilities. Also, a Choice lets you select only one item.
A List refers to an enumeration of a set of items that supports the selection of two or more items.

Answer:

A Set interface is a Collection that cannot contain duplicate elements. It provides access to the mathematical set abstraction. A Set includes only inherited methods from Collection & adds the restriction to prohibit duplicate elements.

Following are the methods declared by a Set interface:

Sr.No. Method & Description
1 add( ) method- It adds an object to the Collection.
2 clear( ) method- It helps to remove all objects from the Collection.
3 contains( ) method- It returns true only if a specified object is an element within the Collection.
4 isEmpty( ) method- This method returns true when a Collection has no elements
5 iterator( ) method- This method returns an Iterator object for a collection that may be used to retrieve an object.
6 remove( ) method- It removes a specified object from the Collection.
7 size( ) method- It returns the no. of elements in the Collection.

Answer:

No, true and false are not keywords; instead, they are literals. You cannot use them as identifiers in your Java programs.

Answer:

A void return type in Java specifies that a method does not return a value.