Core Java Interview Questions- Part 4

Core Java Interview Questions- Part 4Practice is the key when it comes to improving your interview skills. On this page, you’ll find 200 core Java questions, covering questions on  OOPs, Overloading, and Overriding, Inheritance, Exception Handling, Packages, Interfaces, etc. So, start practicing for your next interview with our carefully selected interview questions.

By studying these questions, you will strengthen your knowledge and be able to answer with confidence in your interviews. As these questions have simple answers, you will not have to look anywhere for the answers.

Our core Java interview questions page is a complete guide to interview preparation. It will help you prepare for various jobs including Java developer, backend developer, full-stack developer, and more. So, let’s get started.

Answer:

The Break Statement is a keyword that is useful for terminating the execution of instructions in a loop. It is the best way to end the execution in a loop. There are multiple uses of the Break Statement keyword, but its best use comes in the loop, wherein it terminates the execution of a code inside the loop & code execution resumes after the loop.

Answer:

Polymorphism in Java is the ability of an object to take several forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. There are two types of polymorphism in Java:

  • compile-time polymorphism and
  • runtime polymorphism.cal

Answer:

Encapsulation refers to a mechanism of wrapping the data or variables along with code or methods in a single unit. In encapsulation, the variables (data) of a class are hidden from other classes; thus, it can only be accessed through their current class’s methods.

Answer:

Starvation represents a situation in which a thread cannot progress due to its inability to gain regular access to shared resources. It occurs when the shared resources are made unavailable for a long period by the “greedy” threads.

Answer:

Both ArrayList & Vector differs in the following ways:

  • A vector is synchronized, whereas an ArrayList is non-synchronized.
  • When re-sized, an ArrayList grows by half of its size, while a vector doubles the size of itself by default.

Answer:

Following are the major differences between an Iterator and ListIterator:

  • An Iterator is used to traverse a set, list, & map, whereas a ListIterator is only useful for traversing a list.
  • When using an Iterator, we can traverse only in the forward direction, but ListIterator enables us to traverse in both the forward and backward directions.

Answer:

Significant difference between Enumeration & an Iterator is that an Iterator allows you to remove elements from the collection while traversing, but Enumeration doesn’t allow that.

Answer:

Event handling is a fundamental concept of Java programming that is used to create event-driven programs such as GUI-based windows applications, Applets & Web Applications.

Answer:

Default methods allow developers to add new functionality to the existing interfaces without breaking the old code. Before Java 8, if a new method was added to an interface, the classes’ s implementations were bound to override the new method, even if they did not use the new functionality. However, with Java 8, we can add the new method’s default implementation by using the default keyword.

Answer:

BlockingQueue is used to hold a new task if the number of threads is greater than the CorePoolSize. ThreadPool Executor has the CorePoolSize which governs how many active threads spawn up, with every incoming request. Once the CorePoolSize threads are active, new incoming tasks are added to the Queue, and these threads are actively polling from the Queue to execute them.

Answer:

Though both concurrent & synchronized collections are used for thread safety, the performance of the former is better than the latter. As synchronized collection acquires a lock on the full collection and puts a stop on reads and writes.  Whereas the concurrent collection divides the entire collection into segments and only acquires locks on a particular segment, while other segments remain open for reads & write.

Answer:

During compilation, the Java compiler converts the source code into a low-level JVM interpretable code called byte code. Unlike the non-executable codes generated by other compilers, the JVM interpreter processes the non-executable code & executes it on any machine. Thus, it removes the platform dependency.

Answer:

notify() method provides notification to only one random thread amongst various threats waiting for the lock, whereas notifyAll provides notification to all the threads waiting for a monitor.

Answer:

Following are the major differences between them:

  • CountDownLatch is a construct that a thread looks out for while different threads tally down on the latch until it arrives at zero. Whereas, CyclicBarrier is a reusable construct where a gathering of threads stands by together until the entirety of the threads shows up. By then, the barrier is broken and a move can alternatively be made.
  • CountDownLatch keeps up a count of tasks. While, CyclicBarrier keeps up a count of threads.
  • In CountDownLatch single thread can countdown more than once, this would reduce count by number of times countdown() method is called. In contrast, CyclicBarrier single thread can call awaits only once which would reduce barrier count by one only, even if call awaits() method more than once.
  • CountDownLatch is advanceable. While, CyclicBarrier is not advanceable.

Answer:

StringBuffer is synchronized & thread-safe, which means that two threads cannot call the methods of StringBuffer at the same time. However, StringBuilder is non-synchronized, thus not thread-safe.

Answer:

Following are the major differences between them:

Inheritance is one in which a new class is created (derived class) that inherits the features from the already existing class(means Base class). Whereas, polymorphism is that which can be defined in multiple forms.

Inheritance is basically applied to classes. Whereas, polymorphism is basically applied to functions or methods.

Inheritance supports the concept of reusability and reduces code length in object-oriented programming. While, Polymorphism allows the object to decide which form of the function to implement at compile-time (overloading) as well as run-time (overriding).

Answer:

Following are the differences between them:

  • SAX is called a Simple API for XML Parsing. While, DOM is called as Document Object Model.
  • SAX is an event-based parser. Whereas, DOM stays in a tree structure.
  • SAX Parser is slower than DOM Parser.
  • SAX Parser is suitable for making XML files in Java. In contrast, DOM is not good at making XML files in low memory.
  • SAX Parser loads small part of the XML file is only loaded in memory. While, DOM Parser loads whole XML documents in memory.

Answer:

The main difference between fail-safe iterator & fail-fast iterator is that the former doesn’t throw an exception, in contrast to the latter. Fail-safe iterator works on a clone of the collection instead of the original collection.

Answer:

Both Comparable & Comparator are an interface used to sort the elements of a collection. A Comparator interface belongs to java.util package, whereas a Comparable belongs to java.lang package. Comparator sort collection using two objects applying to it, while Comparable interface only uses one object.

Answer:

Checked Exception is checked during compile-time of a program, while an Unchecked Exception is checked during run-time. Both Checked and Unchecked Exceptions can be created manually & handled using try/catch/ finally.