C++ Programming Quiz: 50 Questions

Q1. Which file extension is typically used for C++ source files?
Common C++ extensions include .cpp, .cc, .cxx, .hpp for headers.
Answer: .cpp
Explanation: .cpp is a common extension for C++ source files; .c is for C.
Q2. Which header provides std::cout and std::cin?
Standard input/output stream objects are declared here.
Answer: iostream
Explanation: <iostream> declares std::cin, std::cout, std::cerr.
Q3. How do you create an object of class Person?
Person p = ;
Call the constructor with parentheses.
Answer: Person()
Explanation: `Person p = Person();` or `Person p;` constructs an object using the default constructor.
Q4. Which keyword prevents a method from being overridden in C++?
Introduced in C++11 to stop further overriding.
Answer: final
Explanation: Marking a virtual function as final prevents derived classes from overriding it.
Q5. Which of these is a C++ reference declaration?
References use ampersand in the declaration.
Answer: int &r = x;
Explanation: A reference is an alias to an existing object and must be initialized.
Q6. Which container provides random-access and contiguous storage?
Elements stored contiguously in memory.
Answer: std::vector
Explanation: std::vector stores elements contiguously and supports random access via operator[].
Q7. How do you declare a function that returns nothing?
foo() { }
Use the keyword for no return value.
Answer: void
Explanation: `void foo()` declares a function that does not return a value.
Q8. Which operator is used to access members through a pointer?
Used when you have a pointer to an object.
Answer: ->
Explanation: `ptr->member` accesses a member of the object pointed to by ptr; `.` is for objects.
Q9. Which keyword creates dynamic storage for an object?
C++ operator that calls constructors.
Answer: new
Explanation: `new Type` allocates memory and calls the constructor; `delete` frees it and calls destructor.
Q10. Which smart pointer expresses unique ownership?
Ownership cannot be shared.
Answer: std::unique_ptr
Explanation: unique_ptr enforces single ownership and cannot be copied (can be moved).
Q11. Which keyword marks a function as virtual?
Used in base classes for polymorphism.
Answer: virtual
Explanation: Declaring a method virtual enables dynamic dispatch when called through a base pointer/reference.
Q12. Which cast performs runtime-checked downcast for polymorphic types?
Requires polymorphic (virtual) base.
Answer: dynamic_cast
Explanation: dynamic_cast checks at runtime and returns nullptr for invalid pointer casts (or throws for references).
Q13. Which keyword makes a member constant for the object?
Prevents modification through that object.
Answer: const
Explanation: Declaring a member function as const promises not to modify the object; const data members cannot be changed after initialization.
Q14. Which feature was introduced in C++11 to deduce variable types?
Let the compiler infer the type from the initializer.
Answer: auto
Explanation: `auto x = 5;` deduces x as int; `decltype` also exists but serves a different purpose.
Q15. Which loop iterates over elements of a container directly (C++11)?
Syntax: for (auto &x : container)
Answer: range-based for
Explanation: `for (auto &elem : container)` iterates directly over elements introduced in C++11.
Q16. Which header provides std::string?
C++ string class header.
Answer: string
Explanation: Include `` to use std::string and its methods.
Q17. Which operator is used to access a namespace member?
Scope resolution operator.
Answer: ::
Explanation: `std::vector` uses `::` to access vector inside namespace std.
Q18. Which keyword declares a template function?
Syntax: template<typename T>
Answer: template
Explanation: Use `template` (or `template`) to declare templates.
Q19. Which STL algorithm sorts a range?
Defined in <algorithm>.
Answer: std::sort
Explanation: `std::sort(begin, end)` sorts elements using operator< by default.
Q20. Which container is ordered and unique by key?
Balanced tree-based set.
Answer: std::set
Explanation: std::set stores unique keys in sorted order (usually a red-black tree).
Q21. Which keyword declares a destructor?
Destructor name is tilde + class name.
Answer: ~ClassName
Explanation: Destructor `~ClassName()` is called when an object is destroyed to release resources.
Q22. Which keyword prevents copying of a class (C++11)?
Use `= delete` on copy constructor/assignment.
Answer: delete (copy constructor)
Explanation: `Class(const Class&) = delete;` disables copying in C++11 and later.
Q23. Which keyword indicates a function does not throw exceptions?
Introduced in C++11 for optimization and safety.
Answer: noexcept
Explanation: `noexcept` marks functions that are guaranteed not to throw; compilers can optimize accordingly.
Q24. Which feature allows moving resources instead of copying (C++11)?
Uses && and std::move.
Answer: move semantics (rvalue references)
Explanation: Rvalue references and `std::move` enable transferring resources efficiently.
Q25. Which header provides std::unique_ptr and std::shared_ptr?
Standard header for smart pointers.
Answer: memory
Explanation: Include `` to use smart pointers like std::unique_ptr and std::shared_ptr.
Q26. Which operator overload allows object + object?
Define as member or free function.
Answer: operator+
Explanation: Overload `operator+` to define addition semantics for user types.
Q27. Which keyword creates an alias type (modern C++)?
`using` is preferred over typedef for templates.
Answer: using
Explanation: `using IntVec = std::vector;` creates a type alias; `typedef` is older syntax.
Q28. Which container provides key-value mapping?
Associative container with ordered keys.
Answer: std::map
Explanation: std::map stores key-value pairs with unique keys in sorted order.
Q29. Which header provides algorithms like std::sort and std::find?
Standard algorithms header.
Answer: algorithm
Explanation: Include `` for std::sort, std::find, std::for_each, etc.
Q30. Which keyword marks a pure virtual function (making a class abstract)?
Syntax: virtual void f() = 0;
Answer: = 0
Explanation: Declaring a virtual function `= 0` makes it pure virtual and the class abstract.
Q31. Which operator returns the size in bytes of a type or expression?
Built-in operator.
Answer: sizeof
Explanation: `sizeof(T)` yields the size in bytes of type T or expression.
Q32. Which keyword allows function overloading resolution to prefer derived-class overloads at compile time?
Enables runtime polymorphism.
Answer: virtual
Explanation: `virtual` enables dynamic dispatch so derived overrides are called via base pointers at runtime.
Q33. Which header provides std::unordered_map?
Hash-based associative container header.
Answer: unordered_map
Explanation: Include `` for hash-table-based maps with average O(1) lookup.
Q34. Which keyword allows a member function to be called on const objects?
Place after parameter list: `int f() const`.
Answer: const (after function)
Explanation: Declaring `int f() const` promises not to modify the object and allows calling on const instances.
Q35. Which C++11 feature allows inline initialization of members?
Example: int x = 5; inside class definition.
Answer: in-class member initializers
Explanation: C++11 allows `int x = 5;` inside class to initialize members directly.
Q36. Which keyword indicates a type alias in old C++ (pre-using)?
Older syntax for type aliases.
Answer: typedef
Explanation: `typedef int Int;` creates an alias; `using` is the modern alternative.
Q37. Which operator is used to access base class members when hidden by derived class?
Use scope resolution with base class name.
Answer: Base::member
Explanation: `Base::foo()` calls the base class version when hidden by derived class.
Q38. Which standard exception is thrown for out-of-range access in std::vector::at?
Thrown when index is outside valid range.
Answer: std::out_of_range
Explanation: `vector::at` throws std::out_of_range for invalid indices; operator[] does not check bounds.
Q39. Which keyword allows a function to be evaluated at compile time (C++11/14/17)?
Introduced in C++11; expanded in later standards.
Answer: constexpr
Explanation: `constexpr` marks functions/variables for compile-time evaluation when possible.
Q40. Which container allows duplicate keys?
Allows multiple equivalent keys.
Answer: std::multiset
Explanation: multiset permits duplicate keys; set requires uniqueness.
Q41. Which header provides std::thread?
C++11 threading support header.
Answer: thread
Explanation: Include `` to use std::thread and related threading utilities.
Q42. Which function template converts values to strings (C++11)?
Converts numeric types to std::string.
Answer: std::to_string
Explanation: `std::to_string(123)` returns `"123"`; introduced in C++11.
Q43. Which operator is used to declare a template parameter list?
Example: template<typename T>
Answer: <>
Explanation: Template parameter lists use angle brackets: `template`.
Q44. Which function returns the number of elements in a container (C++11)?
Common member function for containers.
Answer: size()
Explanation: `container.size()` returns the number of elements; `length()` is for strings/arrays in some contexts.
Q45. Which keyword allows a function to be inlined (suggestion to compiler)?
Compiler may ignore it.
Answer: inline
Explanation: `inline` suggests inlining to the compiler and allows multiple definitions across translation units for small functions.
Q46. Which operator is used to access a member of an object?
Use dot for objects, arrow for pointers.
Answer: .
Explanation: `obj.member` accesses a member of an object; `ptr->member` is used for pointers.
Q47. Which standard facility formats strings (C++20)?
Introduced in C++20 for Python-like formatting.
Answer: std::format
Explanation: `` provides std::format for type-safe formatting similar to Python's f-strings (C++20).
Q48. Which keyword allows a function to be overloaded based on constness?
You can have both `int f()` and `int f() const`.
Answer: const (member function)
Explanation: Const and non-const overloads differ by constness and can be selected based on object constness.
Q49. Which header provides std::optional (C++17)?
Represents optional values without pointers.
Answer: optional
Explanation: Include `` to use std::optional for values that may be absent (C++17).
Q50. Which feature allows multiple return types in a single object (C++17)?
Holds one of several alternative types safely.
Answer: std::variant
Explanation: `std::variant` holds one of the specified types and is useful for type-safe unions (C++17).
Your Score: 0 / 50

Post a Comment

0 Comments