Final Exam Flashcards

(81 cards)

1
Q

What is the value of numbers.size() after the following code?

vector < float > numbers;
numbers.reserve(100);

A

0

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

To declare a c-string and initialize it to the value of “phonebook”,

A

char s1[10]=”phonebook”;

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

Which assignment statements will copy the value “ toaster” into a string variable (str1)?

A

str1 = “toaster”;

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

Given the following code, what is the correct statement to insert the string str2 into str1, directly after the ‘d’?

string str1 = “abcdefg”;
string str2 = “ABCDE”;

A

str1.insert(4,str2);

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

The following code declares a vector of integers named numbers that reserves space for 100 integers.

vector < int > numbers(100);

TRUE/FALSE

A

true

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

Which of the following declarations correctly creates a c-string that can hold the value “phonebook”

char s1=10;
char s1[10];

A

char s1[10];

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

A vector v will automatically increase the allocated size when more than v.size( ) elements are inserted with v.push_back( newElement).

TRUE/FALSE

A

true

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

What is a correct statement about vector member functions studied in this chapter?

A

size( ) tells how many base type objects have been inserted into the vector

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

Using the == operator on a string variable results in the same value as using strcmp on two c-strings.

TRUE/FALSE

A

False

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

If the capacity of a vector named names is 20 and the size of names is 19, which of the following statements are legal?

names.push_back(“myName”);
names[18]=”myName”;
All of these
None of These

A

None of these

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

The base type for a vector can be

A

Any data type

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

Using the resize member function alone, you can increase the capacity of an STL vector.

TRUE/FALSE

A

true

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

Which of the following would correctly read an entire line from an input file stream named fin into a string variable named line?

getline(fin,line);

fin. getline(line);
fin. getline(line,’\n’)
fin. getline(line,80);

A

getline(fin, line);

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

If we use an out of range index with a vector, there will be an error message from the compiler.

TRUE/FALSE

A

False

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

Which of the following returns the fourth character in the string variable named str and checks if there is a fourth character in the string?

str(3);
str.at(3);
str[3];
all of these

A

str.at(3);

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

Write how to print out the value in str?

char str[30];
cin&raquo_space; str;

A

for(int i=0;i<30;i++)

cout &laquo_space;str[i];

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

What is the value of numbers.capacity() after the following code?

vector < float > numbers;
numbers.reserve(100);

A

100

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

Vectors can have any type as the base type.

TRUE/FALSE

A

True

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

Given the following declarations, which of the following is legal syntax?

string str=”your name”;
char c_string[20]=”my name”;

--------------
str = c_string;
c_string = str;
strcpy(c_string, str.c_str());
strcpy(c_string,str);
A

str = c_string;

and

strcpy(c_string,str.c_str());

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

Vector assignment is well behaved.

TRUE / FALSE

A

True

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

strcmp(first, second) returns

A

<0 if first < second
0 if first == second
positive otherwise.

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

Which is the proper way to determine how many characters are in the string variable named str?

A

str.length()

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

You can explicitly use the vector member function resize to increase the capacity of a vector.

TRUE / FALSE

A

True

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

If you want to read into a c-string, you must ensure that the user does not enter more characters than

A

The size of the c-string -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
If a vector named numbers has 20 elements in it, what is the result of executing the following statement? numbers.resize(10);
The last 10 elements are removed
26
Using the [i] on a string variable does not check for illegal values of i. TRUE/FALSE
True
27
When the extraction operator is used to read data into a string, a. it skips all white spaces b. it skips only new lines c. it reads everything on the line d. it reads as many characters that will fit into the c-string
it skips the white spaces
28
What is the difference between strcmp and strncmp?
they both compare, one expects an integer for the number of characters to compare
29
What is the value of str after the following code? string str; a. a garbage string b. the empty string c. the null character d. unknown
b.the empty string
30
When a vector is assigned to another vector
all the values in the vector are copied
31
In a vector, which of the following statements is true? a) indexing vector access is range checked b) the range of legal index values for a vector is 0 to the value of v.size ()-1 c) to add a value use the member function v.push_front() d) to increase or decrease a vector's size v.new_size(newSize)
the range of legal index values for a vector is 0 to the value of v.size ()-1
32
Given the following structure definitions, what is the correct way to print the person's birth year? ``` struct DateType { int day; int month; int year; ``` } struct PersonType { int age; float weight; DateType birthday; } personType person;
cout << person.birthday.year;
33
In a structure definition, the identifiers declared in the braces are called
member names
34
Given the following structure definition, what is the correct way to initialize a variable called today? struct dateType { int day; int month; int year; }
DateType today = {1,1,2000);
35
Given the following class definition and the following member function header, which is the correct way to output the private data? ``` class Person { public: void outPerson(ostream& out); private: int age; float weight; int id; }; ``` ``` void Person:: outPerson(ostream& out) { // what goes here } ```
out << age << weight << id;
36
A derived class has access to the ____ _____ and _____ of it's ancestor classes
the public functions and variables
37
Class data members are almost always public TRUE/FALSE
False
38
All constructors for a class must be private TRUE/FALSE
False
39
In a class, all members are set ____ by default
Private
40
A data type consisting of data members and operations on those members which can be used by a programmer without knowing the implementation details of the data type is called
abstract data type
41
a function may return a structure TRUE/FALSE
True
42
Given the following class definition, what is missing? ``` class ItemClass { public: ItemClass(int newSize, float newCost); int getSize(); float getCost(); void setSize( int newSize); void setCost(float newCost); private: int size; float cost }; ```
a default constructor
43
What would be the best declaration for a mutator function that allows the user of the class to change the age?
void setAge(int newAge);
44
Why do you want to usually make data members private in a class?
Correct ensure data integrity, provide data abstraction, and provide information hiding
45
In a struct, all members are ________ by default.
public
46
The assignment operator may not be used with objects of a class. TRUE / FALSE
False
47
What would be the best declaration for a constructor that would allow the user to initialize the object with an initial age and cost for class Wine?
Wine(int newAge, float newCost);
48
Data members or member functions of a class that are declared to be private may
only be accessed by members of the class.
49
Member functions of a class a ) must be private b ) must be public c ) may be in either section
may be in either section
50
A member function of a class should be made private.....
if it will only be used by other members of the class.
51
A class member function that automatically initializes the data members of a class is called a...
constructor
52
The following is a properly declared overloaded insertion operator for myClass. ostream& operator << (ostream &out, const myClass &obj);
True
53
Given the following class declaration ``` class Rational { public: Rational(); Rational (int numer, int denom); ``` ``` int getNumerator() const; int getDenominator() const; ``` friend void display(ostream& out, const Rational& value); friend bool operator(const Rational& left, const Rational & right); private: int numerator; int denominator; }; what must we add to the calls in order for the following code to compile? Rational myRational(2,3); if (3 < myRational);
another < operator that expects an int as first paramter or a constructor that expects an int for conversion
54
When should you generally pass an object of the class to a friend function as a reference parameter?
If the friend function changes the values of the data member(s) It is more efficient to pass the object by reference
55
You may not change the precedence of operators by overloading them.
true
56
Given the following class, what is syntactically wrong with the implementation of the display function? ``` class Rational { public: Rational(); Rational (int numer, int denom); Rational(int whole); ``` ``` int getNumerator(); int getDenominator(); ``` friend void display(ostream& out, const Rational& value); friend bool operator(const Rational& left, const Rational & right); private: int numerator; int denominator; }; ``` void display(ostream& out, const Rational& value) { ``` out << value.getNumerator() << '/"<< value.getdenominator(); }
The get functions are not const functions
57
When overloading an operator, which of the following is TRUE? a ) One of the arguments must be an object of the class b) The operator can be a friend or a member of the class c) The operator does not have to be a friend or a member of the class d) All of the above e) None of the above
d. all of the above
58
The operator can be a friend or a member of the class TRUE // FALSE
True
59
Given the following class and array declaration, how would you print out the age of the 10th person in the array? ``` class personClass { public: void setAge(int newAge); void setGender( char newGender); void setSalary(float newSalary); ``` ``` int geAge(); char getGender(); float getSalary(); ``` private: int age; char gender; float salary; }; personClass people[100]
cout << people[9].getAge();
60
In the following code fragment, which is the calling object for the less-than operator? string s1, s2; if( s1 < s2 )
s1
61
How many parameters are there in a unary operator implemented as a friend?
1
62
Operators must be friends of the class.
False
63
How many members (data and functions) does the following class have? ``` class Rational { public: Rational(); Rational(int numer, int denom); Rational(int whole); ``` ``` int getNumerator(); int getDenominator(); ``` friend void display(ostream); private: int numerator; int denominator; };
7
64
What is wrong with the following overloaded extraction operator declaration? istream& operator >>(istream& in, const myClass &object);
object should not be const parameter
65
If we have a full selection of accessor and mutator functions, why would we have friend functions?
More efficient access to private data members
66
Functions that are constant member functions may call constant class accessor functions.
True
67
Functions that are constant member functions may call constant class mutator functions.
False
68
Given the following class, which is the correct function header for the display function? ``` class Rational { public: Rational(); Rational(int numer, int denom); Rational(int whole); ``` ``` int getNumerator(); int getDenominator(); ``` friend void display(ostream& out, const Rational& value); private: int numerator; int denominator; };
void display(ostream & out, const Rational & value)
69
Friend functions are members of the class. True / False
False
70
Why are the extraction and insertion operators always implemented as friends of the class rather than as members of the class?
Because the first parameter must be the stream object
71
All operators can be overloaded. True/False
False
72
Which of the following operators CANNOT be overloaded? a) = b) == c) . d) []
c) .
73
In an overloaded insertion or extraction operator, which object should be the first parameter, the stream or the object of the class?
The stream
74
If you have mutators and accessors, you should not have friend functions also. True / False
False
75
Friend functions may directly modify or access the private data members.
True
76
Operators can be overloaded as a) friends of a class b) members of a class c) non-friends, non-members d) all of the above
d) all of the above
77
To overload functions with symbolic names (like + - / << ), you must use the keyword ________ before the symbolic name.
operator
78
You may not change the precedence of operators by overloading them. True / False
True
79
Since accessor functions in a class do not modify or mutate the data members of the object, the function should have the ________ modifier.
const
80
What member functions do you need to allow the compiler to perform automatic type conversions from a type different than the class to the class?
Overloaded constructors
81
Which of the following would be an appropriate function declarations to add two rational numbers? a. void friend operator + (const Rational & left, const Rational & right); b void operatator + (const Rational & left, const Rational & right); c. friend Rational operator + (const Rational & left, const Rational & right); d. Rational operator + (const Rational& left, const Rational & right);
Rational operator + (const Rational& left, const Rational & right);