0% found this document useful (0 votes)
9 views

CS304 - Object Oriented Programming Final Term Short Question & Answers (Lecture 19 to 45)

The document contains a compilation of short questions and answers related to Object Oriented Programming in C++, covering topics from operator overloading and inheritance to templates and exception handling. It provides concise definitions and explanations for key concepts such as virtual functions, smart pointers, and the Standard Template Library (STL). The content is structured as a study guide for students preparing for their final term examination.

Uploaded by

manineedy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

CS304 - Object Oriented Programming Final Term Short Question & Answers (Lecture 19 to 45)

The document contains a compilation of short questions and answers related to Object Oriented Programming in C++, covering topics from operator overloading and inheritance to templates and exception handling. It provides concise definitions and explanations for key concepts such as virtual functions, smart pointers, and the Standard Template Library (STL). The content is structured as a study guide for students preparing for their final term examination.

Uploaded by

manineedy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CS304 - Object Oriented Programming

Final Term Short Question & Answers (Lecture 19 to 45)

Q1: What is the purpose of overloading the stream insertion (<<) and extraction (>>)
operators?

To allow objects of user-defined classes to be input and output using cin and cout, just like
built-in types.

Q2: What is the syntax for overloading the subscript [] operator?

ReturnType operator[](int index);

Q3: What is the difference between pre-increment (++obj) and post-increment (obj++)
operators?

Pre-increment increments then returns the new value, while post-increment returns the original
value then increments.

Q4: What is the syntax for defining a type conversion operator?

operator TargetType() const;

Q5: What are the three types of inheritance in C++?

Public, protected, and private inheritance.

Q6: What is a virtual function?

A function declared in a base class that can be overridden in derived classes to achieve runtime
polymorphism.

Q7: What is a pure virtual function?

A virtual function set to 0 in the base class, making the class abstract and requiring derived
classes to override it.

Q8: What is the purpose of a virtual destructor?

To ensure the proper destructor is called when deleting a derived class object through a base
class pointer.

Q9: What is multiple inheritance?

When a class inherits from more than one base class.

Prepared by Sheikh Abdul Rehman (BC200416639)


CS304 - Object Oriented Programming
Final Term Short Question & Answers (Lecture 19 to 45)

Q10: What is a template?

A blueprint for creating generic classes or functions that can work with different data types.

Q11: What is the syntax for a function template?

template <typename T>


ReturnType functionName(T parameter);

Q12: What is a class template?

A blueprint for creating a family of classes that can work with different data types.

Q13: What is template specialization?

Providing a specific implementation for a template when used with a particular data type.

Q14: What is the Standard Template Library (STL)?

A collection of powerful, reusable, and adaptable software components in C++.

Q15: What are the main components of STL?

Containers, algorithms, and iterators.

Q16: What is an iterator?

An object that provides a way to access elements of a container sequentially without exposing
the underlying structure.

Q17: What is a container in STL?

An object that stores a collection of other objects and organizes them in a well-defined way.

Q18: What is the difference between sequence containers and associative containers?

Sequence containers store elements in a linear sequence, while associative containers store
elements in a way that allows fast retrieval based on keys.

Q19: What is an algorithm in STL?

A function template that implements a procedure to be performed on containers.

Prepared by Sheikh Abdul Rehman (BC200416639)


CS304 - Object Oriented Programming
Final Term Short Question & Answers (Lecture 19 to 45)

Q20: What is exception handling?

A mechanism for handling runtime errors or exceptional situations in a program.

Q21: What are the keywords used for exception handling in C++?

try, catch, and throw.

Q22: What is the purpose of the try block?

To enclose the code that might throw an exception.

Q23: What is the purpose of the catch block?

To handle exceptions that are thrown in the corresponding try block.

Q24: What is the purpose of the throw keyword?

To explicitly throw an exception.

Q25: What is stack unwinding?

The process of destroying local objects and calling destructors for automatic objects when an
exception is thrown.

Q26: What is a function object (functor)?

An object that can be used as if it were a function.

Q27: What is the advantage of using function objects over regular functions?

Function objects can maintain state between calls and can be customized through their member
variables.

Q28: What is a lambda expression?

A convenient way to create anonymous function objects.

Q29: What is the syntax for a basic lambda expression?

[capture clause](parameters) -> return_type { function body }

Prepared by Sheikh Abdul Rehman (BC200416639)


CS304 - Object Oriented Programming
Final Term Short Question & Answers (Lecture 19 to 45)

Q30: What is smart pointer?

An object that acts like a pointer but provides additional features such as automatic memory
management.

Q31: What are the three main types of smart pointers in C++11?

unique_ptr, shared_ptr, and weak_ptr.

Q32: What is the purpose of unique_ptr?

To manage a single object and ensure it is deleted when the unique_ptr goes out of scope.

Q33: What is the purpose of shared_ptr?

To allow multiple pointers to share ownership of a dynamically allocated object.

Q34: What is the purpose of weak_ptr?

To observe an object owned by shared_ptr without affecting its lifetime.

Q35: What is RAII?

Resource Acquisition Is Initialization, a programming technique where resource management is


tied to object lifetime.

Q36: What is a move constructor?

A special constructor that transfers ownership of resources from one object to another instead
of copying.

Q37: What is a move assignment operator?

An assignment operator that transfers ownership of resources from one object to another
instead of copying.

Q38: What is the purpose of the noexcept specifier?

To indicate that a function will not throw exceptions.

Q39: What is a constexpr function?

A function that can be evaluated at compile time.

Prepared by Sheikh Abdul Rehman (BC200416639)


CS304 - Object Oriented Programming
Final Term Short Question & Answers (Lecture 19 to 45)

Q40: What is a default function?

A function that the compiler will automatically generate if it is used but not explicitly defined.

Q41: What is a deleted function?

A function that is explicitly forbidden to be used.

Q42: What is a friend function?

A function that is not a member of a class but has access to its private and protected members.

Q43: What is a friend class?

A class whose members have access to the private and protected members of another class.

Q44: What is the purpose of the const keyword when used with member functions?

To indicate that the function will not modify the object's state.

Q45: What is a mutable data member?

A data member that can be modified even in a const object.

Q46: What is a static data member?

A data member that belongs to the class rather than to any specific object of the class.

Q47: What is a static member function?

A function that belongs to the class rather than to any specific object of the class.

Q48: What is the purpose of the explicit keyword?

To prevent implicit type conversions for constructors or conversion operators.

Q49: What is a namespace?

A declarative region that provides a scope for the identifiers inside it.

Q50: What is the purpose of the using directive?

To bring all names from a namespace into the current scope.

Prepared by Sheikh Abdul Rehman (BC200416639)


CS304 - Object Oriented Programming
Final Term Short Question & Answers (Lecture 19 to 45)

Q51: What is the difference between struct and class in C++?

The default access specifier for members and bases is public for struct and private for class.

Q52: What is an abstract class?

A class that contains at least one pure virtual function and cannot be instantiated.

Q53: What is a concrete class?

A class that can be instantiated, i.e., it has no pure virtual functions.

Q54: What is the diamond problem in multiple inheritance?

An ambiguity that arises when a class inherits from two classes that have a common base class.

Q55: What is virtual inheritance?

A way to solve the diamond problem by ensuring only one instance of the common base class is
inherited.

Q56: What is a vtable?

A table of function pointers used to implement virtual functions.

Q57: What is the purpose of the volatile keyword?

To indicate that a variable may change unexpectedly, preventing certain compiler optimizations.

Q58: What is a copy constructor?

A constructor that creates a new object as a copy of an existing object.

Q59: What is a destructor?

A special member function that is called when an object is destroyed.

Q60: What is the rule of three?

If a class defines any of the copy constructor, copy assignment operator, or destructor, it should
probably explicitly define all three.

Prepared by Sheikh Abdul Rehman (BC200416639)


CS304 - Object Oriented Programming
Final Term Short Question & Answers (Lecture 19 to 45)
Q61: What is the rule of five?

An extension of the rule of three that includes move constructor and move assignment operator.

Q62: What is a delegating constructor?

A constructor that calls another constructor of the same class to do part of the initialization.

Q63: What is a conversion operator?

A member function that defines how to convert an object to another type.

Q64: What is the purpose of the static_cast operator?

To perform conversions between related types, such as those in an inheritance hierarchy.

Q65: What is the purpose of the dynamic_cast operator?

To perform safe downcasting in inheritance hierarchies, with runtime type checking.

Q66: What is the purpose of the const_cast operator?

To add or remove the const qualifier from a variable.

Q67: What is the purpose of the reinterpret_cast operator?

To convert between unrelated types, such as between pointers and integers.

Q68: What is a union?

A special class type where all members share the same memory location.

Q69: What is an enumeration?

A user-defined type consisting of a set of named constants.

Q70: What is the difference between a C-style enum and a C++11 enum class?

An enum class provides stronger type safety and doesn't allow implicit conversions to int.

Q71: What is a reference?

An alias for an existing variable.

Prepared by Sheikh Abdul Rehman (BC200416639)


CS304 - Object Oriented Programming
Final Term Short Question & Answers (Lecture 19 to 45)
Q72: What is the difference between a pointer and a reference?

A reference must be initialized when declared and cannot be null, while a pointer can be null
and can be reassigned.

Q73: What is a function pointer?

A pointer that points to a function rather than a data object.

Q74: What is a member function pointer?

A pointer that points to a member function of a class.

Q75: What is the purpose of the typeid operator?

To obtain type information of an object at runtime.

Q76: What is RTTI?

Run-Time Type Information, a feature that exposes information about an object's data type at
runtime.

Q77: What is a placement new?

A version of the new operator that constructs an object at a specific memory address.

Q78: What is the purpose of the alignof operator?

To query the alignment requirement of a type.

Q79: What is the purpose of the alignas specifier?

To specify the alignment requirement for a type or object.

Q80: What is a trivial class?

A class that has compiler-provided default constructor, copy constructor, copy assignment
operator, and destructor.

Q81: What is a POD type?

Plain Old Data type, a class or struct that is both trivial and standard-layout.

Prepared by Sheikh Abdul Rehman (BC200416639)


CS304 - Object Oriented Programming
Final Term Short Question & Answers (Lecture 19 to 45)
Q82: What is a variadic template?

A template that can take a variable number of template arguments.

Q83: What is perfect forwarding?

A technique to pass arguments to another function while preserving their value category
(lvalue/rvalue).

Q84: What is SFINAE?

Substitution Failure Is Not An Error, a principle used in template metaprogramming.

Q85: What is a type trait?

A template that provides information about types at compile time.

Q86: What is std::initializer_list?

A lightweight proxy object that provides access to an array of objects.

Q87: What is uniform initialization?

A feature introduced in C++11 that provides a consistent syntax for initialization of objects.

Q88: What is a range-based for loop?

A control structure that iterates over all elements in a range without using an explicit iterator.

Q89: What is auto type deduction?

A feature that allows the compiler to deduce the type of a variable from its initializer.

Q90: What is decltype?

A keyword that yields the type of its operand, which is useful in generic programming.

Q91: What is a trailing return type?

A syntax for writing function declarations where the return type comes after the parameter list.

Q92: What is a scoped enumeration?

An enumeration where the enumerators are only accessible through the enumeration name.

Prepared by Sheikh Abdul Rehman (BC200416639)


CS304 - Object Oriented Programming
Final Term Short Question & Answers (Lecture 19 to 45)

Q93: What is a default member initializer?

An initializer for a non-static data member that is used if the member is not initialized by a
constructor.

Q94: What is a delegating constructor?

A constructor that calls another constructor in the same class as part of the initialization.

Q95: What is an inheriting constructor?

A feature that allows a derived class to inherit constructors from its base class.

Q96: What is a user-defined literal?

A feature that allows you to define your own suffixes for literals.

Q97: What is an inline namespace?

A namespace that allows its members to be treated as if they were members of the enclosing
namespace.

Q98: What is a thread_local variable?

A variable that has thread storage duration, meaning each thread has its own copy.

Q99: What is a constexpr variable?

A variable whose value can be computed at compile time.

Q100: What is a nullptr?

A keyword representing a null pointer literal, which is more type-safe than using 0 or NULL.

Prepared by Sheikh Abdul Rehman (BC200416639)

You might also like