Wk7-12 Content Flashcards

1
Q

What is inheritance in C++?

A

A feature that allows a class (derived) to inherit properties and behavior from another class (base).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is a class declared as a derived class in C++?

A

By using the syntax: class DerivedClass : accessSpecifier BaseClass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is polymorphism in C++?

A

The ability of objects of different classes to respond differently to the same function call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is virtual function in C++?

A

A function declared in the base class using the keyword ‘virtual’ and is redefined by the derived class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does virtual function support polymorphism?

A

It allows derived classes to override the base class method implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the significance of the ‘protected’ access specifier?

A

Protected members are accessible within the class, its derived classes, but not outside of them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why is inheritance used in C++, and what are its benefits?

A

For code reusability and to establish a relationship between classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is multiple inheritance?

A

A feature where a class can inherit from more than one base class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the risks of multiple inheritance?

A

It can lead to ambiguities and complexity, especially with diamond problem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How is the ‘override’ keyword used in C++?

A

It ensures that the function is overriding a virtual function from a base class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an abstract class?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you declare a pure virtual function?

A

By assigning 0 to the virtual function: virtual void function() = 0;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the ‘final’ specifier in C++?

A

It prevents a class from being inherited or a virtual function from being overridden.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can constructors and destructors be inherited?

A

Through the use of inheritance, constructors and destructors are called for both base and derived classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is static polymorphism?

A

It is resolved at compile time, e.g., function overloading and templates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is dynamic polymorphism?

A

It is resolved at runtime, e.g., through the use of virtual functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How does the ‘this’ pointer relate to inheritance?

A

It refers to the current object instance, and is used within class methods to refer to the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the ‘super’ or ‘base’ keyword used for in inheritance?

A

It is used to refer to the base class, often used in derived class constructors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is method overriding in C++?

A

Replacing a base class method in a derived class with a new implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is a virtual destructor?

A

A destructor that is declared virtual in the base class to ensure proper resource release.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How does C++ handle constructor inheritance?

A

Derived class constructors call base class constructors implicitly or explicitly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the role of UML in OOP?

A

Unified Modeling Language, it visually represents the design of an OOP system.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How are class relationships represented in UML?

A

Through various types of lines and arrows, indicating relationships like inheritance and association.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the diamond problem in inheritance?

A

A problem that arises when two classes separately inherit from the same base class and are combined into another class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the difference between overloading and overriding?

A

Overloading is having functions with the same name but different parameters, while overriding involves redefining a base class’s virtual function in a derived class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is a copy constructor in C++?

A

A constructor that initializes an object using another object of the same class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

How does a copy constructor work in C++?

A

It copies the values of all data members from one object to another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is a friend function in C++?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

How do you declare a friend function in C++?

A

By using the keyword ‘friend’ before the function prototype within the class definition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is the ‘this’ pointer in C++?

A

A special pointer that points to the object for which the member function is called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How is the ‘this’ pointer used in C++?

A

To refer to members of the class within member functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is operator overloading in C++?

A

Defining a new behavior for an existing operator when used with objects of a user-defined class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How do you overload an operator in C++?

A

By defining a function in the class, using the keyword ‘operator’ followed by the operator to be overloaded.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is a static member in C++?

A

A member that belongs to the class itself, rather than to any specific object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

How do you declare a static member in a class?

A

By using the keyword ‘static’ before the member declaration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What is the purpose of static members in C++?

A

To share common data across all objects of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

How do you access a static member in C++?

A

Using the class name followed by the scope resolution operator (::).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

What is a pointer to a class in C++?

A

A pointer that holds the address of an object of a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

How do you access members of a class using a pointer?

A

By using the arrow operator (->).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What are the benefits of using pointers to classes?

A

They allow for dynamic allocation and manipulation of objects at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What is the difference between deep and shallow copying?

A

Deep copying copies all fields and allocates separate memory, while shallow copying only copies field values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

How do you define a copy assignment operator in C++?

A

By overloading the assignment operator (=) to correctly copy objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

What is the rule of three in C++?

A

If a class defines one of a destructor, copy constructor, or copy assignment operator, it should define all three.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

Why might you declare a friend class?

A

To allow another class access to the private members of the defining class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What is the syntax for declaring a static member function?

A

static returnType functionName(parameters);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

How can static member functions be useful?

A

They can be called without an object and can only access static members of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

What is an inline function in C++?

A

A function whose code is copied to the call site to reduce function call overhead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

How do you ensure proper copying of dynamically allocated resources in a copy constructor?

A

By doing deep copying of the resources.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

What is a unique feature of the copy constructor compared to other constructors?

A

It takes a reference to an object of the same class as its parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

What are the best practices for overloading the assignment operator?

A

Handling self-assignment and freeing existing resources before copying.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

What is an interface in C++?

A

A set of public functions that provide the behavior of a class without a predefined implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

How is an interface implemented in C++?

A

Using abstract classes with pure virtual functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

What is an abstract class in C++?

A

A class that cannot be instantiated and is designed to be a base class for other classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

What makes a class abstract in C++?

A

Having at least one pure virtual function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

How is a pure virtual function defined in C++?

A

Using the syntax ‘= 0’ in the function declaration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

Why use abstract classes in C++?

A

To provide a base class that defines a common interface for derived classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

Can abstract classes have constructors in C++?

A

Yes, but they can only be used for construction of derived class objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

How do you instantiate a derived class from an abstract class in C++?

A

By overriding all the pure virtual functions in the derived class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

What is the purpose of virtual functions in abstract classes?

A

To allow derived classes to have their own implementations of these functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

What happens if a derived class does not override a pure virtual function?

A

The derived class also becomes an abstract class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

Can abstract classes have non-pure virtual functions?

A

Yes, and they can provide a default implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

What is the role of an abstract class in a class hierarchy?

A

It serves as a conceptual foundation upon which concrete classes are built.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

How do abstract classes promote reusability?

A

By allowing multiple derived classes to share common functionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

Why can’t abstract classes be used to create objects directly?

A

Because they have incomplete function implementations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

How do you declare a class as abstract in C++?

A

By including at least one pure virtual function in its definition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

Can an abstract class contain data members in C++?

A

Yes, abstract classes can have data members.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

What is the difference between abstract and concrete classes?

A

Abstract classes have incomplete implementation, while concrete classes are fully implemented.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

How do virtual destructors work in abstract classes?

A

They ensure that destructors of derived classes are called correctly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

Can an abstract class have a non-virtual destructor?

A

Yes, but it can lead to improper cleanup of derived class resources.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
70
Q

What is the difference between abstract classes and interfaces in C++?

A

Abstract classes can have member variables and function implementations, while interfaces cannot.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
71
Q

How does polymorphism relate to abstract classes?

A

Abstract classes allow polymorphic behavior by letting derived classes implement their versions of virtual functions.

72
Q

What are the benefits of using interfaces?

A

They allow for flexible and interchangeable object designs.

73
Q

How does inheritance work with abstract classes?

A

Derived classes inherit the interface and must implement the abstract methods.

74
Q

Why is it important to override pure virtual functions in derived classes?

A

To provide specific implementations for these functions, making the derived class non-abstract.

75
Q

Can an abstract class be derived from another abstract class?

A

Yes, and the derived class remains abstract if it doesn’t implement all inherited pure virtual functions.

76
Q

What is a static library in C++?

A

A collection of pre-compiled code that can be linked into a program.

77
Q

How is a static library created in C++?

A

By compiling code into object files and then archiving them into a library file.

78
Q

What are the file extensions for static libraries in C++, typically?

A

.lib in Windows and .a in Linux.

79
Q

What is a shared library in C++?

A

A library that allows its code to be shared by multiple programs at runtime.

80
Q

What are the benefits of using shared libraries in C++?

A

They reduce the memory footprint and allow for easy updates.

81
Q

What are the file extensions for shared libraries in C++?

A

.dll in Windows and .so in Linux.

82
Q

How do you include a static library in a C++ program?

A

By linking the library file during the compilation process.

83
Q

What is exception handling in C++?

A

A way to handle runtime errors in a program, using try, catch, and throw statements.

84
Q

Why is exception handling important in C++?

A

It allows for more robust and maintainable error handling.

85
Q

What are enumerations in C++?

A

A way to define a set of named integer constants.

86
Q

How do you define an enumeration in C++?

A

Using the enum keyword followed by a name and a list of values.

87
Q

What is the difference between scoped and unscoped enums in C++?

A

Scoped enums (enum class) provide better type safety than unscoped enums.

88
Q

What is the main use of a C++ library?

A

To provide reusable functions and classes to multiple programs.

89
Q

How do you link a shared library in a C++ program?

A

By specifying the library in the compiler/linker settings.

90
Q

What is the main difference between static and shared libraries?

A

Static libraries are included in the executable, while shared libraries are loaded at runtime.

91
Q

How do you handle exceptions thrown by library functions?

A

By using try-catch blocks around the function calls.

92
Q

Can you throw objects as exceptions in C++?

A

Yes, you can throw objects of any type as exceptions.

93
Q

What is the purpose of the ‘.h’ and ‘.cpp’ files in a C++ library?

A

‘.h’ files contain declarations, while ‘.cpp’ files contain implementations.

94
Q

What is the command to create a static library from object files in Linux?

A

ar -crv libname.a file1.o file2.o

95
Q

How do you specify the library path when compiling a C++ program?

A

Using the -L flag to specify the directory and -l to specify the library.

96
Q

What is the significance of using libraries in software development?

A

Libraries promote code reuse and modular design.

97
Q

How do you declare and use an enumeration in C++?

A

Declare with ‘enum’ or ‘enum class’, and use by specifying the enum value.

98
Q

What are the common operations performed in library management?

A

Creating, updating, and linking libraries in software projects.

99
Q

How do shared libraries save space on a system?

A

By allowing multiple programs to use the same library code in memory.

100
Q

What are the advantages of using exception handling in libraries?

A

It provides a standardized way for libraries to report and handle errors.

101
Q

What is an L-value in C++?

A

A variable that points to a specific memory location.

102
Q

What is an R-value in C++?

A

A temporary and short-lived value.

103
Q

How do you create a pointer to an L-value?

A

Using the syntax ‘int &ref’ to make a reference.

104
Q

How do you create a pointer to an R-value?

A

Using the syntax ‘int &&ref’ for an R-value reference.

105
Q

What is the purpose of R-value references in C++?

A

To support move semantics and efficient data transfer.

106
Q

What is a lambda expression in C++?

A

An anonymous function object capable of capturing variables in scope.

107
Q

What is the syntax of a lambda expression in C++?

A

The format is: {} with optional capture list, parameters, and return type.

108
Q

How are lambda expressions used in C++?

A

As concise function objects for short snippets of code.

109
Q

What is the capture list in a lambda expression?

A

It allows the lambda to use variables from its enclosing scope.

110
Q

What is the significance of the capture list in lambdas?

A

It controls what external variables are accessible within the lambda.

111
Q

How do you specify a return type in a lambda?

A

Using ‘->’ followed by the return type after the parameter list.

112
Q

Why are lambdas useful in C++?

A

For creating inline, on-the-spot functions without formal definition.

113
Q

What is a move constructor in C++?

A

A constructor that moves resources from a given object to a new object.

114
Q

What is a move assignment operator?

A

An operator that transfers resources from one object to another, without copying.

115
Q

How does move semantics improve performance?

A

By eliminating unnecessary copying of temporary objects.

116
Q

What are the benefits of using R-value references?

A

They enable efficient data transfer and resource management.

117
Q

How do you use std::move in C++?

A

To convert an L-value to an R-value reference, enabling move semantics.

118
Q

What is the difference between copy and move semantics?

A

Copy semantics duplicate objects, while move semantics transfer existing resources.

119
Q

When should you use move semantics?

A

When dealing with temporary, non-reusable resources.

120
Q

How do lambda expressions improve code readability?

A

By allowing the definition of functionality directly where it’s used.

121
Q

What are iterators in C++, and how are they used?

A

Special variables that point to elements in a container, used for navigating through it.

122
Q

How do you capture variables by value in a lambda?

A

By listing them in the capture list without the ‘&’ prefix.

123
Q

How do you capture variables by reference in a lambda?

A

By listing them in the capture list with the ‘&’ prefix.

124
Q

What is the significance of ‘const’ in lambda expressions?

A

It makes the lambda unable to modify captured variables if they are captured by value.

125
Q

Why might you choose to capture a variable by reference in a lambda?

A

To allow the lambda to modify the captured variable.

126
Q

What is a function template in C++?

A

A blueprint for creating a function that can work with any data type.

127
Q

How do you define a function template in C++?

A

Using the ‘template’ keyword followed by a type parameter list and the function definition.

128
Q

What is a class template in C++?

A

A blueprint for creating a class that can operate with generic types.

129
Q

How do you define a class template in C++?

A

Using the ‘template’ keyword followed by a type parameter list and the class definition.

130
Q

Why are templates used in C++?

A

For code reusability and flexibility with different data types.

131
Q

How do you instantiate a template function?

A

By calling the function with a specific data type, which the compiler then uses to generate the function.

132
Q

How do you instantiate a template class?

A

By declaring an object of the template class with specific data type(s).

133
Q

What is template specialization in C++?

A

Defining a specific implementation of a template for a particular data type.

134
Q

How do you declare a template specialization?

A

Using ‘template<>’ followed by the specialized function or class definition.

135
Q

What are the benefits of using function templates?

A

They allow the same function to work with different data types, reducing code redundancy.

136
Q

How does the compiler process template functions?

A

It generates a specific function for each type of data used with the template.

137
Q

What is the difference between class templates and function templates?

A

Class templates define a blueprint for a class, while function templates define a blueprint for a function.

138
Q

Can class templates have static members?

A

Yes, class templates can have static members.

139
Q

How are class template objects instantiated?

A

By specifying the type in angle brackets when declaring the object.

140
Q

What is a template parameter in C++, and how is it used?

A

A template parameter is a placeholder type or value used in a template definition.

141
Q

Can templates have more than one type parameter in C++?

A

Yes, templates can have multiple type parameters.

142
Q

What is a non-type template parameter?

A

A template parameter that represents a value rather than a type.

143
Q

How do you pass a non-type template parameter?

A

By specifying the value in the template argument list.

144
Q

What is a variadic template in C++?

A

A template that can take a variable number of arguments.

145
Q

How do you declare a variadic template?

A

Using ‘…’ after the last template parameter.

146
Q

What is the typename keyword used for in templates?

A

To specify that a template parameter is a type.

147
Q

Can a function template call other function templates?

A

Yes, function templates can call other templates.

148
Q

How can templates lead to more efficient code?

A

Templates can be optimized by the compiler, leading to more efficient code generation.

149
Q

What is the role of templates in generic programming in C++?

A

Templates provide the ability to write code that can operate on any type, central to generic programming.

150
Q

How do you explicitly specify a template argument?

A

By providing the type in angle brackets when calling the function or instantiating the class.

151
Q

What is a container in C++?

A

A container is a class template that stores a collection of objects.

152
Q

What are some examples of containers in C++?

A

Vector, queue, stack, and linked list are common examples.

153
Q

What is the purpose of using containers?

A

To manage collections of objects in a structured way.

154
Q

How are arrays considered as containers?

A

Arrays like int arr[3] are built-in containers used to store simple objects.

155
Q

What are the advantages of using containers?

A

They offer easy insertion, removal, and management of elements.

156
Q

What is an iterator in C++?

A

An object that provides a standard way to traverse through a container.

157
Q

How do iterators function similarly to pointers?

A

Iterators can move through a container and select elements one at a time.

158
Q

What is a smart pointer in the context of iterators?

A

A smart pointer is a type of iterator that manages memory and resource allocation.

159
Q

How is an iterator different from a pointer?

A

Iterators are more abstract and can work with different container types, unlike pointers.

160
Q

Why are iterators important in C++?

A

They provide a unified way to access elements in different types of containers.

161
Q

What is a vector in C++?

A

A dynamic array that can resize itself automatically when elements are added or removed.

162
Q

What is a queue in C++?

A

A FIFO (First In, First Out) data structure where elements are inserted from the back and removed from the front.

163
Q

What is a stack in C++?

A

A LIFO (Last In, First Out) data structure where elements are added and removed from the top.

164
Q

What is a linked list in C++?

A

A collection of nodes where each node contains data and a reference to the next node.

165
Q

How do you access elements in a container using iterators?

A

Using methods like .begin() and .end() to get iterators to start and end points.

166
Q

What are the benefits of using a linked list?

A

Efficient insertion and deletion of elements without reallocation.

167
Q

How can you reverse the order of elements in a container?

A

Using algorithms like std::reverse with the container’s iterators.

168
Q

How do you insert a new element into a container?

A

Containers have member functions like push_back or insert for adding elements.

169
Q

What is the role of .begin() and .end() in container classes?

A

They return iterators to the beginning and past-the-end elements of the container.

170
Q

How do you remove an element from a container?

A

Using member functions like erase or pop_back, depending on the container type.

171
Q

What is the difference between .begin() and .end() in terms of iterators?

A

.begin() points to the first element, while .end() points to the position after the last element.

172
Q

What are the uses of iterators in range-based loops?

A

They allow iterating over elements of a container efficiently.

173
Q

How do you display the last element in a container?

A

By accessing the element before the container’s .end() iterator.

174
Q

What is the significance of container flexibility in C++?

A

It allows handling collections of elements in a memory-efficient and organized way.

175
Q

How do you sort elements in a container?

A

Using the sort function along with the container’s iterators.