🚀 Coding Interview Questions with Answers (Part 14)
1️⃣3️⃣1️⃣ What is the Difference Between a Pointer and a Reference?
Answer:
Although both pointers and references are used to access variables indirectly, they have key differences.
Pointer
• Stores the memory address of a variable.
• Can be reassigned to point to another variable.
• Can be "NULL" or "nullptr".
• Requires dereferencing ("_") to access the value.
Reference
• Acts as an alias for an existing variable.
• Cannot be reassigned after initialization.
• Cannot be null.
• Accesses the value directly without dereferencing.
Pointers provide more flexibility, while references are generally safer and easier to use.
1️⃣3️⃣2️⃣ What is Exception Handling?
Answer:
Exception handling is a mechanism used to detect and handle runtime errors gracefully without crashing the program.
Most programming languages use the following keywords:
• "try" – Contains code that may throw an exception.
• "catch" – Handles the exception.
• "finally" – Executes regardless of whether an exception occurs (available in many languages).
Benefits:
• Prevents unexpected program termination.
• Improves code reliability.
• Makes debugging easier.
1️⃣3️⃣3️⃣ What is Multithreading?
Answer:
Multithreading is the ability of a program to execute multiple threads simultaneously within a single process.
Advantages:
• Better CPU utilization.
• Faster execution of tasks.
• Improved application responsiveness.
Applications:
• Web servers
• Games
• Video processing
• Download managers
1️⃣3️⃣4️⃣ What is Concurrency?
Answer:
Concurrency is the ability of a system to manage multiple tasks at the same time. The tasks may not execute simultaneously but make progress by sharing CPU time.
Difference from Parallelism:
• Concurrency: Tasks overlap in execution.
• Parallelism: Tasks run simultaneously on multiple CPU cores.
1️⃣3️⃣5️⃣ What is Synchronization?
Answer:
Synchronization is a technique used to control access to shared resources when multiple threads execute concurrently.
Purpose:
• Prevents data inconsistency.
• Avoids race conditions.
• Ensures thread safety.
Common synchronization mechanisms include:
• Mutex
• Semaphore
• Locks
• Monitors
1️⃣3️⃣6️⃣ What is Deadlock?
Answer:
Deadlock is a situation where two or more threads or processes wait indefinitely for resources held by each other, causing the program to stop making progress.
Necessary Conditions for Deadlock:
• Mutual Exclusion
• Hold and Wait
• No Preemption
• Circular Wait
Prevention: Proper resource allocation and lock ordering.
1️⃣3️⃣7️⃣ What is a Race Condition?
Answer:
A race condition occurs when multiple threads access and modify shared data simultaneously, causing unpredictable or incorrect results.
How to Prevent It:
• Synchronization
• Mutexes
• Atomic operations
• Thread-safe data structures
1️⃣3️⃣8️⃣ What is a Lambda Function?
Answer:
A lambda function is an anonymous function that can be defined without a name. It is commonly used for short operations and as arguments to higher-order functions.
Advantages:
• Concise syntax.
• Improves code readability.
• Useful for functional programming.
Examples:
• Python: lambda x: x ** 2
• Java: (x) -> x * 2
1️⃣3️⃣9️⃣ What are Generics?
Answer:
Generics allow classes, interfaces, and methods to work with different data types while maintaining type safety.
Advantages:
• Code reusability.
• Compile-time type checking.
• Reduced type casting.
• Cleaner and safer code.
Examples: List<String>, List<Integer> in Java.
1️⃣4️⃣0️⃣ What is an Iterator?
Answer:
An iterator is an object that allows you to traverse elements of a collection one by one without exposing its internal structure.
Common Operations:
• "hasNext()" – Checks if more elements exist.
• "next()" – Returns the next element.
• "remove()" – Removes the current element (supported in some languages).
Double Tap ❤️ For More