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)?
Q16. Which header provides std::string?stringcstringsstreamiostreamHintExplanation
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?::.->:HintExplanation
Q25. Which header provides std::unique_ptr and std::shared_ptr?memorymemory.hsmartptrptrHintExplanation
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?operator+operator++operator=operator*HintExplanation
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++)?usingtypedefaliastypeHintExplanation
`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?std::mapstd::vectorstd::setstd::listHintExplanation
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?algorithmalgorithmsutilityfunctionalHintExplanation
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)?= 0abstractvirtualpureHintExplanation
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?sizeoflengthofsizecountHintExplanation
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?virtualoverridefinalstaticHintExplanation
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?unordered_mapmaphash_mapassociativeHintExplanation
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?const (after function)mutablevolatilestaticHintExplanation
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?in-class member initializersdesignated initializersaggregate initmeminitHintExplanation
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)?typedefusingaliastypeHintExplanation
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?Base::memberthis->membersuper.memberparent::memberHintExplanation
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?std::out_of_rangestd::bad_allocstd::runtime_errorstd::logic_errorHintExplanation
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)?constexprconstevalconstinlineHintExplanation
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?std::multisetstd::setstd::unordered_setstd::mapHintExplanation
Allows multiple equivalent keys.
Answer: std::multiset Explanation: multiset permits duplicate keys; set requires uniqueness.
Q41. Which header provides std::thread?threadpthreadthreadsconcurrencyHintExplanation
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)?std::to_stringstd::to_strstd::stringifystd::formatHintExplanation
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?<>()[]{}HintExplanation
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)?size()length()count()capacity()HintExplanation
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)?inlinefastregisterconstexprHintExplanation
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?.->::*HintExplanation
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)?std::formatsprintfstd::to_stringstd::stringstreamHintExplanation
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?const (member function)volatilemutablestaticHintExplanation
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)?optionalmaybeutilityvariantHintExplanation
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)?std::variantstd::tuplestd::pairstd::anyHintExplanation
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).
0 Comments