Cpp Ch10
Analyze the following code: #include <iostream> #include <string> using namespace std; class MyDate { public: int year; int month; int day; MyDate() { year = 2003; month = 2; day = 3; } MyDate(int year1, int month1, int day1) { year = year1; month = month1; day = day1; } }; class Name { public: string firstName; char mi; string lastName; MyDate birthDate; Name(string firstName1, char mi1, string lastName1, MyDate birthDate1) { firstName = firstName1; mi = mi1; lastName = lastName1; birthDate = birthDate1; } }; int main() { MyDate birthDate(1990, 3, 4); Name name("John", 'F', "Smith", birthDate); birthDate = MyDate(1991, 1, 2); birthDate.year = 1992; cout << name.birthDate.year << endl; } A. The program displays 1990. B. The program displays 1991. C. The program displays 1992. D. The program displays no thing.
A
Analyze the following code: #include <iostream> #include <string> using namespace std; class MyDate { public: int year; int month; int day; MyDate() { year = 2003; month = 2; day = 3; } MyDate(int year1, int month1, int day1) { year = year1; month = month1; day = day1; } }; class Name { public: string firstName; char mi; string lastName; MyDate birthDate; Name(string firstName1, char mi1, string lastName1, MyDate& birthDate1) { firstName = firstName1; mi = mi1; lastName = lastName1; birthDate = birthDate1; } }; int main() { MyDate birthDate(1990, 3, 4); Name name("John", 'F', "Smith", birthDate); birthDate.year = 1991; birthDate.year = 1992; cout << name.birthDate.year << endl; } A. The program displays 1990. B. The program displays 1991. C. The program displays 1992. D. The program displays no thing.
A
What is the output of the following code? string s("abc"); s.append("welcome"); cout << s << endl; A. abcwelcome B. abc C. welcome D. welcomeabc
A
What is the output of the following code? string s("abcdefg"); cout << s.compare("abb") << endl; A. 1 B. 0 C. -1 D. -2
A
What will be displayed by the following code? #include <iostream> using namespace std; class Count { public: int count; Count(int c) { count = c; } Count() { count = 0; } }; void increment(Count c, int times) { c.count++; times++; } int main() { Count myCount; int times = 0; for (int i = 0; i < 100; i++) increment(myCount, times); cout << "myCount.count is " << myCount.count; cout << " times is " << times; return 0; } A. myCount.count is 0 times is 0 B. myCount.count is 100 times is 100 C. myCount.count is 0 times is 100 D. myCount.count is 100 times is 0
A
Which of the following statements are true? A. A constant member function cannot change the data fields in the object. B. A constant memebr function may be an intance or a static function. C. A constant function may be defined for any functions not just instance member functions. D. A constructor can be defined as a constant member funciton.
A
You should add the static keyword in the place of ? in which of the following function: #include <iostream> using namespace std; class Test { public: ? int square(int n) { return n * n; } ? int getAge() { return age; } private: int age; }; A. in the square function because the function does not use any instance data fields. B. in the getAge function C. in both lthe square function and the getAge function D. none
A
What is wrong in the following code? #include <iostream> using namespace std; class Test { public: static int square(int n) { return n * n; } int getAge() { return age; } static int k = 5; private: int age; }; int main() { cout << Test::square(4); } (Multiple Answer) A. The static variable k cannot be initialized inside the class. B. You should replace static int k = 5 with static int k and declare int Test::k = 5 outside the Test class. C. You should replace static int k = 5 with static int k and declare int Test.k = 5 outside the Test class. D. You should replace static int k = 5 with static int k and declare int Test->k = 5 outside the Test class.
A B
Which of the following statements are correct? (Multiple Answer) A. C++ allows you to pass a parameter of object type in a function by value. B. C++ allows you to pass a parameter of object type in a function by reference. C. Passing objects by reference is commonly used because it saves memory. D. You should define constant reference parameters for objects that are not supposed to be changed in the function.
A B C D
Which of the following statements are true? (Multiple Answer) A. An object can contain another object. The relationship between the two is called composition. B. Composition is actually a special case of the aggregation relationship. C. Aggregation models has-a relationships and represents an ownership relationship between two objects. D. The owner object is called an aggregating object and its class an aggregating class. E. The subject object is called an aggregated object and its class an aggregated class.
A B C D E
What is wrong in the following code? #include <iostream> using namespace std; class Test { public: static int square(int n) { return n * n; } int getAge() { return age; } private: int age; }; int main() { cout << Test.square(4); } (Multiple Answer) A. It is a compile error to invoke Test.square(4). B. You should replace Test.square(4) with square(4). C. You should replace Test.square(4) with Test->square(4). D. You should replace Test.square(4) with Test::square(4).
A D
Analyze the following code: #include <iostream> #include <string> using namespace std; class Name { public: string firstName; char mi; string lastName; Name(string firstName1, char mi1, string lastName1) { firstName = firstName1; mi = mi1; lastName = lastName1; } }; int main() { string firstName("John"); Name name(firstName, 'F', "Smith"); firstName = "Peter"; name.lastName = "Pan"; cout << name.firstName << " " << name.lastName << endl; } A. The program displays Peter Pan. B. The program displays John Pan. C. The program displays Peter Smith. D. The program displays John Smith.
B
What is the output of the following code? string s("abcd"); cout << s.at(1) << endl; A. a B. b C. c D. d
B
What is the output of the following code? string s("abcdefag"); cout << s.find("def") << " " << s.find("a", 3); A. 3 0 B. 3 6 C. 2 4 D. 0 0
B
What is the output of the following code? string s("abcdefg"); char s1[15] = "welcome"; s.copy(s1, 3, 0); cout << s1 << endl; A. welcome B. abccome C. welcomeabc D. abcwelcome
B
What is the output of the following code? string s("abcdefg"); cout << s.substr(1, 3); A. abc B. bcd C. a D. c
B
What is the output of the following code? string s("abcdefg"); s.erase(2, 3); cout << s << endl; A. abcd B. abfg C. abcg D. aefg
B
What is the output of the following code? string s("abcdefg"); s.replace(1, 2, "wel"); cout << s << endl; A. abcdefg B. aweldefg C. welabcdefg D. awelbcdefg
B
What is the output of the following code? string s("abcdefg"); string s1("welcome"); s.swap(s1); cout << s << endl; A. abcdefg B. welcome
B
What will be displayed by the following code? #include <iostream> using namespace std; class Count { public: int count; Count(int c) { count = c; } Count() { count = 0; } }; void increment(Count &c, int &n) { c.count++; n++; } int main() { Count myCount; int times = 0; for (int i = 0; i < 100; i++) increment(myCount, times); cout << "myCount.count is " << myCount.count; cout << " times is " << times; return 0; } A. myCount.count is 0 times is 0 B. myCount.count is 100 times is 100 C. myCount.count is 0 times is 100 D. myCount.count is 100 times is 0
B
Analyze the following code: #include <iostream> using namespace std; class A { public: A(); double getNumber(); private: double number; }; A::A() { number = 1; } double A::getNumber() { return number; } void printA(const A &a) { cout << "The numberr is " << a.getNumber() << endl; } int main () { A myObject; printA(myObject); return 0; } (Multiple Answer) A. The program will compile and run fine. B. The program will compile if the getNumber function is defined as const. C. The program will compile if const A &a is changed to A &a in the function printA. D. The program will compile if A myObject is changed to A myObject(). E. The program will compile if printA(myObject) is changed to printA(&myObject).
B C
Given the declaration Circle x[10], which of the following statements is correct? (Multiple Answer) A. x contains an array of ten int values. B. x contains an array of ten objects of the Circle type. C. Each element in the array is a Circle object. D. You cannot assign a new object to the elements in the array, but you can change the contents in each object element.
B C D
A function that is associated with an individual object is called __________. A. a static function B. a class function C. an instance function D. an object function
C
What is the output of the following code? string s("abc"); s.append("welcome", 0, 3); cout << s << endl; A. abcwelcome B. abc C. abcwel D. welcomeabc
C
What is the output of the following code? string s("abc"); s.append("welcome", 3); cout << s << endl; A. abcwelcome B. abc C. abcwel D. welcomeabc
C
What is the output of the following code? string s("abc"); s.assign(3, 'w'); cout << s << endl; A. abcwww B. abc C. www D. abcwww E. wel
C
What is the output of the following code? string s("abcdefg"); cout << s.substr(3); A. abc B. bcd C. defg D. efg
C
What is the output of the following code? string s("abcdefg"); s.insert(1, 3, 'w'); cout << s << endl; A. abcdefg B. aweldefg C. awwwbcdefg D. awelbcdefg
C
What will be displayed by the following code? #include <iostream> using namespace std; class Count { public: int count; Count(int c) { count = c; } Count() { count = 0; } }; void increment(Count c, int &n) { c.count++; n++; } int main() { Count myCount; int times = 0; for (int i = 0; i < 100; i++) increment(myCount, times); cout << "myCount.count is " << myCount.count; cout << " times is " << times; return 0; } A. myCount.count is 0 times is 0 B. myCount.count is 100 times is 100 C. myCount.count is 0 times is 100 D. myCount.count is 100 times is 0
C
When invoking a function with a reference object parameter, ___________ is passed. A. the contents of the object B. a copy of the object C. the reference of the object D. the object is copied, then the reference of the copied object
C
Variables that are shared by every instances of a class are __________. A. public variables B. private variables C. instance variables D. static variables
D
What is the output of the following code? string s("abc"); s.append(3, 'w'); cout << s << endl; A. abcwelcome B. abc C. abcwel D. abcwww
D
What is the output of the following code? string s("abcd"); cout << s.length() << endl; A. 1 B. 2 C. 3 D. 4 E. 5
D
What is the output of the following code? string s("abcd"); cout << s.size() << endl; A. 1 B. 2 C. 3 D. 4 E. 5
D
What is the output of the following code? string s("abcdefg"); s.insert(1, "wel"); cout << s << endl; A. abcdefg B. aweldefg C. welabcdefg D. awelbcdefg
D
What is the output of the following code? string s("abc"); s.assign("welcome", 0, 3); cout << s << endl; A. abcwelcome B. abc C. welcome D. abcwww E. wel
E
What is the output of the following code? string s("abc"); s.assign("welcome", 3); cout << s << endl; A. abcwelcome B. abc C. welcome D. abcwww E. wel
E
What is the output of the following code? string s("abcd"); s.clear(); cout << s.length() << endl; A. 1 B. 2 C. 3 D. 4 E. 0
E