Java interview questions

Java interview questions | Freshers & Experienced

  • Om Prakash
  • 16th Nov, 2019
  • 634 Followers

Q1. 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.

Q2. Why String is immutable in Java?

Q3. 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.

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

Q5. 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.

Q6. How to sort a collection in Java?

Q7. 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.

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

Q9. 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.

Q10. What are access modifiers in java?

Q11. How to create immutable class in java?

Q12. Give me a real time example of Abstraction?

Q13. What is the Dictionary class?

Q14. Can you create an instance of interface?

Q15. 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.

Q16. 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.

Q17. 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.

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

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

Q20. 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.

Q21. What is use of transient keyword in Java?

Q22. How to suspend a thread?

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

Q24. 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.

Q25. 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().

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

Q27. 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.

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

Q29. How to add and remove nodes in Jtree?

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

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

Q32. 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.

Q33. Why do we need constructor in Java?

Q34. 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.

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

Q36. 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.

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

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

Q39. 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.

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

Q41. Explain What is Connection Pooling?

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

Q43. What is Static binding ?

Q44. Difference between sleep and suspend ?

Q45. 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.

Q46. What is super in Java?

Q47. 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.

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

Q49. What is Externalizable?

Q50. 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.

Q51. How to interrupt a thread explicitly ?

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

Q53. What is runnable?

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

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

Q56. What is difference between Abstract and final?

Q57. Why java doesn't support multiple inheritance?

Q58. How would you design a garbage collector?

Q59. Does Java support pointer variables?

Q60. How to implement multithreading in Java?

Q61. What is difference between JVM and JRE?

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

Q63. Explain final, finally, finalize difference

Q64. What is the difference between JDK and JVM?

Q65. What is overloading and overriding in java?

Q66. 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.

Q67. What is instanceof keyword?

Q68. What is the final method?

Q69. What is JAVA_HOME environment variable?

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

Q71. What is an Iterator interface?

Q72. What are null or Marker interfaces in Java?

Q73. What is Jagged Arrays in java?

Q74. What is the purpose of the Runtime class?

Q75. Can we cast double to a bytein Java?

Q76. What is the advantage of thread yielding?

Q77. What is the Collections API?

Q78. What is the Vector class?

Q79. What are Anonymous Classes?

Q80. What is preemptive scheduling?

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

           

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

               

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

                    

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

             

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

             

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

               

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

              

Q88. What is a constructor?

         

Q89. What are Rules to define a constructor?

            

Q90. What is the purpose of default constructor?

                

Q91. Is constructor inherited?

            

Q92. Can you make a constructor final?

                

Q93. What is static variable?

Q94. What is the static method?

            

Q95. What is static block?

               

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

             

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

            

Q98. What is method overloading?

           

Q99. What is “this” in java?

                  

Q100. What is Inheritance?

              

Q101. Why is multiple inheritances not supported in java?

                   

Q102. What is composition?

           

Q103. What is object cloning?

               

Q104. Can we override static method?

                 

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

                        

Q106. What is Exception Handling?

             

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

                   

Q108. What is Polymorphism?

          

Q109. What is Runtime Polymorphism?

               

Q110. What is dynamic binding?

              

Q111. What is Abstraction?

                 

Q112. What is the difference between abstraction and encapsulation?

              

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

                  

Q114. What is exception propagation?

             

Q115. What is nested class?

                 

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

              

Q117. What is Garbage Collection?

              

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

             

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

                 

Q120. What is a Stream?

                

Q121. What is serialization?

           

Q122. What is Socket Class?

                

Q123. What is a thread?

               

Q124. What is Collection? What is a Collection Framework?

                    

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

                  

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.

Explore Popular Courses on Java

Start working with Landrick that can provide everything you need to generate awareness, drive traffic, connect.