CS162 Quiz 1 chp 9 and 10
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. 1
_______ is a construct that defines objects of the same type. A. A class B. An object C. A function D. A data field
A. A class
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. A constant member function cannot change the data fields in the object.
________ is invoked to create an object. A. A constructor B. The main function C. A function with a return type D. A function with the void return type
A. A constructor
Which of the following statements are true? Can be more than 1 A. A default no-arg constructor is provided automatically if no constructors are explicitly defined in the class. B. At least one constructor must always be defined explicitly. C. Constructors do not have a return type, not even void. D. Constructors must have the same name as the class itself. E. Constructors are invoked when an object is created.
A. A default no-arg constructor is provided automatically if no constructors are explicitly defined in the class. C. Constructors do not have a return type, not even void. D. Constructors must have the same name as the class itself. E. Constructors are invoked when an object is created.
Constructors ___________ and functions _________ are inline defined in the following class A. class A { public: A() { value = 0; } A(double); double f1() { // Return a numberreturn value; }double f2(); private: double value; }; Can be more than 1 A. A() B. A(double) C. f1() D. f2()
A. A() C. f1()
Which of the following statements are true? 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. 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.
Which of the following statements are correct? 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. 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.
Which of the following statements are true? Can be more than 1 A. C++ allows you to separate class declaration from implementation. B. The class declaration describes the contract of the class and the class implementation implements the contract. C. The class declaration simply lists all the data fields, constructor prototypes, and the function prototypes. The class implementation implements the constructors and functions. D. The class declaration and implementation are in two separate files. Both files should have the same name, but with different extension names. E. The class declaration file has an extension name .h and the class implementation file has an extension name .cpp.
A. C++ allows you to separate class declaration from implementation. B. The class declaration describes the contract of the class and the class implementation implements the contract. C. The class declaration simply lists all the data fields, constructor prototypes, and the function prototypes. The class implementation implements the constructors and functions. D. The class declaration and implementation are in two separate files. Both files should have the same name, but with different extension names. E. The class declaration file has an extension name .h and the class implementation file has an extension name .cpp.
Suppose circle1 and circle2 are two Circle objects. What does the following statement do?circle2 = circle1; A. It copies the contents of circle1 to circle2. B. It makes circle2 and circle1 the same object. C. It copies the contents of circle2 to circle1. D. This statement is illegal.
A. It copies the contents of circle1 to circle2.
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); } 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. It is a compile error to invoke Test.square(4). D. You should replace Test.square(4) with Test::square(4).
Which of the following statements are true? Can be more than 1 A. Object names are like array names. Once an object name is declared, it references to an object. B. Object names cannot be reassigned to reference another object. C. An object name is a constant, though the contents of the object may change. D. An object is associated with only one object name.
A. Object names are like array names. Once an object name is declared, it references to an object. B. Object names cannot be reassigned to reference another object. C. An object name is a constant, though the contents of the object may change. D. An object is associated with only one object name.
Which of the following statements are true? Can be more than 1 A. The :: symbol is called the scope operator. B. The binary scope operator can be used as ClassName::member to tell the compiler that a member belongs to a class. C. The unary scope operator can be used as ::var to tell the compiler that the variable is a global variable.
A. The :: symbol is called the scope operator. B. The binary scope operator can be used as ClassName::member to tell the compiler that a member belongs to a class. C. The unary scope operator can be used as ::var to tell the compiler that the variable is a global variable.
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. The program displays 1990.
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. The program displays 1990.
Which of the following statements are true? Can be more than 1 A. The statement Circle circle = Circle() creates a Circle object using the no-arg constructor and copies its contents to circle. B. The statement Circle circle = Circle(5) creates a Circle object with radius 5 and copies its contents to circle. C. Circle circle = Circle() should be replaced by Circle circle. D. Circle circle = Circle(5) should be replaced by Circle circle(5).
A. The statement Circle circle = Circle() creates a Circle object using the no-arg constructor and copies its contents to circle. B. The statement Circle circle = Circle(5) creates a Circle object with radius 5 and copies its contents to circle. C. Circle circle = Circle() should be replaced by Circle circle. D. Circle circle = Circle(5) should be replaced by Circle circle(5).
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); } 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. 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.
Which of the following statements are true? Can be more than 1 A. Use the private keyword to encapsulate data fields. B. Encapsulating data fields makes the program easy to maintain. C. Encapsulating data fields makes the program short. D. Encapsulating data fields helps prevent programming errors. E. If you don't use the public keyword, the visibility is private by default.
A. Use the private keyword to encapsulate data fields. B. Encapsulating data fields makes the program easy to maintain. D. Encapsulating data fields helps prevent programming errors. E. If you don't use the public keyword, the visibility is private by default.
Suppose two header files t1.h and t2.h contain the declarations for class T. What happens if you include both header files in your program? Can be more than 1 A. You will get multiple declaration error if the header files don't have the include guard. B. The compile will automatically decides which implementation to use. C. The program will compile fine and the first header file that is included is used. D. The program will compile fine and the first header file that is included is used if the header files have the include guard.
A. You will get multiple declaration error if the header files don't have the include guard. D. The program will compile fine and the first header file that is included is used if the header files have the include guard.
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. abcwelcome
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. in the square function because the function does not use any instance data fields.
Which of the following statements are true? Can be more than 1 A. local variables do not have default values. B. data fields have no default values. C. A variable of a primitive type holds a value of the primitive type. D. An object name is like a constant, which cannot be reassigned with a new object.
A. local variables do not have default values. B. data fields have no default values. C. A variable of a primitive type holds a value of the primitive type. D. An object name is like a constant, which cannot be reassigned with a new object.
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. myCount.count is 0 times is 0
There are no default value for data fields in a class. A. true B. false
A. true
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. 3 6
__________ represents an entity in the real world that can be distinctly identified. A. A class B. An object C. A function D. A data field
B. An object
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. The program displays John Pan.
What is wrong in the following code? #include <iostream> using namespace std; class TempClass { public:int i; TempClass() { int i = 5; } }; int main() { TempClass temp(2); } A. The program has a compilation error because TempClass does not have a default constructor. B. The program has a compilation error because TempClass does not have a constructor with an int argument. C. The program compiles fine, but it does not run because class C is not public. D. The program compiles and runs fine.
B. The program has a compilation error because TempClass does not have a constructor with an int argument.
Analyze the following code: #include <iostream> using namespace std; class A { public: int s; A(int newS) { s = newS; } void print() { cout << s; } }; int main() { A a; a.print(); } Can be more than 1 A. The program has a compilation error because class A is not a public class. B. The program has a compilation error because class A does not have a default constructor. C. The program compiles and runs fine and prints nothing. D. The program would compile and run if you change A a to A a(5).
B. The program has a compilation error because class A does not have a default constructor. D. The program would compile and run if you change A a to A a(5).
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; } 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. 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.
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. abccome
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. abfg
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. aweldefg
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. 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. bcd
An object is an instance of a __________. A. program B. class C. function D. data
B. class
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. myCount.count is 100 times is 100
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. welcome
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. x contains an array of ten objects of the Circle type. C. Each element in the array is a Circle object.
Given the declaration Circle x, which of the following statement is most accurate? A. x contains an int value. B. x is an object of the Circle type. C. You can assign an int value to x. D. x is a reference to a Circle object.
B. x is an object of the Circle type.
Show the output of the following code: #include <iostream> using namespace std; class A { public: int x; int y; int z; A(): x(1), y(2), z(3){} }; int main() { A a; cout << a.x << " " << a.y << " " << a.z;return 0; } A. 1 1 1 B. 1 1 2 C. 1 2 3 D. 2 2 2 E. 3 3 3
C. 1 2 3
Analyze the following code. #include <iostream> using namespace std; class Test { public: int x;Test() { cout << "Test"; } }; int main() { Test test; cout << test.x; } A. The program has a compile error because test is not initialized. B. The program has a compile error because x has not been initialized. C. The program runs fine, but test.x is unpredictable. D. The program has a compile error because Test does not have a default constructor.
C. The program runs fine, but test.x is unpredictable.
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. abcwel
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. abcwel
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. awwwbcdefg
Suppose you wish to provide an accessor function for a boolean property finished, what signature of the function should be? A. void getFinished() B. bool getFinished() C. bool isFinished() D. void isFinished()
C. bool isFinished()
The keyword __________ is required to define a class. A. public B. private C. class D. friend E. static
C. class
What is the output of the following code? string s("abcdefg"); cout << s.substr(3); A. abc B. bcd C. defg D. efg
C. defg
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. myCount.count is 0 times is 100
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. the reference of the object
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. www
What is the output of the following code? #include <iostream> using namespace std; class Foo { public: int x; // data fieldint y; // data field Foo() { x = 10; y = 10; } void p() { int x = 20; // local variable cout << "x is " << x << " "; cout << "y is " << y << endl; } }; int main() { Foo foo; foo.p(); return 0; } A. x is 10 y is 10 B. x is 20 y is 20 C. x is 20 y is 10 D. x is 10 y is 20
C. x is 20 y is 10
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. 4
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. 4
Analyze the following code. #include <iostream> using namespace std; class B { public: B() { }; int k; } ;int main() { B b; cout << b.k << endl; return 0; } A. The program has a compile error because b.k cannot be accessed. B. The program displays 0. C. The program displays 1. D. The program displays unpredictable number. E. The program has a runtime error because b.k does not have a value.
D. The program displays unpredictable number.
Analyze the following code.#include <iostream>using namespace std;class B{public:B() { };private:int k;};int main(){B b;cout << b.k << endl;return 0;} A. The program displays 0. B. The program displays 1. C. The program displays unpredictable number. D. The program has a compile error because b.k cannot be accessed. E. The program has a runtime error because b.k does not have a value.
D. The program has a compile error because b.k cannot be accessed.
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. abcwww
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. awelbcdefg
Variables that are shared by every instances of a class are __________. A. public variables B. private variables C. instance variables D. static variables
D. static variables
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. 0
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. wel
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. wel