Quiz: C/C++
C/C++ questions from real interviews as a quiz, with an explanation for every option.
Try it: 5 questions
Loading…
Sample questions
Which approaches help prevent deadlocks when working with multiple mutexes? Select all that apply.
- Do not hold one mutex while waiting to acquire another
- Use
std::lockto acquire several mutexes together - Always acquire mutexes in the same order
- Periodically detect deadlocks after they have occurred
- Set an acquisition timeout but keep holding mutexes already acquired
Which statement about the size of an empty class object in C++ is correct?
- The standard requires
sizeofto always be 1 byte. sizeofis usually 1 byte, but the standard does not specify the exact size.sizeofis always 0 because the class has no fields.- The size is always the size of a pointer so the object has a unique address.
- The standard requires
Which statement best describes functional programming?
- Programs are built around functions, while mutation and side effects are kept to a minimum.
- Programs are built around objects that combine state with methods for changing it.
- Programs consist of commands that change shared variables in sequence.
- Programs must use recursion instead of loops and completely avoid input and output.
Which option best describes the typical path from entering a URL to loading a page?
- The browser parses the HTML and displays the page, then establishes a connection and requests the remaining data.
- The browser parses the URL, resolves the IP via DNS if needed, establishes a connection, sends an HTTP request, and processes the response to display the page.
- The browser resolves the IP via DNS, sends an HTTP request, then establishes a TCP connection and performs the TLS handshake.
- The browser sends an HTTP request first, looks up the IP via DNS next, and establishes a connection only after the server responds.
Which statement correctly describes atomic operations and
memory_orderin C++?- Atomicity synchronizes all operations in a thread, while
memory_orderselects the size of the atomic value. - Every atomic operation automatically synchronizes with all atomic operations in other threads.
- An atomic operation is indivisible to other threads, while
memory_orderspecifies guarantees for ordering and visibility across threads. memory_ordermakes an operation indivisible, while the atomic type determines the order in which changes become visible.
- Atomicity synchronizes all operations in a thread, while
Which mapping of protocols to OSI layers is correct?
- HTTP — data link, TCP — transport, IP — session
- HTTP — application, TCP — transport, IP — network
- HTTP — network, TCP — application, IP — transport
- HTTP — transport, TCP — network, IP — application
What is a memory leak in programming?
- Allocated memory is no longer needed but is not freed and remains occupied.
- The program allocates memory and frees it immediately after use.
- The program accesses memory after it has been freed.
- The program allocates more memory than a current operation requires.
What is usually called a bucket in a hash table?
- A memory region allocated only for the hash value itself
- A counter for the total number of collisions in the table
- A function that converts a key into an array index
- A group of elements mapped to the same slot by hashing
What requirements must you satisfy to use a custom type as a key in
std::maporstd::unordered_map? Select all that apply.- Ensure that keys considered equal have the same hash value.
- Provide
std::mapwith a comparator that defines a strict weak ordering. - Provide
std::unordered_mapwith a comparator that orders keys. - Provide
std::mapwith a hash function and an equality check. - Provide
std::unordered_mapwith a hash function and an equality check.
Which statements about a process's address space and executable size are generally true? Select all that apply.
- Initialized global data usually goes into
.data, and its initial values are stored in the executable file. - Typical regions include code and constants, global data, the heap, the stack, and memory-mapped files.
- Global variables are usually placed on the stack, and their initial values are created on each function call.
- The
.bssregion usually stores zero bytes in the file, so its size directly increases the executable's size. - Uninitialized or zero-initialized global data usually goes into
.bss; the loader allocates and zeroes this region.
- Initialized global data usually goes into