Object Oriented Programming Midterm Exam 2 Review

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/80

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

81 Terms

1
New cards

c. int p1, p2, *p3;

Which of the following correctly declare 3 integer pointers?

a. int* p1, p2, p3;

b. int *p1, p2, p3;

c. int p1, p2, *p3;

d. all of the above.

2
New cards

c. p1=&value;

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;

3
New cards

a. 2

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

4
New cards

d. cout << *p1;

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;

5
New cards

b. cout << *p1;

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

6
New cards

a. 3.0

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

7
New cards

c. 11 11

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

8
New cards

d. B and C

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

9
New cards

b. typedef float* floatPtr;

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

10
New cards

a. void f1 (IntPtr& ptr);

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

11
New cards

c. p1 = new string[13];

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);

12
New cards

d. a1 = p1;

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;

13
New cards

e. A, B and C

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

14
New cards

a. delete [] p1;

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;

15
New cards

c. 1005

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

16
New cards

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

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

17
New cards

a. p1 = new int*[numRows];

for(int i=0; i < numRows; i++)p1[i] = new int[numColumns];

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 aboveANSWER

18
New cards

e. A and C

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

19
New cards

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

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.

20
New cards

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

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

21
New cards

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

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

22
New cards

c. member names

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

a. classes

b. structs

c. member names

d. variables

23
New cards

d. The dot operator

You specify an individual member of a struct by using

a. the assignment operator

b. an ampersand

c. an underscore

d. The dot operator

24
New cards

b. assignment operator

To assign values to a structure variable, you use the

a. equals operator

b. assignment operator

c. extraction operator

d. less than operator

25
New cards

c. missing semicolon

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.

26
New cards

a. cout << person.birthday.year;

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;

27
New cards

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

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);

28
New cards

a. member functions for that class

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

29
New cards

d. int Person::getAge()

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()

30
New cards

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

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);

31
New cards

f. B, C and D

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

32
New cards

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

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.

33
New cards

a. a mutator function

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

34
New cards

d. an accessor function

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

35
New cards

c. None of the above

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

36
New cards

c. a constructor

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

37
New cards

b. myPersonClass();

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();

38
New cards

d. myAccount = CDAccount(myRate, myBalance);

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);

39
New cards

b. a default constructor

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

40
New cards

c. ItemClass myItem;

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

41
New cards

c. an abstract data type

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

42
New cards

b. the interface

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

43
New cards

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

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.

44
New cards

d. information hiding

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

45
New cards

d. void setAge(int newAge);

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);

46
New cards

d. 7

1. How many members (data and functions) does the following class have?

class Rational{public:Rational();

Rational(int numer, int denom);

Rational(int whole);

int getNumerator();

int getDenominator();

friend void display(ostream& out, const Rational& value);

private:int numerator;

int denominator;};

a. 2

b. 6

c. 5

d. 7

e. 8

47
New cards

a. members of the class

Who can access private data in a class?

a. members of the class

b. friends of the class

c. everyone

d. B and C

e. no one

48
New cards

b. void display(ostream& out, const Rational& value)

Given the following class, which is the correct function header for the display function?

class Rational{public:

Rational();

Rational(int numer, int denom);

Rational(int whole);

int getNumerator();

int getDenominator();

friend void display(ostream& out, const Rational& value);

private:

int numerator;

int denominator;

};

a. friend void display(ostream& out, const Rational& value)

b. void display(ostream& out, const Rational& value)

c. void Rational::display(ostream& out, const Rational& value)

d. friend void Rational::display(ostream& out, const Rational& value)

49
New cards

d. All of the above

Operators can be overloaded as

a. friends of a class

b. members of a class

c. non-friends, non-members of a class

d. All of the above

50
New cards

b. More efficient access to the private data members.

If we have a full selection of accessor and mutator functions, why would we have friend functions?

a. You should not have them

b. More efficient access to the private data members.

c. The friend function must call the accessor or mutator functions anyway.

d. none of the above

51
New cards

c. const

Since accessor functions in a class do not modify or mutate the data members ofthe object, the function should have the __________ modifier.

a. reference

b. friend

c. const

d. private

52
New cards

e. A and C

Why should you generally pass an object of the class to a friend function as a reference parameter?

a. If the friend function changes the values of the data member(s).

b. If the friend function will not change the values of the data member(s).

c. It is more efficient to pass the object by reference.

d. A and B

e. A and C

53
New cards

c. The integer equivalent of the digit stored in c

If c is a character variable that contains a digit, what does the following function return?

int digit_to_int(char c)

{

return ( int(c) - int('0'));

}

a. The ASCII value of c

b. The character value of c

c. The integer equivalent of the digit stored in c

d. none of the above

54
New cards

c. The function may not modify any of the private data members

What is wrong with the following member function definition given the class below?

class Rational{public:Rational();

Rational(int numer, int denom);

Rational(int whole);

int getNumerator() const;

int getDenominator() const;

friend void display(ostream& out, const Rational& value);

private:int numerator;

int denominator;

};

int Rational::getNumerator() const{

numerator = 0;

return numerator;

}

a. You can not set the numerator to zero

b. The function may not modify numerator, but it can modify denominator

c. The function may not modify any of the private data members

d. nothing

e. A and Bf. A and C

55
New cards

c. The get functions are not const functions

Given the following class, what is syntactically wrong with the implementation of the display function?

class Rational{public:Rational();

Rational(int numer, int denom);

Rational(int whole);

int getNumerator();

int getDenominator();

friend void display(ostream& out, const Rational& value);

private:int numerator;

int denominator;

};

void display(ostream& out, const Rational& value)

{out << value.getNumerator() << '/"<

56
New cards

b. operator

To overload functions with symbolic names (like + - / <<), you must use thekeyword _______ before the symbolic name.

a. const

b. operator

c. reference

d. void

57
New cards

a. s1

In the following code fragment, which is the calling object for the less-than operator?

string s1, s2;

if( s1 < s2 )

a. s1

b. s2

c. <

d. none

58
New cards

f. B or D

Given the following class declaration,

class Rational{

public:

Rational();

Rational(int numer, int denom);

int getNumerator() const;

int getDenominator() const;

friend void display(ostream& out, const Rational& value);

friend bool operator(const Rational& left, const Rational& right);

private:

int numerator;

int denominator;

};

What must we add to the class in order for the following code to compile?

Rational myRational(2,3);

if ( 3 < myRational)

a. We need another < operator that expects an integer as the second parameter.

b. We need another < operator that expects an integer as the first parameter.

c. We need a constructor that expects a rational number

d. We need a constructor that expects an integer

e. A or D

f. B or D

59
New cards

d. All of the above

When overloading an operator, which of the following is true?

a. One of the arguments must be an object of the class

b. The operator can be a friend or a member of the class.

c. The operator does not have to be a friend or a member of the class

d. All of the above

e. None of the above

60
New cards

b. Object should not be a const parameter

What is wrong with the following overloaded extraction operator declaration?

istream& operator >>(istream& in, const myClass &object);

a. Object should not be a pass-by-reference parameter

b. Object should not be a const parameter

c. You can not put the & on the return type

d. nothing

61
New cards

c. friend Rational operator+( const Rational &left, const Rational &right);

Which of the following would be an appropriate function declaration to add two rational numbers?

a. void friend operator+( const Rational &left, const Rational &right);

b. void operatator+( const Rational &left, const Rational &right);

c. friend Rational operator+( const Rational &left, const Rational &right);

d. Rational operator+( const Rational &left, const Rational &right);

62
New cards

c. 2

How many parameters are there in a binary operator implemented as a friend?

a. 0

b. 1

c. 2

d. as many as you need

63
New cards

b. 1

How many parameters are there in a unary operator implemented as a friend?

a. 0

b. 1

c. 2

d. as many as you need

64
New cards

b. void display(const myClass& object)

Given the following function declaration,friend void display(const myClass& object);

Which is the correct header for the definition of the function?

a. void myClass::display(const myClass& object)

b. void display(const myClass& object)

c. friend void display(const myClass& object);

d. friend void display(const myClass& object)

65
New cards

c. friend Rational operator *(const Rational &left, const Rational &right);

Which of the following function declarations would be correct to overload the multiply operator for the Rational numbers class?

a. friend Rational operator times(const Rational &left, const Rational&right);

b. Rational operator times(const Rational &left, const Rational &right);

c. friend Rational operator *(const Rational &left, const Rational &right);

d. Rational operator *(const Rational &left, const Rational &right);

66
New cards

a. Because the first parameter must be the stream object.

Why are the extraction and insertion operators always implemented as friends of the class rather than as members of the class?

a. Because the first parameter must be the stream object.

b. They don't, they could be members

c. Because they return a reference

d. Because the stream is passed by reference.

67
New cards

f. A and D

If you want to be able to compile the following code, Rational r1;

int x;

cout << r1 + x << endl;

which overloaded operator(s) do you need?

a. friend Rational operator+( const Rational& left, int right);

b. friend void operator+ (const Rational& left, int right);

c. friend ostream operator << (ostream& out, const Rational& object);

d. friend ostream& operator << (ostream& out, const Rational& object);

e. A and C

f. A and D

68
New cards

a. Overloaded constructors

What member functions do you need to allow the compiler to perform automatic type conversions from a type different than the class to the class?

a. Overloaded constructors

b. Converters

c. This can not be done

d. This already happens automatically

69
New cards

a. the stream

In an overloaded insertion or extraction operator, which object should be the first parameter, the stream or the object of the class?

a. the stream

b. the object

c. it doesn't matter

d. none of the above

70
New cards

c. .

Which of the following operators can not be overloaded?

a. =

b. ==

c. .

d. []

71
New cards

d. cout << people[9].getAge();

Given the following class and array declaration, how would you print out the age of the 10th person in the array?

class personClass{public:

void setAge(int newAge);

void setGender( char newGender);

void setSalary(float newSalary);

int getAge();

char getGender();

float getSalary();

private:

int age;

char gender;

float salary;};

personClass people[100];

a. cout << people[10];

b. cout << people[9];

c. cout << people[9].age;

d. cout << people[9].getAge();

72
New cards

c. All of the above.

Which of the following statements are true?

a. Array elements may be user-defined types (structs or classes)

b. If a function is expecting a variable of the base type of the array, then you can pass an element of the array to the function.

c. All of the above.

d. None of the above.

73
New cards

c. A and B

Which of the following statements are true?

a. A dynamic array can have a base type which is a class or a struct.

b. A class or a struct may have a member which is a dynamic array.

c. A and B

d. Neither

74
New cards

c. the default constructor

When a dynamic array with a class for a base type is declared, which constructor is called?

a. the copy constructor

b. the destructor

c. the default constructor

d. an explicit constructor

75
New cards

e. all of the above

When you have a class which has a dynamic array as one of it's members, you should also include _____________ in the class.

a. a copy constructor

b. a default constructor

c. a destructor

d. the assignment operator

e. all of the above

f. none of the above

76
New cards

c. when the object of the class goes out of scope

The destructor for a class is called

a. explicitly from the main program

b. when the class is instantiated

c. when the object of the class goes out of scope

d. Only at the end of main

77
New cards

d. all of the above

The copy constructor for a class is called

a. when an object of the class is passed by value to a function.

b. when an object of the class is initialized by another object of the class

c. when a function returns an object of the class

d. all of the above

78
New cards

e. all of the above

(not sure abt this one)

Which of the following are not correct?

a. The destructor of a class is not named the same as the name of the class, but preceded with a tilde

b. The destructor of a class is not called when an object of the class goes out of scope

c. The destructor of a class is not a member of the class

d. The destructor of a class is a void function

e. all of the above

79
New cards

d. All of the above

What happens when you define a class that uses dynamic memory allocation and define a destructor, but no copy constructor?

a. If an object of the class is plugged in for a call-by-value parameter, when the function ends, the parameter's dynamic memory is returned to the freestore at the end of the function execution.

b. When an object that was used as an argument for a call-by-value parameter goes out of scope, it will cause a run-time error.

c. It is possible to modify the values in the argument in the function.

d. All of the above

e. None of the above

80
New cards

c. The pointer(s) to the dynamically declared memory in obj2 are copied to the corresponding pointers in obj1.

If obj1 and obj2 are both objects of a class that uses dynamic memory allocation, but the class does not have an assignment operator, what happens if you execute the following code?

obj1=obj2;

a. A syntax error occurs, you can not assign one object to another object without the = operator

b. A run-time error occurs, because the C++ system does not know how to do the assignment.

c. The pointer(s) to the dynamically declared memory in obj2 are copied to the corresponding pointers in obj1.

d. There is a complete and independent copy of all the dynamic memory from obj2 to obj1

81
New cards

d. void operator = (const myClass& source);

Which of the following are valid declarations for an assignment operator for a class named myClass?

a. void friend operator = (myClass& left, const myClass& source);

b. void operator = (myClass& left, const myClass& source);

c. void friend operator = (const myClass& source);

d. void operator = (const myClass& source);