337 C++ (23-24) Flashcards

1
Q

True or false, you can copy built in arrays

A

False, you cannot copy built in arrays

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

Can you resize built in arrays if they are created on the stack?

A

No. You cannot resize them if they are created on the stack

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

When using a vector, what header file must you include?

A

#include <vector>

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

What is the syntax to declare a vector object?

A
vector <data type> arrayName;

`

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
What does the following code snippet do?

vector<double> a;
A

creates an empty vector

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

`What does the following code snippet do?

vector<double> a(5)

`

A

creates a vector with 5 elements all with 0.

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

True or false, vectors are automatically initialized to 0.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
What does the following do?

vector<double> a(3, 300)
A

Creates a vector that contains 3 elements, each containing 300.0

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

Can [] be used to access vectors?

A
yes, it works just fine for vectors. 
For example 

vector<double> grades(5);
grades[4] = 8.0;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
How do we resize vectors. Let's say we want to resize the following. 

vector <int> a(originalSize); 
A

`
a.resize(newSize);
`

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

What does the following do?
`
v.empty
v.push_back(value);
v.pop_back(value)
v.at(index);
v.assign(n, value);
v.clear();

`

A

`
v.empty /returns true of vector is empty
v.push_back(value); //inserts the value to the end of the vector
v.pop_back(value) //returns and removes the last element
v.at(index); //same as v[index]
v.assign(n, value); //assigns value to the first n elements
v.clear(); //deletes all elements

`

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

Should vectors always be passed by reference?

A
It is recommended that vectors always be passed by reference. If necessary, use the const keyword.

Example

double average(const vector<int> &data) {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is typedef used for?

A

You can use typedef to create a new data type name from existing data types.

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

What is the general format for using typedef to create a new data type name?

A
typedef existing_type_name new_type_name;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
Let's say that we want to define a vector of a vector for a 'matrix'. We currently have:

typedef vector<int> row;
A
typedef vector<row> matrix;

or we can do
typedef vector<vector<int> > matrix;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The following code will create a matrix with 3 rows and 5 columns. Fill in the blanks

`
const int numRows = 3;
const int numCols = 5;

m.resize(numRows); //sets the number of rows

for(int j = ___; j <_______; j++)
m.at(j).resize(______);
`

A

`
const int numRows = 3;
const int numCols = 5;

m.resize(numRows); //sets the number of rows

for(int j = 0; j < numRows; j++)
m.at(j).resize(numCols);
`
We want to create a column for every row.

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

True or false, if a matrix is used as a type for function parameters, it must be defined prior to the prototype of the function.

A

True. This intuitively makes sense, since if a function uses it, it must be defined beforehand.

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

When declaring strings what header file, if any should you include?

A
#include <string>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
What does the following do?

string stringName;
A

It will create an empty string.

20
Q

Are spaces allowed in strings? How many characters is the following?
`
string course( “ENCM 399” );
`

A

The string has 8 characters. Spaces are counted

21
Q

What does the following do?
`
string firstInitial(1, ‘J’);
`

A

This creates a string with 1 element and initializes it to J

22
Q

What would be the contents of student 2?
`
string student1(“John Smith”);
string student 2(student1);
`

A

The second string is also John Smith

23
Q

What would be the range of a string, where N is the length of that string?

A

The index into the string should be in the range of 0 to N-1.

24
Q

What is the difference between .size() and .length()?

A

.length() is part of the string/vector library.
.size() is more general.

Both of them will return the number of characters in a string.

25
Q
What is the following value of b?

string s2(1, ' ');
int b = s2.size()
26
Q
What is the output?
string b("String");
b = 'z';
cout<<b<<endl;
A

b = z. The string on the left hand side will automatically be resized.

27
Q

Should we pass a string by reference in a function?

A

It is recommended but not required that a string be passed by reference in a function.

28
Q

What does the following output?
`
string message(“Hello”);
cout «message;
`

A

Outputs Hello

29
Q
What does the following output?

#include <iostream>
#include <string>

int main() {
    std::string result = "Hello, ";

    // Append additional text using +=
    result += "World!";

    std::cout << result << std::endl;  

    return 0;
}
A

Output: Hello, World!

30
Q
What does the following output?
`
#include <iostream>
#include <string>

int main() {
    std::string result = "Hello, ";

    // Append a specific substring
    result.append("World!", 3, 3);  

    std::cout << result << std::endl;  

    return 0;
}

`
A

Output: Hello, rld

31
Q
What does the following output?
`
#include <iostream>
#include <string>

int main() {
    std::string result = "Hello, ";
    result.append("World!");

    std::cout << result << std::endl;  

    return 0;
}

`
A

Output: Hello, World

32
Q

When will cin stop reading input?

A

It will stop at the first whitespace character. This means you can only read one word at a time.

33
Q

When will the getline function stop reading input?

A

It will read a line of text up to but not including the delimiter character (which defaults to \n).

34
Q
Complete the implementation of the getline function.

string line_of_text;
cout << "Enter a line of text: ";
getline(\_\_\_, \_\_\_\_\_\_\_\_\_,\_\_\_\_)
A
string line_of_text;
cout << "Enter a line of text: ";
getline(cin, line_of_text ,'\n');
35
Q

What will lines 2 and 3 do?
`
string course(“ENCM339 class”);
course.erase(4, 3);
course.insert(4, “ 369”);

`

A

`
string course(“ENCM339 class”);
course.erase(4, 3); // course is “ENCM class”
course.insert(4, “ 369”); // course is “ENCM 369 class”

`

36
Q
What does the following code do?

#include <iostream>
#include <string>
using namespace std;
string trim_trailing_spaces(string& s){
while((s.at(s.size()-1) == ' '|| s.at(s.size()-1) == '\n') && s.size()> 0)
s.pop_back();
return s;
}
string trim_leading_spaces(string& s) {
size_t start = s.find_first_not_of(" \n");
return s.substr(start);
}
int main(void){
string s = " Hello World ";
cout << trim_trailing_spaces(s) << endl; // prints “ Hello World”
cout << trim_leading_spaces(s) << endl; // prints “Hello World”
return 0;
}
A

It will remove leading and trailing space using pop_back

37
Q

What do the following string member functions do?
size()
length()
resize()
capacity
clear()
empty()
back()
front()
push_back()
c_str()
data()
Find()
compare()

A

size() Return length of string
length() Return length of string
resize() Resize string
capacity Return size of allocated storage
clear() Clear string
empty() Test if string is empty
back() Access last character
front() Access first character
push_back() Append character to string
c_str() Get C string equivalent
data() Get string data
Find() Find content in string
compare() Compare strings

38
Q

How would we initialize a pointer to a pointer.

A
int **p2;
We can also use the double ** to dereference. 
**p2 = 16; //assuming that p2 is pointer to another pointer.
39
Q
What does the following code do?

int * a[5];
A

It creates an array of five elements of type int*

40
Q

What is the asterisk used as for the unary and binary operators?

A

Multiplication is a binary operation
Pointer notation and dereferencing of pointers are unary operators

41
Q

What has the higher precedence, [] or *

A

The operator [ ], has higher precedence than unary *
operator. Therefore applying the order of precedence for []
and * implies to read the following declaration as: a is an
array of 3 pointers:
int *a[3];

42
Q
What is the output of the following code?

#include <iostream>

int main() {
    const char* p[3]; // p is an array of char* pointers
    p[0] = "XYZ";    
    p[1] = "KLM";
    p[2] = "ABC";

    // Output statements
    std::cout << p[1] << std::endl;   
    std::cout << *p[1] << std::endl;  
    std::cout << **p << std::endl;    
    std::cout << *(p + 1) << std::endl;

    return 0;
}
43
Q

In the following code, which area is the string located in?
p[0] = “XYZ”;

A

first element points to “XYZ” in the static area

44
Q

What is the third element?
char *arr2[3 ] = { “ABC”, “XYZ”};

A

The third element is a null pointer

45
Q

What is the value of arr4[1]?

const char *s = “KLM”;
char *arr4[ ] = {s , &s[1]};

A

arr4[1] points to “LM”

46
Q
What does the following do?

int main()
{
double *p[3];
int i;
for (i = 0; i < 3; i++)
p[i] = new double[i+2];
**p = 5.3;
p[1][0] = 9.0; //???
A

We assign the first element of the 2nd value to 9.0.

47
Q

Does C and C++ allow arguments in the main function?

A

Yes, the first argument is a pointer that may point to an array. Each pointer may refer to a token from the command line.

Assume we have: ./a.exe cat cow dog
int main(int argc, char **argv) {
  for(int j = 0; j < argc; j++)
  cout << argv[j];
  // point one
  return 0;
}
// prints: 
./a.exe
cat
cow
dog