HW-2

docx

School

The City College of New York, CUNY *

*We aren’t endorsed by this school

Course

103

Subject

Computer Science

Date

Jan 9, 2024

Type

docx

Pages

4

Uploaded by BaronPuppyPerson1126

Report
HW-2 / Chapters 2 and 3 “Abstract Data Types and C++ Classes”, “Container Classes” 1. What is the difference between a class and an object? A. A class is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments to set required member variables. Once a class is defined, only one member can be declared. There’s no difference between a variable and an object, so any object is a variable. B . A class is a kind of data type that defines data members and member functions that operate on the data. An object is an instance of a class and is declared as a variable. Once a class is defined, a programmer can declare many objects of that class and manipulate the objects with its functions. C. A class is a kind of data type that defines data members, and which must exist on a separate file in order to be used within the main program. An object refers to any function defined within a class. D. All of the above. 2. What kind of member of a class supports information hiding? A . The private members. B. The public members. C. The static members. D. All of the above. 3. What is the difference between a sequence and a bag? What additional operations does a sequence require? A. Bag has a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments to set required member variables. Sequence is a structure that has ordered items and requires a specific functions to navigate between these items, such as start , current , is_item . B. Bag is a class with its member functions while sequence is a collection of items. The items in a sequence are arranged in order, but there’s no additional operations to access these items. C. Both contain a collection of items. The items in a sequence are arranged in order, one after another. The additional operations that sequence require are following start , advance , current , remove_current , attach and is_item functions. D. Bag is a class and sequence is a function defined in a bag. Some of the additional arguments that sequence have are following start , advance , current , remove_current , attach and is_item . 4. When an object variable is declared, what happens if the programmer did not write a constructor for the class? A. The compiler automatically creates a simple default constructor. B. The compiler throws an error message. C. The compiler automatically removes an allocated space for the declared object. D. All of the above.
5. Here is the start of a class declaration: (Note: Samuel will be explaining this question on Monday, September 21) class Foo { public: Foo(int num); void fun1(Foo f); void fun2(const Foo f); void fun3(Foo f) const; Which of the three member functions can alter the PRIVATE member variables of the foo object that activates the function? A. Only Foo can alter the private member variables of the object that activates the function. B. Three of the functions can alter the private member variables of the object that activates the function. C. Only fun1 can alter the private member variables of the object that activates the function. D . Two of the functions can alter the private member variables of the object that activates the function. 6. I have an array named data, which contains n integers. I want to print all of the numbers, starting at data[0]. BUT if the number 42 occurs, then I want to stop my printing just before the 42 (without printing the 42!) Here is most of my for-loop to accomplish my goal: for (i = 0; ___________________________________________; i++) cout << data[i] << endl; What is the correct way to fill in the blank? (If there is more than one correct answer, please select E.) A. (data[i] != 42) && (i < n) B . (data[i] != 42) || (i < n) C. (i < n) && (data[i] != 42) D. (i < n) || (data[i] != 42) E . More than one of the above answers is correct. 7. Consider this class definition: class quiz { public: quiz( ); int f( ); int g( ) const; private: double score; }; Which functions can carry out an assignment  score=1.0;  to the private member variable  score ? A. Both f and g can carry out the assignment. B . f can carry out the assignment, but not g. C. g can carry out the assignment, but not f. D. Neither f nor g can carry out the assignment. 8. Use the copy function to copy six elements from the start of an array x into an array y starting at y[42]. A. copy(0, x+6, y+42); B. copy(x, x+6, 42);
C . copy(x, x+6, y+42); D. copy(x, x+6, y-42); 9. Here is a small function definition: void f(int i, int &k) { i = 1; k = 2; } Suppose that a main program has two integer variables x and y , which are given the value 0 . Then the main program calls  f(x, y) ; What are the values of x and y after the function f finishes? A. Both x and y are still 0. B. x is now 1, but y is still 0. C . x is still 0, but y is now 2. D. x is now 1, and y is now 2. 10. Here is a function prototype and some possible function calls: int day_of_week(int year, int month = 1, int day = 1); // Possible function calls: cout << day_of_week( ); cout << day_of_week(1995); cout << day_of_week(1995, 10); cout << day_of_week(1995, 10, 4); How many of the function calls are legal? A. None of them are legal B . Three of them are legal C. One of them is legal D. Two of them are legal E. All of them are legal 11. Which kind of functions can access private member variables of a class? A. Friend functions of the class B. Private member functions of the class C. Public member functions of the class D . All of the above can access private member variables E. None of the above 12. Which of these function calls could change the value of a point p : void rotate_to_upper_right(point& p) { while (//expression) p.rotate90(); } int rotations_needed(point p) { int answer; while (//expression) { p.rotate90(); ++answer; } return answer; }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
cout << rotations_needed(p); rotate_to_upper_right(p); A. None of them. B. Only rotations_needed C . Only rotate_to_upper_right D. Both of them 13. Choose the correct answer for the left-inclusive pattern for an iterator i and a container c. A. for (i = c.start(); i != c.end(); i++) {…} B . for (i = c.begin(); i != c.end(); ++i) {…} C. for (i = c.start(); i != c.count(); ++i) {…} D. for (i = c.front(); i != c.end(); i++) {…} 14. Suppose that you are working on a machine where arrays may have up to 4,000,000,000 items. What is the guaranteed range of the size_t data type? A. Negative 4,000,000,000 to positive 4,000,000,000. B. Zero to positive 32,767 C. Zero to positive 65,535 D . Zero to 4,000,000,000 E. Zero to 8,000,000,000 15. (Optional) . Implement bag’s erase member function. Answer: bag::size_type bag::erase( const value_type& target) { size_type index = 0; size_type many_removed = 0; while (index < used) { if (data[index] == target) { --used; data[index] = data[used]; ++many_removed; } else ++index; } return many_removed; }