Abstraction is the process of hiding complex implementation details and showing only the essential features of an object or system to the user. In Java, it is achieved using abstract classes and interfaces.
When you drive a car, you use the steering wheel and pedals without needing to know the intricate workings of the engine or transmission; those details are abstracted away.
Card 2
List
Answer
A List is an ordered collection (also known as a sequence) that allows duplicate elements. Elements can be accessed by their integer index, and insertion order is maintained.
Think of a grocery list; the order matters, and you can write "milk" twice if you need two cartons.
Card 3
Set
Answer
A Set is a collection that cannot contain duplicate elements. It models the mathematical set abstraction and does not guarantee any specific order of its elements.
A set of unique playing cards in your hand; you cannot have two identical cards.
Card 4
Map
Answer
A Map is an object that maps keys to values, where each key must be unique. It cannot contain duplicate keys, and each key can map to at most one value.
A dictionary maps words (keys) to their definitions (values), and each word is unique.
Card 5
HashMap
Answer
HashMap is a hash table-based implementation of the Map interface, providing unsorted storage and fast average-case performance for basic operations like get and put. It allows one null key and multiple null values.
Imagine a large set of mailboxes (buckets) where each letter (value) is placed into a specific box based on the recipient's name (key) using a quick sorting rule (hashing).
Card 6
Iterator
Answer
An Iterator is an object that enables traversal over a collection of elements one by one. It provides methods like hasNext() to check for more elements and next() to retrieve the next element.
It acts like a cursor that moves through a list, letting you look at each item without revealing the collection's underlying structure.
Card 7
Comparable interface
Answer
The Comparable interface allows objects of a class to be ordered, providing a natural ordering for that class. It requires implementing the compareTo(Object o) method.
If you want to sort a list of Student objects by their student ID, the Student class would implement Comparable and define compareTo based on ID.
Card 8
Checked Exception
Answer
A checked exception is an exception that the Java compiler forces you to handle or declare in your method signature using the 'throws' keyword. These typically represent recoverable problems outside the program's immediate control, like I/O errors.
The compiler "checks" for these at compile time, ensuring you address potential issues like a file not being found or a network connection failing.
Card 9
Unchecked Exception
Answer
An unchecked exception is an exception that the Java compiler does not force you to handle or declare. These are typically runtime errors, such as NullPointerException or ArrayIndexOutOfBoundsException, indicating programming bugs.
These are often preventable by better coding practices, so the compiler doesn't demand handling, assuming the programmer should have avoided them.
Card 10
try-catch-finally
Answer
The try block encloses code that might throw an exception, the catch block handles a specific type of exception if thrown, and the optional finally block executes code regardless of whether an exception occurred or was handled.
This structure ensures that critical cleanup code, like closing a file or database connection, always runs, even if an error interrupts the main logic.
Card 11
throw keyword
Answer
The 'throw' keyword is used to explicitly create and throw an instance of an exception. It signals that an abnormal event has occurred within the program's execution.
If a method detects an invalid input, it might 'throw new IllegalArgumentException()' to signal the caller that something went wrong.
Card 12
throws keyword
Answer
The 'throws' keyword is used in a method signature to declare that the method might throw one or more specified types of checked exceptions. It informs callers that they must handle these potential exceptions.
A method reading from a file might declare 'throws IOException' to indicate that a file reading error could occur, and the caller needs to be prepared for it.
Card 13
Generics
Answer
Generics enable you to write classes, interfaces, and methods that operate on types as parameters, allowing for stronger type checking at compile time and eliminating the need for explicit casts. They promote code reusability and type safety.
Instead of a List of "Objects" that requires casting, Generics allow you to declare a List of "Strings" (List<String>), ensuring only strings can be added and retrieved without casting.
Card 14
Type Parameter
Answer
A type parameter is a placeholder for a specific type that is specified when a generic class or method is instantiated or invoked. It is typically denoted by a single uppercase letter, like 'T' for Type or 'E' for Element.
In 'List<E>', 'E' is the type parameter; when you create 'List<String>', 'String' becomes the concrete type argument for 'E'.
Card 15
Wildcard
Answer
A wildcard, represented by a question mark (?), is used in generic code to represent an unknown type. It allows for flexibility in method arguments that accept generic types, especially with upper-bounded (<? extends T>) or lower-bounded (<? super T>) constraints.
A method designed to print any type of List could accept 'List<?>', or a method processing numbers could take 'List<? extends Number>' to work with Integers, Doubles, etc.
Card 16
Type Erasure
Answer
Type erasure is the process by which the Java compiler removes all generic type information during compilation, replacing type parameters with their bounds (or Object if unbounded) and inserting casts as necessary. This ensures backward compatibility with older Java versions.
At runtime, 'List<String>' and 'List<Integer>' become simply 'List', meaning generic type information is only enforced at compile-time, not preserved during execution.
Card 17
Thread
Answer
A thread is the smallest unit of execution within a process, representing an independent path of execution that runs concurrently with other threads within the same program. Threads share the same memory space of their parent process.
Think of a single program (process) as a factory, and threads are individual workers within that factory, each performing a task simultaneously.
Card 18
Synchronization
Answer
Synchronization is a mechanism used to control access to shared resources by multiple threads, ensuring that only one thread can access a critical section of code at a time. It prevents data corruption and race conditions.
This is like a single-person restroom; only one thread (person) can enter (access the resource) at a time, preventing conflicts.
Card 19
Race Condition
Answer
A race condition occurs when multiple threads access shared data concurrently and at least one of them modifies the data, leading to an unpredictable or incorrect final result depending on the timing of their execution.
If two threads try to increment the same counter variable simultaneously without synchronization, the final count might be less than expected because one increment operation could overwrite another.
Card 20
Volatile keyword
Answer
The 'volatile' keyword ensures that a variable's value is always read from main memory and written to main memory, preventing threads from caching local copies of the variable. This guarantees visibility of changes across threads but does not provide atomicity.
Without 'volatile', one thread might update a shared flag, but another thread might keep reading an outdated cached version of that flag, never seeing the change.
Card 21
Deadlock
Answer
Deadlock is a situation where two or more threads are blocked indefinitely, each waiting for the other to release a resource that it needs. This results in a permanent halt in the execution of the involved threads.
Imagine Thread A holds Resource X and needs Resource Y, while Thread B holds Resource Y and needs Resource X; neither can proceed, causing a standstill.