Java interview questions

Java interview questions | Freshers & Experienced

  • Om Prakash
  • 24th Feb, 2021

Key Features of Java

Below are few major features of Java

Lambda expressions

Methods references

Default methods

Functional interface

Java 8 date/time

Stream classes

Multi-release JARs

API improvements

Q1. Why Introspection is used in Java?

Q2. What invokes the thread's run() method ?

Q3. Explain Autoboxing and Unboxing conversions in Java?

Autoboxing and Unboxing conversions in Java:

Autoboxing refers to the Automatic Conversions that the JAVA compiler makes primitive data types into their corresponding class object such as int to an integer etc. The compiler of the JAVA applies autoboxing whenever a primitive value is passed as a parameter to a method that expects an object of the corresponding wrapper class or is assigned to a variable of the comparable wrapper class of the program.

Unboxing is the reverse process of Autoboxing that is, conversion of an object of a wrapper type to its corresponding primitive value. The compiler of the JAVA applies unboxing when an object of a wrapper class is passed as a parameter to a method that expects a value of the corresponding primitive type or assigned to a variable of the corresponding primitive type class.

Q4. Why String is immutable in Java?

Q5. Explain the difference between a process and a thread?

Difference between a Process and a Thread

Process: Program in execution is commonly referred to as a process. It consists of multiple threads. It is referred to as a task. It has its own address space.

Thread: A thread is a subset or a portion of the process. It is the smallest part of the process that can execute concurrently with other parts or threads of the process. It is commonly referred to as a lightweight process. It uses the process’s address space and shares it with the other threads of that process.

Q6. How to sort a List of Java Bean in Ascending Order?

Q7. What are the difference between Java arrays and Collections?

Difference between Java Arrays and Collections

Arrays are fixed in size and can hold only homogeneous elements. It doesn't have readymade or predefined methods. They can hold both primitive as well as wrapper objects. They use more memory but give better performance.

Collections are variable or dynamic in size and can hold both heterogeneous as well as homogeneous elements. They have predesigned data structures and methods. They can hold only objects. They use less memory and they are not good to use with respect to performance.

Q8. How to sort a collection in Java?

Q9. What are wrapper classes?

A wrapper class is a kind of class whose objects wraps, encloses or contains a primitive data types like char to character, int to an integer, etc. When we have to use data types in the form of objects we have to use wrapper class. In JAVA, it is mainly an object that makes our code fully object-oriented. While we create an object to a wrapper class, it has a field where we can store primitive data types. Wrapper objects take much memory than primitive types so its beneficial to use wrapper class to get more efficiency.

Q10. List few differences between Array list and vector in Java?

Q11. Why main() method of Java program must be static?

The main() method of JAVA is the entry point of any core-level program. It has to be static due to keyword static allows the main to be called without the creation of an object of the class in which the main method is defined. Here the main() method also has to be declared public so that it must be called by code outside of its class when the program has begun. The main() method of JAVA has to be void and if these conditions are not met, it will give an error. The syntax of main() method has to be strictly followed else JVM will be unable to locate it and the program will not run. If it's not declared static then JVM needs to create an instance of main class and as constructor can be overloaded and may have arguments then there would be no certain or consistent way to find it.

Q12. What are access modifiers in java?

Q13. How to create immutable class in java?

Q14. Give me a real time example of Abstraction?

Q15. What is the Dictionary class?

Q16. Can you create an instance of interface?

Q17. What is implicit casting in Java?

Typecasting in JAVA refers to assigning a value of one type to a variable of another type i.e. converting a primitive or interface or class into another type. Implicit type casting is a property of JAVA where the data types are converted without losing its original meaning. It is necessary when we want to change the data types without changing the significance of the values stored inside the variable. Implicit type casting is also known as Widening casting which is an automatic conversion in JAVA. Here The two types of data types are compatible and the target type data is larger than the source data type. Here, the programmer needs not to intervene for the conversion. The JAVA compiler follows certain rules and uses the built-in library functions to convert the variables to common data type before the evaluation of the expression.

Q18. What is JIT ?

JIT (Just in time) production or just in time manufacturing is a methodology that is targeted to reduce time during the production system along with generating the response from suppliers to customers. It refers to an inventory system that supplies management strategy aligning the raw materials from suppliers to production. It also helps organizations to control variability in their workflow.

Q19. What does System.gc() and Runtime.gc() methods do?

Q20. What are Checked and Unchecked exceptions in Java?

The checked exceptions in JAVA are the exceptions that are checked at compile time of the program. If some code within a method throws a checked exception then it must either handle the exception or it must specify the exception by using the throws keywords or try-catch block.SQLException,IOException,ClassNotFoundException etc. are some examples.

Unchecked exceptions are the ones that are not checked at the compile time. Here the program will not give a compilation error even if we did not declare that exception. It is upon the programmer to decide the conditions in advance that cause such exceptions and handle them appropriately and carefully. They are the direct subclasses of RuntimeException class.NullPointerException,ArrayIndexOutOfBoundsException,ArithmeticException etc.Are examples of Unchecked Exceptions.

Q21. What is default access modifier of an Interface in Java?

Q22. Why to use Iterator in Java? Is it is an Interface or Class?

Q23. What is garbage collector in java?

Garbage collector in JAVA is a program that helps in the management of memory automatically. It is referred to as a process of reclaiming the runtime unused memory in an automatic manner. The deallocation of objects in the JAVA program is handled and maintained by JAVA rather than the programmer. The dynamic allocation of objects is done by using the new operators. The object created occupies some memory and it holds it as long as there are references for the use of the object. But if there are no references to an object, it is assumed to be that it is not required, and the memory has to be reclaimed. If it doesn't happen, the memory will not left and the program will suffer memory leaks.

Q24. What is use of transient keyword in Java?

Q25. How to suspend a thread?

Q26. What is deadlock? How can you prevent it, avoid it?

Q27. What is a NullPointerException in Java?

NullPointerException in Java is a RuntimeException which represents a special value that can be assigned to an object reference. This value is thrown when an application tries to use the object reference which has null value it includes the calling an instance method over the object referred by null reference. These values are modified in the instance field of the object where the reference type is array or subtype of throwable.

Q28. What is the difference between Iterator and Enumeration in Java?

The removal of the elements in JAVA while transversing the collection is the main point of difference between the iterator and enumeration in JAVA. The former can remove the element during the transversal of collection due to the presence of remove() method. Iterator is the interface that is found in the java. util()package.It is useful for the transversal of Hashmap, LinkedList, TreeSet, etc. It can be done by three methods using hasNext(),next() and remove(). Enumeration is also one of the interfaces (legacy interface for transversing Vector, Hastagable) found in the java.util() package. It is useful in passing through a collection which is usually of unknown size. Its methods consist of hasMoreElements() and nextElements().

Q29. In how many ways a thread can be created in Java?

Q30. Explain Stream Tokenizer in Java?

The splitting of stream or strings into meaningful independent words is termed as tokenization. It is a usual practice for tool developers. The stream tokenizer class in JAVA takes an input stream and parses it into some tokens allowing them to be read one at a time. Here, the source is a string. The process of parsing is controlled by a table and a number of flags that can be set to different states. The stream tokenizer can be used to recognize identifiers, quoted strings, numbers, and various other comment styles.

Q31. How to convert a string to long in Java?

Q32. How to add and remove nodes in Jtree?

Q33. How do I break out of nested loops in Java?

Q34. How to calculate difference between two dates in Java?

Q35. What is Double Brace initialization in Java?

The consecutive curly braces{} in JAVA is used to denote a double brace initialization. It is applied for the combination of two separate processes. The first brace is applied to create an anonymous inner class and the second one is used for the initialization block. Whenever we use the initialization block for an anonymous inner class it is done for the JAVA double brace initialization. The created inner class will have a reference to the enclosing outer class which can be applied using the 'this' pointer. It is used when there is no standard way of creating and initializing collection at the same time in JAVA. Advantages include less line of codes and creating as well as initializing in the same expression. The disadvantages is that it is obscure, cost an extra class every time we use it and hold a hidden reference to the enclosing instance leading to memory leaks.

Q36. Why do we need constructor in Java?

Q37. How many ways you can create an object in java ?

Q38. How to write custom class Loader ?

Q39. Explain the difference between ArrayList and Vector?

Difference between ArrayList and Vector:

Array list - It is not synchronized and increments almost half of the current arise if the number in any elements exceeds the capacity. Array list does not represent 'not a legacy' class which is introduced in JDK 1.2

Vector list - It is synchronized and increments completely double the array size of the total number of elements exceeding the capacity. It is a legacy class and it is also slow as it synchronizes.

Q40. How to name a thread in executor service ?

Q41. Is there any difference between a = a + b and a += b expressions in Java?

Q42. What are raw types in Java?

Raw types refer to using a generic type or interface without the specification of type parameters or arguments in JAVA. It can be a reference type that is formed by taking the name of a generic type declaration without including the type argument list. It can also be a no static member type of a raw type R which is not inherited from a super interface or superclass of R. Necessarily they behave just like they were before generics were introduced in JAVA. It is used only for the concession to the compatibility of legacy codes. Migration compatibility is the only reason to use them.

Q43. How to check if a file exists or not in Java?

Q44. How to print nodes of a Binary tree in Java?

Q45. What are Inner classes in Java?

Inner class in JAVA denotes that there is one class that is a member of another class. It is also referred to as the Nested class as it is completely declared within the body of another class or interface. It is not like a subclass. We use them to logically group interfaces or group classes in a single place in order to make it more readable and maintainable. It provides a facility to access all the members of outer class which consists of private data members and methods. It requires fewer lines of code makes it more beneficial to use. They can be declared public, private, protected or with default access. It leads to encapsulation.

Q46. Is 'sizeof' is a keyword or method in Java?

Q47. Explain What is Connection Pooling?

Q48. What is the default initialized value of a String in Java.

Q49. What is Static binding ?

Q50. Difference between sleep and suspend ?

Q51. What is JAR file?

JAR is basically known as the Java Archive. Basically, this contains certain class associated files in a compressed manner. It contains different codes, images and metadata, and other resources. These files can be used or supplied during the Java programming platform. This file format is almost similar to the Tape archive (TAR) file format which was used in Unix. These files can be really referred for their security abilities and these files can be used as the building blocks in the Java application.

Q52. What is super in Java?

Q53. What is classloader in Java?

They really perform an important operation in loading the class files into the Java Virtual Machine. What happens here is that there is a Java runtime and at that point in time they just have no idea about the files and their systems because of the presence of the classloaders. So the classes in Java are not mounted all at once into the memory. But when required in an application at that point in time it the JRE gives the command to the classloaders that mount the class into the memory.

Q54. What is difference between final, finally and finalize?

Q55. What is Externalizable?

Q56. What is the difference between JVM and JRE?

Difference between JVM and JRE

JVM stands for JAVA Virtual Machine which is an abstract machine. It is used to provide a runtime environment where java bytecode can be executed. They are available for many software and hardware platforms. It creates five similar runtime areas for the execution of class members. The Java compiler generates the bytecode which is in JVM understandable format. It is the heart of the Java programming language and is used for providing an independent environment. It is an essential part of JRE and JDKas it is inbuilt in both of them.

JRE stands for Java Runtime Environment and is useful for providing a runtime environment. It is for the implementation of JVM It consists of a set of libraries and other files that JVM uses at runtime. It can also be denoted by Java RTE. It gives information about the minimum requirements for the execution of a Java application. It is only used by the end-users of the systems.

Q57. How to interrupt a thread explicitly ?

Q58. What is difference between DTO, VO, POJO, JavaBeans?

Q59. How to use JNDI is used in JMS?

Q60. List the types of data passing mechanisms?

Q61. How to implement RMI in Java

Q62. What is the use of Semaphore?

Q63. What is difference between core Java and Java EE?

Q64. What is runnable?

Q65. What is thread deadlock? How to resolve thread?

Q66. What is the difference between a StringBuffer and String in Java?

Q67. Describe the differences between C++ and Java.

Q68. What is difference between Abstract and final?

Q69. Why java doesn't support multiple inheritance?

Q70. How would you design a garbage collector?

Q71. Does Java support pointer variables?

Q72. How to implement multithreading in Java?

Q73. What is difference between JVM and JRE?

Q74. What is difference between call by value and call by reference?

Q75. Explain final, finally, finalize difference

Q76. What is the difference between JDK and JVM?

Q77. What is overloading and overriding in java?

Q78. What is Enum in Java?

ENUM in Java is used to classify a certain set of values in just one word. It is and should only be done when you know that the set of variables will not be changed and it has a known exact value (days, months, a deck of cards). At that point in time, you can use the Enumerations or Enum. Like for example, you have certain names like the Blue, orange, red, green; here you can simply use the Enum as Colours.

Q79. What is instanceof keyword?

Q80. What is the final method?

Q81. What is the final blank variable?

Q82. What is a transient variable?

Q83. When a thread blocks on I/O, in which state it enters?

Q84. What is the use of % operator?

Q85. What is an immutable class?

Q86. What is difference between HashMap and HashTable?

Q87. What is use of intern() method in Java?

Q88. What is JCA and JPA in java?

Q89. What is OutOfMemoryError in Java? How do you deal with that?

Q90. What is the maximum Heap Size of 32 bit or 64-bit JVM in Windows and Linux?

Q91. What is the difference between JDBC and JNDI?

Q92. What is JAVA_HOME environment variable?

Q93. Can we override toString() method for Enum?

Q94. What is use of ordinal() method in Enum?

Q95. How to convert an String to Enum in Java?

Q96. Is it possible to have multiple public classes in Java ?

Q97. What is the difference between notify and notify All methods ?

Q98. What is an Iterator interface?

Q99. What are null or Marker interfaces in Java?

Q100. What is Jagged Arrays in java?

Q101. What is the purpose of the Runtime class?

Q102. Can we cast double to a bytein Java?

Q103. What is the advantage of thread yielding?

Q104. What is the Collections API?

Q105. What is the Vector class?

Q106. What are Anonymous Classes?

Q107. What is preemptive scheduling?

Q108. What is the difference between JDK, JRE, and JVM?


Q109. How many types of memory areas are allocated by JVM?


Q110. What is JIT ( Just-In-Time) compiler?


Q111. What is ‘write once and run anywhere’ in JAVA?


Q112. If I don’t provide any arguments on the command line, then the String array of Main method will be empty or null?


Q113. What is the default value of the local variables?


Q114. What will be the initial value of an object reference which is defined as an instance variable?


Q115. What is a constructor?


Q116. What are Rules to define a constructor?


Q117. What is the purpose of default constructor?


Q118. Is constructor inherited?


Q119. Can you make a constructor final?


Q120. What is static variable?

Q121. What is the static method?


Q122. What is static block?


Q123. Can we execute a program without a main() method?


Q124. What is the difference between static (class) method and instance method?


Q125. What is method overloading?


Q126. What is “this” in java?


Q127. What is Inheritance?


Q128. Why is multiple inheritances not supported in java?


Q129. What is composition?


Q130. What is object cloning?


Q131. Can we override static method?


Q132. What is the difference between method Overloading and Overriding?


Q133. What is Exception Handling?


Q134. What is final variable, final class, final method?


Q135. What is Polymorphism?


Q136. What is Runtime Polymorphism?


Q137. What is dynamic binding?


Q138. What is Abstraction?


Q139. What is the difference between abstraction and encapsulation?


Q140. What is the difference between Checked Exception and Unchecked Exception?


Q141. What is exception propagation?


Q142. What is nested class?


Q143. What is the purpose of the toString() method in java?


Q144. What is Garbage Collection?


Q145. What is the purpose of finalize() method?


Q146. What is the difference between final, finally and finalize?


Q147. What is a Stream?


Q148. What is serialization?


Q149. What is Socket Class?


Q150. What is a thread?


Q151. What is Collection? What is a Collection Framework?


Q152. What is Pass by Value and Pass by Reference?


Some most complex Java interview questions

  1. How to convert integer to String in Java?
  2. What are Java generics?
  3. What's the difference between a Java int and a Java Integer?
  4. What happens when a class is immutable?
  5. Is Java a pure object-oriented programming language?
  6. What is wrapper class in Java?
  7. What is difference between Iterator and Enumeration?
  8. Why String is immutable in Java ?

About Author :

  • Author of Java interview questions

    OM Prakash

    OM Prakash is Senior Android developer from new Delhi, India. He have rich experience in creating mobiles apps in Android, Ionic, Kotlin, Java. He loves listening old song and watching bolywood movies.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.