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

P3 Recap Questions

The document discusses key concepts in programming for performance, focusing on response time, single-threading, multithreading, asynchronous programming, and error handling. It highlights techniques to improve performance, the role of the call stack, and the differences between callbacks, promises, and async/await. Additionally, it compares single-threading, multithreading, and asynchronous programming regarding memory usage and scalability.

Uploaded by

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

P3 Recap Questions

The document discusses key concepts in programming for performance, focusing on response time, single-threading, multithreading, asynchronous programming, and error handling. It highlights techniques to improve performance, the role of the call stack, and the differences between callbacks, promises, and async/await. Additionally, it compares single-threading, multithreading, and asynchronous programming regarding memory usage and scalability.

Uploaded by

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

Recap Questions for Paradigm 3: Programming for

Performance
Concept Questions

Response Time and Performance


 Define response time and explain why it is a critical metric for performance?

Answer: Response time is the duration between a request and its response. It is critical
because it directly impacts user experience.

 What are the software-level techniques to improve performance besides increasing


processing power?

Answer: Techniques include asynchronous programming, caching, and optimizing


algorithms.

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.

 What is the role of the call stack in single-threaded programming?

Answer: The call stack tracks the execution of functions, managing function calls and
their return points.

 Why is synchronous I/O considered a waste of CPU time?

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.

 Why does multithreading require runtime support (e.g., JVM)?


Answer: The runtime manages thread creation, execution, and interleaving to optimize
CPU usage.

 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.

 Explain how thread interleaving optimizes CPU usage.

Answer: The runtime switches between threads, ensuring the CPU remains utilized while
some threads wait for I/O.

 What is thread safety, and why is it the developer’s responsibility?

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: Single-threading cannot execute independent tasks in parallel, causing


inefficiencies.

 Define asynchrony in the context of programming and describe how it supports


performance.

Answer: Asynchrony allows tasks to run without waiting for others to complete,
optimizing resource usage.

 Explain the role of callback functions in asynchronous programming.

Answer: Callback functions play a crucial role in asynchronous programming by


allowing developers to define the logic that should execute after an asynchronous task
completes. They serve as a mechanism to “register” a function with the runtime or
execution environment (e.g., the browser, Node.js) so it can be invoked at a later time
when the task finishes.

 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.

 Explain how callback-based asynchrony handles errors.

Answer: Errors are passed as the first parameter in callback functions, requiring explicit
handling.

 Why is callback hell difficult to debug and manage?

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.

 Describe the three states of a promise.

Answer: Pending, fulfilled (resolved), and rejected.

 Explain how chaining promises improves code readability compared to nested


callbacks.

Answer: Chaining flattens the structure, making it linear and easier to follow.

 What is the role of the `.then()` and `.catch()` methods in promise-based


programming?

Answer: `.then()` handles resolved promises, while `.catch()` handles errors.

 How does `Promise.all()` differ from `Promise.race()`?

Answer: `Promise.all()` waits for all promises to resolve, while `Promise.race()`


resolves/rejects with the first settled promise.

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.

 Explain why functions using `await` must be defined as `async`.

Answer: `await` can only be used within functions marked as `async`, which
automatically return a promise.

 What is the role of Immediately Invoked Function Expressions (IIFE) in


`async/await` programming?

Answer: IIFE allows immediate execution of asynchronous code without defining a


separate function.

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.

 When should you favor asynchronous programming over multithreading?

Answer: Asynchronous programming is preferable for I/O-intensive tasks to reduce


memory usage and avoid thread management.

You might also like