C++ Core Flashcards

1
Q

What are the 3 different categories of data types present in C++?

A

Fundamental data types, User defined data types, Derived data types

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

Name the C++ fundamental data types

A
  1. Boolean type (bool)
  2. Character types (char, wchar_t)
  3. Integer types (int, long long …)
  4. Floating-point types (double, long double ….)
  5. Void
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the void type?

A
  1. Type with an empty set of values (no value applies to it).
  2. Incomplete type that cannot be completed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an incomplete type?

A

A type that is not defined or void type.

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

What is nullptr_t?

A

Answer:

  1. nullptr_t solves previous ambiguity problems with NULL
  2. nullptr_t is the type of the nullptr literal.
  3. It is a distinct type that it’s not itself a pointer type.

Read more:
https://dzone.com/articles/what-exactly-nullptr-is-in-c

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

What RAII abbreviation means?

A

Resource Acquisition In Initialization

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

What is RAII Intent?

A

Answers:

  1. To guarantee the release of a resource at the end of scope.
  2. To guarantee no memory leaks

Read more:
http://www.vishalchovatiya.com/7-advance-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver

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

What is example RAII Implementation?

A

Answer:

  1. Wrap resource into a class
  2. Resource acquired in the constructor immediately after it’s allocation
  3. Destructor automatically releases resource
  4. Resource used via interface

Read more:
http://www.vishalchovatiya.com/7-advance-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver

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

Why RAII is imporant?

A

Answer:

  1. To not forget to release resource.
  2. If a function returns early (throws, returns, etc…) resource may be leaked.

Read more:
http://www.vishalchovatiya.com/7-advance-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver

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

What are RAII usecases?

A

Answer:
In acquisition/release of resource with:
1)new/delete (memory)
2)lock/unlock (mutex)
3)open/close (file)
4)and others

Read more:
http://www.vishalchovatiya.com/7-advance-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver

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

What is the intent of return type resolver idiom?

A

Answer:
To deduce the type of the object being initialized or assigned to.

Read more:
http://www.vishalchovatiya.com/7-advance-cpp-concepts-idiom-examples-you-should-know/#Return-Type-Resolver

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

What is NULL?

A

NULL is a MACRO that is evaluated to integer with value 0

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

What is the intent of Type Erasure?

A

Answer:
To create generic container that can handle variety of concrete types.

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

What we refer to when we say the implementation’s data model?

A

The sizes of the fundamental data types that are made by each implementation is referred to as the data model.

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

What are the four data models that found wide acceptance?

A

int/long/pointer notation (size in bytes)

32 bit systems:
LP32 (2/4/4 Win16) or ILP32 (4/4/4 Win32, Unix)
64 bit systems
LP64 (4/4/8 Win64) or ILP64 (4/8/8 Unix)

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

What are the integer types modifiers?

A

Signedness: signed (default) and unsigned.
Size: short (at least 16 bits) and long (at least 32 bits) and long long (C++11 at least 64 bits).

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

When integer types overflow is undefined?

A

When you overflow a signed integer.

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

At least how many bits is short?

A

16

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

At least how many bits is int?

A

16

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

At least how many bits is long?

A

32

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

At least how many bits is long long?

A

64

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

What is STL array?

A

STL array is fixed size array implementation from the standard library

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

What are the advantages of STL array over C array?

A

STL array doesn’t degenerate to pointer when passed to function and it knows it’s size

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

Where is STL array stored on the stack or on the heap?

A

On the stack

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

What does the const member function do?

A

1) Function declared const can be called for any object const or non const object
2) It doesn’t allow modification on the object which calls them

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

What is an lambda function?

A

Short snippets of code that

1) Not worth naming
2) Not reused

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

Write to list lambda function syntax?

A

[capture list] (parameters) -> return-type
{
method definition
}

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

What are lambda functions benefits?

A

1) They have application with generic functions like std::for_each, because it’s not appropriate to create a new class for a specific algorithm.
2) Zero cost abstraction
3) Code becomes compact, structured, expressive.

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

When is the notion of invariant applied?

A

It is central to the design of classes.

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

What type of language is C++ (compiled or not compiled).

A

Compiled.

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

What are the steps for generating a C++ program?

A

1) Source text is processed by a compiler producing object files.
2) Object files are linked together producing executable file.

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

Is a C++ program compiled on one computer portable on another?

A

No, an executable C++ programs is created for specific hardware/system combination.

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

When we talk about portability of C++ program what do we mean?

A

Portability of source code. That is the source code could be successfully compiled and run on a variety of systems.

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

What is statically type language?

A

The type of every entity (object, value, name, expression) must be known to the compiler at its point of use.

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

What does the type of object determine?

A

The set of operations applicable to it and set of possible values.

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

What does {} indicate in C++?

A

Grouping

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

What does // indicate in C++?

A

Comment

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

What is main function?

A

Every program must have exactly one main function, it’s where the program starts its execution.

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

What does non-zero value returned from main mean?

A

That the program has failed in some way.

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

What is a string literal?

A

A sequence of characters surrounded by double quotes.

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

What is a declaration?

A

A declaration is a statement that introduces a name to the program.
A declaration specifies the type of the name to inform the compiler what kind of entity the name refers to

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

What is an object?

A

An object is some memory that holds a value of some type.

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

What is a value?

A

A value is a set of bits interpreted according to a type.

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

What is a variable?

A

A variable is a named object.

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

What does sizeof operator do?

A

Determines the size of a type.

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

What does const mean?

A

Specify immutability in interfaces - “I promise not to change this value”

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

What does constexpr mean?

A
  1. To enable and ensure compile-time evaluation - “Evaluated at compile time”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

What does switch do?

A

Tests a value against a set of constants.

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

In declarations what does [] mean?

A

Array of

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

In declarations what does * mean?

A

“pointer to”

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

In e declaration what does & mean?

A

“reference to”

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

What are &, * and [] called in a declaration?

A

Declarator operators.

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

Which types we call user defined?

A

Those that can be built from built-in types, const modifier, and declarator operators.

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

Why do we want to keep representation inaccessible to users?

A

1) To ease use
2) Guarantee consistent use of the data
3) Allows us later improve representation

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

With what is the interface of a class defined?

A

With public members.

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

How are private members accessible?

A

Only through a public interface.

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

What is the basic technique for handling varying amounts of information in C++?

A

A fixed-size handle referring to a variable amount of data “elsewhere” (e.g., on the free store allocated by new)

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

What do we call a constructor?

A

A function with the same name as the class.

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

How is a constructor different from than ordinary function for the initialization of an object?

A

A constructor is guaranteed to be used to initialize objects of its class. Thus, eliminates the problem of uninitialized variables for a class.

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

What is the purpose of enumerators?

A

To represent small sets of integer values. They make code more readable and less error-prone.

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

With what C++ represent interfaces?

A

With declarations.

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

What does declaration do?

A

It specifies all that’s needed to use a function or a type.

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

What is the link between declaration and definition?

A

A declaration must also have a definition somewhere but it can be in another place (In a cpp file, in library, etc.).

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

Why is separate compilation used in C++?

A

To minimize compilation time and to strictly enforce separation of logically distinct parts of a program.

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

What is the advantage of defining function inside a class?

A

It inline by default.

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

What does inline function mean?

A

It replaces function call with the function body.

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

What does inline function mean?

A

It replaces function call with the function body.

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

What does a!=b translate to?

A

operator!=(a,b).

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

What is a container?

A

An object holding a collection of elements.

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

How is a destructor named?

A

Name of the class + prefix ~

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

What is the purpose of destructor?

A

To ensure that memory allocated by the constructor is deallocated.

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

What is handle-to-data model?

A

A method used for managing data that can vary in size during the lifetime of an object.

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

What is the defining characteristic of concrete types?

A

It’s representation is part of its definition.

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

What is the defining characteristic of abstract class?

A

Implementation details are insulated from the user.

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

What does the word virtual mean?

A

“may be redefined later in a class derived from this one”

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

What does pure virtual function entail?

A
  1. That the class is abstract
  2. The function must be defined in some derived class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
77
Q

What is virtual function table or vtbl?

A

During polymorphism an object must contain extra information to allow it to select the right function to call at runtime. Each class has it’s own virtual function table. When a virtual function is called the compiler converts it into an index into a table of pointers to functions (vtbl).

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

What is the space and performance overhead of virtual functions?

A

1) Performance within 25% to normal function calls
2) Overhead one pointer per object & one vtbl for each class

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

When does copy elision apply?

A

The compiler can omit copy constructor, resulting in zero-copy pass-by-value semantics.

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

What is a resource?

A

A resource is something that must be acquired and later (explicitly or implicitly) released

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

What do we call a task?

A

A computation that can be potentially executed concurrently

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

When talking about the C++ standard what we mean by implementation defined?

A

Many things are deemed implementation-defined by the standard. Meaning each implementation must provide a specific, well-defined behavior for a constructor:
unsigned char c1 = 64 // well defined: a char has at least 8 bits
unsigned char c2 = 1256 // implementation-defined: trunctuation if a char has only 8 bits

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

When talking about the C++ standard what do we mean by unspecified behavior?

A

A range of possible behavior is acceptable, but the implementor is not obliged to specify which actually occur. Deciding for something to be unspecified is that the exact behavior is unpredictable for some reason.

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

What is an undefined behavior?

A

No reasonable behavior is required by an implementation. Ex. Accessing allocated memory out of bounds.

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

What is the difference between hosted and freestanding implementation?

A

A hosted implementation includes all the standard library facilities as described in the standard. A freestanding implementation may provide a fewer std facilities and a few headers must be provided…

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

What is ASCII abbreviation?

A

American standard code for information interchange

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

What do we need for x = y + f(2) to make sense?

A

The names x, y and f must be suitably declared. The programmer must specify that x, y, and f exist and that they are of types for which =, +, and () are meaningful.

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

What do we mean by name?

A

Every name(identifier) in a C++ program has a type associated with it. This type determines the operations that can be applied to the name and how such operations are interpreted.

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

Which types are called integral types?

A

The boolean, character and integer types are called integral types.

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

Which types are called arithmetic types?

A

The integral and floating-point types are collectively called arithmetic types.

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

Which types are called user-defined?

A

Enumerations and classes (because they must be defined by users)

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

Which types are called built-in types?

A

Fundamental types, pointers, and references.

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

What values can a boolean have?

A

True and False.

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

Why is boolean used?

A

To express the result of logical operations

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

Do integers implicitly convert to bool values and if so how?

A

Yes, nonzero integers convert to true and 0 converts to false

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

Do pointers implicitly convert to bools if so how?

A

Yes, a non-null pointer converts to true. Pointers with the value nullptr convert to false.

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

What does _t denote?

A

An alias type

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

What is character literal?

A

A character literal is a single character enclosed in single quotes

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

In what forms does integer type comes in?

A

“plain” int, signed int and unsigned int

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

In what sizes does integers come?

A

short, “plain” int, long int, long long int

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

What are plain ints always?

A

Signed

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

Name the integer literals?

A

Decimal, Octal, Hexadecimal

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

How many floating point types are there?

A

float (single precision), double (double precision), and long double (extended precision)

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

What is floating point literal by default?

A

Double

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

When is the void type used?

A

When we want to specify that a function doesn’t return a value. As a base type for pointers to objects of unknown type.

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

How many declarations or definitions can there be?

A

There can be only one definition and many declarations.

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

What are the five parts of a declaration?

A
  1. Optional prefix specifiers (e.g., static or virtual)
  2. A base type (e.g., vector or const int)
  3. A declarator optionally including a name (e.g., p[7], n)
  4. Optional suffix function specifiers (e.g., const or noexcept)
    1. An optional initializer or a function body (e.g., ={7,5,3} or {return x;})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
108
Q

For which constructs is the definitions’s value permanent?

A

types, aliases, templates, functions, and constants

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

Except for which constructs, a declaration is terminated by a semicolon?

A

Except for function and namespace definition.

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

What are the rules for C++ naming?

A
  1. A name consists of sequence of letters and digits.
  2. The first character must be a letter
  3. _ underscore is considered a letter
  4. Keywords cannot be used as names (e.g., class, struct…)
  5. Non local names starting with a an underscore are reserved
  6. Names starting with double underscore or underscore followed by uppercase are reserved
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
111
Q

What do we mean by scope?

A

A declaration introduces a name into a scope; that is, a name can only be used in a specific part of the program text.

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

What is a block?

A

A block is a section of code delimited by {} pair.

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

What is a local name?

A

A name declared in a function or lambda.

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

What is class scope?

A

A name is called a member name ( or class member name) if it is defined in a class outside any function, class, enum class, or other namespace.

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

What is namespace scope?

A

A name is called a namespace member name if it is defined in a namespace outside any function, lambda, class, enum class, or other namespace

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

What is global scope?

A

A name is called a global name if it is defined outside any function, class, enum class, or namespace.

The scope of a global name extends from the point of declaration to the end of the file in which its declaration occurs.

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

What is statement scope?

A

A name is in statement scope if it is defined within the () part of for-, while-, if-, or switch- statement. All names in statement scope are local names.

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

What is function scope?

A

A label is in scope from its point of declaration until the end of the function.

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

What is shadowing?

A

The rule is that a declaration of a name in a block can hide a declaration in an enclosing block or a global name.

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

How does a hidden global name can be accessed?

A

With :: (ex. ::x = 2)

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

What is an initializer?

A

An initializer determines the initial value of an object.

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

Name the four syntactic styles of initialization with a variable of type ‘X’ with a value ‘v’.

A
  1. X a1 {v};
  2. X a2 = {v};
  3. X a3 = v;
  4. X a4(v);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
123
Q

When does the curly-brackets (“{}”) initialization appear in C++?

A

It appears in C++11

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

Which is the recommended syntax for initialization?

A

The recommended syntax is using curly bracket initialization (“{}”).

125
Q

Why is the list initialization recommended?

A

It is less error-prone.

126
Q

Why is list initialization less error-prone?

A
  1. Doesn’t allow implicit narrowing
  2. It doesn’t allow truncation
127
Q

During conversion of one type to another what is narrowing?

A

Narrowing is converting a larger type to a smaller one.

128
Q

During conversion of one type to another what is truncation?

A

When we truncate some of the information (e.g. converting float-point type to integer type).

129
Q

Why is conversion from int to float considered narrowing?

A

Because floating-point representation is an approximation and has less precision.

130
Q

When should we be careful when we use list initialization?

A
  1. When using auto the type will be deduced to be std::initializer_list rather than only T.

Example: auto integer {5} → is going to be equal to std::initializer_list rather than int

  1. When trying to call a specific constructor version when using initializer list the overload that receceives std::initializer_list as argument is preferred.

Example: vector v1 {99} // creates a vector with one element ‘99’

vector v2 (99) // creates a vector with size 99

131
Q

Why is the empty initializer list (“{}”) used?

A

Is used to indicate that default value is required.

132
Q

Which types have default value and what is it?

A
  1. Integral types → suitable representation of 0
  2. Pointers → nullptr
  3. User-defined types → determined by the type’s constructors
133
Q

Which is the only case that an uninitialized variable is useful?

A

When we declare a buffer. Because we can suffer a performance hit.

134
Q

Which variables are implicitly initialized with the initializer list (“{}”)?

A

global, namespace, local static, static member

135
Q

Which variables are not implicitly initialized with the initializer list?

A

Local variables and objects created on the free store unless they are user-defined types with default constructors.

136
Q

When do we use auto?

A

When we want to deduce the type of an object from its initializer.

137
Q

When do we use decltype(expr))?

A

When we want to deduce something that is not a simple initializer, such as the return type of a function or the type of a class member.

138
Q

Can we decorate auto with specifiers?

A

Yes, we can decorate it with &,*, const etc.

139
Q

What are auto and decltype(auto) also called?

A

Placeholder type specifiers.

140
Q

What is bad to do with uninitialized variables?

A

Reading from it is undefined behavior.

141
Q

What is the simplest and most fundamental notion of “object”?

A

An object is a contiguous region of storage.

142
Q

What is the definition of an lvalue?

A

An lvalue is an expression that refers to an object.

143
Q

What was “lvalue” originally coined to mean?

A

Something that can be left on the left-hand side of an assignment.

144
Q

What is an lvalue not declared const called?

A

Modifiable lvalue.

145
Q

What are the two properties that matter for an object when it comes to addressing, copying, and moving?

A

Has identity, Is movable

146
Q

What does it mean for an object to have an identity?

A

The program has the name of, a pointer to, or reference to the object so that is possible to refer to it. Object resides at specific address in memory.

147
Q

What does it mean for an object to be movable?

A

The object may be moved from (i.e. we are allowed to move its value to another location and leave the value in a valid but unspecified state).

148
Q

What properties does lvalue have?

A

It’s identifiable but not movable.

149
Q

What properties does glvalue (generalized lvalue) have?

A

It’s identifiable.

150
Q

What properties does xvalue (expert only lvalue) have?

A

It’s identifiable and movable.

151
Q

What properties does rvalue have?

A

It’s movable.

152
Q

What properties does prvalue(pure rvalue) have?

A

Its not identifiable but its movable.

153
Q

When does the life of an object begin and end?

A

When the constructor completes the object starts living and when the destructor starts the object dies.

154
Q

What are the classifications of objects based on their lifetime?

A

Automatic, Static, Free store, Temporary objects, Thread local.

155
Q

What is an automatic object?

A

In a function when a definition is encountered the object is created and destroyed when its name goes out of scope. Such objects are allocated on the stack.

156
Q

What is a static object?

A

Objects declared in global or namespace scope and statics declared in functions or classes are created and initialized once and live until the program terminates. A static object has the same address throughout the lifetime of the program. They are shared among threads.

157
Q

What is a free store object?

A

Using new and delete, we can create objects whose lifetime we control.

158
Q

What is a temporary object?

A

Their lifetime is determined by their use. If they are bound to a reference their lifetime is that of the reference. Otherwise, they live until the end of the full expression.

159
Q

What is a thread-local object?

A

Objects are created when the thread is and destroyed when their thread is.

160
Q

What is a thread-local object?

A

Objects are created when the thread is and destroyed when their thread is.

161
Q

Why we would want to use type alias?

A
  1. The original name is too long, complicated, or ugly
  2. A programming technique requires different types to have the same name in a context
  3. A specific types is mentioned in one place to simplify maintenance
162
Q

What do you need to know to access an object?

A

It’s address and type.

163
Q

What is the fundamental operation of the pointer?

A

Dereferencing (indirection)

164
Q

What is dereferencing (indirection)?

A

Referring to the object pointed to by the pointer.

165
Q

What is void*?

A

“pointer to an object of unknown type”

166
Q

Which types of objects can be assigned to void*?

A

Research!

167
Q

What operation can you do on void*?

A

Comparison(Equality and Inequality) and can explicitly convert to another type.

168
Q

What is permitted but unsafe to do with void*?

A

Converting it to a type that differs from the type of the object pointed to.

169
Q

What does nullptr represent?

A

A nullptr is a literal, that is, a pointer that doesn’t point to an object.

170
Q

What can nullptr be assigned to?

A

To any pointer type, but not to other built-in types.

171
Q

What are the advantages of nullptr over NULL?

A

Makes code mode readable and avoids potential confusion when a function is overloaded to accept either pointer or an integer.

172
Q

What the array bound must be?

A

A constant expression.

173
Q

How many characters does a string literal have?

A

One more than it appears because it is terminated by the null character implcitly.

174
Q

What is the type of string literal?

A

“array of appropriate number of const characters”

175
Q

Can you return a string literal from a function?

A

Yes, because it’s allocated on the stack.

176
Q

What is a raw string literal?

A

A raw string literal is a string literal where a backlash is just a backslash (and a double quote is just a double quote)

177
Q

What is the syntax for raw string literal?

A

R”(…)”

178
Q

How are pointers and arrays related?

A

The name of the array can be used as a pointer to its initial element.

179
Q

Taking a pointer to the element one beyond the end of an array is guaranteed to work. Taking pointer before the initial element or beyond one-past-the-last element is undefined.

A
180
Q

In what cases object don’t have their initial value changed?

A
  1. Symbolic constants lead to more maintainable code than using literals directly in code
  2. Many pointers are often read through but never written through
  3. Most function parameters are read but not written to
181
Q

How many types of const pointer are there?

A

Two, one is to say that the pointer can’t change and the other is that data pointed to can’t change.

Example: const char* x (Can’t modify data) and char* const y (Can’t modify pointer)

182
Q

What are the rules for const object and pointer?

A

Only const T* can point to const T object.

183
Q

What are the benefits of using a pointer?

A

Pointer allows us to pass potentially large amounts of data around at a low cost.

184
Q

How is using the name of an object different than using a pointer?

A
  1. Different syntax, *p instead of obj and p→m rather than obj.
  2. We can make a pointer point to different objects at different times.
    1. We must be careful when using a pointer than using an object directly; a pointer may be nullptr or point to a different object than excepted.
185
Q

Why do we use lvalue references?

A

To refer to objects whose value we want to change.

186
Q

Why do we use const references?

A

To refer to objects whose value we do not want to change.

187
Q

Why do we use rvalue references?

A

To refer to objects whose value we don’t need to preserve after we have used it (e.g. temporary)

188
Q

What does the notation X& mean?

A

Reference to X.

189
Q

What is X& also called?

A

lvalue reference because it’s used for reference to lvalue

190
Q

What is a must for a reference?

A

It must be initialized at the point of its declaration.

191
Q

How do operators operate on a reference?

A

They don’t operate on the reference but on the variable the reference refers to.

192
Q

Can we reinitialize a reference?

A

No

193
Q

Can we have a pointer to reference?

A

No, we can have a pointer to the object the reference referrs to.

194
Q

Can we define an array of references?

A

No.

195
Q

Is a reference an object?

A

No

196
Q

What must the initializer for type T& be?

A

Must be an lvalue of type T.

197
Q

What must the initializer for const T& be?

A

It doesn’t have to be an lvalue or even of type T.

198
Q

What happens when the initializer for const& T is not an lvalue or of type T?

A
  1. First, implicit type conversion to T is applied if necessary
  2. Then the resulting values are placed in a temporary variable of type T
  3. Finally, the temporary variable is used as the value of the initializer
199
Q

How long does a temporary create to hold a reference initializer persist?

A

Till the end of its reference scope.

200
Q

What are the semantics of function parameters plain references and const references?

A

They are the same as those of initialization.

201
Q

Is passing plain references to functions good practice and why?

A

No, it’s not it’s not clearly obvious that the function that receives a plain reference modifies the argument so it’s better to return it as result.

202
Q

What is copy elision?

A

Compilers omit copy/move c-tor in certain cases resulting in a zero-cost pass by value semantics.

203
Q

What is RVO and give an example?

A

RVO - return value optimization when returning a prvalue it’s guaranteed the compiler to do copy elision

A get() { return A(); }

A a {get()};

Here copy elision applies no copy or move constructor is going to be applied.

204
Q

What is NRVO and give an example?

A

NRVO - Named return value optimization

205
Q

What kind of object is an rvalue reference?

A

Rvalue reference refers to temporary object, which the user can modify, assuming that that the object will never be used again.

206
Q

Why do we need to know if a reference refers to a temporary?

A

Because we can turn expensive copy into cheap move operation.

207
Q

What can rvalue reference bind to?

A

To an rvalue but not an lvalue.

208
Q

What does && declarator mean?

A

“rvalue reference”

209
Q

Is there such thing as const rvalue reference?

A

Yes but we don’t use it.

210
Q

What does std::move do?

A

It does static cast to T&&.

211
Q

What is reference collapse?

A

Reference to reference can happen only as a result of an alias or template type argument. And the lvalue reference always wins.

212
Q

Pointers versus Reference (What are the strenghts and weaknesses)

A
  1. If you need to change which object to refer to, use a PTR
  2. Conversely, if you want to ensure that a name always refers to the same object, use a REF
  3. If you want to use a user-defined operator on something that refers to an object, use a REF
  4. If you want a collection of something that refers to an object, use a PTR
    1. If you need a notion of “no value”, use a PTR
213
Q

What is a struct?

A

Sequence of elements of arbitrary types.

214
Q

What’s the difference between struct and array?

A

An array is an aggregate of elements of the same type. A struct is an aggregate of elements of arbitrary types.

215
Q

How does a struct hold its members?

A

In the order they are declared.

216
Q

What is the size of a struct?

A

The size of a struct object is not necessarily the sum of the sizes of its members. Because of alignment..

217
Q

When can we use a struct name?

A

Whenever the usage doesn’t require knowing the size of the struct we can use it as an incomplete type.

218
Q

Can you combine structures and arrays?

A

Yes, you have an array of structures and a structure containing an array.

219
Q

What is the main advantage of stl array compared to built-in array?

A

That it is proper object type (has an assignment, etc.) and does not implicitly convert to a pointer to an individual element.

220
Q

What is the disadvantage of stl array compared to built-in array?

A

We can’t deduce the number of elements from the length of the initializer.

221
Q

What is the structure type equivalence?

A

1.Two structs are different types even when they have the same members.

222
Q

What is plain old data?

A

A contiguous sequence of bytes in memory.

223
Q

What is the reason to treat an object as plain old data?

A

To move objects around in the most efficient way the hardware is capable of.

Example: Calling copy constructor 100 times is unlikely to be faster than std::memcpy

224
Q

What a POD object must be?

A
  1. A standard layout type
  2. A trivially copyable type
  3. A type with a trivial default constructor
225
Q

What is a trivial type?

A

A type with trivial default constructor and trivial copy and move operations.

226
Q

What is a trivial default constructor?

A

It is trivial if it does not need to do any work

  1. If it is not user-defined
  2. TODO EXPAND
227
Q

When does a type have a standard memory layout?

A

It’s standard you unless it has:

  1. Has non-static member or base that is not standard layout
  2. Has a virtual function
  3. Has a virtual base
  4. Has a member that is a reference
  5. Has multiple access specifiers for non-static data members
  6. Prevents important layout optimizations
  • By having non-static data members in more than one base class or in both the derivated and the base
  • By having a base class of the same type as the first non-static data member

Basically, a standard layout is one that has a layout with an obvious equivalent in C

228
Q

When is a type trivially copyable?

A

Unless it has a non-trivial copy, move, or destructor.

Informally if it can be implemented as a bitwise copy.

229
Q

When is a copy, move or destructor non trivial?

A
  1. It is user-defined
  2. Its class has a virtual function
  3. Its class has a virtual base
    1. Its class has a base or a member that is not trivial
230
Q

Are built-in types trivially copyable and have a standard layout.

A

Yes

231
Q

What characteristics do a array of trivially copyable objects and an array of standard layout objects

A

They are also trivially copyable or have a standard layout.

232
Q

What is a field (bit-field)?

A

You can specify in a struct how many bits each member should be.

233
Q

What is the advantage of using a field?

A

To save data space.

234
Q

What is the disadvantage of using a field?

A
  1. Although the data space is less the resulting binary is bigger
  2. Accessing fewer bits using a bit field is much slower than accessing chars or ints
235
Q

What is the restriction of the type for a bit field?

A

It must be an integral or enumeration type.

236
Q

What is a union?

A

A struct where all the members are allocated at the same address.

237
Q

What is the size of a union?

A

The size of the largest member.

238
Q

How many values can a union object hold?

A

One value at a time.

239
Q

Should you use unions and why?

A

They do provide some performance improvements but in general, you should avoid them because they are error prone.

240
Q

Is a union a type conversion?

A

A union should not be used as type conversion. If you want to use type conversion use the explicit casts. Because the compiler can check some inconsistencies that way.

241
Q

What’s the difference between a union and a class?

A
  1. A union cannot have virtual functions
  2. A union cannot have members of reference type
  3. A union cannot have base classes and cannot be used as one
  4. If a union has a member of a class type with defined default, copy or move ctor or dtor, then that special operation is deleted for the union.
  5. At most one member can have an in-class initializer
242
Q

What’s the difference between a union and a class?

A
  1. A union cannot have virtual functions
  2. A union cannot have members of reference type
  3. A union cannot have base classes and cannot be used as one
  4. If a union has a member of a class type with defined default, copy or move ctor or dtor, then that special operation is deleted for the union.
  5. At most one member can have an in-class initializer
243
Q

What is an anonymous union?

A

A union that doesn’t have a name and can’t be instantiated. We can use anonymous’s union members without mentioning the object name.

244
Q

What is a tagged or discriminated union?

A

A union that is controlled through tags (different enumerators).

245
Q

What is enumeration?

A

A type that can hold a set of integer values specified by the user.

246
Q

What are the members of the enumeration called?

A

Enumerators.

247
Q

How many kinds of enumerations are there and which?

A

2 → enum classes and plain enums

248
Q

What is enum class?

A

Enumeration where the enumerators are local to the enum and their values do not implicitly convert to other types (scoped and strongly typed).

249
Q

What is plain enum?

A

Enumeration where its enumerators are in the same scope as the enum and their values implicitly convert to integers.

250
Q

What is the underlying type of enumerator?

A

An enumeration is represented by some integer type and each enumerator by some integer value. That integer type we call the underlying type.

251
Q

What is the default underlying type of an enumeration?

A

Int

252
Q

By default how are enumerator values assigned?

A

Increasing from 0

253
Q

What can an enumerator be assigned with?

A

By a constant expression of integral type.

254
Q

Can you forward declare an enum?

A

Only if you specify its underlying type.

255
Q

How is the underlying type of plain enum determined?

A

Non negative [0:2^k-1] (2^k smallest power of two for which all enumerators are in range)

Negative [-2^k:2^k-1]

256
Q

What are the rules for explicit conversions of a plain enum?

A

Because the underlying type is unknown going over the range is undefined.

257
Q

What is the usage of an unnamed plain enum?

A

We use it when all we need is a set of integer constants rather than a type to use for variables.

258
Q

Is a declaration a statement?

A

Yes

259
Q

When an expression becomes a statement?

A

When you add semicolon at its end.

260
Q

What does a statement specify?

A

Order of execution.

261
Q

What is the difference between an expression and a statement?

A

A statement does not have a value.

262
Q

What are the main categories of statements?

A

statement, selection-statement, iteration-statement, statement-list, condition, handler-list, handler, empty statement.

263
Q

What does the category statement contain?

A
  1. declaration
  2. expressionopt;
  3. { statement_listopt }
  4. try { statement-listopt }
  5. case constant-expression : statement
  6. default : statement
  7. break ;
  8. continue;
  9. return expressionopt ;
  10. goto identifier ;
  11. identifier : statement
  12. selection-statemenet
  13. iteration-statement
264
Q

What does the category of selection-statement contain?

A

if (condition) statement

if (condition) statement else statement

switch (condition) statement

265
Q

What does the category of iteration-statement contain?

A

while ( condition ) statement

do statement while ( expression ) ;

for ( for-init-statement conditionopt ; expressionopt) statement

for ( for-init-declaration : expression ) statement

266
Q

What does the category of statement list contain?

A

statement statement-listopt

267
Q

What does the category of condition contain?

A
  1. expression
  2. type specifier declarator = expression
      1. type specifier declarator { expression }
268
Q

What does the category of handler-list contain?

A

handler handler-listopt

269
Q

What does the category of handler contain?

A

catch (exception-declaration) { statement-listopt }

270
Q

What statement is a single semicolon?

A

Empty statement

271
Q

What is an empty statement?

A

A single semicolon

272
Q

How is an empty or not sequence of statements within “curly braces” called?

A

Block statement or compound statement

273
Q

When does a name declared inside a block goes out of scope?

A

At the end of the block.

274
Q

Is a declaration also a statement and why?

A

Yes. The reason for allowing declarations wherever a statement can be used is to enable the programmer to minimize the errors caused by uninitialized variables and to allow better locality in code.

275
Q

When a variable is declared when its initializer is executed?

A

Whenever the thread of control passes through the declaration.

276
Q

What can a condition be in a statement?

A

Either an expression or a declaration.

277
Q

Which logical operators are most used in conditions?

A

&&, || and !

278
Q

Whats a specific for && and || operators?

A

They won’t evaluate their second argument unless doing so is necessary.

279
Q

For choosing between two alternatives, which conditional expression is a more direct expression of intent than if-statement?

A

Ternary operator, (a>b)?a:b

280
Q

Can a variable declared inside an if block be used in another branch of the if statement?

A

No

281
Q

What a branch of an if-statement cannot be?

A

It cannot be just a declaration.

282
Q

What a switch statement?

A

A statement that selects among a set of alternatives.

283
Q

What must the expressions in the case labels in a switch statement be?

A

Constant expression or integral or enumeration type.

284
Q

How many times can a value be used in a case label?

A

Not more than once.

285
Q

Can you use if statement in the place of switch and what are the drawbacks?

A

The nature of the operation (testing a single value against a set of constants) is explicit. That makes switch easier to read. It typically allows better generation of code, a jump table can be used.

286
Q

What you must be careful when using switch?

A

A case of switch must be terminated unless you want to carry on to the next case.

287
Q

When should a switch statement have a default?

A

One use is for the default to handle the most common case other is to let the default case catch errors.

288
Q

When should a switch statement not have a default?

A

If switch is intended to have one case for each enumerator of an enumeration.

289
Q

What we must do if we want a variable inside a case label and why?

A

We must enclose it inside a block. To ensure no compiler warnings and use of uninitialized variables.

290
Q

What is a good thing to do when we want to avoid accidental misuse of a variable?

A

To introduce the variable into the smallest scope possible. Meaning to declare it when you can define it with a proper value.

291
Q

What is a good technique for declaring a variable inside the smallest code possible?

A

When using a select statement you can declare the variable inside the condition part and use it in all of its branches.

292
Q

What a declaration in a condition must do?

A

Declare and initialize a single variable or const.

293
Q

What the for-init-statement must be?

A

Either a declaration or an expression statement.

294
Q

What does range for statement give to the programmer?

A

Gives the programmer access to each element of a range.

295
Q

The expression after the colon in range for statement must denote?

A

A sequence (range). That is, it must yield a value for which we can call v.begin() and v.end() or begin(v) and end(v) to obtain iterators.

296
Q

For range for statement to work, what does the compiler look for?

A
  1. The compiler first looks for member being and end and tries to use those.
    1. Otherwise looks for begin/end pair in the enclosing scope.
297
Q

What’s the equivalent for statement of for ranged statement?

A

for (auto p = begin(v); p ≠ end(v); ++p)

298
Q

If you need to modify an element in a range-for loop what do you do?

A

You make the element variable a reference.

299
Q

When using range-for loop what is good to do for large elements?

A

Get them by const reference.

300
Q

What does the general for-statement give more than range-for statement?

A

It provides greater control of the iteration.

301
Q

What does the general for loop statement include?

A

The loop variable, the termination condition, and the expression that updates the loop variable.

302
Q

What happens when the condition of an iteration statement is omitted?

A

The loop will not terminate unless user explicitly exits it by break, return, goto, throw or exit().

303
Q

What does break do?

A

Breaks out of the nearest enclosing switch-statement or iteration-statement.

304
Q

What does continue do?

A

A continue skips the rest of the body of an iteration-statement. After continue the increment part of the loop is executed.Followed by the loop condition.

305
Q

What should comments state?

A

Intent

306
Q

What is an infix operator?

A

Placement of operators between two operands.

307
Q

What is a parser?

A

A program that does parsing is analyzing (breaking to smaller parts) a string of symbols.

308
Q

What is unary operator?

A

Operator that has only one operand.

309
Q

What is binary operator?

A

Operator that has two operands.