Flashcards
A5
Cards in this deck (25)
- 1Name two design patternsSingleton Pattern
Adapter Pattern
Observer Pattern
Factory Method Pattern
Prototype Pattern - 2What is the rule of threeIf a class requires a user-defined destructor, a user-defined copy constructor, or a user-defined copy assignment operator, it almost certainly requires all three.
- 3What is the rule of fiveWhen a class has a user-defined destructor, copyconstructor and copy-assignment operator and move semantics are desirable a move constructor and move assignement operator have to be declared
- 4What is the rule of zeroClasses that have custom destructors, copy/move constructors or copy/move assignment operators should deal exclusively with ownership
- 5What is a disadvantage of a shared_ptr compared to an old raw pointershared_ptr have more overhead in terms of memory usage and performance
- 6What is a friend functionA function outside of a class (non member function) that has acces to the private variables of the class
- 7Name two problems that occure with multithreaded programmingRace Conditions: Race conditions occur when multiple threads access shared resources
Deadlocks: Deadlocks occur when two or more threads are waiting for each other to release resources that they need - 8What means RAIIRAII stands for Resource Acquisition Is Initialization. It is a C++ programming idiom aimed at managing resources.
The core idea behind RAII is that the acquisition of a resource should be tied to the initialization of an object, and the release of the resource should be tied to the destruction of the object. - 9Name two recources, which can be freed with RAIIMemory allocation using 'new'
Memory allocation using 'malloc()'
File Handles - 10What are dangling pointer and why are they dangerousA dangling pointer is a pointer that points to a memory location that has been deallocated or freed. In other words, it refers to a pointer that no longer points to a valid object or memory location. Dangling pointers are dangerous because they can lead to undefined behavior when accessed, which can result in crashes, data corruption, or security vulnerabilities.
- 11Name at least two smart pointers provided by the standard library and explain their differencesunique_ptr: only one pointer is allowed to point to the recource
shared_ptr: multiple pointer are allowed to point to the same recource. memory is freed only when the last pointer is removed. - 12What is a static memebr functionA static member function in C++ is a function that belongs to a class rather than to instances (objects) of the class
- 13What is the difference between the static and dynamic type in C++Static Type:
The static type of an object refers to the type that is known at compile-time.
Dynamic Type:
The dynamic type of an object refers to the actual type of the object during runtime. - 14Name two options to allocate memory in C++static allocation:
memory allocation a t compiletime or during program startup
Dynamic allocation:
Allocation of memory during runtime - 15What is the output of the code? #include using namespace std; int x = 5; int &f() { return x; } int main() { f() = 10; cout<10
- 16What is a copy constructorA copy constructor in C++ is a special member function that is used to create a new object as a copy of an existing object of the same class.
- 17Do we have a String primitive data type in C++? If not, what is Stringno it is a type of the standard library
- 18What is the purpose of the 'delete' operatorIn C++, the delete operator is used to deallocate memory that was previously allocated dynamically using the new operator. It is specifically used to release the memory associated with a single object or an array of objects that was allocated on the heap.
- 19If the 'new' operator is to be avoided what is a reasonable solutionInstead of dynamic memmory allocation.
Static allocation could be used for the objects with fixed size and lifetime are needed
Unsicher (SS19) - 20What is a pure virtual functionPure virtual functions serve as placeholders for functions that must be implemented by derived classes, allowing the base class to define an interface that derived classes must adhere to.
ist has the virtual keyword and ends with = 0 - 21How is static and dynamic polymorphism realized in C++?Static polymorphism:
- function/operator overloading
Dynamic Polymorphism:
- virtual functions
-interfaces - 22What does the std::allocator class doThe std::allocator class in C++ is a standard library template class that provides a mechanism for allocating and deallocating memory for objects. It serves as the default allocator used by standard library containers such as std::vector, std::list, std::deque, etc., to manage the memory associated with the elements stored in these containers.
- 23When should be a "destructor" declared virtual and whyit is needed for a base class when it is intended to be derived and the derived classes are managed through base class pointer or references
- 24What is the difference between variable declaration and variable definitionVariable declaration is the process of specifying the data type and name of a variable without allocating memory or initializing its value.
Variable definition is the process of allocating memory and optionally initializing the value of a variable. - 25Name two differences between a reference variable and a pointer variablepointer:
use a *
to get the value use a *
can be the nullptr
reference:
use a &
to get the value just use the name
can not be null
Information
- Description
- - - Flashcards on A5, created by N B on 18/03/2024.
- Created by
- N B · Mar 2024
- Updated
- Mar 18, 2024
- Views
- 0
- Rating
More on C++ Programming Basics