---Advertisement---

Java Interview Questions and Answers (Part-I)

By Manisha

Published On:

---Advertisement---

Q21. What is a Copy Constructor in Java?

A copy constructor is a special constructor used to create a new object by copying the values from an existing object of the same class. Though Java doesn’t provide it explicitly like C++, developers can define it manually by passing an object as a parameter.

Example:
MyClass(MyClass obj) { this.value = obj.value; }


Q22. What is Object Cloning in Java?

Object cloning in Java allows the creation of an exact copy of an object. This is done using the clone() method from the Cloneable interface. Java supports:

  • Shallow Copy: Copies the object but not its nested objects.
  • Deep Copy: Copies the object along with its referenced nested objects.

Q23. Is Java a Pure Object-Oriented Language?

No. Java supports primitive data types (e.g., int, char, boolean), which are not objects. Hence, Java is not considered a purely object-oriented programming language.


Q24. What is a Package in Java?

A package is a namespace that organizes a set of related classes and interfaces. It helps in modular development and prevents class name conflicts.

  • Built-in Packages: java.util, java.io, java.lang
  • User-defined Packages: Created using package keyword.

Q25. What is Coercion in Java?

Coercion refers to the type conversion from one data type to another:

  • Implicit (Automatic): E.g., int to double.
  • Explicit (Casting): E.g., (int) 5.9 converts to 5.

Q26. Can Private Methods be Overridden in Java?

No. Private methods are not visible to subclasses and hence cannot be overridden. If a subclass declares a method with the same name, it’s considered a new method.


Q27. What are the Lifecycle Phases of a Java Thread?

Java threads transition through the following states:

  1. New – Thread is created.
  2. Runnable – Ready to run.
  3. Blocked – Waiting for a resource.
  4. Waiting – Indefinite wait until notified.
  5. Timed Waiting – Waits for a fixed time (e.g., sleep()).
  6. Terminated – Thread has completed execution.

Q28. What is a Marker Interface in Java?

A marker interface has no methods and is used to signal metadata to the JVM or tools.

Examples: Serializable, Cloneable
Modern Java prefers annotations over marker interfaces.


Q29. What is a Memory Leak in Java?

A memory leak occurs when unused objects are still referenced and not eligible for garbage collection, leading to unnecessary memory usage and performance degradation.


Q30. Difference Between new and newInstance() in Java?

Featurenew KeywordnewInstance() Method
Used forCompile-time object creationRuntime (dynamic) instantiation
PerformanceFastSlower due to reflection
Syntax SimplicityEasyComplex, uses reflection APIs

Q31. Difference Between JDK, JRE, and JVM?

  • JDK (Java Development Kit): Tools + JRE for development.
  • JRE (Java Runtime Environment): JVM + libraries for running Java apps.
  • JVM (Java Virtual Machine): Executes Java bytecode.

Hierarchy: JDK > JRE > JVM


Q32. Abstraction vs Encapsulation in Java?

FeatureAbstractionEncapsulation
DefinitionHiding internal logicHiding data within a class
FocusOn behaviorOn data security and binding
Achieved viaInterfaces, Abstract classesAccess modifiers, private fields

Q33. What is Inheritance in Java?

Inheritance allows a child class to acquire properties and methods from a parent class, promoting code reuse.

  • Uses extends keyword.
  • Enables method overriding.
  • Supports single and multilevel inheritance.

Q34. What are Functional Interfaces in Java?

Functional interfaces contain only one abstract method and can be implemented using lambda expressions.

Examples: Runnable, Callable, Comparator


Q35. What is Polymorphism in Java?

Polymorphism allows a method or object to behave differently based on context.

  • Compile-time: Method Overloading.
  • Runtime: Method Overriding.

Q36. Role of default Keyword in Interfaces?

Introduced in Java 8, the default keyword allows methods in interfaces to have a default implementation, ensuring backward compatibility without breaking existing classes.


Q37. What is an Interface in Java?

An interface is a contract that defines abstract methods for classes to implement.

  • Supports multiple inheritance.
  • Can have default and static methods (Java 8+).
  • Declared using interface keyword.

Q38. Difference Between ArrayList and Vector?

FeatureArrayListVector
SynchronizationNot synchronized (faster)Synchronized (thread-safe)
GrowthGrows by 50%Grows by 100% (doubles size)

Q39. What is an Abstract Class in Java?

An abstract class is a class that cannot be instantiated and may contain both abstract and concrete methods.

  • Declared using abstract keyword.
  • Must be extended by subclasses.

Q40. Difference Between HashMap and ConcurrentHashMap?

FeatureHashMapConcurrentHashMap
Thread SafetyNot thread-safeThread-safe
PerformanceFast (non-threaded)Efficient in multithreaded scenarios
Null KeysAllows one null keyDoes not allow null keys

The Next 10 Questions-II: JAVA

---Advertisement---

Leave a Comment