Final Exam Flashcards

1
Q

List 3 limitations with C-style strings.

A

1) Forgetting to end string with ‘\0’
2) Not allocating enough memory for string
3) Cannot add, delete, or combine elements

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

What is the preprocessor directive for strings?

A

include

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

What are two ways to initialize a string variable?

A
string name = "Mike";
or
string name("Mike");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you use string concatenation?

A

Use + operator.

fullname = firstname + lastname;

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

What is the preprocessor directive for writing to files?

A

include

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

What are the five steps of writing to a file?

A

1) Create an output stream object
2) Establish connection to file
3) Verify connection
4) Start writing
5) Close file

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

What is the syntax for declaring an output stream object?

A

ofstream fout;

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

What is the syntax for opening a file?

A

fout.open(“filename.txt”, ios::out);

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

What is the syntax for verifying an output stream connection?

A
if(!fout.is_open())
{
// cerr & exit
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the syntax for closing a file?

A

fout.close();

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

When do you need to convert a string to a c-sring and what is the syntax for converting a C++ string to C-style string?

A

Convert to c-string when using a string as the file name in an output file stream.

Syntax: stringName.c_str();

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

What is the function that returns the size of a string?

A

stringName.length()

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

What is the function that returns the size of a vector?

A

vectorName.size()

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

What is the preprocessor directive needed to use the sort function?

A

include

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

What is the syntax of the sort function?

A

sort(list.begin(), list.end());

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

What are two drawbacks to vectors?

A

1) Vectors take up more memory

2) Could take more time

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

When should arrays be used over vectors?

A

When the array is of a fixed size and when speed is an issue

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

What is the major limitation of arrays?

A

Size is constant and cannot be changed. Therefore, you might have allocated memory for the array that you did not use.

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

What are the six steps of reading a file?

A

1) Know the format of the file.
2) Create an input stream object (input file handler)
3) Establish connection to file.
4) Verify connection.
5) Start reading.
6) Close the file.

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

What is the syntax of creating an input stream object/input file handler?

A

ifstream fin;

21
Q

What is the syntax of opening a file for reading?

A

fin.open(“filename.txt”, ios::in);

22
Q

What is the syntax for verifying connection to an input file handler?

A
if (!fin.is_open())
{
// cerr & exit
}
23
Q

What is the syntax for reading data from a file into a variable x?

A

fin&raquo_space; x;

24
Q

What is the syntax for closing an input file stream?

A

fin.close();

25
Q

What is the syntax for reading a file until the read fails?

A
fin >> x;
while (!fin.fail())
{
cout > x;
}
26
Q

When will the function fail() return true?

A

1) End of file is reached, or

2) Read error: trying to read the wrong data type

27
Q

What is the simpler equivalent to using the fail() function in a while loop?

A

while (fin)

28
Q

How can you check for a read error (i.e. fail function returns true before the end of a file)? Describe syntax.

A
if (!fin.eof())
{
// cerr and exit
}
29
Q

What are some common errors in fstream?

A

1) Writing to a file which was opened for reading
2) Reading a file which was opened for writing
3) Forgetting to open/close a file
4) Not checking for read failure

30
Q

What is the difference between using ofstream and ostream when passing file streams?

A

ofstream can pass fout, but ostream can pass cout or fout

31
Q

What is the syntax for initializing a vector of 5 items and initializing all items to -1.0?

A

vector list(5, -1.0);

32
Q

What are two ways to initialize a C-style string?

A

char s[] = “Hello”;
or
char s[5] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’ }

33
Q

What is the syntax for removing the last element of a vector?

A

vectorName.pop_back();

34
Q

Write syntax for passing an array so that it can be edited.

A

function(int arrayName[]);

35
Q

Write syntax for passing an array so that it cannot be edited.

A

function(const int arrayName[]);

36
Q

Write syntax for passing a vector so that it can be edited.

A

function(vector & list);

37
Q

Write syntax for passing a vector so that it cannot be edited.

A

function(const vector & list);

38
Q

Write syntax for passing a string so that it can be edited.

A

function(string & name);

39
Q

Write syntax for passing a string so that it cannot be edited.

A

function(const string & name);

40
Q

Write syntax for using an array with dynamic memory allocation.

A

int n;
int * x;

n = 4;
x = new int[n];

delete [] x;

41
Q

What is it called when dynamic memory allocation is not deleted?

A

memory leak

42
Q

What is the output?

int x = 8;
cout

A

address of x is outputted

43
Q

What is the output?

int x = 8;
int *ptr = &x;
cout

A

address of x is outputted

44
Q

What is the output?

int x = 8;
int *ptr = &x;
cout

A

8

45
Q

What are the two types of memory, and describe them.

A

1) Static memory: determined at compile time, allocated on program execution
2) Dynamic memory: allocated at run-time

46
Q

Describe the syntax of the string replace function.

A

s.replace(6, 3, “Hi”);

Replaces starting with the 6th index, replaces 3 places with “Hi”

47
Q

Describe the syntax of the string insert function.

A

s.insert(6, “Hi”);

Inserts before the 6th index, inserts “Hi”

48
Q

Write a replace function to change the string “Hello World” to “Hello Mars”

A

s.replace(6, 5, “Mars”);

49
Q

Write an insert function to change the string “Hello World” to “Hello Beautiful World”

A

s.insert(6, “Beautiful “);