Java Interview Questions and Answers – Part 5

Java Interview Questions and Answers for experienced
1. What is the difference between error and an exception?
  • Errors are abnormal conditions that should never occur. Not to be confused with the compile time errors.
  • A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught.
  • An exception is a condition that a programmer has already predicted. These can be caught unlike errors. They occur due to bad input, etc.
2. What is checked and unchecked exception?
  • The exceptions that are not the sub classes of RuntimeException and its sub classes are called checked exceptions. A checked exception forces the developer to handle it. For example, IOException is a checked exception.
  • The exceptions that are the exceptions of RuntimeException and its sub classes are called unchecked exceptions. The class Error and its subclasses also are unchecked exceptions.
  • An unchecked exception does not force the developer to handle it, either by catch block or by throws clauses. The developer may not predict these exceptions to handle.
    For example, ArrayIndexOutOfBoundsException is an unchecked exception.
3. What is a stream?

Stream is a flow of data from a source to sink. Typically, our program is at one end of the stream and some other node like file at other end.

4. What are the two types of data supported in streams ?

Raw Bytes and Unicode Characters. Typically term stream refers to byte streams and term reader and writer refers to character stream.

5. Which are the Java classes that implement character streams ?

Reader and Writer Classes

6. Which are the Java classes that implement byte streams ?

Inputstream and OutputStream Classes

7. What are the classes used to read and write entire line from a file ?

BufferedReader and BufferedWriter.

8. Explain Object Class ?

Object Class is the root class for all classes in Java. When we declare a class with out any inheritance (extends), Java implicitly extends that class from object class. For example

is same as

9. How to compare to Objects ?

To compare two objects, Java use “equals” method. For example

10. What are class loaders?
  • The class loader describes the behavior of converting a named class into the bits responsible for implementing that class.
  • Class loaders eradicate the JREs need to know anything about files and file systems when running Java programs.
  • A class loader creates a flat name space of class bodies that are referenced by a string name and are written as:
    Class r = loadClass(String className, boolean resolveIt);
  • Class loaders loads classes into JVM at run time. The classes are loaded into JVM by some instances of a java.lang.ClassLoader class.
11. What is static and dynamic class loading in Java? 
  • Static Class Loading: Creating objects and instance using new keyword is known as static class loading. The retrieval of class definition and instantiation of the object is done at compile time.
  • Dynamic Class Loading: Loading classes using Class.forName() method. Dynamic class loading is done when the name of the class is not known at compile time
12. What are command line arguments ?

When a Java Program is launched, we can provide zero or more command line arguments to specify the configuration information. These arguments are string. We can provide command line arguments as shown below

13. Explain the difference between abstract class and interfaces.
  • Abstract class can’t be instantiated, it can only be inherited while interfaces has to be implemented.
  • Abstract class can have implemented methods which interfaces can have only definitions of the methods without implementation.

The differences between abstract class and interface are as follows:

Abstract Class Interface
Can have abstract methods and concrete methods Can have only method signatures and static final members
All methods are not public by default All methods by default abstract and public
An abstract class can extend another abstract class An interface can extend another interface but not a class
The extended class can override the methods of its super class and its hierarchy An implementing class can implement multiple interfaces
All abstract methods must have abstract access modifier All methods in an interface must not have abstract access modifier

The following examples illustrate the differences:

14. How to copy arrays?

We will explain copying one array to another with the help of an example. Let us consider to arrays intArray1 and intArray2. Java uses arraycopy() to copy between arrays.

Arguments of “arraycopy” are
Argument 1 – Source Array
Argument 2 – Starting position of the source array from which elements are copied
Argument 3 – Destination Array
Argument 4 – Starting position of the destination array to which elements are copied
Argument 5 – No of elements to be copied from the start position.

Profile photo of Siva

About Siva

Senior Hadoop developer with 4 years of experience in designing and architecture solutions for the Big Data domain and has been involved with several complex engagements. Technical strengths include Hadoop, YARN, Mapreduce, Hive, Sqoop, Flume, Pig, HBase, Phoenix, Oozie, Falcon, Kafka, Storm, Spark, MySQL and Java.

Leave a comment

Your email address will not be published. Required fields are marked *