Core Java Interview Questions- Part 10

Core Java Interview Questions- Part 10Getting ready for a Java interview? This page is made to help you with that. Here, you’ll find a list of the most common Java interview questions. These questions test your understanding of Java basics like classes and objects, as well as advanced ideas like multithreading, exception handling, and collections.
Each question is explained in simple terms so you can learn faster. Whether you’re new to Java or already have some experience, this guide can help you improve your skills and feel ready for your interview.
Use this time to review key concepts and write your own answers. Practice makes perfect, and going over these questions will help you stay calm and confident in your interview. With effort and the right preparation, you’ll be ready to impress any employer.

Answer:

The principal use of Assertions is to check logically impossible situations like checking the state a code expects before running & its state after it finishes running. Unlike other errors or exceptional handling, Assertions are usually disabled during runtime.

Answer:

Thread are needed in Java to start a Java program. It is a lightweight process that works independently to improve the utilization of the CPU. Threads accelerate the speed of Java applications by doing multiple things simultaneously. It helps us to achieve parallelism in a Java program.

Answer:

Following are the key differences between Runnable interface and thread class in Java:

  • Thread is a class. It is used to create a thread. While, Runnable is a functional interface which is used to create a thread.
  • Thread has multiple methods including start() and run(). Whereas, Runnable has only abstract method run().
  • Thread requires more memory. In contrast, Runnable interface requires less memory.

Answer:

Time slicing refers to a scheduling process that divides the available CPU time amongst various runnable threads. The allocation of CPU time depends on the priority of a thread. In the time-slicing process, a task executes for a particular time & re-enters the pool of ready tasks. The thread scheduler decides which task should be executed next, based on priority & other factors.

Answer:

Yes, it is possible to make an array volatile in Java. However, we can only make the reference pointing to an array & not the whole array. If a thread changes the reference variable & points to another array, that will guarantee the array volatility. In case multiple threads make changes in the array elements, they cannot change before a volatile modifier renders the guarantee.

Answer:

A class becomes garbage-collected when nothing references it. There are several ways of making a class reachable & prevent it from being eligible for garbage collection:

  • When the objects of the class are still reachable;
  • The class object represents that class is still reachable;
  • The ClassLoader that has loaded the class is still reachable;
  • And when other classes loaded by the ClassLoader are still reachable.

Answer:

The following are the differences between the factory pattern & abstract factory pattern:

  • A factory pattern is a single method, while an abstract factory is an object.
  • The level of abstraction for an abstract factory pattern is one step higher than the factory pattern.
  • The factory method pattern returns the common parent class, but the abstract factory pattern returns one of several factories.

Answer:

An Observable class is used to create subclasses that other parts of a program can observe. When the object of such a subclass undergoes any change, then the observing classes get notified. Thereon, the update( ) method is called whenever an object is notified of any change.

Answer:

The Comparator interface in Java controls the order of a certain collection of objects & data structures. It is present in java.util.Comparator. The Comparator interface is capable of comparing two objects & two classes. We can perform sorting by implicitly using data structures & explicitly implementing sort methods.

Answer:

A private constructor helps to prevent external instantiation of subclasses & classes. Objects can be created, but the creation can be done internally.

Answer:

The following are the advantages of Java Sockets:

  • Java Sockets are flexible for general communication & easy to implement.
  • Java Sockets causes low network traffic, unlike CGI scripts HTML forms that can generate the whole web page for each new request.

Answer:

Yes, an application with multiple classes can have the main method. Java Virtual Method will look for the main() method in that class whose name we mentioned.

Answer:

The key differences between HashMap & HashTable are as follows:

  • HashMap is non-synchronized, but HashTable is synchronized and slower in performance.
  • HashMap uses an iterator to traverse, while the HashTable uses an enumerator to traverse.
  • The iterator in HashMap is fail-fast, but an enumerator in HashTable isn’t fail-fast.
  • HashMap permits only one null key & null values, while Hashtable doesn’t allow any key or value as null.

Answer:

A priority queue refers to an abstract data type that has a priority associated with each element. In the priority queue, elements with higher priority get served before the elements with lower priority. The order of elements is either natural or as per the comparator.

Answer:

A class with no access modifiers is visible through its package access. It means that the class can only be accessed by other interfaces & classes defined inside the same package.

Answer:

The restrictions are placed during compilation in a switch statement, wherein each case’s values must evaluate to the value that can be promoted to an int value.

Answer:

The principal use of Assertions is to check logically impossible situations like checking the state a code expects before running & its state after it finishes running. Unlike other errors or exceptional handling, Assertions are usually disabled during runtime.

Answer:

The Void class means an uninstantiable placeholder class that references the Class Object, representing the primitive Java type void.

Answer:

A Locale class object in Java represents a cultural, political, or geographical region. It is a mechanism for identifying the objects, not a container for an object. A Locale comprises the fields like script, languages, variant, country, & extensions.