C++ Primer - Chapter 2 (Variables & Basic Types) Flashcards

1
Q

True or false? When we define a const with the same name in multiple files, it is as if we had written definitions for separate variables in each file.

A

True.

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

What is a “reference”?

A

A compound type that “refers to” another type.

A reference defines an alternative name for an object.

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

Name the primitive data types in C++.

A

The arithmetic types (i.e. boolean value, characters, integers, and floating-point numbers) and the special type, void.

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

What is wrong with the following code?
int i=0;
int *p=i;

A
int i=0;
int *p=i;
// Error; an integral value cannot be assigned to a pointer.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
Is this code legal? If not, why not?
const double pi = 3.14;
double *ptr = pi;
const double *cptr = π
*cptr = 42;
A

The code is not legal.

const double pi = 3.14; 
double *ptr = pi;
// Error, as ptr is a plain pointer.
const double *cptr = π
// Okay, as cptr can point to a double that is const.
*cptr = 42;
// Error, as we cannot assign to *cptr.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Are arithmetic expressions involving a signed and unsigned integer typically cast to a signed or unsigned integer?

Further, what is the result of i+u in the following?
int i=-42;
unsigned u=10;

A

Unsigned.

The sum i+u yields (2^32)-32. Note, that i is converted to an unsigned before the expression is evaluated.

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

A ________ is a name that is a synonym for another type.

A

A type alias is a name that is a synonym for another type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Explain each of the following definitions. Indicate whether any are illegal and, if so, why.
int i = 0;
(a) double* dp = &i; 
(b) int *ip = i; 
(c) int *p = &i;
A
int i = 0;
(a) double* dp = &i; 
// Invalid, the types do not match.
(b) int *ip = i; 
// Invalid, cannot assign an integral value to a pointer..
(c) int *p = &i;
// Valid, the types match.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What form does the declarator of a reference take?

A

We define a reference type by writing a declarator of the form &d, where d is the name being declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
Which of the following initializations are legal? Explain why.
int i=0;
(a) const int *p1 = &i; 
(b) const int &const r2;
(c) const int i2 = i, &r = i;
A
int i=0;
(a) const int *p1 = &i; 
// Legal, but p1 can't be used to change the value of i.
(b) const int &const r2;
// Illegal; a reference is not an object, so it can't be const, and it must be initialised.
(c) const int i2 = i, &r = i;
// Legal if i is an int or const int.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can you define a reference to a reference?

A

No, references are not objects so you cannot create a reference to a reference.

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

What is the result of:
int *ip1, *ip2;
double dp, *dp2;

A

Note that the * must be repeated for each pointer variable:

int *ip1, *ip2;
Both ip1 and ip2 are pointers to int.

double dp, *dp2;
Here, dp2 is a pointer to double and dp is a double.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
Which, if any, of the following assignments are invalid? If they are valid, explain what they do.
int i = 0, &r1 = i; 
double d = 0, &r2 = d;
(a) r2 = 3.14159; 
(b) r2 = r1;
(c) i = r2; 
(d) r1 = d;
A
int i = 0, &r1 = i; 
double d = 0, &r2 = d;
(a) r2 = 3.14159; 
// Valid, and also changes the value of d as r2 is bound to d.
(b) r2 = r1;
// Valid, the value of r2 and d are changed to 0.
(c) i = r2; 
// Valid.
(d) r1 = d;
// Valid, i and r1 will have value 0.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a nonprintable character? Give an example.

A

A character with no visible representation, such as a control character, a backspace, newline, and so on.

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

What can constexpr references or pointers be initialised with?

A

A constexpr pointer can be initialised with the nullptr literal or literal 0.

A constexpr pointer or reference can also point to, or bind to, an object that remains at a fixed address.

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

True or false? (a) and (b) below are equivalent.
typedef char *pstring;
(a) const pstring cstr = 0;
(b) const char *cstr = 0;

A

False.

typedef char *pstring;
(a) const pstring cstr = 0; 
(b) const char *cstr = 0;
// In (a), because of the typedef, when pstring is used in a declaration, the base type is a pointer to char. 
// In (b) the base type is a char and * is part of the declarator.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a “compound type”? Give two examples of a compound type.

A

A type that is defined in terms of another type. Examples include references and pointers.

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

How can the type of a integer literal be specified? Is this prefixed or suffixed?

A

u or U (unsigned)
l or L (long)
ll or LL (long long)

The specification is applied as a suffix.

N.B.: Use capital letters for best practice, e.g. ‘l’ can be mistaken for a ‘1’.

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

What is a type alias?

A

A synonym for another type.

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

Is the following program legal? If so, what values are printed?
int i = 100, sum = 0;
for (int i = 0; i != 10; ++i)
{
sum += i;
}
std::cout &laquo_space;i &laquo_space;” “ &laquo_space;sum &laquo_space;std::endl;

A

The program:

int i = 100, sum = 0;
for (int i = 0; i != 10; ++i)
{
sum += i;
}
std::cout << i << " " << sum << std::endl;

is legal, and the output is: 100 45.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
Name the problems, if any, of the following code:
double dval;
double *pd = &amp;dval; 
double *pd2 = pd; 
int *pi = pd;
pi = &amp;dval;
A

Note that the types of the pointer and the object to which it points must match:

double dval;
double *pd = &amp;dval;
// This is okay as the initializer is the address of a double.
double *pd2 = pd; 
// This is okay as the initializer is a pointer to double.
int *pi = pd; 
// This is an error as the types of pi and pd differ.
pi = &amp;dval; 
// This is an error; cannot assign the address of a double to a pointer to int.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What do uninitialised pointers typically use (under most compilers) as an address?

A

Under most compilers, the bits in memory where the pointer resides are used as an address.

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

When is decltype((variable)) a reference type?

A

decltype((variable)) is always a reference type.

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

What is wrong with the following code?

const int a;

A

const variables must have explicit initialisers.

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

What is the difference between the following?
extern int i;
int j;

A
extern int i; 
// declares but does not define i
int j; 
// declares and defines j
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is a “literal”? What is the general syntax for a literal character and a literal string?

A

A value such as a number, a character, or a string of characters; a literal’s value is self-evident.

Literal characters are enclosed in single quotes and literal strings in double quotes.

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

Name the two subcategories of arithmetic types.

A

Integral types and floating-point types.

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

How many bits does a char, wchar, char16_t and char32_t take up?

A

char = 8-bit
wchar (wide) = 16-bit
char16_t (fixed width) = 16-bit
char32_t (fixed width) = 32-bit

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

What is an “object” defined as in C++ Primer?

A

Something that occupies a region of memory and has a type.

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

What happens when an out-of-range value is assigned to a signed type?

Further, what is the value of “ofr_char” in the following expression?
signed char ofr_char=256;

A

The result is undefined behaviour (UB); the program may work, crash, or produce garbage values.

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

What does a definition do?

A

It makes a name or identifier known to the program and allocates storage for the variable. It may also assign an initial value (initialisation).

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

What is the minimum size of a float, double and long double in bits?

A

float = 32-bit
double = 64-bit
long double = 64-bit (but can be 96- or 128-bit)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q
Describe the following code:
int i = 42;
int *p; 
int *&amp;r = p; 
r = &amp;i;
*r = 0;
A
int i = 42;
int *p; 
// The variable p is a pointer to int.
int *&amp;r = p; 
// Here, r is a reference to the pointer p.
r = &amp;i; 
// The reference r refers to a pointer; assigning &amp;i to r makes p point to i.
*r = 0; 
// Dereferencing r yields i (the object to which p points) and changes i to 0.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

True or false? Pointers and references to const are top-level consts.

A

False, they’re low-level consts.

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

Can the type of a reference and the object to which it refers differ?

A

In general, no.

However, there are two particular cases where they can:

(1) When the const specifier is applied, and;
(2) When the object is of a derived class type.

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

How many octal digits can be used in an escape sequence?

A

Up to 3.

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

What can a plain reference not be bound to?

A

A reference cannot be bound to a literal or the result of a general expression.

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

What is a “declarator”?

A

The part of a declaration that includes the name being defined and an optional type modifier.

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

How can a variable be declared but not defined?

A

By adding the extern keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q
Given the variable definitions:
int i, *p1;
const int ic=1; 
const int *const p3=&amp;ic;
Which of the following assignments are legal? Explain why.
(a) i = ic; 
(b) p1 = p3;
(c) p1 = &amp;ic;
A
Define the variables:
int i, *p1;
const int ic=1; 
const int *const p3=&amp;ic;

(a) i = ic;
Legal, i is a plain int and copying doesn’t affect a const object.
(b) p1 = p3;
Illegal, otherwise p1 would be able to change the data pointed to by p3.
(c) p1 = &ic;
Illegal; p1 must be a pointer to const.

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

What is a “temporary” variable?

A

An unnamed object created by the compiler while evaluating an expression.

A temporary exists until the end of the largest expression that encloses the expression for which it was created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q
What is the problem with the following code?
int ci=1;
auto &amp;g = ci;
auto &amp;h = 42; 
const auto &amp;j = 42;
A
const int ci=1;
auto &amp;g = ci; 
// This is fine; top-level consts in the initializer are not ignored.
auto &amp;h = 42; 
// This is an error; we can’t bind a plain reference to a literal.
const auto &amp;j = 42;
// This is okay; we can bind a const reference to a literal.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q
Will the compiler complain about the following functions being the same? Why?
int func(int i) {...}
int func(const int i) {...}
A

Yes, because top-level consts are ignored when copying objects (to initialise the function parameters).

Notice here that the values are not passed by reference.

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

Do pointers need to be initialised?

A

No.

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

What type of const is not ignored when copying?

A

Low-level const.

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

What is wrong with the following code?

int &refVal2;

A

A reference must be initialised.

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

What is the term given for the scope that is outside all other scopes?

A

Global scope.

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

Determine the type of each of the following literals. Explain the differences among the literals in each of the four examples:

(a) ’a’, L’a’, “a”, L”a”
(b) 10, 10u, 10L, 10uL, 012, 0xC
(c) 3.14, 3.14f, 3.14L
(d) 10, 10u, 10., 10e-2

A

(a) char, wchar, string (‘a’+’\0’) and a wide character array of size 2.
(b) int, unsigned, long int, unsigned long, int (=10), int (=12)
(c) double, float, long double
(d) int, unsigned, double, double.

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

What happens when an initialiser is provided to a variable defined as extern outside a function? What about when the variable is defined as extern inside a function?

A

When a variable defined as extern is initialised outside a function, the extern keyword is overridden. When the variable is defined as extern inside a function, it is an error to provide an initialiser.

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

What does it mean for an object to be top-level const?

A

That an object itself is const and cannot be altered.

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

What digit does 0xe or 0xE represent?

A

14.

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

What does the following code produce?
unsigned u = 10, u2 = 42;
std::cout &laquo_space;u2 - u &laquo_space;std::endl;
std::cout &laquo_space;u - u2 &laquo_space;std::endl;

A
u2-u = 32
u-u2 = 2^32 - 32
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

When can a const variable use the same operations as a nonconst variable?

A

When the operation does not alter the variable.

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

An __________ starts with the keyword using followed by the alias name and an =.

A

An alias declaration starts with the keyword using followed by the alias name and an =.

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

What is the term given for the initialisation of a variable when no explicit initializer is given?

A

Default initialisation.

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

Is it possible to have a const reference? Is it possible to have a const pointer?

A

References are not objects, so they cannot be made const. Pointers, on the other hand, are, so they can be made const.

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

How can we make the compiler deduce the type of a variable for us?

A

Using the “auto” keyword.

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

What type does a have in the following code?
int i = 0, &r = i;
auto a = r;

A

int i = 0, &r = i;
auto a = r;
Here, a is an int (r is an alias for i, which has type int).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q
What does the following code produce?
unsigned u = 10;
int i = 10, i2 = 42;
std::cout << i2 - i << std::endl;
std::cout << i - i2 << std::endl;
std::cout << i - u << std::endl;
std::cout << u - i << std::endl;
A

i2-i = 32
i-i2=-32
i-u=0
u-i=0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q
Given the variable definitions:
const int ic=1, &amp;r = ic; 
const int *const p3=&amp;ic;
int *p1, *const p2=&amp;ic;
Which of the following assignments are legal? Explain why.
(a) p3 = &amp;ic;
(b) p2 = p1; 
(c) ic = *p3;
A
Define the variables:
const int ic=1, ic2=1, &amp;r = ic; 
const int *const p3=&amp;ic;
int *p1, *const p2=&amp;ic;

(a) p3 = &ic2;
// Illegal, p3 is a const pointer and cannot be assigned to after initialisation.
(b) p2 = p1;
// Illegal, p2 is a const pointer and cannot be assigned to after initialisation.
(c) ic = *p3;
// Illegal, ic is a const variable and cannot be assigned to after initialisation.

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

Name up to four levels of scope.

A

Global — names defined outside any other scope.
Class — names defined inside a class.
Namespace — names defined inside a namespace.
Block — names defined inside a block.

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

What is a constant expression?

A

An expression whose value cannot change and one that can be evaluated at compile time.

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

What does a variable definition consist of?

A

A type specifier followed by one or more variable names separated by a comma and terminated with a semicolon.

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

Can a reference be defined without an initialiser?

A

No, there is no way to rebind a reference so it must be initialised.

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

What happens when we define a reference with an initialiser?

A

Instead of copying the initialiser’s value, we bind the reference to its initialiser.

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

What four states can a pointer be in?

A
  1. It can point to an object.
  2. It can point to the location just immediately past the end of an object.
  3. It can be a null pointer, indicating that it is not bound to any object.
  4. It can be invalid; values other than the preceding three are invalid.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

For each of the following declarations indicate whether the object being declared has top-level or low-level const.

(a) const int v2 = 0;
(b) int v1 = v2;
(c) int *p1 = &v1, &r1 = v1;
(d) const int *p2 = &v2, *const p3 = &i, &r2 = v2;

A
(a) const int v2 = 0; 
Top-level const; v2 is a const object.
(b) int v1 = v2;
v1 is nonconst; top-level const is ignored when copying.
(c) int *p1 = &amp;v1, &amp;r1 = v1;
Both p1 and r1 are nonconst.
(d) const int *p2 = &amp;v2, *const p3 = &amp;i, &amp;r2 = v2;
p2 is low-level const, p3 is both low-level and top-level const, and r2 is low-level const.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q
What is the value of j in the following program?
int i = 42;
int main()
{
int i = 100;
int j = i;
}
A

100.

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

Does a constexpr impose a top-level or low-level const?

A

Top-level.

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

What is the difference between ‘a’ and “a”?

A

The compiler appends the null character ‘\0’ to a string literal (so “a” is corresponds to ‘a’ and ‘\0’).

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

How can the type of a floating-point literal be specified? Is this prefixed or suffixed?

A

f or F (float)
l or L (long)

The specification is applied as a suffix.

N.B.: Use capital letters for best practice, e.g. ‘l’ can be mistaken for a ‘1’.

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

Explain the following definitions. For those that are illegal, explain what is wrong and how to correct it.

(a) std::cin&raquo_space; int input_value;
(b) int i = { 3.14 };
(c) double salary = wage = 9999.99;
(d) int i = 3.14;

A
(a) std::cin >> int input_value;
// The right-hand operand of the input operator must be an object. Correction: 
int input_value; 
std::cin >> input_value;
(b) int i = { 3.14 };
// Error; leads to a loss of information.
(c) double salary = wage = 9999.99; 
// Wage has not been declared. Correction:
double wage; 
double salary = wage = 9999.99; 
(d) int i = 3.14;
// Okay, but will lead to a loss of information.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
73
Q

True or false? A literal is a constant expression.

A

True.

74
Q

How many times can be a variable be declared? How many times can be a variable be defined?

A

A variable can be declared many times but it can only be defined once.

75
Q

What is a “word”?

A

The natural unit of integer computation on a given machine.

On a 32-bit machine a word is typically 4 bytes.

76
Q

How can a variable be used in more than one file?

A

A variable can be used in more than one file by declarations that are separate from the variable definition. This involves the use of the extern keyword.

77
Q
In the following code, what types do b, c, d, and e have?
int i = 0;
const int ci = i, &amp;cr = ci;
auto b = ci; 
auto c = cr;
auto d = &amp;i;
auto e = &amp;ci;
A
int i = 0;
const int ci = i, &amp;cr = ci;
auto b = ci; 
// The variable b is an int (top-level const in ci is dropped).
auto c = cr; 
// Here, c is an int (cr is an alias for ci whose const is top-level).
auto d = &amp;i; 
// The variable d is an int* (&amp; of an int object is int*).
auto e = &amp;ci; 
// The variable e is const int* (&amp; of a const object is low-level const).
78
Q

What is the problem with the following code?
auto i = 0, *p = &i;
auto sz = 0, pi = 3.14;

A
auto i = 0, *p = &amp;i; 
// This is okay; i is int and p is a pointer to int.
auto sz = 0, pi = 3.14; 
// This is an error; sz and pi have inconsistent types.
79
Q

What digit does 0xf or 0xF represent?

A

15.

80
Q

Is the following code valid?
double dval = 3.14;
const int &ri = dval;

A

Yes. We can initialise a reference to const from any expression that can be converted to the type of the reference.

N.B. This would not be true with a plain reference.

81
Q

What type of const is ignored when copying?

A

Top-level const.

82
Q

Define “bind”.

A

Associating a name with a given entity so that uses of the name are uses of the underlying entity.

83
Q

Describe the general form of a declaration.

A

Generally, a declaration is a base type followed by a list of declarators.

Each declarator names a variable and gives the variable a type that is related to the base type.

84
Q

When is the following expression valid?

constexpr int sz = size();

A

When size() is a constexpr function.

85
Q

Explain whether each of the following is a declaration or a definition:

(a) extern int ix = 1024;
(b) int iy;
(c) extern int iz;

A

(a) Definition if placed outside a function, error if inside a function.
(b) Declares and defines iy.
(c) Declares but does not define iz.

86
Q

What is the difference between an uninitialised pointer and a null pointer?

A

A null pointer has value 0x0 but an uninitialised pointer may point to anywhere in memory.

87
Q

Can a void pointer be used to operate on the object to which it points?

A

No, as the type must be known but a void pointer only stores the address of an object, not its type.

88
Q

What is a “byte”?

A

The smallest chunk of addressable memory. On most machines a byte is 8 bits.

89
Q

What is an “escape sequence”?

A

An escape sequence is a sequence used to print so-called “nonprintable” characters.

90
Q

What does a variable declaration specify?

A

The type specifier and name (i.e. the identifier).

91
Q

True or false? A reference to const can be used to alter a variable’s value.

A

False.

92
Q

Determine the types and values of each of the following variables.

(a) int* ip, i, &r = i;
(b) int i, ip = 0;
(c) int
ip, ip2;

A
(a) int* ip, i, &amp;r = i; 
// ip is a pointer to an int, i is an int, and r is a reference that refers to the int i. 
// Note that declarator must be provided for each identifier.
(b) int i, *ip = 0; 
// i is an int and ip is a pointer to an int that has been initialised to null.
(c) int* ip, ip2;
// ip is a pointer to an int and ip2 is an int.
93
Q

Can constexpr pointers point to variables defined inside a function? Why?

A

No. Variables defined inside a function ordinarily are not stored at a fixed address.

94
Q

What is the issue with the following code?
int i = 42, p = &i, &r = i;
decltype(r + 0) b;
decltype(
p) c;

A
int i = 42, *p = &amp;i, &amp;r = i;
decltype(r + 0) b; 
// This is okay; addition yields an int; b is an (uninitialised) int.
decltype(*p) c;
// This is an error; c is int&amp; and must be initialised.
95
Q

If an object is “initialised”, what does that mean?

A

It is assigned a value at the moment of its creation.

96
Q

Why must the type of a pointer and the object to which it points match?

A

Because the type of the pointer is used to infer the type of the object that is being pointed to.

97
Q

What is the name of the following syntax used to initialise a variable?
int a={0};
int a{0};

A

List initialisation.

98
Q

Why is the initialisation of p legal but that of lp illegal in the following?
int i = 42;
void *p = &i;
long *lp = &i;

A

The object p is a void pointer so can hold the address of any variable.

The initialisation of lp is illegal because i and *lp do not have the same type; *lp is a long int but i is an int.

99
Q

When a pointer can point to a const object, we refer to that const as a ___________.

A

When a pointer can point to a const object, we refer to that const as a low-level const.

100
Q

Which of the following definitions, if any, are invalid? Why?

(a) int ival = 1.01;
(b) int &rval1 = 1.01;
(c) int &rval2 = ival;
(d) int &rval3;

A

(a) int ival = 1.01;
Valid, but will lead to a loss of information.
(b) int &rval1 = 1.01;
Invalid, a plain reference cannot bind to a temporary or an object of a different type.
(c) int &rval2 = ival;
Valid, as ival is an int.
(d) int &rval3;
Invalid, all references require initialisation.

101
Q

How can we make a variable unchangeable?

A

We can make a variable unchangeable by defining the

variable’s type as const.

102
Q
What is wrong with the following code?
const int ci = 1024;
const int &amp;r1 = ci;
r1 = 42;
int &amp;r2 = ci;
A

const int ci = 1024;
const int &r1 = ci;
r1 = 42;
This is an error as r1 is a reference to const.
int &r2 = ci;
This is not legal as a reference bound to a const variable must also be a reference to const.

103
Q

What does a declaration do?

A

It makes a name or identifier known to the program.

104
Q

What is a “variable”?

A

A named object or reference that a program can manipulate. In C++, variables must be declared before they are used.

105
Q

What is a pointer? How are they similar to and different to a reference?

A

A compound type that “points to” another type.

Like references, pointers are used for indirect access to other objects.

Unlike a reference, a pointer is an object in its own right.

106
Q

What does a compiler do to a const object when the object is initialised using a compile-time constant?

A

It replaces uses of the variable with its corresponding value during compilation.

107
Q

True or false? Variables defined using the auto keyword do not need to be initialised.

A

False, the auto keyword infers the type from the initialiser.

108
Q

True or false? Variables defined inside a function are always stored at fixed locations.

A

False. Variables defined inside a function ordinarily are not stored at a fixed address.

109
Q

What is the default type of a floating-point literal?

A

Double.

110
Q

What does the type of a variable determine?

A
  1. The size and layout of the variable’s memory;
  2. The range of values that can be stored within that memory, and;
  3. The set of operations that can be applied to the variable.
111
Q

How can we avoid including a header multiple times?

A

By using a header guard.

112
Q

What is a “base type”?

A

A type specifier, possibly qualified by const, that precedes the declarators in a declaration. The base type provides the common type on which the declarators in a declaration can build.

113
Q

When can a pointer be dereferenced?

A

When the pointer is valid, i.e. it either points to an object, to the location just immediately past the end of an object, or when it is a null pointer.

114
Q

Which, if any, of the following names are invalid?

(a) int double = 3.14;
(b) int _;
(c) int catch-22;
(d) int 1_or_2 = 1;
(e) double Double = 3.14;

A

(a) Valid.
(b) Valid, as identifiers can begin with an underscore. However, this should not be used in general.
(c) Invalid.
(d) Invalid, identifiers can only begin with letters or an underscore.
(e) Valid but confusing; avoid duplicating built-in type names.

115
Q

What is the result of the following?

long double ld = 3.1415926536;
int a{ld}, b = {ld}; 
int c(ld), d = ld;
A

int a{ld}, b = {ld}; is an error because list initialisation does not allow the loss of information during initialisation.

int c(ld), d = ld; is ok, but the value will be truncated.

116
Q

Can a pointer be used to access an object in either of the following cases:

  1. It points to the location just immediately past the end of an object.
  2. It is a null pointer.
A

No, these are specific cases when an object cannot be accessed through a pointer and will result in undefined behaviour.

117
Q

How & and * are used in declarations and as operators?

A

In declarations, & and * are used to form compound types; a reference and pointer, respectively. In expressions, these same symbols are used to denote an operator; the address-of and dereference operator, respectively.

118
Q

What is an “alias declaration”?

A

A declaration that starts with the “using” keyword followed by an =.

119
Q

Is a char signed or unsigned?

A

It may be either; it depends on your compiler!

120
Q

True or false? decltype ignores top-level const.

A

False.

121
Q
Determine the types deduced in each of the following definitions. 
const int i = 42;
auto j = i; 
const auto &amp;k = i; 
auto *p = &amp;i;
const auto j2 = i, &amp;k2 = i;
A

const int i = 42;
auto j = i;
The variable j is an int (top-level const is ignored).
const auto &k = i;
The variable k has type “const int&” (a reference to const int).
auto p = &i;
The variable p has type “const int
” (i.e. a pointer to const; top-level const cannot be ignored here).
const auto j2 = i, &k2 = i;
The variable j2 has type const int and k2 is a reference to const.

122
Q

Explain the following definitions. Identify any that are illegal.

(a) int i, *const cp;
(b) int *p1, *const p2;
(c) const int ic, &r = ic;
(d) const int *const p3;
(e) const int *p;

A
(a) int i, *const cp; 
// Illegal; cp is a const pointer, so must be initialised.
(b) int *p1, *const p2;
// Illegal; p2 is a const pointer, so must be initialised.
(c) const int ic, &amp;r = ic; 
// Illegal; ic is a const object so it must be initialised.
(d) const int *const p3;
// Illegal; p3 is a const pointer so needs to be initialised.
(e) const int *p;
// Legal; p is a pointer to const and does not need to be initialised.
123
Q

When is decltype(variable) a reference type?

A

decltype(variable) is a reference type only if variable is a reference.

124
Q

Fill in the blank:

A ________ makes a name known to the program

A

A declaration makes a name known to the program.

125
Q

Are literals stored in the largest or smallest type that they can be stored?

A

Smallest.

126
Q

‘\x4d’ and ‘\115’ correspond to ‘M’ so what does the following print?

std::cout &laquo_space;“Hi \x4dO\115!\n”;

A

“Hi MOM!” followed by a newline.

127
Q

What are the built-in arithmetic types?

A

Characters, integers, booleans and floating-point types.

128
Q

Fill in the blanks:

A declaration is a ________ followed by a list of ________.

A

A declaration is a base type followed by a list of declarators.

129
Q

What types are decimal literals placed in?

A

The smallest type of int, long int or long long int in which the value fits.

130
Q

How can a const variable be shared between multiple files if the initialiser is not a constant expression?

A

By using the extern keyword.

131
Q

Fill in the blank:

A ________ is a part of the program in which a name has a particular meaning

A

A scope is a part of the program in which a name has a particular meaning.

132
Q

What is the type deduced when we apply decltype to an expression that is not a variable?

A

We get the type that that expression yields.

133
Q

Is it legal or illegal to have a pointer to a pointer?

A

Legal. A pointer is an object in memory, so like any object it has an address. Therefore, we can store the address of a pointer in another pointer.

134
Q
In the following code, determine the type of each variable and the value each variable has when the code finishes:
int a = 3, b = 4;
decltype(a) c = a;
decltype((b)) d = a;
\++c;
\++d;
A
int a = 3, b = 4;
decltype(a) c = a;
// The variable c has type int.
decltype((b)) d = a;
// The variable d has type int&amp; and is bound to a.
\++c;
// Increments c alone.
\++d;
// Increments d and a (as d is an alias for a).

Final values:
a=4; b=4; c=4; d=4;

135
Q

In what two ways can we define a type alias?

A

With a “typedef” and with the “using” keyword.

136
Q

What does the following program do?
int i = 42;
int *p1 = &i;
*p1 = *p1 * *p1;

A

The program:

int i = 42;
int *p1 = &i;
*p1 = *p1 * *p1;

initialises i to 42 then squares the value by dereferencing the pointer p1; nothing happens to p1 afterwards.

137
Q

How is a variable of built-in type, which is defined outside a function, initialised when no initial value is provided? What about when it is defined inside a function?

A

Variables of a built-in type defined outside a function are default initialised to zero.

Variables of a built-in type defined inside a function are uninitialised.

138
Q

Is it possible to have a pointer to a reference? Is it possible to have a reference to a pointer?

A

A reference is not an object, hence we may not have a pointer to a reference.

However, because a pointer is an object, we can define a reference to a pointer.

139
Q

What operations can be performed with a void pointer?

What are the limitations of a void pointer?

A

We can compare a void pointer to another pointer, pass it to or return it from a function, and assign it to another void* pointer.

We cannot use a void pointer to operate on the object it addresses (as we don’t know that object’s type, and the type determines what operations we can perform on the object).

140
Q
Will the compiler complain about the following functions being the same? Why?
int func(int&amp; i) {...}
int func(const int&amp; i) {...}
A

No. The second function declaration has a reference to const parameter; low-level consts are not ignored when initialising the function parameters from its arguments.

141
Q

What character is appended by the compiler to a string literal?

A

The null character, ‘\0’

142
Q

What does it mean for an object to be low-level const?

A

That the base type of the compound type is const.

143
Q

Define “const”.

A

Type qualifier used to define objects that may not be changed.

144
Q

What floating-point types are there?

A

float, double, long double.

145
Q

Which of the following are legal? For those that are illegal, explain why.

(a) const int buf;
(b) int cnt = 0;
(c) const int sz = cnt;
(d) ++cnt; ++sz;

A
(a) const int buf; 
A const variable must have an explicit initialiser.
(b) int cnt = 0;
Legal.
(c) const int sz = cnt; 
Legal.
(d) ++cnt; ++sz;
cnt can be incremented but sz cannot as it is a const variable.
146
Q

How can the type of a variable be deduced from an expression (and not its initialiser)?

A

Using the “decltype” type specifier.

147
Q

How can the type of a character or character string literal be specified? Is this prefixed or suffixed?

A

u (Unicode, char16_t)
U (Unicode, char32_t)
L (wide character, 16-bit)
u8 (utf-8, char)

The specification is applied as a prefix.

148
Q

How is a top-level const deduced when using the auto keyword?

A

By explicit use of the const keyword.

149
Q

What keyword is used to indicate an expression is const?

A

constexpr.

150
Q

True or false? A variable that is initialised from a const variable does not have to be a const variable.

A

True, as long as the variable being assigned to is not a reference.

151
Q

What digit does 0xa or 0xA represent?

A

10.

152
Q

True or false? A string literal is an array of constant chars.

A

True.

153
Q

How many hexidecimal digits may be used in an escape sequence?

A

All the hex digits following \x are used.

154
Q
What is the issue with the following code?
const int ci = 0, &amp;cj = ci;
decltype(ci) x = 0; 
decltype(cj) y = x;
decltype(cj) z;
A
const int ci = 0, &amp;cj = ci;
decltype(ci) x = 0; 
// This is okay, x has type const int.
decltype(cj) y = x; 
// This is okay, y has type const int&amp; and is bound to x.
decltype(cj) z; 
// This is an error; z is a reference and must be initialised.
155
Q

What digit does 0xc or 0xC represent?

A

12.

156
Q

What types are octal or hexidecimal literals placed in?

A

The smallest type of int, unsigned int, long int, unsigned long, long long int or unsigned long long in which the value fits.

157
Q

What signed integral types are there?

A

short int, int, long int, long long int.

158
Q

What character types are there?

A

char, wchar (wide char), char16_t, char32_t.

159
Q

How is the type of a variable deduced using the “auto” keyword?

A

Using the initialiser.

160
Q
Where is the error in the following?
int i = 42;
const int &amp;r1 = i; 
const int &amp;r2 = 42; 
const int &amp;r3 = r1 * 2; 
int &amp;r4 = r * 2;
A
int i = 42;
const int &amp;r1 = i; 
// This is okay; we can bind a const int&amp; to a plain int object.
const int &amp;r2 = 42;
// This is okay; r1 is a reference to const.
const int &amp;r3 = r1 * 2;
// This is okay; r3 is a reference to const.
int &amp;r4 = r * 2;
// This is an error; can't bind an expression to a nonconst reference.
161
Q

What digit does 0xb or 0xB represent?

A

11.

162
Q

What does “default initialisation” mean?

A

The initialisation of an object when no explicit initialiser is given.

163
Q

What is the result of two string literals separated by a space, tab or newline?

A

They will be concatenated into a single string literal.

164
Q

What, if any, are the differences between the following definitions:

int a=9, b=7;
int a=09, b=07;

A

A 0 preceding a digit corresponds to an octal number, thus 09 does not yield valid syntax. In octal notation, a ‘9’ is represented by ‘011’.

165
Q

What are the values of 20, 024 and 0x14? What notation is value given in?

A
  1. The notations are decimal, octal and hexadecimal, respectively.
166
Q

How must a const object be defined?

A

const objects must be initialised, because there is no way to give them a value after they are defined.

167
Q

What happens when an out-of-range value is assigned to an unsigned type?

Further, what is the value of “ofr_char” in the following expression?
unsigned char ofr_char=-1;

A

The result is the remainder of the value modulo the number of values the target type can hold.

An unsigned char can hold a maximum of 256 values so “ofr_char” has value 255.

168
Q

What digit does 0xd or 0xD represent?

A

13.

169
Q

True or false? A variable definition is a declaration.

A

True.

170
Q

Can decimal literals be negative?

A

No; they are only non-negative. A minus sign is just an operator that acts on an unsigned literal.

171
Q

What can a reference to const be bound to?

A

A nonconst object, a literal, or a more general expression.

172
Q

What form does the declarator of a pointer take?

A

We define a pointer type by writing a declarator of the form *d, where d is the name being defined.

173
Q

Where does the distinction between top-level and low-level const matter?

A

When we copy an object. Top-level consts are ignored; low-level consts are not.

174
Q

When we ask for a reference to an auto-deduced type, are top-level consts in the initialiser ignored?

A

No.

175
Q

True or false? auto ordinarily ignores top-level consts.

A

True.

176
Q

What does the following code print?
int i, &ri = i;
i = 5; ri = 10;
std::cout &laquo_space;i &laquo_space;” “ &laquo_space;ri &laquo_space;std::endl;

A

The output of the program:

int i, &ri = i;
i = 5; ri = 10;
std::cout &laquo_space;i &laquo_space;” “ &laquo_space;ri &laquo_space;std::endl;

will be: 10 10.

177
Q
Is the following code legal?
int errNumb = 0;
int *const curErr = &amp;errNumb;
const double pi = 3.14159;
const double *const pip = pi;
A

The code is indeed legal.

int errNumb = 0;
int *const curErr = &amp;errNumb;
// The const pointer curErr will always point to errNumb.
const double pi = 3.14159;
const double *const pip = pi;
// The object pip is a const pointer to const which means it can point to a const object, the second const simply means that the pointer cannot be changed upon initialisation.
178
Q

How is a pointer dereferenced?

A

Using the dereference operator, i.e. *.

179
Q

How are generalised escape sequences written?

A

Using octal following a backslash, or hexadecimal digits following \x.

180
Q

We use the term ___________ to indicate that the pointer itself is a const.

A

We use the term top-level const to indicate that the pointer itself is a const.

181
Q

What is a void pointer?

A

The type void* is a special pointer type that can hold the address of any object.

A void* pointer holds an address, but the type of the object at that address is unknown.

182
Q

Which of the following initializations are legal? Explain why.

(a) int i = -1, &r = 0;
(b) int *const p2 = &i;
(c) const int i2 = -1, &r = 0;
(d) const int *const p3 = &i2;

A
(a) int i = -1, &amp;r = 0; 
// Illegal; r is a reference which cannot be initialised with a literal (without a const type specifier).
(b) int *const p2 = &amp;i;
// Legal.
(c) const int i2 = -1, &amp;r = 0; 
// Legal; i2 is a const int and r is a reference to const, which can bind to a literal.
(d) const int *const p3 = &amp;i2;
// Legal.