P3 Recap Questions
P3 Recap Questions
Performance
Concept Questions
Answer: Response time is the duration between a request and its response. It is critical
because it directly impacts user experience.
Single-Threading
Explain how single-threading works. Why are statements in single-threading
synchronous and blocking?
Answer: Single-threading executes statements one after the other, synchronously, with
each statement blocking subsequent ones until it completes.
Answer: The call stack tracks the execution of functions, managing function calls and
their return points.
Answer: The CPU remains idle while waiting for the I/O operation to complete, instead
of performing other tasks.
Multithreading
What are the key differences between threads and processes in terms of memory
usage?
Answer: Threads share the same memory space, while processes have separate memory
spaces.
What is the purpose of the `start()` and `run()` methods in Java’s `Thread` class?
Answer: `start()` initiates a new thread and calls `run()`, which contains the thread’s
execution logic.
Answer: The runtime switches between threads, ensuring the CPU remains utilized while
some threads wait for I/O.
Answer: Thread safety ensures shared data remains consistent. Developers must manage
synchronization to avoid race conditions.
Asynchronous Programming
What is the main limitation of single-threading when dealing with independent
statements or tasks?
Answer: Asynchrony allows tasks to run without waiting for others to complete,
optimizing resource usage.
What is the event loop, and how does it manage callbacks in a single-threaded
environment?
Answer: The event loop monitors asynchronous tasks, placing their callbacks in the call
stack when ready.
Why are runtimes like Node.js not suitable for heavy computational tasks?
Answer: Runtimes like Node.js are not suitable for heavy computational tasks because
Node.js operates on a single-threaded event loop that is primarily designed to handle I/O-
bound operations efficiently, not CPU-intensive computations.
Callback Hell
What is callback hell, and why does it occur?
Answer: Callback hell is the excessive nesting of callbacks, making code difficult to read
and manage. It occurs in deeply dependent asynchronous tasks.
Answer: Errors are passed as the first parameter in callback functions, requiring explicit
handling.
Answer: The nesting increases complexity, making the flow of execution hard to trace.
Promises
What is a promise, and how does it differ from a traditional callback?
Answer: A promise represents a future value and allows chaining, unlike callbacks,
which execute immediately and can’t chain easily.
Answer: Chaining flattens the structure, making it linear and easier to follow.
Async/Await
What is the main advantage of using `async/await` over promises?
Answer: It simplifies asynchronous code, making it more readable and easier to manage.
Answer: `await` can only be used within functions marked as `async`, which
automatically return a promise.
Practical Comparisons
Compare single-threading, multithreading, and asynchronous programming in terms
of memory usage, performance, and scalability.
Answer: Single-threading uses one call stack and blocks, multithreading uses multiple
stacks and shares memory, and asynchronous programming is non-blocking and scalable
for I/O tasks.