CPP 22 02CPP C++ Certified Professional Programmer Practice Exam, Exams of Technology

Covers advanced C++ concepts such as OOP design, inheritance, polymorphism, templates, STL, smart pointers, exception handling, memory management, and modern C++ (11/14/17) features. Realistic coding problems enhance professional-level C++ competency.

Typology: Exams

2024/2025

Available from 12/02/2025

shilpi-jain-1
shilpi-jain-1 🇮🇳

4.2

(5)

29K documents

1 / 126

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPP 22 02CPP C++ Certified Professional
Programmer Practice Exam
**Question 1.** Which of the following statements about the C++ `auto` type
specifier is correct?
A) It can only be used for variables of builtin types.
B) It deduces the type from the initializer at compile time.
C) It forces the variable to be of type `int`.
D) It is only valid inside class templates.
Answer: B
Explanation: `auto` tells the compiler to deduce the variable’s type from its
initializer, allowing any type, not just builtin ones.
**Question 2.** In C++20, which feature allows a function to be called with a
compiletime constant expression and be evaluated at compile time?
A) `constexpr`
B) `consteval`
C) `constinit`
D) `inline`
Answer: B
Explanation: `consteval` forces a function to be evaluated at compile time;
`constexpr` permits but does not require compiletime evaluation.
**Question 3.** Which of the following is a correct way to declare a lambda that
captures all local variables by reference and returns an `int`?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download CPP 22 02CPP C++ Certified Professional Programmer Practice Exam and more Exams Technology in PDF only on Docsity!

Programmer Practice Exam

Question 1. Which of the following statements about the C++ auto type specifier is correct? A) It can only be used for variables of built‑in types. B) It deduces the type from the initializer at compile time. C) It forces the variable to be of type int. D) It is only valid inside class templates. Answer: B Explanation: auto tells the compiler to deduce the variable’s type from its initializer, allowing any type, not just built‑in ones. Question 2. In C++20, which feature allows a function to be called with a compile‑time constant expression and be evaluated at compile time? A) constexpr B) consteval C) constinit D) inline Answer: B Explanation: consteval forces a function to be evaluated at compile time; constexpr permits but does not require compile‑time evaluation. Question 3. Which of the following is a correct way to declare a lambda that captures all local variables by reference and returns an int?

Programmer Practice Exam

A) [&] () - > int { return x; } B) [=] () int { return x; } C) [&] (int) - > int { return x; } D) [=] () - > int { return x; } Answer: A Explanation: [&] captures by reference, () - > int specifies no parameters and an int return type. Question 4. What does the C++ keyword [[nodiscard]] indicate when applied to a function? A) The function must be called with a pointer argument. B) The return value should not be ignored by the caller. C) The function cannot be overloaded. D) The function is thread‑local. Answer: B Explanation: [[nodiscard]] generates a warning if the caller discards the function’s return value. Question 5. Which of the following containers provides constant‑time insertion and removal at both ends but does not guarantee contiguous storage? A) std::vector B) std::deque

Programmer Practice Exam

Answer: A Explanation: std::unique_ptr is non‑copyable; ownership can only be transferred via move semantics. Question 8. Which overload of operator new is called when allocating an array of objects? A) operator new(std::size_t) B) operator new[](std::size_t) C) operator new(void*, std::size_t) D) operator new(std::size_t, std::nothrow_t const&) Answer: B Explanation: operator new[] is used for array allocations, while operator new handles single‑object allocations. Question 9. What is the result of applying std::move to an lvalue of type std::vector<int>? A) It copies the vector. B) It casts the vector to an rvalue reference, enabling move semantics. C) It deletes the vector. D) It makes the vector const. Answer: B

Programmer Practice Exam

Explanation: std::move yields an rvalue reference, allowing the move constructor or move assignment to be invoked. Question 10. Which of the following statements about constexpr functions in C++14 is true? A) They must contain only a single return statement. B) They may contain loops and local variables. C) They cannot be called at runtime. D) They must be declared inline. Answer: B Explanation: C++14 relaxed the restrictions on constexpr functions, permitting loops, conditionals, and local variables. Question 11. In the context of C++ exception handling, which of the following is the most specific catch clause that will handle a std::runtime_error exception? A) catch(...) B) catch(std::exception&) C) catch(std::runtime_error&) D) catch(std::logic_error&) Answer: C Explanation: catch(std::runtime_error&) matches exactly that derived type, while the others are more general.

Programmer Practice Exam

Question 14. What does the std::launder function accomplish in C++17? A) It clears the memory of an object. B) It obtains a pointer to an object after a placement new that may have changed its type. C) It forces a reinterpret cast. D) It creates a deep copy of a container. Answer: B Explanation: std::launder is used to obtain a usable pointer to an object whose storage has been reused, ensuring defined behavior. Question 15. Which of the following statements about std::span (C++20) is correct? A) It owns the elements it references. B) It can be resized at runtime. C) It provides a view over a contiguous sequence without owning it. D) It replaces std::vector entirely. Answer: C Explanation: std::span is a non‑owning view of a contiguous range, similar to a pointer + size pair. Question 16. In C++20, what does the co_await operator do?

Programmer Practice Exam

A) It creates a new thread. B) It suspends a coroutine until the awaited expression is ready. C) It forces compile‑time evaluation. D) It declares a constant expression. Answer: B Explanation: co_await is used inside coroutines to suspend execution until the awaited awaitable provides a result. Question 17. Which of the following is a valid way to declare a class template with a non‑type template parameter of type int? A) template<int N> class Buffer; B) template<class N> class Buffer; C) template<auto N> class Buffer; D) Both A and C are valid. Answer: D Explanation: Both a named non‑type parameter (int N) and a generic auto non‑type parameter are allowed. Question 18. Which of the following statements about std::optional is false? A) It can hold a value of the specified type or be empty. B) Accessing the value without checking may throw an exception.

Programmer Practice Exam

Answer: A Explanation: Ts... args expands the pack Ts into a list of parameters, each named args (the name is optional). Question 21. Which of the following statements about std::variant is true? A) It can hold multiple active alternatives simultaneously. B) It uses type‑based dispatch to select the active alternative at compile time. C) Accessing the wrong alternative throws std::bad_variant_access. D) It is only usable with trivially copyable types. Answer: C Explanation: std::variant holds exactly one alternative; attempting to retrieve a non‑active alternative results in std::bad_variant_access. Question 22. What is the effect of the export keyword in a C++ module interface unit (C++20)? A) It makes the module visible to all translation units without import. B) It marks a declaration as part of the module’s public interface. C) It exports a variable to the linker. D) It replaces the need for header files entirely. Answer: B Explanation: Within a module interface, export designates entities that are visible to importers of the module.

Programmer Practice Exam

Question 23. Which of the following best describes the Rule of Five in C++? A) If a class defines any of the five special member functions, it should define all five. B) A class must have exactly five constructors. C) The compiler will generate five overloads for each operator. D) It applies only to classes that manage raw pointers. Answer: A Explanation: The Rule of Five states that if a class defines any of destructor, copy/move constructor, or copy/move assignment operator, it should explicitly define all five to manage resources correctly. Question 24. In C++23, which new feature allows a constexpr lambda to have a template parameter list? A) constexpr template B) template before the lambda introducer ([]). C) auto before the lambda body. D) Lambdas cannot be templated in C++23. Answer: B Explanation: C++23 permits generic lambdas with explicit template parameters using []<typename T>(T) { … }.

Programmer Practice Exam

B) It removes elements that satisfy a predicate. C) It creates a view that includes only elements satisfying a predicate. D) It transforms each element using a function. Answer: C Explanation: filter produces a lazy view that yields only those elements for which the predicate returns true. Question 28. Which of the following is true about std::shared_ptr’s use_count() method? A) It is guaranteed to be thread‑safe without external synchronization. B) It may return an outdated value if another thread concurrently modifies the count. C) It can be called on a null shared_ptr. D) It returns the number of weak_ptr objects referencing the same control block. Answer: B Explanation: use_count() is not atomic; concurrent modifications can make the returned count stale. It is safe to call on a null pointer but returns 0. Question 29. In C++20, which of the following statements about consteval functions is correct? A) They can be called at runtime if the result is not used. B) They must produce a constant expression and are evaluated at compile time.

Programmer Practice Exam

C) They are equivalent to constexpr functions with the inline specifier. D) They may contain dynamic memory allocation. Answer: B Explanation: consteval forces compile‑time evaluation; the function cannot be invoked at runtime. Question 30. Which of the following is a correct way to declare a deleted copy constructor for class Foo? A) Foo(const Foo&) = delete; B) Foo(Foo&) = delete; C) Foo(const Foo&) = 0; D) Foo(const Foo&) {} Answer: A Explanation: Using = delete after the declaration explicitly disables the copy constructor. Question 31. Which of the following statements about the volatile qualifier in modern C++ is most accurate? A) It guarantees atomicity of reads and writes. B) It is primarily used for memory‑mapped I/O and signal handlers. C) It forces the compiler to inline the variable. D) It makes a variable thread‑local.

Programmer Practice Exam

Question 34. Which of the following statements about std::atomic_ref is true? A) It creates a new atomic object that copies the original value. B) It provides atomic operations on an existing object without owning it. C) It can only be used with integral types. D) It requires the referenced object to be constexpr. Answer: B Explanation: std::atomic_ref offers atomic access to an existing object, acting as a non‑owning wrapper. Question 35. Which of the following is the correct syntax to create a std::unique_ptr that uses a custom deleter of type void(*)(int*)? A) std::unique_ptr<int, void(*)(int*)> p(new int, custom_deleter); B) std::unique_ptr<int> p(new int, custom_deleter); C) std::unique_ptr<int, decltype(custom_deleter)> p(custom_deleter); D) std::unique_ptr<int, void(*)(int*)> p(custom_deleter); Answer: A Explanation: The deleter type must be part of the unique_ptr template arguments; the constructor receives the raw pointer and the deleter. Question 36. Which of the following statements about std::bitset is false?

Programmer Practice Exam

A) Its size must be known at compile time. B) It provides bitwise operators such as &, |, and ^. C) It can be resized at runtime. D) It offers to_ulong() conversion when the value fits. Answer: C Explanation: std::bitset<N> has a fixed size N determined at compile time; it cannot be resized. Question 37. In C++20, what does the std::format function return? A) A std::string containing the formatted output. B) A char* allocated with new. C) An std::ostream reference. D) Nothing; it prints directly to stdout. Answer: A Explanation: std::format produces a std::string according to a format string and arguments. Question 38. Which of the following best describes the effect of the [[unlikely]] attribute? A) It tells the optimizer that the annotated branch is rarely taken. B) It makes the function return false by default.

Programmer Practice Exam

D) auto foo(this auto&& self) - > decltype(self.bar()); Answer: D Explanation: The syntax this auto&& introduces a forwarding reference to the object, enabling perfect forwarding of cv‑qualifiers and value category. Question 41. Which of the following statements about std::shared_mutex is correct? A) It allows multiple concurrent writers but only one reader. B) It provides exclusive locking for writers and shared locking for readers. C) It cannot be used with std::unique_lock. D) It is deprecated in C++20. Answer: B Explanation: std::shared_mutex enables multiple readers to hold a shared lock simultaneously while writers obtain an exclusive lock. Question 42. Which of the following is a valid way to create a coroutine that returns a std::future<int> in C++20? A) std::future<int> foo() { co_return 42; } B) std::future<int> foo() { co_await std::async([]{ return 42; }); } C) std::future<int> foo() { co_yield 42; } D) std::future<int> foo() { co_return std::async([]{ return 42; }); } Answer: A

Programmer Practice Exam

Explanation: A function returning std::future<T> and containing co_return becomes a coroutine; the compiler generates the appropriate promise type. Question 43. Which of the following statements about std::atomic_flag is true? A) It supports load and store operations. B) It is the only atomic type that provides a test‑and‑set operation. C) It is lock‑free on all platforms. D) It can be used as a boolean flag without explicit memory order specifications. Answer: B Explanation: std::atomic_flag provides test_and_set and clear; other atomic types provide load/store, but atomic_flag is the minimal flag type. Question 44. Which of the following expressions will cause a hard compile‑time error in a constexpr function? A) if (false) { static_assert(false); } B) int* p = nullptr; *p = 5; C) return 42; D) return std::string("hi"); Answer: B Explanation: Dereferencing a null pointer in a constexpr context is not allowed and results in a hard error; the other options are either allowed or not evaluated.