Java Interview Questions - Adhithya Srinivasan Edition

5
Q: Define immutable object?

A: An immutable object can’t be changed once it is created.
Q: Explain the usage of this() with constructors?

A: It is used with variables or methods and used to call constructer of same class.
Q: Explain Set Interface?

A: It is a collection of element which cannot contain duplicate elements. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.
Q: Explain TreeSet?

A: It is a Set implemented when we want elements in a sorted order.
Q: What is Comparable Interface?

A: It is used to sort collections and arrays of objects using the collections.sort() and java.utils. The objects of the class implementing the Comparable interface can be ordered.
Q: Difference between throw and throws?

A: It includes:

   Throw is used to trigger an exception where as throws is used in declaration of exception.

   Without throws, Checked exception cannot be handled where as checked exception can be propagated with throws.

Q: Explain the following line used under Java Program:

public static void main (String args[ ])

A: The following shows the explanation individually:

   public: it is the access specifier.

   static: it allows main() to be called without instantiating a particular instance of a class.

   void: it affirns the compiler that no value is returned by main().

   main(): this method is called at the beginning of a Java program.

   String args[ ]: args parameter is an instance array of class String

Q: Define JRE i.e. Java Runtime Environment?

A: Java Runtime Environment is an implementation of the Java Virtual Machine which executes Java programs. It provides the minimum requirements for executing a Java application;
Q: What is JAR file?

A: JAR files is Java Archive fles and it aggregates many files into one. It holds Java classes in a library. JAR files are built on ZIP file format and have .jar file extension.
Q: What is a WAR file?

A: This is Web Archive File and used to store XML, java classes, and JavaServer pages. which is used to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML files, static Web pages etc.
Q: Define JIT compiler?

A: It improves the runtime performance of computer programs based on bytecode.
Q: What is the difference between object oriented programming language and object based programming language?

A: Object based programming languages follow all the features of OOPs except Inheritance. JavaScript is an example of object based programming languages
Q: What is the purpose of default constructor?

A: The java compiler creates a default constructor only if there is no constructor in the class.
Q: Can a constructor be made final?

A: No, this is not possible.
Q: What is static block?

A: It is used to initialize the static data member, It is excuted before main method at the time of classloading.
Q: Define composition?

A: Holding the reference of the other class within some other class is known as composition.
Q: What is function overloading?

A: If a class has multiple functions by same name but different parameters, it is known as Method Overloading.
Q: What is function overriding?

A: If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding.
Q: Difference between Overloading and Overriding?

A: Method overloading increases the readability of the program. Method overriding provides the specific implementation of the method that is already provided by its super class parameter must be different in case of overloading, parameter must be same in case of overriding.
Q: What is final class?

A: Final classes are created so the methods implemented by that class cannot be overridden. It can’t be inherited.
Q: What is NullPointerException?

A: A NullPointerException is thrown when calling the instance method of a null object, accessing or modifying the field of a null object etc.
Q: What are the ways in which a thread can enter the waiting state?

A: A thread can enter the waiting state by invoking its sleep() method, by blocking on IO, by unsuccessfully attempting to acquire an object's lock, or by invoking an object's wait() method. It can also enter the waiting state by invoking its (deprecated) suspend() method.
Q: How does multi-threading take place on a computer with a single CPU?

A: The operating system's task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.
Q: What invokes a thread's run() method?

A: After a thread is started, via its start() method of the Thread class, the JVM invokes the thread's run() method when the thread is initially executed.
Q: Does it matter in what order catch statements for FileNotFoundException and IOException are written?

A: Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.
Q: What is the difference between yielding and sleeping?

A: When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state.
Q: Why Vector class is used?

A: The Vector class provides the capability to implement a growable array of objects. Vector proves to be very useful if you don't know the size of the array in advance, or you just need one that can change sizes over the lifetime of a program.
Q: How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?

A: 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.
Q: What are Wrapper classes?

A: These are classes that allow primitive types to be accessed as objects. Example: Integer, Character, Double, Boolean etc.
Q: What is the difference between a Window and a Frame?

A: The Frame class extends Window to define a main application window that can have a menu bar.
Q: Which package has light weight components?

A: javax.Swing package. All components in Swing, except JApplet, JDialog, JFrame and JWindow are lightweight components.
Q: What is the difference between the paint() and repaint() methods?

A: The paint() method supports painting via a Graphics object. The repaint() method is used to cause paint() to be invoked by the AWT painting thread.
Q: What is the purpose of File class?

A: It is used to create objects that provide access to the files and directories of a local file system.
Q: What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?

A: The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.
Q: Which class should you use to obtain design information about an object?

A: The Class class is used to obtain information about an object's design and java.lang.Class class instance represent classes, interfaces in a running Java application.
Q: What is the difference between static and non-static variables?

A: A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.
Q: What is Serialization and deserialization?

A: Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects.
Q: What are use cases?

A: It is part of the analysis of a program and describes a situation that a program might encounter and what behavior the program should exhibit in that circumstance.
Q: Explain the use of sublass in a Java program?

A: Sub class inherits all the public and protected methods and the implementation. It also inherits all the default modifier methods and their implementation.
Q: How to add menushortcut to menu item?

A: If there is a button instance called b1, you may add menu short cut by calling b1.setMnemonic('F'), so the user may be able to use Alt+F to click the button.
Q: Can you write a Java class that could be used both as an applet as well as an application?

A: Yes, just add a main() method to the applet.
Q: What is the difference between Swing and AWT components?

A: AWT components are heavy-weight, whereas Swing components are lightweight. Heavy weight components depend on the local windowing toolkit. For example, java.awt.Button is a heavy weight component, when it is running on the Java platform for Unix platform, it maps to a real Motif button.
Q: What's the difference between constructors and other methods?

A: Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.
Q: Is there any limitation of using Inheritance?

A: Yes, since inheritance inherits everything from the super class and interface, it may make the subclass too clustering and sometimes error-prone when dynamic overriding or dynamic overloading in some situation.
Q: When is the ArrayStoreException thrown?

A: When copying elements between different arrays, if the source or destination arguments are not arrays or their types are not compatible, an ArrayStoreException will be thrown.
Q: Can you call one constructor from another if a class has multiple constructors?

A: Yes, use this() syntax.
Q: What's the difference between the methods sleep() and wait()?

A: The code sleep(2000); puts thread aside for exactly two seconds. The code wait(2000), causes a wait of up to two second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.
Q: When ArithmeticException is thrown?

A: The ArithmeticException is thrown when integer is divided by zero or taking the remainder of a number by zero. It is never thrown in floating-point operations.

    Java Interview Questions - Adhithya Srinivasan Edition