module 1 Flashcards
(34 cards)
Write an example of an int
named n
that assigned the value 2
using the Uniform Initialization Syntax.
int n{2};
Rewrite this array initialization using the Uniform Initialization Syntax
int a[] = {1, 2, 3};
int a[]{1, 2, 3};
Is the following a declaration, a definition, or both?
int x;
both
Is the following a declaration, a definition, or both?
void f(int);
Declaration
What type does the .size()
method return?
size_t
What standard library must be included in order to use the type size_t
?
#include <cstddef>
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;
option b; function-style “casts”
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;
option c; never use C/Java?C# style casts
Declare a variable called p
that is a pointer to an int
.
int* p;
What pointer type can only hold an address, and cannot be dereferenced?
void*
Given the following code:
#include <iostream> using namespace std; int main() { int i = 7; int* ip = &i; int** ipp = &ip; }
What is **ipp
?
7; the rvalue of i
Given the following code:
#include <iostream> using namespace std; int main() { int i = 7; int* ip = &i; int** ipp = &ip; }
What is *ipp
?
a pointer to an int; the lvalue of ip
Given the following code:
#include <iostream> using namespace std; int main() { int i = 7; int* ip = &i; int** ipp = &ip; }
What is ipp
?
a pointer to a pointer to an int; the lvalue of ipp
Given the following code, which line will throw an error?
~~~
\ given:
const char * p1;
\ a)
*p1 = ‘c’;
\ b)
++p1;
~~~
Line a throws an error
const
before the asterisk will const
the contents
Given the following code, which line will throw an error?
~~~
\ given:
char const * p1;
\ a)
*p1 = ‘c’;
\ b)
++p1;
~~~
Line a throws an error
const
before the asterisk will const
the contents
Given the following code, which line will throw an error?
~~~
\ given:
char * const p1;
\ a)
*p1 = ‘c’;
\ b)
++p1;
~~~
line b will throw an error
const
after the asterisk will const
the pointer
Given the following code, which line will throw an error?
~~~
\ given:
const char * const p1;
\ a)
*p1 = ‘c’;
\ b)
++p1;
~~~
Trick question, they will both throw an error
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
#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]) << " "; } }
What does the following line do?cout.setf(ios::hex, ios::basefield);
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
What will the following code output?
#include <iostream> int main() { using namespace std; int i = 7; int& r = i; \++r; cout << i << endl; }
8
r
is another name for the existing local variable i
Write a function called swap()
that will take two int
objects and swap their rvalues.
void swap(int& a, int& b) { int temp = a; a = b; b = temp; }
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 «_space;a[i] «_space;‘ ‘;
cout «_space;endl;
}
// Output -> 10 1 2 20
~~~
int& current() // Returns a reference { return a[index]; }
Given the following code, what will i
be?
int i = 42; int &r1 = i; const int &r2 = i; r1 = 0;
i
will be 0
Given the following code, what will r1
be?
int i = 42; int &r1 = i; const int &r2 = i; r1 = 0;
r1
will be 0