COSC I Part 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What value will be returned by the function call mystery(0)? int mystery(int number) { if (number == 0) return number; else return (number + mystery(number - 1)); } //END int mystery(int number)

0

What will be output when the following C++ program runs? #include <iostream> using namespace std; class Dilbert { private: int a; int b; int c; public: Dilbert( int i=0, int j=0, int k=0 ); void setABC( int i, int j, int k ); void mystery() const; int getA() { return a; } int getB() { return b; } int getC() { return c; } }; //end class dilbert int main(int argc, const char * argv[]) { Dilbert d1; cout << d1.getA() << " "; cout << d1.getB() << " "; cout << d1.getC() << "\n"; return 0; } Dilbert::Dilbert( int i, int j, int k) { this->a = i; this->b = j; this->c = k; } void Dilbert::setABC(int m, int n, int o) { a = m; b = n; c = o; } void Dilbert::mystery() const { cout << this << endl; }

0 0 0

Consider the self-referential Widget objects below. The numbers shown represent the value of the object's weight data member. At one point these objects were part of a larger, complete linked list. Assume that the drawing conventions discussed in class apply to this graphical representation. There are ___________ dangling pointer(s) and ___________ "lost" object(s) that represent memory leaks.

0 dangling pointers and 2 "lost" objects

The drawing below represents the "state" of a linked list of Widget objects. All objects on the list are instances of the Widget class. The class has a member function getWeight() that returns the value of the object's weight data member. The class also has a member function getNext() that returns the address stored in the object's next data member. The next data member is a pointer to a Widget object. The number listed within the drawing of each Widget is the value of its weight data member. After the code below executes there will be _________ Widget objects deleted, _________ dangling pointers, and _________ "lost objects" that represent memory leaks. Widget *current = widgetList; Widget *byebye = NULL; bool done = false; while ( !done && current != NULL ) { if ( (current->getWeight() ) > 9.9 ) { byebye = current current = current->getNext(); delete byebye; done = false; } else { current = current->getNext(); } }

0, 0, 0 the list is unchanged by the given while while loop.

The drawing below represents the "state" of a linked list of Widget objects. All objects on the list are instances of the Widget class. The class has a member function getWeight() that returns the value of the object's weight data member. The class also has a member function getNext() that returns the address stored in the object's next data member. The next data member is a pointer to a Widget object. The number listed within the drawing of each Widget is the value of its weight data member. After the code below executes there will be _________ Widget objects deleted, _________ dangling pointers, and _________ "lost objects" that represent memory leaks. Widget *current = widgetList; bool done = false; while ( !done && current != NULL ) { if ( (current->getWeight() ) > 7.9 ) { delete current; done = true; } else { current = current->getNext(); } }

1, 2, 2. Object with weight 8.0 is deleted, the next pointer of object with weight 7.6 is dangling and the current pointer is dangling; objects with weight 3.2 and 2.6 are "lost" and represent memory leaks.

What value will be returned by the function call mystery(5)? int mystery(int number) { if (number == 0) return number; else return (number + mystery(number - 1)); } //END int mystery(int number)

15

Consider the self-referential Widget objects below. The numbers shown represent the value of the object's weight data member. At one point these objects were part of a larger, complete linked list. Assume that the drawing conventions discussed in class apply to this graphical representation. There are ___________ dangling pointer(s) and ___________ "lost" object(s) that represent memory leaks.

2 dangling pointers and 2 "lost" objects

What will the following program display on the screen? #include <iostream> using namespace std; const int V = 1980; class Package { private: int value; public: Package() { value = V; cout << value << endl; } Package(int v) { value = 20; cout << value << endl; } ~Package() { cout << "Good bye!" << endl; } }; int main() { Package obj1(1976); Package obj2; return 0; }

20 1980 Good bye! Good bye!

Given the following variable initializations: int a[5] = { 0, 10, 20, 30, 40 }; int k = 3; int *p = a + 1; What is the output from the following statement: cout << *(a + k);

30

The drawing below represents the "state" of a linked list of Widget objects. All objects on the list are instances of the Widget class. The class has a member function getWeight() that returns the value of the object's weight data member. The class also has a member function getNext() that returns the address stored in the object's next data member. The next data member is a pointer to a Widget object. The number listed within the drawing of each Widget is the value of its weight data member. After the code below executes there will be _________ Widget objects deleted, _________ dangling pointers, and _________ "lost objects" that represent memory leaks. Widget *current = widgetList; Widget *byebye = NULL; bool done = false; while ( !done && current != NULL ) { if ( (current->getWeight() ) > 1.9 ) { byebye = current current = current->getNext(); delete byebye; done = false; } else { current = current->getNext(); } }

5, 2, 0. All objects are deleted, there are no memory leaks, but there are two dangling pointers. The widgetList pointer was never moved, so it is still pointing to the address of the first object in the list, but that memory has been deallocated and is no longer valid. The current pointer moved past the end of the list, so it is pointing to NULL and is valid. The pointer byebye stopped on the last object which was also deallocated so byebye is a dangling pointer.

What will the following program display on screen? #include <iostream> using namespace std; class Tank { private: int gallons; public: Tank() { gallons = 50; } Tank(int gal) { gallons = gal; } int getGallons() { return gallons; } }; int main() { Tank storage1, storage2, storage3(20); cout << storage1.getGallons() << endl; cout << storage2.getGallons() << endl; cout << storage3.getGallons() << endl; return 0; }

50 50 20

What is the output generated by the following block of code? int x = 7; int *ptr = &x; cout << *ptr << endl;

7

Given the array definition: int numbers[ ] = {2, 4, 6, 8, 10}; What will the following statement display? cout << *(numbers + 3) << endl;

8

The ____________ operator may be used to assign one object to another, or to initialize one object with another object's data. By default, each member of one object is copied to its counterpart in the other object.

=

A class copy constructor is called when:

A variable/object is being initialized from an object of the same class. A function is called with a value parameter of the class. A function is returning a value that is an object of the class.

What does ADT stand for?

Abstract Data Type

A constructor may have a return type of ________. A) int B) bool C) void D) Any of the above E) None of the above

E

To indicate that a linked list is empty, you should set the pointer to its head to the value __________________________.

NULL

What will be output when the following C++ program runs? #include <iostream> using namespace std; class Dilbert { private: int a; int b; int c; public: Dilbert( int i=0, int j=0, int k=0 ); void setABC( int i, int j, int k ); void mystery() const; int getA() { return a; } int getB() { return b; } int getC() { return c; } }; //end class dilbert int main(int argc, const char * argv[]) { Dilbert d1; d1.mystery(); return 0; } Dilbert::Dilbert( int i, int j, int k) { this->a = i; this->b = j; this->c = k; } void Dilbert::setABC(int m, int n, int o) { a = m; b = n; c = o; } void Dilbert::mystery() const { cout << this << endl; }

a hexadecimal memory address

What action is performed by a class' default copy constructor?

a memberwise assignment

what action is preformed by a class' default copy constructor?

a memberwise assignment

A class member function that uses the value of a class variable but does not change it is know as a(n) ________________ function.

accessor

assuming that ptr is a pointer to an int, what happens when you add 4 to it?

add 4 times the size of int in memory

The basic linked list operations are:

adding an element to a list removing an element from the list traversing the list destroying the list

which arithmetic operators can be applied to pointers?

addition and subtraction

Each byte in memory is assigned a unique ________________________.

address

the ____________ operator can be used to determine a variable's address

address, &

Recursive functions work by breaking a complex problem down into subproblems of the same type. This breaking down process stops when it reaches a ________________________.

base case

A(n) _________________ is a member function that is automatically called when a class object is created.

constructor

a(n) _________ is a member function that is automatically called when a class object is created

constructor

The class Stuff has both a copy constructor and an overloaded = operator. Assume that blob and clump are both instances of the Stuff class. For the statement below, indicate whether the copy constructor or the overloaded = operator will be called: Stuff splat = clump;

copy constructor

The class Stuff has both a copy constructor and an overloaded = operator. Assume that blob and clump are both instances of the Stuff class. For the statement below, indicate whether the copy constructor or the overloaded = operator will be called: showValues(blob); //blob is passed by value

copy constructor

A class may have many constructors, but can only have one ______________ constructor.

default

When a program is finished with a chunk of dynamically allocated memory, it should free it with the __________ operator.

delete

When the indirection operator (*) is placed in front of a pointer variable name, it ____________________________ the pointer, thereby allowing you to actually work with the value to which the pointer variable points.

dereferences

A(n) _________________ is a member function that is automatically called when a class object is destroyed.

destructor

creating new variables while a program is running is called

dynamic memory allocation

What is the general case of the recursive function shown below? int mystery(int number) { if (number == 0) return number; else return (number + mystery(number - 1)); } //END int mystery(int number)

else return (number + mystery(number - 1));

Object-oriented programming is centered around objects that __________________ both data and the functions that operate on them.

encapsulate

object-oriented programming is centered around objects that _____ both data and the functions that operate on them

encapsulate

A copy constructor is a special constructor that is called whenever a new object is created and initialized with the data of another object of a different class.

false

All C++ operators may be overloaded.

false

Declaring a pointer to a class requires special syntax that is different than declaring a pointer to C++ simple data types (int, char, etc.)

false

It is common to name an mutator function with the word get followed by the name of the member variable whose value it is setting.

false

Just as a class can have multiple constructors, it can also have multiple destructors.

false

No mathematical operations are allowed on pointer variables.

false

Object oriented programming is centered around functions, or procedures.

false

The recursive factorial function accepts an argument and calculates its factorial. Its base case is when the argument is missing.

false

There is no recursive method for finding the greatest common divisor (gcd) of two numbers.

false

Assume that an array named a1 exists and that its size is 100 and that its elements have been correctly assigned values. Consider the following C++ code. for (int x = 0; x < 100; x++) cout << a1[x] << endl; Which of the following blocks of code is equivalent to the loop above (note that some of the options could contain logic and/or syntax errors)?

for (int x = 0; x < 100; x++) cout << *(a1 + x) << endl;

A(n) ___________________ is a function that is not a member of a class, but has access to the private members of the class.

friend

The ______________________ points to the first node in a linked list.

head

What is the base case of the recursive function shown below? int mystery(int number) { if (number == 0) return number; else return (number + mystery(number - 1)); } //END int mystery(int number)

if (number == 0) return number;

the _____________ operator can be used to work with the variable a pointer points to

indirection, dereferencing, *

When the implementation code for a class function is located inside the class definition itself, the function is called a(n) _____________ function.

inline

an object is a(n) ____ of a class

instance

Only an object's ________________ functions can directly access and make changes to its data.

member

A class member function that stores a value in a member variable or changes the value of a member variable is called a(n) ____________________ function.

mutator

a pointer that contains the address 0 is called a(n) _________ operator

null

C++ allows you to overload ____

operators and functions

The class Stuff has both a copy constructor and an overloaded = operator. Assume that blob and clump are both instances of the Stuff class. For the statement below, indicate whether the copy constructor or the overloaded = operator will be called: blob.operator=(clump);

overloaded = operator

The class Stuff has both a copy constructor and an overloaded = operator. Assume that blob and clump are both instances of the Stuff class. For the statement below, indicate whether the copy constructor or the overloaded = operator will be called: clump = blob;

overloaded = operator

Two or more functions may have the same name, as long as their _________ are different.

parameter lists

________ variables are designed to hold addresses

pointer

Array names can be used as ____,and vice versa.

pointers

Array names can be used as ____________________ and vice versa.

pointers

The double colon symbol (::) is called the _______________ operator.

scope resolution

the double colon symbol (::) is called the ___________ operator

scope resolution

A data structure that points to an object of the same type as itself is known as a(n) ___________________________ data structure.

self-referential

Every instance of a class maintains a hidden pointer to itself. The identifier of that pointer is:

this

what is the purpose of the new operator?

to dynamically allocate memory at run time

A class is a programmer-defined data type that describes what objects of the class will look like when they are created.

true

A copy constructor must have a single parameter that is a reference to the same class. Forgetting the & that identifies reference parameters will result in compiler errors.

true

A recursive function is one that calls itself.

true

An abstraction is a general model of something.

true

An array name, without brackets and a subscript, actually represents the starting address of the array. This means that an array name is really a pointer to the first element of the array.

true

C++ relational operators may be used to compare pointer values.

true

Dynamically allocated data structures may be linked together in memory to form a chain.

true

Every variable is assigned a memory location whose address can be retrieved using the "address of" operator: &. The address of a memory location is called a pointer.

true

If the programmer does not specify a copy constructor for the class, then the compiler automatically calls a default copy constructor when necessary.

true

In addition to providing a means for the creation of objects, convert constructors provide a way for the compiler to convert a value of a given type to an object of the class.

true

In some object-oriented programming languages, the procedures that an object performs are called methods.

true

It is common to name an accessor function with the word get followed by the name of the member variable whose value it is getting.

true

It is important for programs that use dynamically allocated memory to ensure that each call to new is eventually followed by a call to delete that frees the allocated memory.

true

Multiple assignment statements (e.g. a = b = c) work because the build-in assignment operator is implemented so that it returns the value of its left operand after the assignment has been performed.

true

Object oriented programming is centered around objects.

true

Some problems naturally lend themselves to recursive solutions.

true

You should consider the use of recursion when there is a way to express the solution of a problem in terms of solutions of simpler, or smaller, problems of the same type.

true

Below is the prototype for a function that takes two pointers to integer variables as its parameters. The purpose of the function is to exchange the values stored in the integer variables to which the two pointers point. Consider the proposed implementation code for this function and select all options that would correctly accomplish the intent of the function. void exchange ( int *p, int *q );

void exchange( int *p, int *q ) { int temp = *p; *p = *q; *q = temp; }


Ensembles d'études connexes

Math Common Core Standards + Glencoe Course 2 Volume 2

View Set

Prof Kolesar's Adult Health Midterm EAQs

View Set

BCIS 4720 Chapter 2 - Practice Exam

View Set

Chapter 2: The Environment and Corporate Culture

View Set

Seans florida exam study guide 2023 march :)

View Set

Chapter 42 - Fluids_IV Therapy & Maintaining Flow Rate

View Set

Chapter 13. Marketing: Helping Buyers Buy

View Set