C++ Flashcards

1
Q

What is the Rule of Three in C++ and what standard was it introduced?

A

Guideline for writing classes to manage dynamically allocated resources. It states that if you need to modify the destructor, copy constructor, or copy assignment operator you should modify all three.

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

IWhat is the Rule of Five in C++ and what standard was it introduced?

A

The Rule of Five adds the Move Constructor and Move Assignment Operator to the Rule of Three. It was introduced in C++11.

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

What does the static keyword do in C++?

A

When static is used inside a function, the variable persists across function calls. It is only initialized once. When used with globals, that variable or function’s scope is limited to the file in which it is declared. In a class, it creates a class variable or function that is accessible by all instances of the class.

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

What does the volatile keyword do in C++?

A

Volatile indicates to the compiler that the variable’s value can change unexpectedly due to external circumstances like a GPIO pin. This ensures the compiler doesn’t optimize the variable in ways that depend on it being a constant.

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

What is a pure virtual function in C++?

A

A pure virtual function is declared in an abstract class and it has no implementation and must be overriden in child classes

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

What’s pass-by-reference versus pass-by-value?

A

Pass by value creates a deep copy of the argument. Modifying the variable in the function does not affect its value in the outer scope. Pass by reference allows a function to modify the value of the original variable that is passed in as an argument

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

What’s the difference between pass-by-reference and pass-by-pointer?

A

They both allow modifying the original variable inside a function call, they both avoid copying. The difference is in the implementation of pointers and references. Pointers can be null, references must always refer to a valid object. Pointers can be reassigned, references cannot. References do not need manual memory management whereas pointers do if they point to dynamically allocated memory

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

What’s the difference between Shallow and Deep copying?

A

Shallow copying has both the new and original object point to the same place in memory. Deep copying gives the new object is own memory.

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

How can shallow copy issues be avoided with classes in C++?

A

Shallow copying occurs when both the original object and its copy point at the same block of memory. The default copy constructor and copy operator use shallow copies. They must be overridden to give the new class its own memory.

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

What are the three types of Smart Pointers?

A

Unique, Shared,

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

What’s the difference between a process and a thread?

A

A process is a running program. It has its own address space and is managed by the OS. Communication between processes requires IPCs like pipes, sockets, and message queues. Threads in a process share an address space and can share data. Their order of operation is determined by a scheduler.

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

What’s the difference between a mutex and a lock?

A

Mutexes are basic and have two states: locked and unlocked. Locks encapsulate mutex usage. Lock_guards acquire a mutex in the constructor and ensure its released when the lock_guard goes out of scope.

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

What’s are new and delete in C++?

A

the new operator returns a pointer to newly allocated memory. Delete takes a ptr to memory and deallocates it. To delete an array you must use delete[]. It’s recommended to use smart pointers instead of new and delete in modern C++.

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

What is an Rvalue in C++?

A

An RValue is a temporary expression which is used to initialize objects or compute the value of an operand. I.e. in i = 42, 42 is an Rvalue. Rvalues don’t have a directly accessible address.

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

What is an Lvalue in C++?

A

LValues have addresses that can be accessed. I.e. in i = 42, i is an lvalue.

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

What’s the difference between a string and a char in C++?

A

a Char is a fundamental data type in C++ it is 1 byte. Strings are a sequence of characters. They can dynamically resize to accomodate varying length of text.

17
Q

What are Move Semantics in C++? When were they introduced?

A

C++11. Move semantics are used to manage heavy-weight objects that need to be passed around in a program. This way memory-intensive objects only need to be created once and can be moved around efficiently. This also allows transferral of ownership such as with unique pointers.

18
Q

What are Promises and Futures in C++?

A

Components of asynchronous programming. They are used for communicating between tasks in a multi-threaded environment without the need for explicit synchronization primitives like mutexes. The promise allows a producer thread to communicate the result of an operation to another thread. The Future allows the consumer to retrieve it.

19
Q

What is a lock_guard in C++? How does it differ from a unique_lock?

A

Lock guards employ RAII on mutexes. Acquires mutex lock in constructor and releases it in destructor. unique_lock allows manual unlocking and locking as well as deferred, timed, and conditional locking.

20
Q

What’s a queue in C++? What’s the difference between push_back and emplace_back?

A

FIFO. push() adds a copy of the argument to the container. emplace constructs an object directly in the queue using the given arguments. This avoids copying.

21
Q

How can you construct a generic message queue for communicating between threads in C++?

A

A generic message queue should be implemented in a class. It should hold a deqeue which should handle templates and is only modified under a lock.

22
Q

What are Preprocessors, Compilers, and Linkers?

A

Preprocessors handle # directives like includes, defines, and conditionals. #ifdef. It replaces or modifies source code based on the above. Compilers translate preprocessed source code into machine code. It also optimizes and performs syntax analysis on source code. The linker then combines the object files generated by the compiler into a single executable or library.

23
Q

Why would you need to modify a destructor?

A

If a class manages dynamically allocated memory (e.g. use of new), the destructor needs to be modified to properly deallocate it.

24
Q

Why would you need to modify the copy constructor or copy assignment operator?

A

If a class manages dynamically allocated memory, the copy constructor and operator should be modified to ensure deep copying of the resource.

25
Q

What’s a Constructor?

A

A Constructor is ran when an instance of a class is created.

26
Q

What’s a Copy Constructor?

A

The Copy Constructor creates a new object by copying an existing object of the same type. By default it uses shallow copying. If a class manages dynamically allocated memory it needs to override this to deep copy.

27
Q

What’s a Copy Assignment Operator?

A

Assigns the contents of one object to another of the same type.

28
Q

What’s a Move Constructor?

A

A Move Constructor constructs a new object by “stealing” the resources owned by another object of the same type. The Move Constructor transfers resources from temporary objects.

29
Q

What’s a Move Assignment Operator?

A

A Move Assignment operator transfers resources from one object to another similarly to the Move Constructor but for already constructed objects.

30
Q

What is an rvalue reference?

A

An rvalue reference allows storing and modifying rvalues via the && operator. rvalue references are used to reduce the need for copying and managing of temporary rvalues.
int l = i + j creates an rvalue i + j and then copies it into l and deletes the value
int &&l = i + j would instead simply create the temporary variable i + j and hold it in memory. Rvalue references are lvalues

31
Q

What is RAII?

A

Resource Acquisition is Initialization is the idea that resources such as dynamically allocated memory are typically managed by wrapping a class around their handle and using it to ensure that the resource is allocated and deallocated correctly