Instructions: This homework assignment covers some basics of classes in C++. The answers to these questions can be determined from the lecture notes and the assigned readings in the text..
Opscan forms will be passed out in class. Write your name and code your ID number on the opscan form. Turn in your completed opscan at class on Thursday July 18. No late opscans will be accepted.
For questions 1 and 2,
consider the class declaration:
class CreditCard{
public:
void Payment( /* in */ float amount );
void Charge( /* in */ float amount );
float Balance()
const;
private:
float balance;
};
1. Which member function is
an observer operation?
1) Payment
2) Charge
3) Balance
4) 1 and 2 only
5) 1, 2, and 3
6) None of these
2. Which member function is
a mutator operation?
1) Payment
2) Charge
3) Balance
4) 1 and 2 only
5) 1, 2, and 3
6) None of these
3. Which of the following
statements about C++ classes is false?
1) Classes can have private member functions.
2) Classes can have public, private and protected members.
3) By default, members of classes are public.
4) Classes can have public data members.
5) Aggregate assignment is
permitted for classes.
6) None of these, (all are true).
4. Which of the following
C++ built-in operations are defined for class objects?
1) =
2) .
3) *
4) 2 and 3 only
5) 1 and 2
6) None of these
5. Given the class
declaration
class beta {
public:
void set( char
);
private:
char code;
};
which line of the
following client code causes a compile-time error?
beta alpha;
// Line 1
alpha.set();
// Line 2
alpha.code = ‘A’;
// Line 3
1) line 1
2) line 2
3) line 3
4) lines 1 and 2
5) lines 2 and 3
6) None of these
6. A class AClass has a member function G that takes no parameters, returns an char value, and does not modify any of the data members. Which of the following would be the correct
function prototype for G in the class declaration?
1) char G() const;
2) const char G();
3) AClass::char G()
const;
4) const char
AClass::G();
5) char AClass::G()
const;
6) None of these
7. Suppose that the class
declaration of AClass includes the following function
prototype.
bool LessThan(
/* in */ AClass anotherObject );
Which of the following
tests in the client code correctly compares two AClass objects alpha and beta?
1) if (alpha <
beta)
2) if
(alpha.LessThan(beta))
3) if
(LessThan(alpha, beta))
4) if
(alpha.LessThan.beta)
5) if
(LessThan(alpha).beta)
6) None of these
8. If the designer of a C++ class wishes to allow clients to inspect but not modify private data, what is the best approach?
1) Provide an observer function
as a class member.
2) Provide a mutator function
as a class member.
3) Declare the data to be
public, not private.
4) Provide an additional class
constructor.
5) Do nothing because it is not
acceptable to let clients inspect private data.
6) None of these
For questions 9 and 10, consider the class declaration:
class SomeClass {
public:
...
SomeClass ();
//
Postcondition:
// Private data initialized to zero
SomeClass ( /*
in */ int n );
//
Postcondition:
// Private data initialized to n
private:
int privy;
};
and client code
SomeClass gamma;
SomeClass omega(1);
9. After omega is created, what is the value of omega.privy?
1) 0
2) 1
3) n
4) Unknown, but the declaration
of omega is valid.
5) The declaration of omega is invalid.
6) None of these
10. After gamma is created, what is the value of gamma.privy?
1) 0
2) 1
3) n
4) Unknown, but the declaration
of gamma is valid.
5) The declaration of gamma is invalid.
6) None of these
11. Given
the declarations: int
*ptr1 = new int(0),
*ptr2 = new int(1);
which of the
following is a logically valid assignment?
1) ptr1 = ptr2;
2) *ptr1 = ptr2;
3) *ptr1 = *ptr2;
4) 1 and
3
5) 1, 2,
and 3
6) None
of these
12. What is
output by the following program fragment?
int alpha = 35;
int beta =
50;
int* ptr1 = α
int* ptr2 = β
*ptr2 = *ptr1;
cout << *ptr1
<< ' ' << *ptr2 << endl;
cout <<
alpha << ' ' << beta
<< endl;
1) 35 50
35 50
2) 50 50
35 50
3) 35 50
50 50
4) 50 50
50 50
5) None of these
13. Given the C++ statement: (*alpha[i]).beta
= 1.5;
(assuming it is syntactically valid) which of the following
descriptions is correct?
1) alpha is a pointer variable.
2) i is a pointer variable.
3) beta is a pointer member of a struct.
4) alpha is an array of pointer variables.
5) 1
and 3 only
6) 1
and 4 only
7) None
of these
14. Given the C++ statement: alpha->beta[i]
= 1.5;
(assuming it is
syntactically valid) which of the following descriptions is correct?
1) alpha is a pointer variable.
2) i is a pointer variable.
3) beta is a pointer member of a struct.
4) alpha is an array of pointer variables.
5) 1
and 3 only
6) 1
and 4 only
7) None
of these
15. Given the declarations: int dna = 41;
int *hhg = &dna;
which of the following can be used to increment dna?
1) hhg++;
2) *(dna++);
3) (*(&dna))++;
4) (*(&hhg))++;
5) 1 and 4 only
6) None of these
For questions 16 through
18, consider the declarations below and assume the initial memory layout shown:
int Count = 1;
int *Ptr;
Identifier |
Address |
Value |
Count |
6440 |
1 |
Ptr |
6444 |
?? |
16. Suppose the following statements are
executed: Ptr = new int(69);
(*Ptr)++;
what value will be
printed by the statement cout << Ptr
<< endl;
1) 6444
2) 6448
3) 69
4) 70
5) There’s not enough information to say.
6) None of these
17. Assuming the statements given in question 16
have been executed, what value will be printed by the statement:
cout << *Ptr << endl;
1) 6444
2) 6448
3) 69
4) 70
5) There’s not enough information to say.
6) None of these
18. Suppose the following statements are
executed: Ptr = &Count;
(*Ptr)++;
what value will be
printed by the statement cout << Count
<< endl;
1) 6440
2) 6444
3) 6448
4) 1
5) There’s not enough information to say.
6) None of these
For questions 19 through 20, consider the declarations below and assume the initial memory layout shown. Assume that memory addresses correspond to 32-bit words of memory.
const int Size = 10;
int *List = new
int[Size];
int Idx = 0;
Identifier |
Address |
Value |
List |
6448 |
8000 |
Idx |
6440 |
0 |
19. Suppose the following loop is executed:
for (Idx = 0; Idx < Size; Idx++) {
List[Idx] = Size - Idx;
}
what value will be
printed by the statement cout << List[4]
<< endl;
1) 4
2) 6
3) 0
4) 644C
5) 8004
6) 8016
7) None
of these
20. Suppose the following loop is executed:
for (Idx = 0; Idx < Size; Idx++) {
int* p = List + Idx;
*p = Size – Idx;
}
what value will be
printed by the statement cout << *(List+6)
<< endl;
1) 4
2) 6
3) 0
4) 644C
5) 8004
6) 8016
7) None
of these