C++ Interview Questions Hindi mein

C++ कोर्स के 25 सवाल, हर सवाल के सही जवाब और explanation के साथ। जवाब देखने से पहले ख़ुद सोचें।

Getting Started with Modern C++

1. Which C++ standard is usually described as the start of "modern C++"?

  1. C++11
  2. C++98
  3. C++03
  4. C++23
जवाब देखें

जवाब: C++11

C++11 introduced auto, lambdas, move semantics and smart pointers, transforming everyday C++.

पाठ: What C++ Is and Where It Is Used

2. What does the linker do?

  1. Expands #include directives
  2. Checks syntax of each source file
  3. Runs the program
  4. Combines object files and libraries into an executable, resolving references between them
जवाब देखें

जवाब: Combines object files and libraries into an executable, resolving references between them

The linker resolves symbols across object files and libraries to produce the final binary.

पाठ: Compiling, Linking and Warnings

3. What does brace initialisation such as `int x{3.7};` do?

  1. Silently truncates to 3
  2. Rounds to 4
  3. Fails to compile because it is a narrowing conversion
  4. Creates a double
जवाब देखें

जवाब: Fails to compile because it is a narrowing conversion

List initialisation forbids narrowing conversions, catching accidental data loss.

पाठ: Types, Variables, const and auto

Control Flow, Functions and Program Structure

4. Why prefer `enum class` over a plain `enum`?

  1. It is faster at run time
  2. It allows floating-point values
  3. Its enumerators are scoped and do not implicitly convert to integers
  4. It is required for switch statements
जवाब देखें

जवाब: Its enumerators are scoped and do not implicitly convert to integers

Scoped enums avoid name clashes and accidental integer conversions.

पाठ: Control Flow and Range-Based Loops

5. What is the usual way to pass a large std::vector that a function only reads?

  1. By value
  2. By const reference
  3. By non-const reference
  4. As a global variable
जवाब देखें

जवाब: By const reference

const reference avoids copying while guaranteeing the function will not modify the argument.

पाठ: Functions and Parameter Passing

6. Why does defining a non-inline function in a header included by two source files cause a link error?

  1. It violates the One Definition Rule: the function is defined twice in the program
  2. Headers cannot contain functions
  3. The compiler ignores headers
  4. Namespaces are missing
जवाब देखें

जवाब: It violates the One Definition Rule: the function is defined twice in the program

Each translation unit gets its own definition, so the linker finds duplicate symbols.

पाठ: Headers, Source Files, Namespaces and the ODR

Memory, Pointers and RAII

7. What happens to a local std::vector when the function it was declared in returns?

  1. Its destructor runs automatically and frees its memory
  2. It leaks unless you call delete
  3. It moves to the heap
  4. It stays alive until the program ends
जवाब देखें

जवाब: Its destructor runs automatically and frees its memory

Automatic objects are destroyed at the end of their scope, and the vector frees its buffer.

पाठ: Stack, Heap and Object Lifetime

8. Which statement about C++ references is true?

  1. A reference can be null
  2. A reference can be reseated to refer to another object
  3. A reference stores a copy of the object
  4. A reference must be initialised and always refers to the same object
जवाब देखें

जवाब: A reference must be initialised and always refers to the same object

References are aliases bound at initialisation; they cannot be null or reseated.

पाठ: Pointers and References

9. Which smart pointer expresses exclusive, single ownership with no reference-counting overhead?

  1. std::shared_ptr
  2. std::weak_ptr
  3. std::unique_ptr
  4. A raw pointer
जवाब देखें

जवाब: std::unique_ptr

unique_ptr has exactly one owner and is as cheap as a raw pointer.

पाठ: RAII and Smart Pointers

Classes, Copying and Moving

10. Why is a member initialiser list preferred over assigning members in the constructor body?

  1. It is required for all members
  2. It makes members public
  3. Members are initialised directly instead of being default-constructed then assigned, and const or reference members require it
  4. It runs after the constructor body
जवाब देखें

जवाब: Members are initialised directly instead of being default-constructed then assigned, and const or reference members require it

Initialiser lists initialise members once, correctly, before the body runs.

पाठ: Classes, Constructors and Destructors

11. What does std::move actually do?

  1. It copies the object to the heap
  2. It casts its argument to an rvalue reference so that a move operation can be chosen
  3. It deletes the original object
  4. It always makes code faster
जवाब देखें

जवाब: It casts its argument to an rvalue reference so that a move operation can be chosen

std::move is a cast; the move constructor or assignment performs the actual transfer.

पाठ: Copy and Move Semantics and the Rule of Zero, Three and Five

12. What is object slicing?

  1. Copying a derived object into a base-class value, losing the derived part
  2. Splitting a class into two files
  3. Calling a virtual function
  4. Deleting an object twice
जवाब देखें

जवाब: Copying a derived object into a base-class value, losing the derived part

Base-class values hold only the base part; use references or pointers for polymorphism.

पाठ: Inheritance, Virtual Functions and Polymorphism

The Standard Library

13. Which container should be your default choice for a sequence of elements in C++?

  1. std::vector
  2. std::list
  3. std::map
  4. std::deque
जवाब देखें

जवाब: std::vector

vector is contiguous, cache-friendly and efficient for most workloads.

पाठ: Containers and Their Costs

14. What does the capture list `[&total]` in a lambda mean?

  1. The lambda receives total as a parameter
  2. The lambda copies total
  3. total becomes a global variable
  4. The lambda captures total by reference, so changes affect the original variable
जवाब देखें

जवाब: The lambda captures total by reference, so changes affect the original variable

& captures by reference; without & the variable would be copied into the lambda.

पाठ: Iterators, Algorithms, Lambdas and Ranges

15. Which type best expresses "a function may or may not return an integer"?

  1. int with -1 as a special value
  2. std::variant<int, int>
  3. std::optional<int>
  4. int*
जवाब देखें

जवाब: std::optional<int>

std::optional makes absence explicit in the type instead of relying on magic values.

पाठ: Vocabulary Types: optional, variant, string_view and span

Templates and Generic Programming

16. When does the compiler generate code for a function template?

  1. When the template is declared
  2. At run time
  3. When it is used with specific types (instantiation)
  4. Only in debug builds
जवाब देखें

जवाब: When it is used with specific types (instantiation)

Templates are instantiated for each set of template arguments actually used.

पाठ: Function and Class Templates

17. What is the main benefit of C++20 concepts?

  1. They make programs run faster
  2. They state template requirements explicitly, giving clearer interfaces and error messages
  3. They replace classes
  4. They enable garbage collection
जवाब देखें

जवाब: They state template requirements explicitly, giving clearer interfaces and error messages

Concepts document and check requirements on template arguments at the call site.

पाठ: Concepts and Constraints (C++20)

18. What does consteval guarantee about a function?

  1. Every call is evaluated at compile time
  2. It runs only at run time
  3. It is inlined
  4. It cannot use loops
जवाब देखें

जवाब: Every call is evaluated at compile time

consteval functions are immediate functions: each call must produce a compile-time constant.

पाठ: auto, decltype, constexpr and Compile-Time Programming

Errors, Concurrency and Undefined Behaviour

19. How should exceptions usually be caught in C++?

  1. By const reference
  2. By value
  3. By pointer
  4. With catch(...) only
जवाब देखें

जवाब: By const reference

Catching by const reference avoids slicing and unnecessary copies.

पाठ: Exceptions, noexcept and std::expected

20. What happens if two threads modify the same int without synchronisation?

  1. The result is always correct
  2. The compiler adds a lock automatically
  3. One thread is blocked automatically
  4. It is a data race, which is undefined behaviour
जवाब देखें

जवाब: It is a data race, which is undefined behaviour

Unsynchronised concurrent writes (or a write with a read) are data races and undefined behaviour.

पाठ: Threads, Mutexes and Atomics

21. Which tool detects out-of-bounds accesses and use-after-free at run time?

  1. ThreadSanitizer
  2. clang-format
  3. AddressSanitizer
  4. The linker
जवाब देखें

जवाब: AddressSanitizer

AddressSanitizer instruments memory accesses to catch these errors.

पाठ: Undefined Behaviour, Sanitizers and Common Bugs

Building, Competitive Programming and Revision

22. In modern CMake, how should you set include directories for a target?

  1. With global include_directories() for everything
  2. By copying headers into /usr/include
  3. With target_include_directories() on the specific target
  4. CMake cannot handle include directories
जवाब देखें

जवाब: With target_include_directories() on the specific target

Target-based commands keep settings attached to the targets that need them.

पाठ: Projects with CMake and Package Managers

23. Why use `std::accumulate(a.begin(), a.end(), 0LL)` instead of `0` for a large sum of long long values?

  1. 0LL is faster to type
  2. The initial value's type sets the accumulator type; 0 would accumulate in int and could overflow
  3. accumulate requires 0LL
  4. It sorts the vector first
जवाब देखें

जवाब: The initial value's type sets the accumulator type; 0 would accumulate in int and could overflow

accumulate returns and accumulates in the type of the initial value.

पाठ: C++ for DSA and Competitive Programming

24. According to modern C++ guidelines, how should ownership of a heap object usually be expressed?

  1. A smart pointer such as std::unique_ptr
  2. A raw pointer with a comment
  3. A global variable
  4. A C-style cast
जवाब देखें

जवाब: A smart pointer such as std::unique_ptr

Smart pointers make ownership explicit and release resources automatically.

पाठ: Modern C++ Guidelines

25. Which statement about a moved-from std::vector is correct?

  1. It is destroyed immediately
  2. Using it in any way is undefined behaviour
  3. It still holds all its original elements
  4. It is in a valid but unspecified state and can be assigned a new value or destroyed
जवाब देखें

जवाब: It is in a valid but unspecified state and can be assigned a new value or destroyed

Moved-from standard objects remain valid; you may reassign or destroy them.

पाठ: Revision and Interview Questions

← कोर्स पर वापस जाएँ