Object Oriented Programming FINAL Exam (class)

0.0(0)
studied byStudied by 3 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/233

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

234 Terms

1
New cards

If p1 is an integer pointer variable with the value of 1000, p1++ changes P1 to point to the memory location 1001.

FALSE

2
New cards

When you return a dynamic array to the freestore, you must include the number of elements in the array.

False

3
New cards

You can assign an array to a pointer variable.

TRUE

4
New cards

The size of dynamic arrays must be declared at compile time.

FALSE

5
New cards

int *p1;

declares a static variable.

FALSE

6
New cards

Dynamically created variables have no name.

TRUE

7
New cards

If p1 and p2 are both pointers that point to integers in memory, the condition p1==p2 will be true if the values that are in those memory locations are the same.

FALSE

8
New cards

Even though pointers point to addresses which are integers, you can not assign an integer to a pointer variable.

TRUE

9
New cards

In the following statement, all the variables are pointers.

int* p1, p2;

FALSE

10
New cards

A pointer can be stored in an integer variable.

FALSE

11
New cards

A ___________ is the memory address of a variable.

pointer

12
New cards

Declare a pointer variable named ptr to an integer.

int *ptr;

13
New cards

In the statement cout << p1;, the is called the ________________

dereferencing operator

14
New cards

The & operator is called the _______________

address of operator

15
New cards

Write the code that assigns to p1 (an integer pointer variable) the pointer to a dynamically created integer.

p1 = new int;

16
New cards

Dynamic variables are created at __________

run time

17
New cards

Dynamic variables are created from the ___________.

freestore or heap

18
New cards

Write the code to return the dynamic memory pointed to by p1 to the freestore.

delete p1;

19
New cards

The size of a dynamic array is defined at ___________

run-time

20
New cards

Write the code to declare a dynamic array of strings (use the string pointer variable p1) that has as many elements as the variable arraySize.

p1 = new string[arraySize];

21
New cards

Which of the following correctly declares 3 integer pointers?

a. int* p1, p2, p3;

b. int *p1, p2, p3;

c. int p1, p2, *p3;

d. all of the above.

c. int p1, p2, *p3;

22
New cards

Which of the following assigns to p1 the pointer to the address of value?

a. *p1=&value;

b. p1=value;

c. p1=&value;

d. &p1 = *value;

c. p1=&value;

23
New cards

What is the output of the following code fragment?

int v1=2, v2=-1, p1, p2;

p1 = & v1;

p2= & v2;

p2=p1;

cout << *p2 << endl;

a. 2

b. -1

c. -2

d. 1

a. 2

24
New cards

Which of the following statements correctly prints out the value that is in the memory address that the pointer p1 is pointing to?

a. cout << &p1;

b. cout << p1;

c. cout << int* p1;

d. cout << *p1;

d. cout << *p1;

25
New cards

Given that p1 is a pointer variable of the string class, which of the following are legal statements?

a. p1 = new int;

b. cout << *p1;

c. p1 = new char[10];

d. *p1 = new string;

e. B and D

b. cout << *p1;

26
New cards

What is the output of the following code fragment?

float *p1;

p1 = new float(3);

cout << *p1;

a. 3.0

b. unknown, the address p1 points to is not initialized

c. unknown, the code is illegal, p1 points to a dynamic array

d. 0.0

a. 3.0

27
New cards

What is the output of the following code?

int p1, p2;

p1 = new int;

p2 = new int;

*p1=11;

*p2=0;

p2=p1;

cout << p1 <<" " << p2 << endl;

a. 11 0

b. 0 11

c. 11 11

d. 0 0

c. 11 11

28
New cards

What is wrong with the following code fragment?

int p1, p2;

p1 = new int;

p2 = new int;

*p1=11;

*p2=0;

p2=p1;

cout << p1 <<" " << p2 << endl;

delete p1;

delete p2;

a. nothing

b. p1 and p2 both have the same value, so the delete p2 will cause an error

c. You have a memory leak.

d. B and C

d. B and C

29
New cards

Which of the following correctly declares a user-defined data type that defines a pointer to a float?

a. float* floatPtr ;

b. typedef float* floatPtr;

c. typedef floatPtr *float;

d. typedef floatPtr* float

b. typedef float* floatPtr;

30
New cards

Given that a typedef for IntPtr defines a pointer to an integer, what would be the correct declaration for a function that expects a reference to an integer pointer?

a. void f1 (IntPtr& ptr);

b. void f1 (IntPtr&* ptr);

c. void f1 (IntPtr*& ptr);

d. All of the above

a. void f1 (IntPtr& ptr);

31
New cards

Which of the following correctly declares a dynamic array of strings?

a. p1 = new string(13);

b. p1 = new string[];

c. p1 = new string[13];

d. p1 = new stringArray(13);

c. p1 = new string[13];

32
New cards

Given that p1 is an integer pointer variable, and a1 is an integer array, which of the following statements are not legal code?

a. p1= a1;

b. cout << p1[0];

c. cin >> p1[0];

d. a1 = p1;

d. a1 = p1;

33
New cards

Assuming that the pointer variable p1 is of the correct type and size is an integer with some value > 1, which of the following declarations are legal?

a. p1 = new string[size];

b. p1 = new ifstream[size];

c. p1 = new char[size*size];

d. A and B

e. A, B and C

e. A, B and C

34
New cards

Which of the following statements correctly returns the memory from the dynamic array pointer to by p1 to the freestore?

a. delete [] p1;

b. delete p1[];

c. delete *p1;

d. delete p1;

a. delete [] p1;

35
New cards

If p1 is an integer pointer that is pointing to memory location 1001, and an integer takes 4 bytes, then (p1+1) evaluates to:

a. 1002

b. 1004

c. 1005

d. Unknown

c. 1005

36
New cards

Given that p1 is a pointer, p1++

a. always causes a run time error

b. advances p1 by one unit of the type of variable to which p1 points

c. adds 1 to whatever p1 is pointing to

d. adds one element to the array that p1 is pointing to

b. advances p1 by one unit of the type of variable to which p1 points

37
New cards

If a program requires a dynamically allocate two-dimensional array, you would allocate the memory by using

a. p1 = new int*[numRows];

for(int i=0; i < numRows; i++)

p1[i] = new int[numColumns];

b. p1 = new int*[numRows][numColumns];

c. p1 = new[numRows][numColumns]int;

d. none of the above

a. p1 = new int*[numRows];

for(int i=0; i < numRows; i++)

p1[i] = new int[numColumns];

38
New cards

If two pointer variables point to the same memory location, what happens when one of the pointers is freed?

a. The other pointer should be considered to be un-initialized

b. The other pointer still points to a valid memory address

c. If you attempt to free the other pointer a run-time error will occur.

d. All of the above

e. A and C

e. A and C

39
New cards

Which of the following is not true?

a. a pointer can be assigned the address of an array

b. an array can be assigned the value in a pointer variable

c. if a pointer points to an array, it can be used in place of the array name

d. if a pointer points to an array, the pointer does not know how many elements are in the array

b. an array can be assigned the value in a pointer variable

40
New cards

In which case would you consider using a dynamic array?

a. If the array is small, and the size is known before the program runs.

b. If the program needs to get the size of the array from the user

c. If the array size is big, but known at compile time

d. You should always use a dynamic array

b. If the program needs to get the size of the array from the user

41
New cards

This code returns a reference to an object. It should compile, but something is wrong. What is wrong?

Foo& someFunction(int i)

{

Foo f;

f.doSomethingIncredible(i);

return f;

}

a. It returns a reference to a local variable that is destroyed (goes out of scope)when the function exits

b. Variable i should be passed by pointer

c. You cannot return a variable by reference

a. It returns a reference to a local variable that is destroyed (goes out of scope)when the function exits

42
New cards

A struct variable is declared differently from a predefined type such as an int.

FALSE

43
New cards

Two different structure definitions may have the same member names.

TRUE

44
New cards

A structure can only be passed to a function as a call-by-value parameter

FALSE

45
New cards

A function may return a structure.

TRUE

46
New cards

Different class may not have member functions with the same name.

FALSE

47
New cards

A class member function may be private.

TRUE

48
New cards

Class data members are almost always public.

FALSE

49
New cards

It is possible to have multiple private labels in a class definition.

TRUE

50
New cards

The assignment operator may not be used with objects of a class.

FALSE

51
New cards

All constructors for a class must be private.

FALSE

52
New cards

A derived class is more specific than its parent, or base class.

TRUE

53
New cards

The keyword ________ defines a structure type definition.

struct

54
New cards

A structure definition ends with the closing brace and a _________.

semicolon

55
New cards

A structure variable is a collection of smaller values called ____________ values

member

56
New cards

When a structure contains another structure variable as one of its members, it isknown as a ___________________.

hierarchical structure

57
New cards

When several items (variables or variables and functions) are grouped togetherinto a single package, that is known as ______________.

(data) encapsulation

58
New cards

The double colon (::) is known as the __________ operator.

scope resolution operator

59
New cards

Who can access private members in a class?

only other members of the class

60
New cards

A member function that allows the user of the class to find out the value of aprivate data type is called a ___________________.

accessor function

61
New cards

A member function that allows the user of the class to change the value of aprivate data type is called a ___________________

mutator function

62
New cards

If you have a class with a member function called display(ostream& out), that will send the values in the class to the parameter stream, and you need to call that function from within another member function, how would you call it to print the data to the screen? ___________________________

display(cout);

63
New cards

What can a constructor return? _______________

nothing

64
New cards

The name of a constructor is _____________

the name of the class

65
New cards

The constructor of a class that does not have any parameters is called a__________ constructor

default

66
New cards

In the following class constructor definition, the part of the header starting with asingle colon is called the ________________.

BankAccount::BankAccount(): balance(0), interest(0.0)

initialization section

67
New cards

A class in which modifications to the implementation appear to be invisible to theuser of the class is known as _________________

an Abstract Data Type (ADT)

68
New cards

A member function that gets called automatically when an object of the class isdeclared is called a _______________

constructor

69
New cards

If class A is derived from class B, then B is a _______ of A

parent

70
New cards

C++11 allows you to directly set the member variables to initial values in the definition of the class. This feature is called __________________

member initializers

71
New cards

C++11 allows you to have a constructor call another constructor. This feature iscalled _________________________

constructor delegation

72
New cards

In a structure definition, the identifiers declared in the braces are called

a. classes

b. structs

c. member names

d. variables

c. member names

73
New cards

You specify an individual member of a struct by using

a. the assignment operator

b. an ampersand

c. an underscore

d. The dot operator

d. The dot operator

74
New cards

To assign values to a structure variable, you use the

a. equals operator

b. assignment operator

c. extraction operator

d. less than operator

b. assignment operator

75
New cards

What is wrong with the following structure definition?

struct MyStruct{int size;

float weight;

}

a. Nothing

b. Can not have mixed data types in a structure

c. missing semicolon

d. Braces are not needed

c. missing semicolon

76
New cards

Given the following structure definitions, what is the correct way to print the person's birth year?

struct DateType

{

int day;

int month;

int year;

}

struct PersonType

{

int age;

float weight;

DateType birthday;

}

PersonType person;

a. cout << person.birthday.year;

b. cout << year;

c. cout << birthday.year;

d. cout << peson.year;

a. cout << person.birthday.year;

77
New cards

Given the following structure definition, what is the correct way to initialize a variable called today?

struct DateType

{

int day;

int month;

int year;

}

a. DateType today(1,1,2000);

b. DateType today = (1,1,2000);

c. DateType today = {1,1,2000);

d. DateType today = {1,1,2000,0);

c. DateType today = {1,1,2000);

78
New cards

When defining a class, the class should be composed of the kind of values a variable of the class can contain, and

a. member functions for that class

b. the keyword private

c. other class definitions

d. nothing else

a. member functions for that class

79
New cards

Which of the following is the correct function definition header for the getAge function which is a member of the Person class?

a. int getAge();

b. int getAge()

c. int Person:getAge()

d. int Person::getAge()

d. int Person::getAge()

80
New cards

Given the following class definition and the following member function header,which is the correct way to output the private data?

class Person

{

public:

void outputPerson(ostream& out);

private:

int age;

float weight;

int id;

};

void Person::outputPerson(ostream& out)

{

//what goes here?

}

a. out << person.age << person.weight << person.id;

b. out << person;

c. out << age << weight << id;

d. outputPerson(person);

c. out << age << weight << id;

81
New cards

Why do you want to usually make data members private in a class?

a. so that no one can use the class.

b. ensure data integrity

c. provide data abstraction.

d. provide information hiding.

e. B and D

f. B, C and D

f. B, C and D

82
New cards

A member function of a class should be made private

a. always

b. only if it will never be used

c. if it will only be used by other members of the class

d. never, it is illegal to make a member function private

c. if it will only be used by other members of the class

83
New cards

A member function that allow the user of the class to change the value in a data member is known as

a. a mutator function

b. a mutation

c. a manipulator function

d. an accessor function

a. a mutator function

84
New cards

A Member function that allows the user of the class to see the value in a data member is known as

a. a mutator function

b. a mutation

c. a manipulator function

d. an accessor function

d. an accessor function

85
New cards

If you design a class with private data members, and do not provide mutators and accessors, then

a. The private data members can still be accessed from outside the class by using the & operator

b. The data can not be changed or viewed by anyone.

c. None of the above

d. A and B

b. The data can not be changed or viewed by anyone.

86
New cards

A class member function that automatically initializes the data members of a class is called

a. the init function

b. an operator

c. a constructor

d. a cast

c. a constructor

87
New cards

If you have a class named myPersonClass, which of the following correctly declare a constructor in the class definition?

a. myPersonClass::myPersonClass();

b. myPersonClass();

c. init();

d. cast();

b. myPersonClass();

88
New cards

given the following class definition, how could you use the constructor to assign values to an object of this class?class CDAccount{

public:

CDAccount();

CDAccount(float interest, float newBalance);

float getBalance();

float getRate();

void setRate(float interest);

void setBalance(float newBalance);

private:

float balance, rate;

};

and the following object declarationCDAccount myAccount;

a. myAccount = CDAccount(float myRate, float myBalance);

b. myAccount = CDAccount {myRate, myBalance};

c. myAccount = CDAccount[myRate, myBalance];

d. myAccount = CDAccount(myRate, myBalance);

d. myAccount = CDAccount(myRate, myBalance);

89
New cards

Given the following class definition, what is missing?

class ItemClass

{

public:

ItemClass(int newSize, float newCost);

int getSize();

float getCost();

void setSize(int newSize);

void setCost(float newCost);

private:

int size;

float cost;

};

a. nothing

b. a default constructor

c. accessor functions

d. mutator functions

b. a default constructor

90
New cards

Given the following class definition, how would you declare an object of the class, so that the object automatically called the default constructor?

class ItemClass

{

public:

ItemClass();

ItemClass(int newSize, float newCost);

int getSize();

float getCost();

void setSize(int newSize);

void setCost(float newCost);

private:

int size;

float cost;

};

a. ItemClass() myItem;

b. ItemClass myItem(1, 0.0);

c. ItemClass myItem;

d. ItemClass myItem();

e. You can not do this

c. ItemClass myItem;

91
New cards

A data type consisting of data members and operations on those members which can be used by a programmer without knowing the implementation details of the data type is called

a. an abstract definition type

b. an available data type

c. an abstract data type

d. a primitive data type

c. an abstract data type

92
New cards

Which part of the ADT tells the programmer using it how to use it?

a. the implementation

b. the interface

c. the abstractness

d. the scope resolution

b. the interface

93
New cards

If you are designing a class for an ADT, you can tell if the class is an ADT if

a. when you change the implementation of the class, none of the rest of the program needs to change.

b. when you change the interface of the class, nothing else needs to change.

c. you change the private part and the rest of the program using the ADT does not compile.

d. everything must be changed

a. when you change the implementation of the class, none of the rest of the program needs to change.

94
New cards

Developing an ADT means that the user of your class does not have to know the details about how the class is implemented. This is known as

a. interface

b. implementation

c. testing and debugging

d. information hiding

d. information hiding

95
New cards

Given the following class, what would be the best declaration for a mutator function that allows the user of the class to change the age?

class Wine

{

public:

Wine();

int getAge();

float getCost();

private:

int age;

float cost;

}

a. int getAge(int newAge);

b. Wine();

c. void setAge();

d. void setAge(int newAge);

d. void setAge(int newAge);

96
New cards

Given the following class, what would be the best declaration for a constructor that would allow the user to initialize the object with an initial age and cost?

class Wine

{

public:

Wine();

int getAge();

float getCost();

private:

int age;

float cost;

}

a. int getAge(int newAge);

b. Wine();

c. Wine(int age);

d. Wine(int newAge, float newCost);

d. Wine(int newAge, float newCost);

97
New cards

Given the following class and object declaration, how would you print out the age and cost of a bottle of wine?

class Wine

{

public:

Wine();

int getAge();

float getCost();

private:

int age;

float cost;

}

Wine bottle;

a. cout << bottle;

b. cout << Wine.age, Wine.cost;

c. cout << bottle.getAge() << bottle.getCost();

d. cout << bottle.getAge << bottle.getCost;

e. cout << bottle.age << bottle.cost;

c. cout << bottle.getAge() << bottle.getCost();

98
New cards

Data members or member functions of a class that are declared to be private may

a. only be accessed by the main program

b. only be accessed by members of the class

c. not be accessed by the class

d. are considered to be global variables

b. only be accessed by members of the class

99
New cards

Member functions of a class

a. may not be in the private section

b. must be in the private section

c. may be in either section

d. can not be called in the main program

c. may be in either section

100
New cards

In a struct, all members are ____________ by default

a. public

b. private

c. global

d. all of the above

a. public