Smart Future Point

Java


Interview Question For Java Programming


1. What is Java?
Show Answer

Ans. Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is platform-independent due to the "Write Once, Run Anywhere" (WORA) principle, enabled by the Java Virtual Machine (JVM).


2. What are the key features of Java?
Show Answer

Ans. Key features of Java include:

  • Object-Oriented
  • Platform-Independent
  • Secure
  • Robust
  • Multithreaded
  • High Performance (via Just-In-Time compilation)
  • Distributed and Dynamic

3. What is the difference between JDK, JRE, and JVM?
Show Answer

Ans.

  • JDK (Java Development Kit): A full-featured software development kit that includes JRE, compilers, and tools to develop Java applications.
  • JRE (Java Runtime Environment): Provides the environment to run Java applications, containing JVM and Java class libraries.
  • JVM (Java Virtual Machine): Converts bytecode into machine code and enables Java's platform independence.

4. What are the data types in Java?
Show Answer

Ans. Java supports two categories of data types:

  • Primitive Data Types: byte, short, int, long, float, double, char, boolean.
  • Non-Primitive (Reference) Data Types: Strings, Arrays, Classes, Interfaces, Enums.

5. What is a class and an object in Java?
Show Answer

Ans. A class in Java is a blueprint for creating objects and defines properties (fields) and behaviors (methods). An object is an instance of a class that contains actual values and can perform actions using methods defined in the class.


6. What is the difference between method overloading and method overriding?
Show Answer

Ans. Method overloading occurs when multiple methods in the same class have the same name but different parameters. Method overriding occurs when a subclass provides a specific implementation of a method already defined in its superclass.


7. What is inheritance in Java?
Show Answer

Ans. Inheritance is a mechanism in Java by which one class can inherit the properties (fields) and behaviors (methods) of another class. It promotes code reusability.


8. What is encapsulation in Java?
Show Answer

Ans. Encapsulation is the process of wrapping code and data together into a single unit, typically a class. It helps protect data by restricting direct access using access modifiers (private, protected, public).


9. What is polymorphism in Java?
Show Answer

Ans. Polymorphism means the ability of a variable, function, or object to take multiple forms. It can be achieved through method overloading (compile-time) and method overriding (runtime).


10. What is abstraction in Java?
Show Answer

Ans. Abstraction is the concept of hiding implementation details and showing only the functionality to the user. It is achieved using abstract classes and interfaces.


11. What is the difference between abstract class and interface in Java?
Show Answer

Ans. An abstract class can have both abstract and non-abstract methods, whereas an interface can only have abstract methods (prior to Java 8). Interfaces support multiple inheritance; abstract classes do not.


12. What is the final keyword in Java?
Show Answer

Ans. The final keyword is used to declare constants, prevent method overriding, and inheritance of classes.


13. What are constructors in Java?
Show Answer

Ans. A constructor is a special method used to initialize objects. It has the same name as the class and has no return type.


14. What is the difference between == and equals() in Java?
Show Answer

Ans. The == operator compares references (memory locations), while the equals() method compares the actual content or values of objects.


15. What is a static keyword in Java?
Show Answer

Ans. The static keyword is used to define variables and methods that belong to the class rather than any object instance.


16. What is exception handling in Java?
Show Answer

Ans. Exception handling is a mechanism to handle runtime errors, ensuring normal program flow. It uses keywords like try, catch, finally, throw, and throws.


17. What is the difference between checked and unchecked exceptions?
Show Answer

Ans. Checked exceptions are checked at compile-time (e.g., IOException), while unchecked exceptions are checked at runtime (e.g., NullPointerException).


18. What is multithreading in Java?
Show Answer

Ans. Multithreading is a Java feature that allows concurrent execution of two or more threads for maximum CPU utilization.


19. What are the differences between String, StringBuilder, and StringBuffer?
Show Answer

Ans. String is immutable, StringBuilder is mutable and not thread-safe, StringBuffer is mutable and thread-safe.


20. What is garbage collection in Java?
Show Answer

Ans. Garbage collection is an automatic process in Java that reclaims memory used by objects that are no longer reachable in the program.


21. What is a package in Java?
Show Answer

Ans. A package is a namespace that organizes classes and interfaces. It helps avoid name conflicts and controls access using protected and default access levels.


22. What is the use of the super keyword?
Show Answer

Ans. The super keyword refers to the immediate parent class object. It is used to access parent class methods, variables, and constructors.


23. What is the this keyword in Java?
Show Answer

Ans. this refers to the current class instance. It is commonly used to resolve naming conflicts and pass the current object as a parameter.


24. What is method overriding in Java?
Show Answer

Ans. Method overriding occurs when a subclass provides its own implementation of a method defined in the parent class using the same method name and parameters.


25. Can Java support multiple inheritance?
Show Answer

Ans. Java does not support multiple inheritance with classes to avoid ambiguity. However, it supports multiple inheritance using interfaces.


26. What is a wrapper class in Java?
Show Answer

Ans. Wrapper classes convert primitive data types into objects. Examples include Integer, Double, Character, etc.


27. What are annotations in Java?
Show Answer

Ans. Annotations provide metadata about the program but are not part of the program logic. Examples: @Override, @Deprecated, @SuppressWarnings.


28. What is the difference between ArrayList and LinkedList in Java?
Show Answer

Ans. ArrayList is backed by an array and offers fast random access, while LinkedList uses a doubly-linked list structure and is better for frequent insertions/deletions.


29. What is synchronization in Java?
Show Answer

Ans. Synchronization is used to control access to shared resources by multiple threads to prevent data inconsistency. It can be done using the synchronized keyword or blocks.


30. What is the difference between an interface and a class in Java?
Show Answer

Ans. A class is a blueprint for objects with method implementations, while an interface is a contract that contains abstract methods. Classes can implement multiple interfaces but extend only one class.


31. What is a static block in Java?
Show Answer

Ans. A static block is used for static initialization of a class. It runs once when the class is loaded into memory.


32. What is the difference between static and instance methods?
Show Answer

Ans. Static methods belong to the class and can be called without an instance. Instance methods require an object of the class to be invoked.


33. What is the final keyword in Java?
Show Answer

Ans. The final keyword is used to define constants, prevent method overriding, and inheritance of classes.


34. What is the difference between == and equals() in Java?
Show Answer

Ans. The == operator compares object references, while equals() compares the actual contents of the objects.


35. What is a constructor in Java?
Show Answer

Ans. A constructor is a special method used to initialize objects. It has the same name as the class and no return type.


36. Can a constructor be private in Java?
Show Answer

Ans. Yes, private constructors are used in singleton design patterns to restrict object creation from outside the class.


37. What is garbage collection in Java?
Show Answer

Ans. Garbage collection is the process of automatic memory management that deletes unused objects to free memory.


38. What is the difference between abstract class and interface?
Show Answer

Ans. An abstract class can have both abstract and concrete methods, while an interface only has abstract methods (until Java 8+ which allows default and static methods).


39. What are access modifiers in Java?
Show Answer

Ans. Access modifiers define the visibility of classes, methods, and variables. Types: public, private, protected, and default.


40. What is the difference between throw and throws in Java?
Show Answer

Ans. throw is used to explicitly throw an exception, while throws is used in method signatures to declare possible exceptions.


41. What is exception handling in Java?
Show Answer

Ans. Exception handling is a mechanism to handle runtime errors using try, catch, finally, throw, and throws keywords.


42. What is a try-with-resources statement?
Show Answer

Ans. It's a try block that declares resources, which are closed automatically after execution, used primarily for managing I/O streams.


43. What is multithreading in Java?
Show Answer

Ans. Multithreading allows concurrent execution of two or more threads to perform tasks simultaneously for better performance.


44. What are the states of a thread in Java?
Show Answer

Ans. The thread states are: New, Runnable, Blocked, Waiting, Timed Waiting, and Terminated.


45. What is the difference between process and thread?
Show Answer

Ans. A process is an independent executing program, while a thread is a lightweight subprocess that shares the same memory space.


46. What is the Java Collections Framework?
Show Answer

Ans. It is a set of classes and interfaces that implement commonly reusable data structures such as List, Set, Map, and Queue.


47. What is the difference between HashMap and Hashtable?
Show Answer

Ans. HashMap is not synchronized and allows one null key, while Hashtable is synchronized and does not allow any null keys or values.


48. What is the use of the transient keyword in Java?
Show Answer

Ans. The transient keyword prevents variables from being serialized during object serialization.


49. What is a singleton class in Java?
Show Answer

Ans. A singleton class allows only one instance to be created and provides a global point of access to it.


50. What is the difference between String, StringBuffer, and StringBuilder?
Show Answer

Ans. String is immutable, StringBuffer is mutable and thread-safe, and StringBuilder is mutable but not thread-safe.


51. What is a marker interface?
Show Answer

Ans. A marker interface is an empty interface used to signal the JVM or compiler, such as Serializable or Cloneable.


52. What is method overloading?
Show Answer

Ans. Method overloading is when multiple methods have the same name but different parameter lists in the same class.


53. What is the difference between compile-time and runtime polymorphism?
Show Answer

Ans. Compile-time polymorphism is achieved through method overloading, while runtime polymorphism is achieved through method overriding.


54. What is the use of instanceof in Java?
Show Answer

Ans. The instanceof operator checks if an object is an instance of a specific class or implements an interface.


55. What is classloader in Java?
Show Answer

Ans. A classloader is a subsystem of the JVM responsible for loading class files when required.


56. What is the difference between public, private, protected, and default access?
Show Answer

Ans. Public is accessible everywhere, private only within the class, protected in the same package or subclass, and default only in the same package.


57. What is autoboxing and unboxing?
Show Answer

Ans. Autoboxing is the automatic conversion of primitive types to wrapper classes, and unboxing is the reverse process.


58. What is a daemon thread?
Show Answer

Ans. A daemon thread runs in the background and does not prevent the JVM from exiting once all user threads are done.


59. What is the difference between final, finally, and finalize?
Show Answer

Ans. final is a keyword, finally is a block that executes after try-catch, and finalize() is a method called by the garbage collector before object destruction.


60. What is the Java Virtual Machine (JVM)?
Show Answer

Ans. JVM is a part of the Java Runtime Environment that executes Java bytecode and provides platform independence.


61. What is method hiding in Java?
Show Answer

Ans. Method hiding occurs when a static method in a subclass has the same signature as a static method in the parent class. The method that gets called depends on the reference type.


62. What is the use of the volatile keyword in Java?
Show Answer

Ans. The volatile keyword ensures visibility and ordering of changes to variables across threads. It prevents caching of variables and guarantees the latest value is read.


63. What is object cloning in Java?
Show Answer

Ans. Object cloning is the process of creating an exact copy of an object using the clone() method from the Cloneable interface.


64. What are static nested classes?
Show Answer

Ans. A static nested class is a nested class that is declared static. It can access the static members of the outer class and doesn't require an outer class instance to be instantiated.


65. What is a functional interface?
Show Answer

Ans. A functional interface contains exactly one abstract method and can be used as the assignment target for lambda expressions. Example: Runnable, Comparator.


66. What is a lambda expression?
Show Answer

Ans. A lambda expression is a concise way to represent an anonymous function. It provides a clear and concise way to implement functional interfaces.


67. What is the Stream API in Java 8?
Show Answer

Ans. Stream API allows functional-style operations on streams of data such as map, filter, reduce, etc., and enables bulk data processing.


68. What are default methods in interfaces?
Show Answer

Ans. Default methods are methods in interfaces with default implementations. They allow interfaces to evolve without breaking existing implementations.


69. What is Optional in Java?
Show Answer

Ans. Optional is a container object used to contain not-null objects. It helps avoid null pointer exceptions.


70. What is the difference between fail-fast and fail-safe iterators?
Show Answer

Ans. Fail-fast iterators throw ConcurrentModificationException if the collection is modified, while fail-safe iterators work on a clone of the collection.


71. What is reflection in Java?
Show Answer

Ans. Reflection is an API that allows inspection and modification of classes, interfaces, methods, and fields at runtime.


72. What is the use of enums in Java?
Show Answer

Ans. Enums define a set of constant values. They are type-safe and can have fields, methods, and constructors.


73. What is the purpose of the transient keyword?
Show Answer

Ans. The transient keyword prevents fields from being serialized during object serialization.


74. What is the difference between shallow copy and deep copy?
Show Answer

Ans. A shallow copy copies object references, whereas a deep copy creates copies of all objects recursively.


75. What is the purpose of the strictfp keyword?
Show Answer

Ans. The strictfp keyword ensures consistent floating-point calculations across platforms.


76. What is the difference between stack and heap memory in Java?
Show Answer

Ans. Stack memory is used for method execution and local variables, while heap memory stores objects and class instances.


77. What is method reference in Java 8?
Show Answer

Ans. A method reference is a shorthand for calling a method. It uses the :: operator and is used with functional interfaces.


78. What is serialization and deserialization?
Show Answer

Ans. Serialization is converting an object into a byte stream. Deserialization is the process of converting a byte stream back into an object.


79. What is the difference between Comparable and Comparator?
Show Answer

Ans. Comparable provides a natural ordering using the compareTo() method, while Comparator allows custom ordering using compare().


80. What is the role of the Java Memory Model (JMM)?
Show Answer

Ans. JMM defines how threads interact through memory and ensures visibility and ordering of variables across threads.


81. What is the difference between Path and File in Java NIO?
Show Answer

Ans. Path is an interface in Java NIO for representing file paths, while File is a legacy class used for file and directory operations.


82. What is a memory leak in Java?
Show Answer

Ans. A memory leak occurs when objects are no longer used but still referenced, preventing garbage collection.


83. What is the difference between stack overflow and out of memory error?
Show Answer

Ans. Stack overflow occurs when the stack memory is full due to deep recursion. OutOfMemoryError occurs when heap memory is exhausted.


84. What are weak references in Java?
Show Answer

Ans. Weak references allow objects to be garbage collected even if they are still referenced. Useful for memory-sensitive caches.


85. What is the Fork/Join framework?
Show Answer

Ans. It is used for parallel task execution by breaking tasks into smaller sub-tasks using recursive decomposition.


86. What is a phantom reference?
Show Answer

Ans. A phantom reference is the weakest level of reference and is used to determine exactly when an object is removed from memory.


87. What is the difference between Array and ArrayList?
Show Answer

Ans. Arrays are fixed in size and can hold primitives, while ArrayLists are dynamic and only hold objects.


88. What is the role of the finalize() method?
Show Answer

Ans. It is called by the garbage collector before destroying the object to perform cleanup, though its use is discouraged.


89. What is JIT compiler?
Show Answer

Ans. Just-In-Time compiler converts bytecode to native code during runtime for performance optimization.


90. What is the difference between runtime exception and checked exception?
Show Answer

Ans. Checked exceptions must be handled at compile-time, while runtime exceptions occur during execution and can be optionally handled.


91. What is a static import?
Show Answer

Ans. Static import allows static members to be used without class qualification, improving code readability.


92. What is a hybrid inheritance in Java?
Show Answer

Ans. Hybrid inheritance is a mix of two or more types of inheritance. Java doesn't support it directly with classes but allows it through interfaces.


93. What are varargs in Java?
Show Answer

Ans. Varargs allow a method to accept a variable number of arguments, declared using ....


94. What is the difference between System.out and System.err?
Show Answer

Ans. System.out is used for standard output, and System.err is used to display error messages.


95. What is the purpose of the assert keyword?
Show Answer

Ans. The assert keyword is used for debugging to test assumptions in code. It throws an error if the condition is false.


96. What is the difference between java and javac?
Show Answer

Ans. java launches the Java application, while javac is the compiler that converts Java source code into bytecode.


97. What is a memory model in Java?
Show Answer

Ans. The Java Memory Model (JMM) defines rules for how threads interact through memory, ensuring consistency and visibility.


98. What is the difference between notify and notifyAll?
Show Answer

Ans. notify() wakes up one waiting thread, while notifyAll() wakes up all waiting threads on the object.


99. What is the role of the java.util.concurrent package?
Show Answer

Ans. It provides classes for concurrency and multithreading such as ExecutorService, BlockingQueue, and CountDownLatch.


100. What are immutable classes in Java?
Show Answer

Ans. Immutable classes are those whose objects cannot be modified once created. Example: String, Integer.


SCHOLARSHIP ADMISSION
Coumputer Course

Popular Courses

(123)
Web Development
(123)
FULL STACK JAVA
PROGRAMING
(123)
PYTHON PROGRAMING
smartfuturepoint