C++ Intro Flashcards

C++ but before reading the codebase or any advanced books

1
Q

What does it mean that C++ is a “compiled language”?

A

It means the compiler takes object files, and turns them into executables.

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

What does it mean that C++ is “statically-typed”?

A

It means that the type of every object must be known to the compiler when it’s used.

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

What is a “built-in type”?

A

It’s one that can be built from the fundamental types.

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

What is a “user-defined type”?

A

It’s one that is built out of other types using abstraction mechanisms.

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

Give two examples of user-defined types.

A

Classes, enums.

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

How does the compiler handle virtual functions?

A

It creates the “virtual function table”, a table of pointers to functions.

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

What is it called when you do like: #indef FOO_BAR_BAZ_H__ #define FOO_BAR_BAZ_H__ … #endif // FOO_BAR_BAZ_H__

A

It’s called an include guard.

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

Why would you use an include guard?

A

To prevent the same header file from being included multiple times during compilation.

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

What are #ifndef, #define, and #endif called?

A

They are called “preprocesssor directives”

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

What does the “extern keyword mean” e.g. extern const int foo?

A

You use it in header files to say “I have this variable”. It’s up to the source file to define it.

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

How much memory is allocated when you put “extern” in your header file?

A

None.

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

What happens if you declare an extern variable in your header file, and then don’t define it in your source file?

A

You get a linker error like “unresolved external symbol”?

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

Why isn’t the extern keyword used for class declarations?

A

Because a class is a type, not a variable – when you say “class” you are just defining the type, not instantiating a specific variable.

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

What does the static keyword do?

A

It is similar to “private” in Java, but it’s about file scope – which files can access the property. Static means that only this file can access them. This is called “internal linkage”.

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

What does “internal linkage” mean and how do you indicate it?

A

It means the identifier is only visible in the source file where it is included. You indicate it with the “static” keyword.

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

Why is almost everything in a header file static?

A

Because you don’t need other files to be able to see those properties, just the source file.

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

Would you declare a public interface function as static?

A

No, you want every file to be able to see it, not just the source file.

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

What does it mean when a variable definition – not just declaration – is static?

A

This is declaring a singleton, ensuring it is only created once.

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

Under what two circumstances is there code in the header file rather than declarations?

A

You would declare small inline functions that you want inserted directly at the call site, to eliminate function call overhead. And you would declare templates, since the compiler needs the complete definition so it can generate code wherever the template is used.

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

What is the difference between include and import?

A

Include processes the complete text of the file every time it’s included, and the order matters. Import is just done once.

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

What are the things that you import called?

A

Modules

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

How do you define a module?

A

You put “module;” at the top of the file, and then “export module ModuleName”.

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

What version of C++ introduced modules?

A

C++ 20.

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

What is a namespace?

A

A namespace is something you use to group declarations together without worrying about naming conflicts.

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

When you do “#include ” you have to put “std::” in front of everything. What are two ways to not have to do this?

A

You could do “using std::foo” to specifically let you say “foo”, or “using namespace std” so you don’t have to use the prefix anywhere at all.

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

What is namespace scope and how far does it extend?

A

Namespace scope means the name is defined in a namespace and not in any particular function or class. It extends to the end of the namespace.

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

What are the two parts of a class?

A

Interface and implementation

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

What is the difference between interface and implementation?

A

Interface is used by everyone. Implementation is the private details the class keeps to itself.

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

What keywords to interface and implementation correspond to?

A

Public (interface) and private (implementation)

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

What is the syntax for a constructor?

A

ClassName(params) :var{assignment}, var{assignment} { stuff }

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

How do you do variable assignment in the constructor? What is the syntax?

A

You declare initializers before the body. The syntax is “:var{assignment}”.

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

What is it called when you do “:var{assignment}”

A

The member initializer list

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

What does it mean when you define a method as const?

A

It means it will not modify the object that it’s a member of.

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

What is the syntax for defining a const method?

A

You put “const” before the body: double real() const { … }

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

What is it called when you separate the function body definitions from their declarations?

A

Separate compilation

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

Where do the declarations go, and where do the definitions go, for a class?

A

Declarations go in the header file, definitions go in the source file.

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

What is the difference between an enum class and an enum?

A

In an enum class, the enumerators (enum values) are placed in the class’s scope. So you would access them like EnumClassName::Red. In a raw enum, the values just go in the surrounding scope as aliases for ints.

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

What is an operator function?

A

It’s when you define an actual operator, like + or =.

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

What happens at compile time when you do “foo + 1”?

A

The compiler replaces it with FooType::operator+(foo, FooType{1})

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

What does the “virtual” keyword do?

A

It defines a virtual function, which is like an abstract function.

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

What is a virtual function?

A

It is a function that may be redefined later in a derived class.

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

What is a PURE virtual function and how do you define it?

A

It is a function where the derived class MUST define the function. You define it by putting “= 0” at the end.

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

What is it called when a class has a pure virtual function?

A

The class is called an abstract class.

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

What is special about an abstract class?

A

You can not instantiate it.

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

What is a polymorphic type?

A

It’s when you define an abstract class, like “Shape”, that you want to be the parent of a bunch of other classes.

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

What does it mean when a function has “= 0” after it?

A

It means it’s a pure virtual function – an abstract function that MUST be overridden by the child class.

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

What is the syntax for class inheritance?

A

class Triangle : public Shape {

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

How do you override a virtual function in the derived class?

A

You use the “override” keyword

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

What does it mean when a function has the “override” keyword after it?

A

It means it is overriding a virtual function in the parent class.

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

What is the one situation where you don’t have to use the override keyword for a virtual function?

A

When you’re overriding the destructor.

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

What are the six essential operations?

A

Constructor, Destructor, copy+move, copy+move assignment.

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

What does it mean when an essential operation has “= default” at the end?

A

It means just use the default implementation.

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

What is the default implementation of copy?

A

Run copy on every single member variable.

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

What is an example scenario where you would not want to use the default implementation?

A

When you have pointer members, copying them would be disastrous.

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

What do you do if you want to prevent an essential operation from being called at all?

A

Use the “delete” keyword.

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

What does the “delete” keyword do?

A

It’s used on essential operations to prevent them from being called.

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

What is the “rule of zero”?

A

It means a class should define either all or none of its essential operations.

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

What does “explicit” mean, for a constructor?

A

It means that the constructor must be explicitly called, and can’t be implicitly called by passing in its initializer variables to a function that requires it.

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

What is “this”?

A

A variable referencing the object for which the function is being called.

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

What is a reference variable?

A

It’s like an alias for an existing object.

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

How do you define a reference variable?

A

You put & at the end of the type, like “std::string& foo”.

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

What is the difference in & placement between a reference and an address?

A

& in the type is a reference. & in the variable name is its addresss.

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

What is “value semantics”?

A

It means that assignment is always copy – you will never have the thing Java does where “foo = bar” makes foo a reference to the same object bar is a reference to, and if you change foo you will also change bar.

64
Q

If I want to make it so when I change foo, then bar also changes, what would I do?

A

You would use pointers, and store the address of bar in foo, and then change *foo.

65
Q

Why would you ever want to use a reference variable?

A

When you want to pass-by-reference instead of pass-by-value.

66
Q

Why would you ever pass a pointer instead of a reference variable?

A

A pointer can be nullptr, so you can pass in optional or unset values. A reference must always be a valid object.

67
Q

What does it mean when a function parameter is “const”?

A

It means the function will not modify that variable.

68
Q

I am seeing “const List& foo” as the parameter in my method signature. What does that mean?

A

It means I want a reference to a list, and I promise not to make any changes to it. I do not want you to copy the list for me, I just want to read it by-reference.

69
Q

If you forget to put & after the type name in your method signature, what will happen?

A

The entire object will get copied and passed in, since it is by-value.

70
Q

If you’re just going line-by-line, why would you do “const std::string& foo = ‘bar’” instead of just saying “std::string foo = ‘bar’”?

A

If you make an actual string, then “bar” must be copied into it. Instead, if you just make a string reference, then you only create “bar” once and then just point to it.

71
Q

What is a dangling reference?

A

It’s when you allocate space for something in a method, and then return the reference to it, but then the space is freed, so the reference points to nothing. This has undefined behavior.

72
Q

What is the difference between ::, ., and ->?

A

:: is used to access static members of a class. . is used to access members. -> is used to access members through a pointer.

73
Q

I have a pointer foo to a class Type with member variable x. How do I get x?

A

foo->x.

74
Q

What is “Hungarian notation”?

A

It’s when you start constants with “k” for “konstant”.

75
Q

What is the type of a function like int get(double& x, double y)?

A

int(double&, double)

76
Q

What is a narrowing conversion? Give an example.

A

It’s one that loses information, like int x = 7.8.

77
Q

How do you avoid narrowing conversions?

A

Use bracket assignment, like int x {7.8} is a compiler error.

78
Q

What is bracket assignment, and why do you want to use it sometimes?

A

Bracket assignment is like int x {4}, you use it to avoid narrowing conversions on accident.

79
Q

What does constexpr mean?

A

It means something that must be evaluated at compile time, you use it for things where you don’t have to wait until runtime to figure out their value.

80
Q

How do you define an array?

A

char v[6], the [] goes after the variable name.

81
Q

When do you use unsigned?

A

When doing bit manipulation.

82
Q

What is the syntax for defining a struct type?

A

struct Vec { int sz; double* elem; };

83
Q

What is a union?

A

A struct that occupies as much space as its largest member, and has one of its member variables values.

84
Q

What is a variant?

A

A variable that stores the value of one of a set of alternative types.

85
Q

When to use union? When to use variant?

A

Variant is basically a type-safe union. But it was only introduced in C++ 17, before that you had to use union.

86
Q

Why is “vector” such a popular type?

A

Because it can be added to efficiently.

87
Q

What do you use to add to a vector?

A

push_back

88
Q

What happens if you look beyond the end of a vector?

A

You get an out-of-range error.

89
Q

What is the library for a doubly-linked list?

A

list

90
Q

What is the library for a singly-linked list?

A

forward_list

91
Q

What is the library for a dictionary?

A

map

92
Q

What is the library for a dictionary where the key can occur many times?

A

multimap

93
Q

What is the library for a double-ended queue?

A

deque

94
Q

What is the library for a set?

A

set

95
Q

How do you define a pointer?

A

char* p = &val

96
Q

How do you get the value a pointer points to?

A

*p

97
Q

What is it called when you do *p on a pointer?

A

Dereferencing?

98
Q

What is the generic pointer type?

A

void *

99
Q

What is void * and why is it so popular?

A

It is the generic pointer type; any pointer can cast to-and-from void * without loss of information.

100
Q

What is the value of a pointer that isn’t pointing to anything?

A

nullptr

101
Q

What is the syntax for a pointer to a function?

A
  • before the name – double (*comp)(int, int) type
102
Q

What libraries should you use instead of pointers?

A

The smart pointer libraries unique_ptr and shared_ptr

103
Q

Why are smart pointers so useful?

A

They have destructors that are called implicitly to destroy the object they point to.

104
Q

What is the difference between unique_ptr and shared_ptr?

A

A unique_ptr is owned in only one place, and moved around. When it is destroyed, its object is destroyed. A shared_ptr is owned in many places, and copied around. Its object is only destroyed when all shared_ptrs pointing to it are destroyed.

105
Q

What does “initialization” mean?

A

It means making a particular piece of memory into a valid object.

106
Q

What are two other terms for heap?

A

Dynamic memory, free store

107
Q

What does the new operator do?

A

Allocates memory on the heap.

108
Q

What is the relationship between allocation and scope?

A

Allocation is INDEPENDENT of scope and will persist until deleted.

109
Q

How do you free up memory from the heap?

A

The delete operator.

110
Q

How do you free the memory allocated to an array?

A

delete[] (the brackets after the keyword)

111
Q

Why do you want to avoid naked new/delete operations? What should you do instead?

A

Because it is hard to keep track of everything and easy to slip up. You should use destructors and smart pointers instead.

112
Q

Why should you never return a reference?

A

Because local variables disappear when the function returns, so the thing the reference was pointing to will be gone.

113
Q

What is the default behavior for passing function parameters and how do you change it?

A

Default behavior is pass-by-value. Change it by passing reference variables.

114
Q

What is an rvalue reference? How is it indicated?

A

A thing that is about to be destroyed. Denoted by &&.

115
Q

What does && indicate?

A

An rvalue reference – a thing about to be destroyed.

116
Q

What function are rvalue references related to?

A

std::move – they take control of the rvalue and then it’s deleted after.

117
Q

What does RAII stand for?

A

Resource Acquisition Is Initialization

118
Q

What does RAII mean?

A

It means the constructor should acquire all the resources necessary for a class to operate, and the destructor should release them.

119
Q

What is the syntax for the destructor?

A

~ClassName

120
Q

What does ~Class indicate?

A

It’s the destructor.

121
Q

What is a template?

A

It’s a parameterized class or function, like a Java generic.

122
Q

How do you say that something is a template?

A

You put “template” above it.

123
Q

“template” what is T?

A

It’s the type parameter that you use in the template declaration.

124
Q

What happens to a template at compile time?

A

It instantiates the templates for the particular types provided.

125
Q

What does it mean when you say “template”?

A

It is a “constrained template” saying the type parameter must be an “Element” subclass.

126
Q

What is a functor?

A

It’s when you define an object that can be called like a function, by defining a () operator.

127
Q

What is a “compile-time if”?

A

It’s when you do “if constexpr(whatever)” instead of “if (whatever)”. When you use it, only the selected branch is instantiated – this improves performance.

128
Q

What is a concept?

A

It’s where you define some property of the template type, and say that the template type is constrained by that property.

129
Q

How do you define a concept?

A

You use the concept keyword, and write “concept Foo = requires(T a, T b) { property… };”

130
Q

How do you use a concept?

A

Just like any other template constraint. template

131
Q

What version of C++ introduced concepts?

A

C++ 20.

132
Q

What is a variadic template?

A

It’s a template that has an arbitrary number of arguments.

133
Q

Why would you want to uses a variadic template?

A

If you are defining a template function that has an arbitrary number of parameters.

134
Q

What is “duck typing”?

A

It means that the meaning of an operation depends solely on its operand values.

135
Q

Where does the term “duck typing” come from?

A

If it walks like a duck and quacks like a duck, it’s a duck.

136
Q

How do you do an optional variable?

A

std::optional is the type, and you make it with std::make_optional(thing). Then you do .has_value() and .value() to access it.

137
Q

How would you make an empty optional?

A

std::nullopt

138
Q

If a file is like foo.bar.baz, what is the namespace?

A

It’s three nested namespaces foo, bar, and baz

139
Q

What is it when you just have namespace {…} with no name?

A

An anonymous namespace

140
Q

Why would you use an anonymous namespace?

A

To keep things defined in it from being visible anywhere else – basically making them private.

141
Q

Why would you use an anonymous namespace when you could just use static?

A

The anonymous namespace is cleaner and less repetitive.

142
Q

Why would you create a unique_ptr only to immediately dereference it?

A

So that you have something managing the memory and deleting it when the object goes out of scope.

143
Q

What do you do if you want to return a value without copying everything in it?

A

Use std::move, which transfers ownership of the object to the caller.

144
Q

What are three ways to return a large object from a method?

A

Use std::move, return a smart pointer to the object, define the object in the calling scope and pass it by-reference to be created.

145
Q

What happens when you make a smart pointer with const type?

A

The object the pointer is holding becomes read-only to that pointer.

146
Q

What is the convention for indicating a member variable for a class?

A

You end the name with an underscore.

147
Q

Why do some variable names end with an underscore?

A

It indicates that they are member variables for a class.

148
Q

What does “friend” mean?

A

It means you are granting another function/class access to the private/protected members of this class.

149
Q

“friend class FooBar” – what does this mean?

A

It means FooBar has the right to see private/protected members of this class.

150
Q

What is the difference between the register and the stack?

A

The register is the CPU cache, and the stack is RAM. Register is much faster.

151
Q

What are the restrictions placed on usage of the register?

A

You can’t dereference something that is in a register.

152
Q

Why would passing by reference be slower?

A

Passing by-reference is essentially creating a pointer that you then dereference, meaning whatever you pass in must be on the stack, because you can’t dereference something that is in a register. If you pass-by-value, the value can be in a register, which is much faster. Furthermore, if the value was already on a register, it now has to be copied to the stack so it can be passed-by-reference and dereferenced.

153
Q

What are three situations where you would use “auto”?

A

Range-based loops of a varying type where you don’t know the type in advance, assignment/initialization cases where it’s repetitive to re-specify the type, and generics.

154
Q

What important information is hidden by auto?

A

Whether the type is const, or a pointer.

155
Q

Why would you use string view?

A

It saves time because it doesn’t copy the string, it creates a view of it in O(1).

156
Q

When should you not use string view?

A

For storage – it doesn’t own any data.