Skip to content

Latest commit

 

History

History
38 lines (27 loc) · 3.44 KB

tricky_questions.md

File metadata and controls

38 lines (27 loc) · 3.44 KB

Here are a few tricky questions in C++ that often challenge programmers, especially those new to the language:

  1. Deep Copy vs Shallow Copy:

    • Question: Explain the difference between a deep copy and a shallow copy. What problems might arise when using shallow copy?
    • Context: This question tests understanding of how objects are copied in C++. Shallow copy copies the object's members as they are, including the memory addresses, leading to multiple objects sharing the same dynamically allocated memory. Deep copy creates a separate copy of dynamically allocated memory, avoiding potential issues with shared resources.
  2. Virtual Functions and Polymorphism:

    • Question: What are virtual functions and how do they enable polymorphism in C++?
    • Context: This question probes understanding of one of the key features of object-oriented programming in C++. Virtual functions allow derived classes to override methods in base classes, enabling runtime polymorphism.
  3. Undefined Behavior and Sequence Points:

    • Question: What is undefined behavior in C++, and can you give an example involving sequence points?
    • Context: Undefined behavior is a concept where the C++ standard does not define what happens, allowing for unpredictable results. A classic example is int i = 0; i = i++ + ++i;, which involves modifying a variable multiple times between sequence points.
  4. Resource Management (RAII):

    • Question: Explain the RAII (Resource Acquisition Is Initialization) idiom and its importance in C++.
    • Context: This is about managing resources like memory, file handles, etc., in a safe way. RAII ensures that resources are properly released when objects go out of scope, preventing resource leaks.
  5. Template Specialization and Overloading:

    • Question: What is template specialization in C++, and how is it different from function overloading?
    • Context: This question tests knowledge of advanced template features. Template specialization provides a way to define a specific implementation for a particular data type, whereas overloading allows multiple functions with the same name but different parameters.
  6. Move Semantics and Rvalue References:

    • Question: What are move semantics and rvalue references, and how do they improve performance in C++?
    • Context: Introduced in C++11, move semantics and rvalue references allow the efficient transfer of resources from temporary objects (rvalues) to other objects, reducing unnecessary copying.
  7. The Rule of Three/Five:

    • Question: What are the Rule of Three and the Rule of Five in C++, and when are they applied?
    • Context: These rules relate to the correct implementation of copy control, which includes the copy constructor, copy assignment operator, destructor (Rule of Three), and, additionally, the move constructor and move assignment operator (Rule of Five).
  8. Const Correctness:

    • Question: What is const correctness in C++, and why is it important?
    • Context: This concept involves using the const keyword to prevent modification of objects. It's crucial for writing safe and predictable code, especially in the context of function parameters and class methods.

Each of these questions delves into fundamental and complex aspects of C++ programming, testing not just language syntax but also concepts crucial to effective and efficient C++ code development.

code