module 1 Flashcards

(34 cards)

1
Q

Write an example of an int named n that assigned the value 2 using the Uniform Initialization Syntax.

A
int n{2};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Rewrite this array initialization using the Uniform Initialization Syntax

int a[] = {1, 2, 3};
A
int a[]{1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is the following a declaration, a definition, or both?

int x;
A

both

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

Is the following a declaration, a definition, or both?

void f(int);
A

Declaration

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

What type does the .size() method return?

A

size_t

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

What standard library must be included in order to use the type size_t?

A
#include <cstddef>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which of the following is the best practice way to change the type of x to an int and store it in n?

// a)
int n = static_cast<int>(x);
// b)
int n = int(x);
// c)
int n = (int)x;
A

option b; function-style “casts”

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

Which of the following is the worst practice way to change the type of x to an int and store it in n?

// a)
int n = static_cast<int>(x);
// b)
int n = int(x);
// c)
int n = (int)x;
A

option c; never use C/Java?C# style casts

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

Declare a variable called p that is a pointer to an int.

A
int* p;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What pointer type can only hold an address, and cannot be dereferenced?

A

void*

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

Given the following code:

#include <iostream>
using namespace std;

int main()
{
    int   i = 7;
    int*  ip = &i;
    int** ipp = &ip;
}

What is **ipp?

A

7; the rvalue of i

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

Given the following code:

#include <iostream>
using namespace std;

int main()
{
    int   i = 7;
    int*  ip = &i;
    int** ipp = &ip;
}

What is *ipp?

A

a pointer to an int; the lvalue of ip

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

Given the following code:

#include <iostream>
using namespace std;

int main()
{
    int   i = 7;
    int*  ip = &i;
    int** ipp = &ip;
}

What is ipp?

A

a pointer to a pointer to an int; the lvalue of ipp

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

Given the following code, which line will throw an error?
~~~
\ given:
const char * p1;
\ a)
*p1 = ‘c’;
\ b)
++p1;
~~~

A

Line a throws an error

const before the asterisk will const the contents

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

Given the following code, which line will throw an error?
~~~
\ given:
char const * p1;
\ a)
*p1 = ‘c’;
\ b)
++p1;
~~~

A

Line a throws an error

const before the asterisk will const the contents

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

Given the following code, which line will throw an error?
~~~
\ given:
char * const p1;
\ a)
*p1 = ‘c’;
\ b)
++p1;
~~~

A

line b will throw an error

const after the asterisk will const the pointer

17
Q

Given the following code, which line will throw an error?
~~~
\ given:
const char * const p1;
\ a)
*p1 = ‘c’;
\ b)
++p1;
~~~

A

Trick question, they will both throw an error

18
Q

Write a void function called inspect() that will take a const void* and the number of bytes as parameters and prints the bytes of the object.

print in hex, with each byte separated by a space

A
#include <iostream>
#include <cstddef>
#include <iomanip>

using namespace std;

void inspect(const void* ptr, size_t num_bytes) {
    const unsigned char* char_ptr = static_cast<const unsigned char*>(ptr);
    cout.setf(ios::hex, ios::basefield);
    for (size_t i = 0; i < num_bytes; ++i) {
        cout << setw(2) << setfill('0') << int(char_ptr[i]) << " ";
    }
}
19
Q

What does the following line do?
cout.setf(ios::hex, ios::basefield);

A

it alters how an int will be output; ios::basefield acts as a mask to clear any previously set base format, and ios::hex will then set the current bitmask to hex so all subsequent int objects will be output with a hexadecimal base

20
Q

What will the following code output?

#include <iostream>

int main()
{
    using namespace std;
    int i = 7;
    int& r = i;
    
    \++r;
    cout << i << endl;
}
A

8

r is another name for the existing local variable i

21
Q

Write a function called swap() that will take two int objects and swap their rvalues.

A
void swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}
22
Q

Write the function called current() that will operate according to the following code:
~~~
#include <iostream></iostream>

int a[4] = {0,1,2,3};
int index = 0;
int main()
{
using namespace std;
current() = 10; // replace a[0]
index = 3;
current() = 20; // replace a[3]
for (int i = 0; i < 4; ++i)
cout &laquo_space;a[i] &laquo_space;‘ ‘;
cout &laquo_space;endl;
}
// Output -> 10 1 2 20
~~~

A
int& current()		// Returns a reference
{
    return a[index];
}
23
Q

Given the following code, what will i be?

int i = 42;
int &r1 = i;
const int &r2 = i;
r1 = 0;
24
Q

Given the following code, what will r1 be?

int i = 42;
int &r1 = i;
const int &r2 = i;
r1 = 0;
25
Given the following code, what will `r2` be? ``` int i = 42; int &r1 = i; const int &r2 = i; r1 = 0; ```
`r2` will be `0` | `r2` cannot directly be changed (`r2 = 5`), but changes with `i` or `r1`
26
Write a for loop that will convert each char in the following string, `s`, to uppercase: ``` string s{"Hello World!!!"}; // for loop here cout << s << endl; // output -> HELLO WORLD!!! ```
``` for (auto &c : s) { c = toupper(c); } ```
27
What is the output of the following code? ``` int a[5]{1, 2, 3, 4, 5}; cout << *a << endl; ```
1 (`*a == a[0]`) | array name in expression == pointer to the first element
28
What is the output of the following code? ``` int a[5]{1, 2, 3, 4, 5}; int i = 3; cout << *(a + i) << endl; ```
4 | `*(a + i) == a[i]`
29
Write an example of the `strcpy()` function defined in the `` library.
``` char a[] = "Hello"; char b[10]; strcpy(b, a); ``` | `strcpy()` is unsafe
30
What happens if the destination in `strcpy()` does not have enough room reserved?
It keeps going and overwrites other variables; this is a common security vulnerablility called "buffer overrun" | this is why it is unsafe to use `strcpy()`
31
Given `int a[n];` where `n` is a `const`, what is an idiom find `n`?
``` n == sizeof(a) / sizeof(a[0]); ```
32
What will the following code output? ``` int a[] {1, 2, 3, 4, 5}; int* p = a + 1; cout << p - a << endl; ```
1
33
What will the following code output? ``` int a[] {1, 2, 3, 4, 5}; int* p = a + 1; int* q = a + 4; cout << q - p << endl; ```
3
34
What will the following code output? ``` int a[2][3]; cout << sizeof(a) << endl; ```
24