read this code: 200+ C++ Programming MCQs | PCBooks - Master C++ Language Online

C++ - Programming MCQ Questions and Answers

Test your knowledge of Microsoft Word with these interactive multiple-choice questions.

« First 7 8 9 10 Last »
« Previous Page Next Page »

141. Which of these is not a valid C++ arithmetic operator?

  • a) +
  • b) -
  • c) *
  • d) ×
Answer: D - × is not a valid operator (use * for multiplication).

142. What is the correct syntax to declare a partial template specialization?

  • a) template<typename T> class MyClass<T*> { };
  • b) template<typename T*> class MyClass { };
  • c) class MyClass<typename T*> { };
  • d) template<*T> class MyClass { };
Answer: A - Partial specialization keeps some template parameters generic.

143. Which of the following is true about std::atomic?

  • a) It guarantees thread safety for all operations
  • b) It prevents compiler optimizations
  • c) It provides lock-free operations when possible
  • d) It cannot be used with built-in types
Answer: C - std::atomic uses lock-free operations when supported by the hardware.

144. What is the output of: cout << (1 << 4);

  • a) 1
  • b) 4
  • c) 8
  • d) 16
Answer: D - Left shift by 4 multiplies 1 by 2⁴ (16).

145. Which of these is not a valid C++20 feature?

  • a) std::format
  • b) std::jthread
  • c) std::generator
  • d) std::source_location
Answer: C - std::generator is not in C++20 (planned for C++23).

146. What is the purpose of the '[[no_unique_address]]' attribute?

  • a) To allow empty base optimization for data members
  • b) To prevent address-taking operations
  • c) To mark singleton classes
  • d) To disable RTTI for a class
Answer: A - It enables EBO for non-static data members.

147. Which STL container provides constant-time insertion at both ends?

  • a) vector
  • b) list
  • c) deque
  • d) array
Answer: C - deque (double-ended queue) provides O(1) insertion at both ends.

148. What is the correct way to declare a user-defined conversion operator?

  • a) operator int() const;
  • b) int operator() const;
  • c) convert int() const;
  • d) operator() int const;
Answer: A - Conversion operators use operator type() syntax.

149. Which of these is not a valid C++ type trait?

  • a) std::is_polymorphic
  • b) std::is_abstract
  • c) std::is_functional
  • d) std::is_final
Answer: C - is_functional is not a standard type trait.

150. What is the output of: cout << sizeof(std::function<void()>);

  • a) 4
  • b) 8
  • c) 16
  • d) Implementation defined
Answer: D - std::function size depends on implementation (typically 32-64 bytes).

151. Which of these is not a valid C++17 feature?

  • a) if constexpr
  • b) Structured bindings
  • c) std::optional
  • d) std::syncstream
Answer: D - std::syncstream was introduced in C++20.

152. What is the purpose of the '[[nodiscard]]' attribute?

  • a) To prevent function inlining
  • b) To warn when return value is ignored
  • c) To disable return value optimization
  • d) To mark functions as pure
Answer: B - [[nodiscard]] warns if a function's return value is ignored.

153. Which STL algorithm is used to find adjacent duplicates?

  • a) std::find
  • b) std::search
  • c) std::adjacent_find
  • d) std::unique
Answer: C - std::adjacent_find finds first adjacent equal elements.

154. What is the correct way to declare a template template parameter?

  • a) template<template<typename> typename T>
  • b) template<typename template<typename> T>
  • c) template<template T<typename>>
  • d) template<typename T<typename>>
Answer: A - template<template<typename> typename T> is the correct syntax.

155. Which of these is not a valid C++14 feature?

  • a) Return type deduction for normal functions
  • b) Variable templates
  • c) Generic lambdas
  • d) std::expected
Answer: D - std::expected is not in C++14 (proposed for C++23).

156. What is the output of: cout << (true + true);

  • a) 0
  • b) 1
  • c) 2
  • d) true
Answer: C - true converts to 1 (1+1=2).

157. Which of these is not a valid C++ string operation?

  • a) substr
  • b) find
  • c) append
  • d) join
Answer: D - join is not a member function (available in some libraries).

158. What is the purpose of the '[[noreturn]]' attribute?

  • a) To indicate a function doesn't return to its caller
  • b) To disable return value optimization
  • c) To mark functions that throw exceptions
  • d) To prevent tail-call optimization
Answer: A - Used for functions like exit() or throw that don't return.

159. Which STL algorithm is used to replace elements?

  • a) std::change
  • b) std::replace
  • c) std::swap
  • d) std::assign
Answer: B - std::replace replaces all occurrences of a value in a range.

160. What is the correct way to declare a fold expression?

  • a) (args + ...)
  • b) ... + args
  • c) fold(args +)
  • d) args fold(+)
Answer: A - Fold expressions use (pack op ...) or (... op pack) syntax.
« First 7 8 9 10 Last »
« Previous Page Next Page »

Learn Computer Skills with PCBooks

Master computer skills with PCBooks! Explore interactive tutorials, MCQ tests, and online exams for programming, web development, and IT fundamentals. Perfect for beginners and advanced learners.

Online MCQ Tests for Programming

Practice programming MCQ questions and improve your coding skills with our online quizzes. Whether you're learning Python, Java, or C programming, PCBooks has you covered.

Free Web Development Tutorials

Learn HTML, CSS, and JavaScript with our free web development tutorials. Test your knowledge with interactive quizzes and online exams.