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.

Java Interview Questions and Answers – Part 4

Java Interview Questions and Answers
1. What is the difference between the Boolean & operator and the && operator?
  • If an expression involving the Boolean & operator is evaluated, both operands are evaluated. Then the & operator is applied to the operand.
  • When an expression involving the && operator is evaluated, the first operand is evaluated. If the first operand returns a value of true then the second operand is evaluated. The && operator is then applied to the first and second operands. If the first operand evaluates to false, the evaluation of the second operand is skipped.
2. What is the difference between the >> and >>> operators?
  • The >> operator carries the sign bit when shifting right.
  • The >>> zero-fills bits that have been shifted out.
3. Which Java operator is right associative?

The = operator is right associative.

4. Can a double value be cast to a byte?

Yes, a double value can be cast to a byte.

5. To what value, a String variable be automatically initialized?

The default value of an String type is null.

6. What is a transient variable?

Transient variable is a variable that may not be serialized.

7. To what value, a Boolean type variable be automatically initialized?

The default value of the Boolean type is false.

8. What is the difference between the prefix and postfix forms of the ++  operator?
  • The prefix form performs the increment operation and returns the value of the increment operation.
  • The postfix form returns the current value all of the expression and then performs the increment operation on that value.
9. What happens when you add a double value to a String?

The result is a String object.

10. How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
  • Unicode requires 16 bits and ASCII require 7 bits Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits.
  • UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.
11. How does Java handle integer overflows and underflow?

It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.

12. How many static initializes can we have ?

As many as we want, but the static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope.

13. What is the difference between an if statement and a switch statement?
  • The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed.
  • The switch statement is used to select among multiple alternatives. It uses an int
    expression to determine which alternative should be executed.
14. What is numeric promotion?
  • Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place.
  • In numerical promotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required.
15. Which characters may be used as the second character of an identifier, but not as the first character of an identifier?

The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.

16. What restrictions are placed on the values of each case of a switch statement?

During compilation, the values of each case of a switch statement must evaluate to a value that can be promoted to an int value.

17. What is a native method?

A native method is a method that is implemented in a language other than Java.

Java applications can call code written in C, C++, or assembler. This is sometimes done for performance and sometimes to access the underlying host operating system or GUI API using the JNI. The steps for doing that are:

  • First write the Java code and compile it
  • Then create a C header file
  • Create C stubs file
  • Write the C code
  • Create shared code library (or DLL)
  • Run application

It is possible to execute C or C++ code by a Java application. This process is done by taking certain performance issues and access to the underlying host operating systems into consideration. To execute these programs, Java Native Interface framework is used.

18. What are order of precedence and associativity, and how are they used?

Order of precedence determines the order in which operators are evaluated in expressions. Associatity determines whether an expression is evaluated left-to-right or right-to-left.

19. What is the difference between inner class and nested class?
  • When a class is defined within a scope of another class, then it becomes inner class.
  • If the access modifier of the inner class is static, then it becomes nested class.
20. What is the difference between a public and a non-public class?

A public class may be accessed outside of its package. A non-public class may not be accessed outside of its package.

Java Interview Questions and Answers – Part 3

Java Interview Questions and Answers
1. What is JIT Compiler ?

JIT compiler coverts a byte code of one operating system to the current operating system’s executable code. This instruction is given by JVM of the current system. The byte is compiled into native platform code. The execution speed is greatly improved by this process, because the platform is directly executing the native byte code.

2. What is the difference between the String and StringBuffer classes?

String class is immutable. The characters of string objects can not be changed.This is because the String class is declared as final due to which it cannot be inherited and so its functions cannot be overridden.

StringBuffer is mutable. The characters of StringBuffer objects can be modified. It has similar functionality of String class and can manipulate the string objects.

StringBuffer’s performance is faster than String when concatenating other strings. String class concatenates the other string by using + sign. After concatenation, it returns another String object. This string object can be reassigned to the original String reference.

Ex : str = str + “World!” ; // where str=”Hello”

The concatenation creates a temporary String object, and is reassigned to str.
Using StringBuffer, the concatenation is done by using append() method.

Ex : str.append(“World!”); // str is a StringBuffer object and assigned “Hello”.

String class provides the concatenation facility with concat() method. But it creates a new String object and returns it. Where as the StringBuffer just appends or adds the new string to the existing StringBuffer object. This process is pretty quicker than concatenation using + or concat() method of String class.

3. What is the difference between a break statement and a continue statement?

A break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement.

4. Can an anonymous class be declared as implementing an interface and extending a class?

Partly Yes. An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.

5. What are the legal operands of the instanceof operator?

The left operand is an object reference or null value and the right operand is a class, interface, or array type.

6. Can a method be overloaded based on different return type but same argument type ?

No, because the methods can be called without using their return type in which case there is ambiguity for the compiler.

7. What is constructor chaining and how is it achieved in Java ?

A child object constructor always first needs to construct its parent (which in turn calls its parent constructor.). In Java it is done via an implicit call to the no-args constructor as the first statement.

8. If we have defined only one constructor with ClassName(arg1) then can we access default constructor without any arguments.

No. We can’t make a call to new ClassName(). But we can create objects by passing arguments to constructor call. If we need to use default constructor, we must provide default constructor in the class. 

9. Is it mandatory to provide constructor in each class to instantiate its objects ?

No. It is not mandatory to provide constructor definition, if we do not provide any such constructors, java provides constructors automatically with the default values for all fields. But if we provide at least one constructor of our own, then we must provide default constructor to instantiate it with no arguments. Otherwise Java will not provide default constructor.

10. Can a for statement loop indefinitely?

Yes, a for statement can loop indefinitely. For example, consider the following: for(;;);

11. What are static methods?

Static methods are the methods, which are declared with the keyword as static. These methods are the modifiers for the class methods. They are used to affect the entire class not the instance of it. These methods are always invoked without reference to a particular instance of a class.

The restrictions that have been imposed are as follows:

  • A static method can only call and access other static methods and data respectively.
  • A static method cannot reference to the current object using keywords “super” or “this”.
12. What is method overloading?

Method overloading is a type of polymorphism that includes two or more methods with the same name in same class but, the condition is that it should have different arguments, otherwise an error might occur. This method overloading is very advantageous, as it allows us to implement many methods with the same syntax and semantics.

The Overloaded methods that should follow the criteria are as follows:

  • Overloaded methods can change the return type and access modifier
  • Overloaded methods can declare new or broader checked exceptions
  • A method can be overloaded in the same class or in a subclass
13. What is method overriding?

Method overriding is a type of polymorphism that is different from the overloading method, as it allows you to declare the method with the same arguments. The advantage of using this is that it defines the behavior of the specific subclass.

It doesn’t provide very strict restrictive access modifier. The method that marked as public and protected can’t be overridden. We also cannot override a method marked final and static.

14. What is super()?

super() keyword is used to access the method and member variables of the super-class. It can refer the member or the hidden variable of the super-class. It can also invoke the overridden method. super() should be used to access the hidden variable and it should be the first keyword written in the constructor.

Java Interview Questions and Answers – Part 2

This post is continuation for previous post on Java Interview Questions and Answers. We will try to focus only the concepts that are required for Hadoop Developers and Hadoop Administrators.

Java Interview Questions and Answers:
1. What is the difference between a JDK and a JRE ?

JDK, Java Development Kit is for development purpose and it includes execution environment also. But JRE is purely java run time environment and hence we will not be able to compile our source files using a JRE.

2. Is Java a pure object oriented language ?

No. Java uses primitive data types and hence is not a pure object oriented language.

3. Are arrays primitive data types ?

No. In Java, Arrays are objects.

4. Can a class declared as private be accessed outside it’s package ?


5. What is the access scope of a protected method ?

A protected method can be accessed by the classes within the same package  or by the subclasses of the class in any package.

6. What is the purpose of declaring a variable as final ?

A final variable’s value can’t be changed. final variables should be initialized  before using them.

7. What is the impact of declaring a method as final ?

A method declared as final can’t be overridden. A sub-class can’t have the same method signature with a different implementation.

8. When we don’t want our class to be inherited by any other class. What should we do?

We should declare the class as final. But we can’t define our class as final, if it is an abstract class. A class declared as final can’t be extended by any other class.

9. Can we declare a static variable inside a method ?

No. Static variables are class level variables and they can’t be declared inside a method. If declared, the class will not compile.

10. What is an Abstract Class ?

A Class which doesn’t provide complete implementation is known as an abstract class. Abstract classes enforce abstraction.

11. Can an abstract class be declared final?

No. An abstract class without being inherited is of no use and hence will result in compile time error.

12. What is the use of an abstract variable?

Variables can’t be declared as abstract. only classes and methods can be declared as abstract.

13. Can we create an object of an abstract class?

No. Abstract classes can’t be instantiated.

14. Can an abstract class be defined without any abstract methods?

Yes it’s possible. This is basically to avoid instance creation of the class.

15. Class C implements Interface I containing method m1 and m2 declarations. Class C has provided implementation for method m2. Can i create an object of Class C?

No. It is not possible. Class C should provide implementation for all the methods in the Interface I. Since Class C didn’t provide implementation for m1 method, it has to be declared as abstract. Abstract classes can’t be instantiated.

16. Can a method inside an Interface be declared as final?

No. Doing so will result in compilation error. public and abstract
are the only applicable modifiers for method declaration in an interface.

17. Can an Interface implement another Interface?

No. Interfaces doesn’t provide implementation hence an interface cannot implement another interface.

18. Can an Interface extend another Interface?

Yes an Interface can inherit another Interface, so an Interface can extend more than one Interface.

19. Can a Class extend more than one Class?

No. A Class can extend only one class but can implement any number of Interfaces.

20. Can an Interface be final?

No. Doing so will result in compilation error.

A few more java interview questions and answers are continued in the next page.

Continue reading

Java Interview Questions and Answers

In this post, we will try to cover some of the important Java interview questions and answers that are required for aspirants facing hadoop developer interviews. We will touch base only the concepts that are needed for an hadoop developer or administrator.

Java Interview Questions and Answers for Experienced:
1. What is a JVM ?

JVM is Java Virtual Machine which is a run time environment for the compiled java class files.

2.  What is Platform Independence ? Are JVM’s Platform Independent ?

The ability to write and compile programs in  one platform (eg Windows) and can execute the same compiled object in any other supported platform (eg Linux) is called as Platform independence. Java is Platform Independent. I.e. We can write and compile java programs on windows machine and execute the classes on Linux machine. But JVM is not platform independent as it is specific to each platform run time environment.

3. What is the difference between a break statement and a continue

A break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement.

4. Does Java support pointers?

No. Because of their complexity and reliability issues Java doesn’t support Pointers.

5. Does Java support Multiple Inheritance? If not, How can we achieve Multiple Inheritance in Java?

No. But a class can implement multiple interfaces to overcome the lack of multiple inheritance.

6. How to compile all the files in a Folder ?

Use “javac *.java” command from the directory location of the java files to compile all the files at a time.

7. How to place compiled Java class files in a different directory ?

Use “javac -d <directory location> <SourceProgram>.java” command to place the compiled class files into a separate directory location. We need to give our required destination directory location in -d option.

8. What is the base class of all classes ?


9. What is difference between Path and Classpath ?

Path and Classpath are operating system level environment variables. Path is
used to define where the system can find the executable files and classpath is used to specify the location of .class files.

10. What are local variables ?

Variables declared within a block of code, like methods are called Local variables. Local variables should be initialized before accessing them.

11. What are instance variables ?

Variables that are defined at the class level are known as Instance variables. Instance
variables need not be initialized before using them as they are automatically initialized to their default values.

12. What are constant variable in Java ?

The variables declared as static and final are constant variables. So only one copy of the variable exists for all instances of the class and the value can’t be changed also.
Example: static final int PI = 2.14;

13. What is a Constructor ?

Constructors are used to initialize an object. Constructor’s are called when a method is initialized. Constructors cannot be inherited.

  • Constructor has the same name as the class.
  • Class can have more than one constructor.
  • Constructor can take zero, one, or more parameters.
  • Constructor has no return value.
  • Constructor is always called with the new operator.


14. What is a Java package ?

Java Packages are used to group related classes. Packages should be defined at the start of source file. A class can have only one package. If packages are not declared it belongs to default package. Packages are hierarchical separated by columns. An example package definition is given below

15. What is the restriction for a package statement within a java source code file ?

A package statement must appear as the first line in a source code file (excluding blank lines and comments).

16. Which package is imported by default ?

java.lang package is imported by default. It is imported even without a package declaration.

17. What are private, protected and public access specifiers ?

Private is the most restrictive, while public is the least restrictive. There is no real difference between protected and the default type (also known as package protected) within the context of the same package, however the protected keyword allows visibility to a derived class in a different package.

18. What is the return type of the main() method ?

main() method doesn’t return anything. Hence it is declared as void.

19. Is it mandatory to define a main() method in all java classes ?

No not required. main() method should be defined only if the source class is
a java application.

20. When will we define a method as static ?

When a method needs to be accessed even before the creation of the object
of the class then we should declare the method as static.