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 »

161. Which of these is not a valid C++ operator precedence level?

  • a) Multiplication before addition
  • b) Bitwise AND before logical OR
  • c) Assignment before comparison
  • d) Shift before relational
Answer: C - Assignment has lower precedence than comparison in C++.

162. What is the correct syntax for a constrained template (C++20)?

  • a) template<std::integral T>
  • b) template<typename T> requires std::integral<T>
  • c) template<typename T: std::integral>
  • d) Both a and b
Answer: D - Both syntaxes are valid ways to apply constraints.

163. Which of these is not true about consteval functions?

  • a) They must be evaluated at compile time
  • b) They can call constexpr functions
  • c) They can be evaluated at runtime
  • d) They cannot have static variables
Answer: C - consteval functions are strictly compile-time only.

164. What is the output of: cout << (5 & 3);

  • a) 1
  • b) 7
  • c) 8
  • d) 15
Answer: A - Bitwise AND of 5 (101) and 3 (011) is 1 (001).

165. Which of these is not a valid C++23 feature?

  • a) std::mdspan
  • b) std::print
  • c) std::generator
  • d) std::expected
Answer: B - std::print is not in C++23 (it's std::format with output).

166. What is the purpose of the '[[assume]]' attribute?

  • a) To mark code assumptions for optimization
  • b) To indicate thread safety
  • c) To enforce exception specifications
  • d) To declare abstract methods
Answer: A - [[assume]] allows programmers to express assumptions to the optimizer.

167. Which STL container provides best cache locality?

  • a) std::list
  • b) std::vector
  • c) std::map
  • d) std::unordered_map
Answer: B - vector stores elements contiguously for best cache performance.

168. What is the correct way to declare a coroutine (C++20)?

  • a) co_task my_coroutine() { co_await x; }
  • b) task my_coroutine() co { co_await x; }
  • c) task my_coroutine() { co_await x; }
  • d) coroutine my_coroutine() { co_await x; }
Answer: C - Any function containing co_await/co_yield/co_return is a coroutine.

169. Which of these is not a valid C++ memory order?

  • a) memory_order_relaxed
  • b) memory_order_acquire
  • c) memory_order_sequential
  • d) memory_order_seq_cst
Answer: C - memory_order_sequential is not valid (use memory_order_seq_cst).

170. What is the output of: cout << sizeof(std::optional<int>);

  • a) 4
  • b) 8
  • c) 12
  • d) Implementation defined
Answer: B - Typically 8 bytes (4 for int + 4 for bool, with padding).

171. Which of these is not a valid C++20 ranges adapter?

  • a) views::filter
  • b) views::transform
  • c) views::map
  • d) views::reverse
Answer: C - views::map doesn't exist (use views::transform).

172. What is the purpose of the '[[likely]]' attribute?

  • a) To mark exception-prone code
  • b) To indicate branch prediction hints
  • c) To force inlining
  • d) To disable optimizations
Answer: B - [[likely]] helps compiler optimize for the expected path.

173. Which STL algorithm is used to partition elements?

  • a) std::sort
  • b) std::partition
  • c) std::group
  • d) std::divide
Answer: B - std::partition rearranges elements based on a predicate.

174. What is the correct way to declare a template lambda?

  • a) []<typename T>(T x) { return x; }
  • b) template<typename T> [](T x) { return x; }
  • c) lambda<typename T>(T x) { return x; }
  • d) []template<T>(T x) { return x; }
Answer: A - Template parameters come between [] and parameter list.

175. Which of these is not a valid C++23 feature?

  • a) std::stacktrace
  • b) std::expected
  • c) std::generator
  • d) std::networking
Answer: D - Networking was postponed from C++23.

176. What is the output of: cout << (10 / 4.0);

  • a) 2
  • b) 2.5
  • c) 2.0
  • d) 3
Answer: B - Floating-point division preserves fractional part.

177. Which of these is not a valid C++ chrono clock?

  • a) std::chrono::system_clock
  • b) std::chrono::steady_clock
  • c) std::chrono::wall_clock
  • d) std::chrono::high_resolution_clock
Answer: C - wall_clock is not a standard clock type.

178. What is the purpose of the '[[deprecated]]' attribute?

  • a) To mark obsolete features
  • b) To disable compiler optimizations
  • c) To indicate thread-unsafe code
  • d) To prevent inlining
Answer: A - It marks features that may be removed in future versions.

179. Which STL algorithm is used to generate values?

  • a) std::fill
  • b) std::generate
  • c) std::create
  • d) std::make
Answer: B - std::generate assigns values from a generator function.

180. What is the correct way to declare a template alias?

  • a) template<typename T> using Vec = std::vector<T>;
  • b) template alias Vec<T> = std::vector<T>;
  • c) alias template<T> Vec = std::vector<T>;
  • d) template Vec<typename T> = std::vector<T>;
Answer: A - Template aliases use 'using' keyword with template parameters.
« 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.