Java Interview Question

Q51. Why do we need wrapper classes?

It is sometimes easier to deal with primitives as objects. Moreover most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many utility methods also. Because of these resons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection. Also we can pass them around as method parameters where a method expects an object.

Q52. What is the difference between error and an exception?

An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors and you can not repair them at runtime. While exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.).

Q53. How to create custom exceptions?

Your class should extend class Exception, or some more specific type thereof.

Q54. If I write return at the end of the try block, will the finally block still execute?

Yes even if you write return as the last statement in the try block and no exception occurs, the finally block will execute. The finally block will execute and then the control return.

Q55. If I write System.exit (0); at the end of the try block, will the finally block still execute?

No in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes.

Q56. Which methods of Serializable interface should I implement?

The serializable interface is an empty interface, it does not contain any methods. So we do not implement any methods.

Q57. What is Marker Interface in Java?

Marker Interface in java is an interface with no fields or methods within it. It is used to convey to the JVM that the class implementing an interface of this category will have some special behavior. Hence, an empty interface in java is called a marker interface.
Some examples of marker interfaces:
  • java.lang.Cloneable.
  • java.rmi.Remote.
  • java.util.EventListener.
  • Q58. What is the common usage of serialization?

    Whenever an object is to be sent over the network, objects need to be serialized. Moreover if the state of an object is to be saved, objects need to be serilazed.

    Q59. What is Externalizable interface?

    Externalizable is an interface which contains two methods readExternal and writeExternal. These methods give you a control over the serialization mechanism. Thus if your class implements this interface, you can customize the serialization process by implementing these methods.

    Q60. What happens to the static fields of a class during serialization?

    There are three exceptions in which serialization doesnot necessarily read and write to the stream. These are
    1. Serialization ignores static fields, because they are not part of any particular object state.
    2. Base class fields are only hendled if the base class itself is serializable.
    3. Transient fields.

    Q61. What one should take care of while serializing the object?

    One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.

    Q62. What is the roll of class-loader ?

    The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems because of classloaders.

    Q63. What is JIT compiler?

    The JIT compiler is enabled by default, and is activated when a Java method is called. The JIT compiler compiles the bytecodes of that method into native machine code, compiling it "just in time" to run. When a method has been compiled, the JVM calls the compiled code of that method directly instead of interpreting it.

    Q64. Can we define a interface inside the class?

    Yes, we can create interface inside the class such iterfaces are called Nested-Interface.

    Q65. What is aggregation in OOPS?

    Aggregation is a special form of association. It is also a relationship between two classes like association, however its a directional association, which means it is strictly a one way association. It represents a Has-A relationship. For example consider two classes Student class and Address class.

    Q66. What is static block ?

    The static block in java is use to initialize the static data-members of a class. It execute only once in program life-cycle at the time of class loading.

    Q67. What is singleton class?

    Singleton class controls object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes.

    Q68. Explain final, finally and finalize in Java?

    Final class can't be inherited,
    final method can't be overridden and final variable value can't be changed.
    Finally is used to place important code, it will be executed whether exception is handled or not.
    Finalize is used to perform clean up processing just before object is garbage collected.

    Q69. What is JFC in Java?

    JFC is short for Java Foundation Classes, which encompass a group of features for building graphical user interfaces (GUIs) and adding rich graphics functionality and interactivity to Java applications. It is defined as containing the features shown in the table below. Feature. ... Swing GUI Components.

    Q70. What is variable argument in Java?

    Varargs is a helper syntax and it enables use of variable number of arguments in a method call. In method definition variable aruguments are indicated by elipsis (…) and is called as 'variable arity method' and 'variable arity parameter' in java language specification.

    Q71. What is collection in Java?

    The Java collections framework (JCF) is a set of classes and interfaces that implement commonly reusable collection data structures. Although referred to as a framework, it works in a manner of a library. The JCF provides both interfaces that define various collections and classes that implement them.

    Q72. What is transient variable in java?

    A transient variable is a variable that cannot be serialized. The transient keyword can be used to indicate the Java Virtual Machine that the variable is not part of the persistent state of the object.

    Q73. What is volatile variable?

    Declaring a volatile Java variable means: The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory"; Access to the variable acts as though it is enclosed in a synchronized block, synchronized on itself.

    Q74. What is functional interface?

    Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method 'compareTo' is used for comparison purpose. Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions.

    Q75. What is Lambda Expression in Java.

    Java lambda expressions are new in Java 8. Java lambda expressions are Java's first step into functional programming. A Java lambda expression is thus a function which can be created without belonging to any class.
    Java Lambda expression is use for Anonymous Classes. Before lambda expressions, anonymous inner classes were an option.

    (8AM to 10PM):

    +91-8527238801 , +91-9451396824

    © 2017,, All rights reserved