Pages

Java Question

Java Questions
1. What is a Java Refresher ?

A Java program is mostly a collection of objects talking to other objects by invoking each other's methods. Every object is of a certain type, and that type is defined by a class or an interface.


2. What is Java Virtual Machine (JVM) ?

JVM is an abstract computing machine. JVM is a platform independent execution environment that converts Java byte code into machine language and executes it.


3. What is a Class ?
Definition 1 : A Template that describes the kinds of state and behavior that objects of its type support.
Definition 2 : A Class is group of objects that have common property. It is a template or blue print from which objects are created.
A Class in Java can contains :
1. Data Members 2. Block 3. Constructor 4. Method

4. What is an Object ?
Definition 1 : A runtime entity that has state and behavior is known as an Object.
Definition 2 : At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviors defined by its class.
An Object has three characterstics :
1. Identity 2. State (Instance Variables) 3. Behavior (Methods)

5. What is a State (Instance Variables) ?

Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's state.


6. What is a Behavior (Methods)?

When a programmer creates a class, she creates methods for that class. Methods are where the class' logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.


7. What is a Encapsulation?

Encapsulation is the process of wrapping up of data and methods together into single unit.


8. What is a Abstraction?

Abstraction is the process of hiding the implementation details and showing only functionalities to the user.


9. What is an Inheritance?
Definition 1 : Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. Inheritance represent IS-A relationship.
Definition 2 : Inheritance is the method by which objects of one class get the properties of objects of another class. Inheritance represent IS-A relationship.

10. What is a Polymorphism ?
Definition 1 : Polymorphism is the ability of methods to behave differently based on the object calling it.
Definition 2 : Polymorphism allows the same functions to act differently classes.

11. What are the Access Modifiers ?
1. Public 2. Private 3. Protected 4. Default

12. What is a Public ?

Public members are visible to sub classes if those sub classes are in the same package as the super class. For a sub class outside the packages, the public members can be access. (or) A class declaration with the public keyword gives all class from all packages access to the public class.


13. What is a Private ?

Private members are visible only current class only. Private member are not visible other classes in same package.


14. What is a Protected ?

Protected members are visible to sub classes if those sub classes are in the same package as the super class. For a sub class outside the packages, the protected members can be access through inheritance.


15. What is a Default ?

Default members are visible to sub classes if those sub classes are in the same package as the super class.


16. What are the Non - Access Modifiers ?
1. Static 2. Final 3. Native 4. Volatile 5. Synchronized 6. Abstract 7. Transient 8. Strictfp

17. What is Volatile ? What are the rules ?

The volatile modifier tells the JVM that a thread accessing variable must always reconcile its own private copy of the variable with the master copy in memory.

Rules :
  1. Volatile applied variable only.
  2. Volatile can be applied to static variables.
  3. Volatile can not be applied to final variables.
  4. Volatile is used in multi-processor environment.
  5. Volatile and transient cannot come together.

18. What is a Transient?

A Transient variable is a variable that can not 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. Transient is a keyword. Transient can be applied only variables. Transient variable can not be seriablized.


19. What is Native ? What are the rules ?

The Native method indicates that a method is implemented in platform dependent.

Rules :
  1. Native can be applies to only methods.
  2. Native can be applied to static method also.
  3. Native cannot be applied abstract.
  4. Native can throw exception.
  5. Native method is like an abstract method.

20. What is a Synchronized

Synchronized is a keyword. Synchronized can be applied only methods. Synchronized method can be accessed only one thread at a time. (or) The synchronized keyword indicates that a method can be accessed by only one thread at a time.


21. What is a Strictfp?

Strictfp is a keyword in the Java programming language that restricts floating point calculations to ensure portability. The Strictfp was introduced into Java with the Java Virtual Machine version 1.2. Strictfp applied classes and methods only. Strictfp can be applied methods and classes.


22. What is a abstract ?

An abstract is a keyword. An abstract can be applied classes and methods.
Abstract Method:- An Abstract Method is a method that can be declared but not implemented.
Abstract Class:- An abstract class is a class that can be declared abstract key word. An abstract class may or may not be abstract methods. An abstract class can not make an object but can be sub classed.


23. What is a final ?

Final is a keyword. Final can be applied variables, methods and classes. Final Variables can not be modified. Final methods can be overloading but can not overriding. Final Classes can not extended.


24. What is a blank final variable ?

A final variable that is not initialized at the time of declaration is known as blank final variable. It can be initialized only in constructor.

Example : final String panCardNo;

25. What is a static final variable ?

A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.


26. What is a Throw-able ?

Throw-able is a class that represents all errors and exception which may occur in Java Virtual Machine.


27. What is a Exception ?

Exception is an event that disrupt the normal flow of the program.


28. What is a Exception Handling ?

Exception Handling is a mechanism to handle the run time error.


29. What is a Try Block ?

Try Block is used to enclose the code that might throw an exception. Try Block must be declared inside the method only.


30. What is a Catch Block ?

Catch Block is used to handle the exceptions. Catch Block must be declared after the try block inside the method only.


31. Which is the famous term used to describe Java ?

WORA mean Write One Run Anywhere. Java is guaranteed to be Write Once, Run Anywhere.


32. What is Iterator ?

Iterator is an interface that contains methods to retrieve the elements one by one from a collection object. Iterator is used to retrieve the elements in only forward direction.

Iterator has 3 Methods :
  1. boolean hasNext() method return true if the iterator has more elements.
  2. element next() method returns the next element in the iterator.
  3. void remove() method removes from the collection the last element returned by the iterator.

33. What is Enumeration ?

Enumeration is an interface that contains methods to retrieve the elements one by one from a collection object.

Enumeration has two methods :
  1. boolean hasMoreElements() method returns true if the enumeration has more elements.
  2. element nextElements() method returns the next element in the enumeration.

34. What is ListIterator ?

ListIterator is an interface that contains methods to retrieve the elements one by one form a collection object. ListIterator is used to retrieve the elements in forward and backward direction.

Forward Direction Iteration
  1. hasNext() method returns true when the list has more elements to traverse while traversing in the forward direction
  2. next() method returns the next element of the list and advances the position of the cursor.
  3. nextIndex() method returns the index of the element that would be returned on calling the next() function.
Backward Direction Iteration
  1. hasPrevious() method returns true when the list has more elements to traverse while traversing in the reverse direction
  2. previous() method returns the previous element of the list and shifts the cursor one position backwards.
  3. previousIndex() method returns the index of the element that would be returned on calling the previous() function.

35. What is Literals ? What are they ?

A Literal is a value that is stored into a variable directly in the program. Literals are :

1. Integer Literal 2. Float Literal 3. Character Literal 4. String Literal 5. Boolean Literal

36. Priority of operators in Java ?
  1. First, the contents inside the braces : ( ) and [ ] will be executed.
  2. Next, ++ and -- will be executed.
  3. Next, *, / and % will be executed.
  4. Next, + and - will be executed.
  5. Next, Relational operators are executed.
  6. Next, Boolean and Bit wise operators are executed.
  7. Next, Logical operators are executed.
  8. Then, Ternary operators are executed.
  9. Assignment operators are executed at the last.

37. What is Eaves Dropping ?

Eaves Dropping means reading others' data illegally on Internet.


38. What is Tampering ?

Not only reading others' data but also modifying on Internet.


39. What is Object Cloning ?

The Object Cloning is used to create the extra copy of an object.


40. How many types have an Object Cloning ?

They are two types have an Object Cloning. They are :

1. Shallow Cloning 2. Deep Cloning

41. What is Shallow Cloning ?

When the cloned object is modified same modification will also affect the original object.


42. What is Deep Cloning ?

When the cloned object is modified, if the original is not modified.


42. What is method signature ?

Method Signature represents the method name along with method parameters.


43. What is arguments ?

The things you specify between the parentheses when you are invoking a method.
doStuff("a",2); // invoking doStuff, so a and 2 are arguments.


44. What is parameters ?

The things in the method's signature that indicates what the methods must receive when it's invoked.
void doStuff(String a, int b); // We are expecting two parameters are String and int.


45. How many Bitwise operators are available in Java ?
  1. Bitwise Complement Operator (∼).
  2. Bitwise AND Operator (&).
  3. Bitwise OR Operator (|).
  4. Bitwise XOR Operator (^).
  5. Bitwise Lift Shift Operator (<<).
  6. Bitwise Right Shift Operator (>>).
  7. Bitwise Zero Fill Right Shift Operator (>>>).

46. How many operators are available in Java ?
1. Arithmetic Operator 2. Unary Operator 3. Increment Operator 4. Decrement Operator
5. Assignment Operator 6. Relational Operator 7. Logical Operator 8. Boolean Operator
9. Bitwise Operator 10. Ternary Operator 11. instanceOf Operator 12. dot Operator
13. new Operator 14. cast Operator

47. What is difference between Singleton Class and Static Members ?
  1. Singleton is an object where as Static is not an object.
  2. Singleton can be extended/sub classed where as Static can not be extended/sub classed.
  3. Singleton can be passed around as an object where as Static can not be passed around as an object.
  4. Singleton can be garbage collected where as Static can not be garbage collected.
  5. Singleton class are bounded on run time where as Static members are bounded on compile time.
  6. Singleton class can be overridden by extending where as Static members can not be overridden.
  7. Singleton class is a design pattern where as Static is a keyword.
  8. Static keyword provides better performance than Singleton class.

48. How to Print Double Quotes in Java ?

System.out.println("\"Hello\"");


49. How to create war file manually ? jar –cvf projectname.war *
-c is used to create file.
-v is used to generate the verbose output.
-f to specify the archive file name.
The * (asterisk) symbol signifies that all the files of this directory including sub directory.

50. How to extract war file manually? jar –xvf projectname.war
-x means extraction.

51. What is Method Overloading (or) Static Binding?

If a class have multiple methods by same name but different parameters, it is known as Method Overloading.


52. What is Method Overriding (or) Dynamic Binding?

If a sub class provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding.


53. What is an Interface?

An interface is a collection of methods definitions without implementation and can also include constant declarations.


54. What is Marker Interface?

An interface that have no variable and methods is known as a Marker Interface.


55. What is Functional Interface?

Functional Interface are new additions in Java 8 which permit exactly one abstract method inside them. These interfaces are also called Single Abstract Method Interface (SAM Interface). These can be represented using Lambda Expressions, method reference and constructor reference as well. Java 8 introduces an annotation that is @FunctionInterface.


56. What is Serialization?

Serialization is the process of converting the data into stream of bytes and storing into the files or database.


57. What is a Lombok ?

Lombok is used to reduce boilerplate code for model/data objects, e.g., it can generate getters and setters for those objects automatically by using Lombok annotations. The easiest way is to use the @Data annotation.


58. What is Static Class?

We can create inner class in Java. Static keyword declared in inner classes, these classes called Static Class.


59. What is Static Keyword?

The Static Keyword is used in Java mainly for memory management. Static Keywords can be called and executed without creating the objects. Static Keyword can apply variables, methods, blocks and Inner Classes.


60. What is Instance Variable?

A Variable that is created inside the class but outside the method. Instance variables does not get memory at compile time. Instance variables get memory at runtime when instance is created.


61. What is Anonymous Class?

An object that have no reference is known as Anonymous Object. If you have to use an object only once.


62. What is BitSet Legacy Class?

A BitSet class creates a special type of array that holds bit values. This array can increase in size as need.


63. What is Properties Class?

Properties is a sub class of HashTable. Properties is used to maintain lists of values in which the key is a string and the value is also a string.


64. What is Dictionary Class?

The Dictionary class provides the capability to store key – value pairs.


65. What is static import?

Static import feature of Java 5 facilities. The Java programmer to access any static members of a class directly. These is no need to qualify it by the class name.


66. What is Checked Exception?

The Exceptions that are checked at compile time by the Java Compiler are called Checked Exceptions.


67. What is Un-Checked Exception?

The Exceptions that are checked at run time by the JVM are called Un-Checked Exception.


68. What is Finally Block?

Finally Block is used which contains the code for clean-up and is always executed after the try and catch block.


69. What is Finalized?

Finalized is a method. Finalized method available in java.lang.Object. Finalized method is used for perform clean up processing just before object is Garbage Collected.


70. What is Break?

Break is a keyword. Break is used the program to stop execution of the inner most loop and start processing the next line of code after the block.


71. What is Continue?

Continue is a keyword. Continue is used only the current iteration of the inner most loop to termination and the next iteration of the same loop to start if the condition of the loop is met. (Or) Continue is used inside a loop to repeat the next iteration of the loop.


72. What is Return?

Execution jumps immediately back to the calling method.


73. What is Thread?

A Thread is a separate path of execution of a group of statements.


74. What is Daemon Thread?

A Daemon Thread is a thread that executes continuously without any interruption.


75. What is mean by notify ( ) method?

notify ( ) method is used to send notification to a waiting thread that the object is available.


76. What is mean by notifyAll ( ) method?

notifyAll ( ) method is used to send notification to all waiting threads at once that the object is available.


77. What is System.exit ( ) method?

All program execution stops when the JVM shutdown.


78. What is This?

This is a Keyword. This is a reference variable that refers to the current object.


79. What is Super?

Super is a Keyword. Super is a reference variable that is used to refer immediate parent class object.


80. What is Accessor Method?

Accessor method are the methods that simply access or read the instance variables. Accessor methods do not modify the instance methods. We can use getName() and getAge() methods are called Accessor Methods.


81. What is Mutator Method?

Mutator methods are the methods not only access the instance variables but also modify the instance variables. We can use setName() and setAge() methods are called Mutator Methods.


82. What is Hash Code?

Hash Code is a unique identification number allotted to the object by the JVM.


83. What is an Array?

Array represents a collection of homogeneous data elements.


84. What is String?

String is a collection of characters is called String. String is a Final Class in Java. String is an Immutable Object.


85. What is String Constant Pool?

String objects are stored in a special memory area known as String Constant Pool inside the Heap Memory.


86. What is JIT Compiler?

JIT stands for Just In Time. JIT Compiler is the part of JVM which increases the speed of execution of a Java Program.


87. What is Streams (Java 8 Feature)?

Stream represents a sequence of objects from a source, which supports aggregate operations. A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.


88. What is JAR File?

JAR stands for Java Achiever. JAR File is a file that contains compressed version of several dot class files, audio files, image files (or) directories.


89. What is a Console?

Console is a class. Console class provides methods to read input from the console and write formatted output to the console.


90. What is a File?

File is a class. File cannot used to actually read (or) write data. File can used to making new files, searching for files, deleting files, making directories and working with paths etc.


91. What is FileReader?

FileReader is a class. FileReader can used to read single character files, the whole stream of characters (or) a fixed number of characters.


92. What is FileWriter?

FileWriter is a class. FileWriter can used to write single character files, the whole stream of characters (or) a fixed number of characters.


93. What is BufferReader?

BufferReader is a class. BufferReader is used to read large chunks of data from a file at once and keep this data in a buffer.


94. What is BufferWriter?

BufferWriter is a class. BufferWriter is used to write large chunks of data to a file at once and keep this data in a buffer.


95. What is PrintWriter?

PrintWriter is a class. PrintWriter class prints formatted representations of objects to a text output stream.


96. What is FileOutputStream?

FileOutputStream is a class. FileOutputStream class is an output stream for writing data to a file.


97. What is FileInputStream?

FileInputStream is a class. FileInputStream class obtains input bytes from a file in a file system.


98. What is ObjectOutputStream?

ObjectOutputStream is a class. ObjectOutputStream class writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read using an ObjectInputStream.


99. What is ObjectInputStream?

ObjectInputStream is a class. ObjectInputStream class deserializes primitive data and objects previously written using an ObjectOutputStream.


100. What is Boxing and Un-Boxing?

Boxing is used to converting a primitive data type into an object. Un-Boxing is used to converting an object into its corresponding primitive data type.


101. What is Synchronized Block?

Synchronized Block is useful to synchronize a block of statements.


102. What is Synchronized Object?

The object on which threads are synchronized.


103. What is Thread Synchronization (or) Thread Safe?

When a thread is already acting on an object, preventing any other thread from acting on the same object.


104. What is Narrowing and Widening?

Narrowing means converting a higher data type into a lower data type is called Narrowing.

Widening means converting a lower data type into a higher data type is called Widening.


105. What is a Package?

Package represents a directly that contains related group of classes and interfaces.


106. What is a Control Statements and a Sequential Statement?

Control Statement means the statements that are executed randomly and repeatedly.

Sequential Statements means the statements which are executed one by one that means line by line.


107. What is Type Casting?

Type Casting means converting one data type into another data type is called Type Casting. Type Casting have two types.

1. Implicit Casting
2. Explicit Casting

108. What is Implicit Casting and Explicit Casting?

Implicit Casting means automatic done by the compiler internally is called Implicit Casting.
Explicit Casting means a casting done by the programmer is called Explicit Casting.


109. What is an ArrayList ?
  1. ArrayList is a Class.
  2. ArrayList class is non synchronized.
  3. ArrayList class maintains insertion order.
  4. ArrayList is found in the java.util package.
  5. ArrayList class can contain duplicate elements.
  6. ArrayList is like an array, but there is no size limit.
  7. ArrayList class uses a dynamic array for storing the elements.
  8. ArrayList allows random access because array works at the index basis.
  9. ArrayList inherits the AbstractList class and implements List interface.
  10. ArrayList implements the List interface so we can use all the methods of List interface here.
  11. We can add or remove elements anytime. So, it is much more flexible than the traditional array.
  12. ArrayList manipulation is little bit slower than the LinkedList in Java because a lot of shifting needs to occur if any element is removed from the array list.

110. What is LinkedList ?
  1. LinkedList is a Class.
  2. LinkedList class uses doubly linked list to store the elements.
  3. LinkedList provides a linked-list data structure.
  4. LinkedList inherits the AbstractList class and implements List and Deque interfaces.
  5. LinkedList class can contain duplicate elements.
  6. LinkedList class maintains insertion order.
  7. LinkedList class is non synchronized.
  8. LinkedList class, manipulation is fast because no shifting needs to be occurred.
  9. LinkedList class can be used as list, stack or queue.

111. What is a HashSet ?
  1. HashSet is a Class.
  2. HashSet allows null value.
  3. HashSet class is non synchronized.
  4. HashSet contains unique elements only.
  5. HashSet is the best approach for search operations.
  6. HashSet stores the elements by using a mechanism called hashing.
  7. HashSet inherits the AbstractSet class and implements Set interface.
  8. The initial default capacity of HashSet is 16, and the load factor is 0.75.
  9. HashSet class is used to create a collection that uses a hash table for storage.
  10. HashSet doesn't maintain the insertion order. Here, elements are inserted on the basis of their hashcode.

112. What is a LinkedHashSet ?
  1. LinkedHashSet is a Class.
  2. LinkedHashSet class is a Hashtable and Linked list implementation of the set interface.
  3. LinkedHashSet inherits HashSet class and implements Set interface.
  4. LinkedHashSet class contains unique elements only like HashSet.
  5. LinkedHashSet class provides all optional set operation and permits null elements.
  6. LinkedHashSet class is non synchronized.
  7. LinkedHashSet class maintains insertion order.

113. What is a TreeSet ?
  1. TreeSet is a Class.
  2. TreeSet class is non synchronized.
  3. TreeSet class maintains ascending order.
  4. TreeSet class doesn't allow null element.
  5. TreeSet class objects are stored in ascending order.
  6. TreeSet class access and retrieval times are quiet fast.
  7. TreeSet class contains unique elements only like HashSet.
  8. TreeSet class implements the Set interface that uses a tree for storage.
  9. TreeSet inherits AbstractSet class and implements the NavigableSet interface.

114. What is a HashMap ?
  1. HashMap is a Class.
  2. HashMap maintains no order.
  3. HashMap is non synchronized.
  4. HashMap contains only unique keys.
  5. HashMap contains values based on the key.
  6. HashMap class is found in the java.util package.
  7. HashMap may have one null key and multiple null values.
  8. HashMap inherits the AbstractMap class and implements the Map interface.
  9. HashMap allows us to store key and value pair, where keys should be unique.
  10. HashMap in Java is like the legacy Hashtable class, but it is not synchronized.
  11. It is easy to perform operations using the key index like updation, deletion, etc.
  12. The initial default capacity of Java HashMap class is 16 with a load factor of 0.75.
  13. HashMap allows us to store the null elements as well, but there should be only one null key.
  14. If you try to insert the duplicate key, it will replace the element of the corresponding key.

115. What is HashTable ?
  1. HashTable is a Class.
  2. Hashtable class is synchronized.
  3. Hashtable class contains unique elements.
  4. A Hashtable contains values based on the key.
  5. Hashtable class doesn't allow null key or value.
  6. A Hashtable is an array of a list. Each list is known as a bucket.
  7. Hashtable class implements a hashtable, which maps keys to values.
  8. Hashtable inherits Dictionary class and implements the Map interface.
  9. The position of the bucket is identified by calling the hashcode() method.
  10. The initial default capacity of Hashtable class is 11 whereas loadFactor is 0.75.

116. What is LinkedHashMap ?
  1. LinkedHashMap is a Class.
  2. LinkedHashMap is non synchronized.
  3. LinkedHashMap maintains insertion order.
  4. LinkedHashMap contains unique elements.
  5. LinkedHashMap contains values based on the key.
  6. LinkedHashMap may have one null key and multiple null values.
  7. LinkedHashMap inherits HashMap class and implements the Map interface.
  8. The initial default capacity of Java HashMap class is 16 with a load factor of 0.75.
  9. LinkedHashMap class is Hashtable and Linked list implementation of the Map interface, with predictable iteration order.

117. What is TreeMap ?
  1. TreeMap is a Class.
  2. TreeMap is non synchronized.
  3. TreeMap contains only unique elements.
  4. TreeMap contains values based on the key.
  5. TreeMap class is a red-black tree based implementation.
  6. TreeMap is same as HashMap instead maintains ascending order.
  7. TreeMap cannot have a null key but can have multiple null values.
  8. TreeMap implements the NavigableMap interface and extends AbstractMap class.
  9. TreeMap provides an efficient means of storing key-value pairs in sorted order.

118. What is ArrayDeque ?
  1. ArrayDeque is a Class.
  2. ArrayDeque has no capacity restrictions.
  3. ArrayDeque is faster than LinkedList and Stack.
  4. Null elements are not allowed in the ArrayDeque.
  5. Unlike Queue, we can add or remove elements from both sides.
  6. ArrayDeque is not thread safe, in the absence of external synchronization.
  7. ArrayDeque class provides the facility of using deque and resizable-array.
  8. ArrayDeque inherits AbstractCollection class and implements the Deque interface.

119. What is EnumSet ?
  1. EnumSet is a class.
  2. EnumSet is not synchronized.
  3. EnumSet inherits AbstractSet class and implements the Set interface.
  4. EnumSet class is the specialized Set implementation for use with enum types.
  5. EnumSet is a high performance set implementation, much faster than HashSet.
  6. EnumSet does not allow null Objects and throws NullPointerException if we do so.
  7. EnumSet uses a fail-safe iterator, so it won’t throw ConcurrentModificationException if the collection is modified while iterating.
  8. All of the elements in an EnumSet must come from a single enumeration type that is specified when the set is created either explicitly or implicitly.

120. What is EnumMap ?
  1. EnumMap is a Class.
  2. EnumMap class is the specialized Map implementation for enum keys.
  3. EnumMap class inherits Enum and AbstractMap classes.
121. What is Pagination?

Fetching millions of records from data base consumes almost all CPU power and memory of machine. Hence, we break millions of records into small chunks showing limited number of records per page. The best example of this is Google Search Pagination which allows user to navigate to next page by page number and explore limited records per page.


122. Rules for Exception handling with respect to Method Overriding ? Rule 1 : If parent-class method doesn’t declare any exception.
  1. Then child-class overriding-method can declare any type of unchecked exception. This is the only possibility.
  2. If child-class overriding-method declares checked-exception, then compiler throws compile-time error stating.
  3. Then child-class overriding-method can declare no exception in the overriding-method of child-class.
  4. This is very much same as that of overridden-method of parent-class (exactly same method-signature).
Rule 2 : If parent-class method declares unchecked–exception.
  1. Then child-class overriding-method can declare any type of unchecked exception. Not necessarily same exception as that of parent-class’ method (only for unchecked exception).
  2. If child-class overriding-method declares any checked-exception, then compiler throws compile-time error stating.
  3. Then child-class overriding-method can declare no exception in the overriding-method of child-class.
Rule 3 : If parent-class method declares checked exception.
  1. Then child-class overriding-method can declare any type of unchecked exception.
  2. Then child-class overriding-method can declare same type of checked exception or one of its sub-class or no exception or sub-type of declared checked exception.
  3. Then child-class overriding-method can declare no exception in the overriding-method of child-class.
Rule 4 : If parent-class method declares combination of both checked & unchecked exceptions
  1. Then child-class overriding-method can declare any type of unchecked exception.
  2. Then child-class overriding-method can declare same type of checked-exception or one of its sub-class or no exception.
  3. Then child-class overriding-method can declare no exception in the overriding-method of child-class.

123. Java Virtual Machine Architecture ?
  1. Class Loader Sub System
    1. Loading
      1. Bootstrap Class Loader
      2. Extension Class Loader
      3. Application Class Loader
    2. Linking
      1. Verify
      2. Prepare
      3. Resolve
    3. Initialization
  2. Runtime Data Area
    1. Method Area
    2. Heap Area
    3. Stack Area
      1. Local Variable Array
      2. Optional Stack
      3. Frame Data
    4. PC Register
    5. Native Method Stack
  3. Execution Engine
    1. Interpreter
    2. JIT Compiler
      1. Intermediate Code Generator
      2. Code Optimizer
      3. Target Code Generator
      4. Profiler
  4. Java Native Interface
  5. Native Method Libraries

124. What is Spliterator ?

Spliterator is an interface. Spliterator is an internal iterator that breaks the stream into the smaller parts. These smaller parts can be processed in parallel. These smaller parts could be an Array (or) a Collection (or) an IO Channel (or) a Generator Function.


125. What are the Features of Spliterator ?
  1. Spliterator helps in parallel processing.
  2. Spliterator has been introduced in Java 8.
  3. The trySplit() method is used partition the spliterator, if it is possible.
  4. To iterate elements sequentially in a single Thread, use forEachRemaining() method.
  5. Spliterator helps in combining the hasNext() and next() operations into one method.
  6. Spliterator provides support for parallel processing of stream of elements for any collection.
  7. Spliterator provides tryAdvance() method to iterate elements individually in different threads.

125. What are the Spliterator Methods ?
  1. int characteristics() methods returns the list of characteristics of the spliterator. It can be any of ORDERED, DISTINCT, SORTED, SIZED, NONNULL, IMMUTABLE, CONCURRENT, and SUBSIZED.
  2. long estimateSize() method returns an estimate of the number of elements that would be encountered by a forEachRemaining() traversal, or returns Long.MAX_VALUE if infinite, unknown, or too expensive to compute.
  3. default void forEachRemaining(Consumer action) method performs the given action for each remaining element, sequentially in the current thread, until all elements have been processed or the action throws an exception.
  4. default Comparator getComparator() method, if the spliterator’s source is SORTED by a Comparator, returns that Comparator.
  5. default long getExactSizeIfKnown() method returns estimateSize() if this Spliterator is SIZED, else -1.
  6. default boolean hasCharacteristics(int characteristics) method returns true if the dpliterator’s characteristics() contain all of the given characteristics.
  7. boolean tryAdvance(Consumer action) method, if a remaining element exists, performs the given action on it, returning true; else returns false.
  8. Spliterator trySplit() method, if the spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator.

126. What is Exception Propagation ?

Forwarding the exception object to the invoking method is known as Exception Propagation.

Rule 1 : By Default, Unchecked Exception are forwarded in calling chain (Propagated).
Rule 2 : By Default, Checked Exception are not forwarded in calling chain.

127. What is the default size of ArrayList ?

The Default size of ArrayList is 10.


128. What is the default size of Vector ?

The Default size of Vector is 10.


129. What is the default size of HashTable ?

The Default size of HashTable is 11.


130. What is the default size of HashSet ?

The Default size of HashSet is 16.


131. What is the default size of HashMap ?

The Default size of HashMap is 16.


132. What are the default values of short ?

The default value of short is zero.


133. What are the default values of long ?

The default value of long is zero.


134. What are the default values of byte ?

The default value of byte is zero.


135. What are the default values of int ?

The default value of int is zero.


136. What are the default values of float ?

The default value of float is 0.0.


137. What are the default values of double ?

The default value of double is 0.0.


138. What are the default values of boolean ?

The default value of Boolean is false.


139. What are the default values of char ?

The default value of char is \u000.


140. What are the default values of object reference ?

The default value of object reference is null.


141. How many classes are available in Java 1.0 ?

212 Classes are available in Java 1.0.


142. How many classes are available in Java 1.1 ?

504 Classes are available in Java 1.1.


143. How many classes are available in Java 1.2 ?

1520 Classes are available in Java 1.2.


144. How many classes are available in Java 1.3 ?

1842 Classes are available in Java 1.3.


145. How many classes are available in Java 1.4.0 ?

2991 Classes are available in Java 1.4.0.


146. How many classes are available in Java 1.5.0 ?

3279 Classes are available in Java 1.5.0.


147. How many classes are available in Java 1.6.0 ?

3793 Classes are available in Java 1.6.0.


148. How many classes are available in Java 1.7.0 ?

4024 Classes are available in Java 1.7.0.


149. How many classes are available in Java 1.8.0 ?

4240 Classes are available in Java 1.8.0.


150 How many classes are available in Java 1.9.0 ?

6005 Classes are available in Java 1.9.0.


151. How many classes are available in Java 1.10.0 ?

6002 Classes are available in Java 1.10.0.


152. How many classes are available in Java 1.11.0 ?

4411 Classes are available in Java 1.11.0.


153. How many classes are available in Java 1.12.0 ?

4433 Classes are available in Java 1.12.0.


154. How many packages are available in Java 1.0 ?

8 packages are available in Java 1.0.


155. How many packages are available in Java 1.1 ?

23 packages are available in Java 1.1.


156. How many packages are available in Java 1.2 ?

59 packages are available in Java 1.2.


157. How many packages are available in Java 1.3 ?

76 packages are available in Java 1.3.


158. How many packages are available in Java 1.4.0 ?

135 packages are available in Java 1.4.0.


159. How many packages are available in Java 1.5.0 ?

166 packages are available in Java 1.5.0.


160. How many packages are available in Java 1.6.0 ?

203 packages are available in Java 1.6.0.


161. How many packages are available in Java 1.7.0 ?

209 packages are available in Java 1.7.0.


162. How many packages are available in Java 1.8.0 ?

217 packages are available in Java 1.8.0.


163. Is it possible to re-throw exceptions ?

Yes, We can re-throw exception from catch block to another class where it can be handled.


164. What is Throwable ?

Throwable is a class that represents all errors and exceptions which may occur in Java.


165. What is the super class for all exceptions ?

Exception is the super class of all exceptions in Java.


166. What is the static blank final variable ?

A static final variable is not initalized at the time of declaration is known as Static Blank Final Variable. It can be initalized only in static black.
Example : static final String name;


167. Can we define an interface inside the class ?

Yes, If we can define an Interface inside the class.


168. What is Static Classes ?

We can create inner class in Java. Static Keyword declared in inner classes, these classes called Static Classes.


169. What is Static Keyword ?

The Static Keyword is used in Java mainly for memory management. Static Keyword can be called and executed without creating the objects. Static can apply variables, methods, blocks and Inner classes.


170. What is the different between Iterator and ListIterator ?

Iterator can retrieve the elements only in forward direction bet ListIterator can retrieve the elements in forward and backward also.


171. Can you store a primitive data type into a collection ?

No. Collections store only Objects.


172. What is Annonymous Object ?

An Object that have no reference is knone as Annonymous Object. If you have to use an object only.


173. What is different between ArrayList and Vector ?
  1. ArrayList is not synchronized where as Vector is synchronized.
  2. ArrayList is not a legacy class where as Vector is a legacy class.
  3. ArrayList increases its size by 50% of the array size where as Vector increases its size by doubling of the array size.

174. What is different between HashSet and TreeSet ?

HashSet maintains no order where as TreeSet maintains ascending order.


175. What is different between ArrayList and LinkedList ?
  1. ArrayList uses a dynamic array where as LinkedList uses a double LinkedList.
  2. ArrayList is not efficient for manipulation becuase a lot of shifting is required where as LinkedList is efficient for manipulation.

176. What is different between HashMap and HashTable ?
  1. HashMap is not synchronized where as HashTable is synchronized.
  2. HashMap can contains one null key and multiple null values where as HashTable can not contains any null key nor values.

177. What is different between HashSet and HashMap ?

HashSet contains only values where as HashMap contains entry (key, values).


178. What is different between HashMap and TreeMap ?
  1. HashMap can contains one null key where as TreeMap can not contains any null key.
  2. HashMap maintains no order where as TreeMap maintains ascending order.

179. What are the Collection Interface ?
  1. Set Interface
  2. Map Interface
  3. List Interface
  4. Map.Entry Interface
  5. SortedSet Interface
  6. SortedMap Interface
  7. Collection Interface
  8. Enumeration Interface

180. What are the Legacy Classes ?
  1. Vector
  2. Stack
  3. Dictionary
  4. Properties
  5. HashTable
  6. BitSet

181. What is Properties Legacy Class ?

Properties is a sub class of HashTable. It is used to maintain lists of values in which the key is a string and the value is also a string.


182. What is hash-collision in HashTable and how it is handled in Java ?

Two different keys with the same hash value. Two different entries will be kept in a single hash bucket to avoid the collision.


183. Can we intialize blank final variable ?

Yes but only in constructor.


184. What is different between Iterator and Enumeration ?
  1. Iterator can retrieve legacy and non legacy elements where as Enumeration can retrieve only legacy elements.
  2. Iterator is fail-fast where as Enumeration is fail-fast.
  3. Iterator is slo than Enumeration where as Enumeration is faster than Iterator.

185. How many methods have the Object Class ?

11 methods have the Object Class.

  1. finalize()
  2. wait()
  3. wait(long int)
  4. native wait(long)
  5. notify()
  6. notifyAll()
  7. equal()
  8. hashCode()
  9. toString()
  10. clone()
  11. getClass()

186. What is StringTokenizer ?

StringTokenizer is a class. StringTokenizer is used to break a string into pieces called tokens.

StringTokenizer has three methods :
  1. int countTokens() method counts and returns the number of tokens available in a StringTokenizer Object.
  2. boolean hasMoreTokens() method returns true if StringTokenizer has more elements.
  3. String nextToken() method returns the next token from the StringTokenizer.

187. What is difference between String, StringBuilder, and StringBuffer ?
Factor String StringBuilder StringBuffer
Storage Area Constant String Pool Heap Area Heap Area
Mutability Immutable Mutable Mutable
Thread Safety Yes No Yes
Performance Fast More Efficient Less Efficient

188. What are the differences between Heap and Stack Memory in Java ?
Features Stack Heap
Memory Stack memory is used only by one thread of execution Heap memory is used by all the parts of the application
Access Stack memory can’t be accessed by other threads Objects stored in the heap are globally accessible.
Memory Management Follows LIFO manner to free memory Memory management is based on the generation associated with each object.
Lifetime Exists until the end of execution of the thread Heap memory lives from the start till the end of application execution.
Usage Stack memory only contains local primitive and reference variables to objects in heap space Whenever an object is created, it’s always stored in the Heap space.

189. What is different between Set and List` ?
  1. A Set represents a collection of elements where as A List represents ordered collection of elements.
  2. A Set will not allow duplicate values to be stored where as List will allow duplicate values.
  3. A Set will not allow null elements where as List will allow null elements.
  4. Accessing elements by index is possible in List where as accessing elements by index is not possible in set

190. Can you make List elements synchronized ?

Yes, Collections class provides methods to make List elements as synchronized.
public static List synchronizedList(List list) [ ]


191. Can you make Set elements synchronized ?

Yes, Collections class provides methods to make Set elements as synchronized.
public static Set synchronizedSet(Set set) [ ]
public static SortedSet synchronizedSortedSet(SortedSet set) [ ]


192. Can you make Map elements synchronized ?

Yes, Collections class provides methods to make Map elements as synchronized.
public static Map synchronizedSet(Map map) [ ]
public static SortedMap synchronizedSSortedMap(SortedMap map) [ ]


193. Why use Inheritance ?

Method Overriding (So Runtime Polymorphism) and Code Reusability.


194. How many types of Inheritance ?
  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance (One to One)
  4. Hybrid Inheritance
  5. Hierarchical Inheritance (One to Many)

195. Five types of Inheritance but Java Support Three Types, What are they ? Single Inheritance :
Example : FirstClass -------> SecondClass

Multilevel Inheritance (One to One) :
Example : FirstClass -------> SecondClass -------> ThirdClass

Hierarchical Inheritance (One to Many) :
Example : SecondClass -------> FirstClass, ThirdClass -------> FirstClass, FourthClass -------> FirstClass

196. What is Wrapper Class ?

Wrapper Class is a class that allow primitive types to be accessed an object.


197. What is a Native Method ?

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


198. Can we override the overloaded methods ?

Yes. We can override the overloaded methods.


199. What is different between xxxValue(), valueOf() and parseXXX() ?

xxxValue() method to convert a wrapper to a primitive.

valueOf() method to convert a String to a wrapper.

parseXXX() method to convert a String to a primitive.


200. Can you declare the main method as final ?

Yes, such as public static final void main(String [ ] args) { }.


201. Can you have virtual functions in Java ?

Yes, All functions in Java are virtual functions by default.


202. Can we override main() method ?

No. main() method is a static method and is thus not polymorphic.


203. Can we return any value in try block ?

Yes. We can return any value in try block.


204. Can we return any value in catch block ?

Yes. We can return any value in catch block.


205. Can we return any value in finally block ?

Yes. We can return any value in finally block.


206. Can we return any value in try, catch, finally block at a time ?

No. We can return any value either from try or from catch or from finally block.


207. Can we define a class inside the interface ?

Yes. If we define a class inside the interface.


208. What is different between Comparable and Comparator`?
  1. Comparable provides only one sort of sequence where as Comparator provides multiple sort of sequence.
  2. Comparable is used to compare itself by using with another object where as Comparator is used to compare two data types or objects.
  3. Comparable provides only one method namely compareTo() where as Comparator provides two methods namely compare() method and equals() method.
  4. Compareble is found in java.lang.package where as Comparator is found in java.util.package.
  5. If we implement Compareble interface actual class is modified where as if we implement Comparator interface actual class is not modified.