Exercise/Home Work Class 4 (Solution)

CPSC 230 (Lecture 34)

Department of Physics, Computer Science and Engineering

Christopher Newport University

Question 1

Object-oriented programming primarily focuses on the

(a) classes.

(b) functions.

(c) variables.

(d) interface.

Question 2

The proper format for a struct is

(a) struct Time

int hour

int minute;

(b) struct Time {

int hour,

int minute,

}

(c) struct Time {

int hour;

int minute;

}

(d) struct Time {

int hour;

int minute;

};

Question 3

structs are not allowed to contain

(a) pointers to structs of different types.

(b) structs of the same type.

(c) pointers to themselves.

(d) both floats and ints.

Question 4

Which of the following is not true?

(a) classes contain both data members and member functions

(b) a class definition must be terminated with a semicolon

(c) all classes can be represented as structs

(d) the body of a class definition is delineated with left and right braces

Question 5

Member access specifiers (public and private) can appear

(a) in any order and multiple times.

(b) in any order (public first or private first) but not multiple times.

(c) in any order and multiple times, if they have brackets separating each type.

(d) multiple times, but all specifiers of the same type must be grouped together.

Question 6

Member function definitions

(a) always require the binary scope operator (::).

(b) only require the binary scope operator when being defined outside of the scope of

their class.

(c) can use the binary scope operator anywhere, but become public functions.

(d) must use the binary scope operator in their function prototype.

Question 7

Variables defined inside a member function of a class have

(a) file scope.

(b) class scope.

(c) function scope.

(d) class or function scope, depending on whether the binary scope resolution operator (::) is used.

Question 8

By default, class variables declared without an access modifier

(a) can be modified by functions outside the class.

(b) cannot be modified except by private functions of other classes.

(c) can only be modified by private functions inside that class.

(d) can be modified by any function inside that class or by friends of the class.

Question 9

Non-static member variables declared private

(a) can never be accessed by the client.

(b) can never be modified by the client.

(c) can be accessed and/or modified by any object of the same class.

(d) can be accessed and/or modified by public member functions and by friends of the

class.

Question 10

The type of function a client would use to check the balance of his bank account would be

(a) an access function.

(b) a predicate function.

(c) a utility function.

(d) a constructor

Question 11

Utility functions

(a) are part of a class’ interface

(b) are separate member functions that support operations of the class’ public member functions

(c) are intended to be used by clients of a class

(d) are a type of destructor

Question 12

Constructors are not

(a) required to be explicitly defined.

(b) called automatically when an object is initialized.

(c) able to be overloaded.

(d) member functions.

Question 13

A class may contain multiple constructors if

(a) they have different names.

(b) they have different argument lists.

(c) they have the same argument list.

(d) they have different return types.

Question 14

Which of the following is not true of a constructor and destructor of the same class?

(a) they both have same name aside from the tilde (~) character.

(b) they are both called once per object (in general).

(c) they both are able to accept default arguments.

(d) both are called automatically, even if not defined in the class.

Question 15

Given the class definition

class CreateDestroy {

public:

CreateDestroy() { cout < "constructor called, "; }

~CreateDestroy() { cout < "destructor called, "; }

};

What will the following program output?

int main() {

CreateDestroy c1;

CreateDestroy c2;

return 0;

}

(a) constructor called, destructor called, constructor

called, destructor called

(b) constructor called, destructor called

(c) constructor called, constructor called

(d) constructor called, constructor called, destructor

called, destructor called