0% found this document useful (0 votes)
3 views4 pages

Java

The document provides a comprehensive overview of Java, including its components such as JDK, JVM, and JRE, as well as concepts like memory management, OOP principles, and exception handling. It also discusses Java's collection framework, threading, and various interfaces and classes, highlighting their functionalities and differences. Key topics include the String pool, garbage collection, and the fail-fast property of iterators.

Uploaded by

d7frpg49wb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Java

The document provides a comprehensive overview of Java, including its components such as JDK, JVM, and JRE, as well as concepts like memory management, OOP principles, and exception handling. It also discusses Java's collection framework, threading, and various interfaces and classes, highlighting their functionalities and differences. Key topics include the String pool, garbage collection, and the fail-fast property of iterators.

Uploaded by

d7frpg49wb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1. What is Java, JDK, and JVM/JRE?

Java: A programming language and platform that provides a system for developing and running
applications.
JDK (Java Development Kit): A software development kit for Java developers, including tools like
compilers, debuggers, and libraries.
JVM (Java Virtual Machine): A virtual machine that enables Java applications to run on any platform
without modification. It reads and executes bytecode.
JRE (Java Runtime Environment): A package that provides libraries, Java Virtual Machine (JVM), and
other components to run Java applications.

2. Explain Java classloaders: Bootstrap, Extension, Application classloader.


Bootstrap Classloader: The parent of all classloaders, responsible for loading core Java classes (from the
JDK’s lib directory).
Extension Classloader: Loads classes from the JDK’s ext directory.
Application Classloader: Loads classes from the classpath specified by the user (i.e., the application’s
specific libraries).

3. What is memory management in Java?


Java uses an automatic garbage collection mechanism to manage memory. Memory is divided into the
following areas:
Heap: Stores objects.
Stack: Stores method calls and local variables.
Method Area: Stores class definitions.
Program Counter (PC) Register: Keeps track of the currently executing instruction.
Native Method Stack: Used for native methods.

4. What are OOPS concepts?


Abstraction: Hiding implementation details and showing only the necessary features of an object.
Encapsulation: Wrapping data and methods together into a single unit (class) and restricting access using
access modifiers.
Polymorphism: The ability of an object to take multiple forms. This includes method overloading and
overriding.
Inheritance: A mechanism where one class acquires the properties and behaviors of another class.

5. Composition vs Inheritance
Composition: A "has-a" relationship where one object contains another object.
Inheritance: An "is-a" relationship where one class inherits properties and behavior from another.

6. How to override private/static methods?


Private Methods: Private methods cannot be overridden, but they can be redefined in a subclass.
Static Methods: Static methods can be hidden (not overridden) in the subclass by defining a method with
the same signature.

7. Explain the following keywords in Java: static, this, super, final, finally, finalize, volatile, transient.
static: Defines class-level members that are shared across all instances of the class.
this: Refers to the current object instance.
super: Refers to the superclass of the current object.
final: Used to define constants, prevent method overriding, or restrict inheritance.
finally: A block of code that always executes after a try-catch block, regardless of an exception.
finalize: A method called by the garbage collector before an object is destroyed.
volatile: Ensures visibility of changes to a variable across threads.
transient: Prevents serialization of a field.

8. What is an inner class/anonymous inner class?


Inner Class: A class defined within another class.
Anonymous Inner Class: A class with no name, defined and instantiated in one expression, commonly
used to implement interfaces or extend classes.

9. Can an inner class be static?


Yes, an inner class can be static. It is known as a static nested class. It behaves like a top-level class but
is nested within another class.

10. What is Garbage Collection in Java?


Garbage collection is the automatic process of reclaiming memory by destroying objects that are no
longer in use or reachable.

11. Interface vs Abstract class


Interface: Defines method signatures without implementation. A class implements an interface.
Abstract Class: Can define both abstract methods (without implementation) and concrete methods (with
implementation). A class extends an abstract class.

12. What is the String pool in Java and its benefits?


The String pool is a special memory region for storing unique String objects to optimize memory usage.
When two String variables point to the same literal value, they reference the same object in the pool.

13. Why is String immutable in Java?


Strings are immutable to ensure thread safety, security, and consistency, as they can be shared and
cached in various parts of the program without risk of modification.

14. What is String intern()?


intern() is a method in the String class that checks if a String is already in the String pool. If it is, it returns
the reference to the existing String; otherwise, it adds the String to the pool.

15. What is the Exception Hierarchy in Java?


Java exceptions extend the Throwable class, which has two main branches:
Error: Used for serious issues like memory overflow.
Exception: Used for recoverable errors. It is divided into Checked (compile-time) and Unchecked
(runtime) exceptions.

16. Error vs Exception


Error: Represents a serious problem that cannot be handled by the program (e.g., OutOfMemoryError).
Exception: Represents conditions that a program can handle (e.g., IOException).

17. What are checked and unchecked exceptions with examples?


Checked Exceptions: Exceptions that must be handled during compilation (e.g., IOException,
SQLException).
Unchecked Exceptions: Exceptions that occur during runtime (e.g., NullPointerException,
ArrayIndexOutOfBoundsException).

18. Is NullPointerException checked or unchecked?


NullPointerException is an unchecked exception, meaning it occurs at runtime and is a subclass of
RuntimeException.

19. What are Marker interfaces, Serializable, Immutable, and Singleton?


Marker Interface: An interface with no methods, used to mark classes for specific behavior (e.g.,
Serializable).
Serializable: An interface that enables an object to be serialized (converted to a byte stream).
Immutable: A class whose state cannot be changed after it is created (e.g., String).
Singleton: A design pattern that restricts the instantiation of a class to one object.
20. What does Threadsafe mean?
Thread-safe means that a class or method can be safely used by multiple threads at the same time
without leading to data corruption or inconsistent results.

21. Explain Threading, Life Cycle, Thread Communication, and Thread Local.
Threading: The process of running multiple threads (independent units of execution) within a program.
Life Cycle: A thread goes through various states such as New, Runnable, Blocked, Waiting, Timed
Waiting, and Terminated.
Thread Communication: Threads communicate using methods like wait(), notify(), and notifyAll().
Thread Local: A special type of variable that is isolated to a thread, so each thread has its own copy.

22. Why must wait(), notify(), and notifyAll() be called from synchronized methods or blocks?
These methods must be called from synchronized methods or blocks because they work on the intrinsic
lock (monitor) of the object, ensuring proper thread synchronization.

23. What is Callable and Future?


Callable: A functional interface similar to Runnable but can return a result or throw an exception.
Future: Represents the result of an asynchronous computation. It provides methods to check the status
and retrieve the result of a computation.

24. What is FutureTask?


FutureTask is a concrete implementation of the Future interface that can be used to track the progress of
an asynchronous task.

25. What is the Collection Framework?


The Collection Framework is a set of classes and interfaces that provide various types of collections (e.g.,
lists, sets, maps) and algorithms for working with them.

26. What is the hierarchy of classes in the Collection Framework?


Collection (interface)
List (interface)
ArrayList, LinkedList
Set (interface)
HashSet, TreeSet
Queue (interface)
PriorityQueue, LinkedList
Map (interface)
HashMap, TreeMap

27. What are Generics in the Collection Framework?


Generics enable type safety in collections by allowing you to specify the type of elements stored in a
collection (e.g., List<String>).

28. How does HashMap work? Explain the equals and hashcode contract.
A HashMap stores key-value pairs, using the key’s hashCode to determine the bucket in which the entry
is stored. The equals() method is used to compare keys for equality. The contract states that:
If two objects are equal according to equals(), they must have the same hashCode.
If two objects have different hashCode, they are not equal.

29. ArrayList vs LinkedList vs Array


ArrayList: A dynamic array that allows random access and is backed by an array.
LinkedList: A doubly-linked list that allows efficient insertions and deletions but slower random access.
Array: A fixed-size collection of elements, with constant-time access but no flexibility in size.

30. What are concurrent collection classes?


Concurrent collections are thread-safe collections designed for concurrent access (e.g.,
ConcurrentHashMap, CopyOnWriteArrayList).

31. Comparable vs Comparator interfaces?


Comparable: Defines a natural order for objects of a class using the compareTo() method.
Comparator: Defines an external order for objects using the compare() method.

32. How does Collections.sort() work using Comparable and Comparator?


Comparable: Objects must implement compareTo() for natural sorting.
Comparator: Allows custom sorting by passing a comparator to Collections.sort().

33. Enumeration vs Iterator vs ListIterator


Enumeration: An older interface for iterating through collections. It is less powerful than Iterator.
Iterator: Allows iteration through a collection and removal of elements.
ListIterator: A specialized iterator for lists that allows bidirectional traversal and modification of elements.

34. What is the fail-fast property of Iterator and ConcurrentModificationException?


Fail-fast: An iterator immediately throws a ConcurrentModificationException if the collection is modified
while it is being iterated.
Fail-safe: A collection or iterator that allows modification without throwing an exception (e.g.,
CopyOnWriteArrayList).

35. What does Collections.synchronizedCollection(Collection c) do?


Collections.synchronizedCollection() returns a thread-safe collection that synchronizes all methods on the
collection.

You might also like