CMSC 208 TEST 1 Name______________
March 7, 1996 Total Points: 100
TRUE or FALSE
1. When a parameter is passed by reference, the actual parameter can be
any expression.
2. With parameter passage by reference, the address of the caller's
actual parameter is sent to the function.
MULTIPLE CHOICE
3. Which one of the following is NOT an assertion?
a. a function precondition
b. a function postcondition
c. a loop condition
d. a loop invariant
4. Consider the function
void Func ( int& n )
{
n = 245;
}
Which of these comments describes the direction of data flow for n?
a. /* in */
b. /* out */
c. /* inout */
5. Consider the function
void Func ( int n )
{
cout << 3 * n ;
}
Which of these comments describes the direction of data flow for n?
a. /* in */
b. /* out */
c. /* inout */
6. Consider the function
void Func ( int& n )
{
n = 3 * n;
}
Which of these comments describes the direction of data flow for n?
a. /* in */
b. /* out */
c. /* inout */
7. Consider the function
void DoThis ( int& alpha, int beta )
{
int temp;
alpha = alpha + 100;
temp = beta;
beta = 999;
}
Suppose that the caller has integer variables gamma and delta whose
values are 10 and 20, respectively. What are the values of gamma
and delta after the function call
DoThis ( gamma, delta);
a. gamma = 10 and delta = 20
b. gamma = 110 and delta = 20
c. gamma = 10 and delta = 999
d. gamma = 110 and delta = 999
e. none of the above
8. Consider the function
void Func ( int& alpha, int beta )
// PRE: ?
// POST: ?
{
int delta = beta;
alpha = beta;
beta = 23;
cout << delta * beta;
}
Which of these should be included in the precondition for the function?
a. Assigned(alpha)
b. Assigned(beta)
c. Assigned(delta)
d. both a and b above
e. all of a, b, and c above
9. Choose the best loop invariant for the following loop.
// ASSERT: oddSum == 0 && count == 1 && max > 10
while (count != 2*max + 1 ) {
oddSum = oddSum + count;
count = count + 2;
}
// ASSERT: oddSum == 1 + 3 + 5 ... + (2*max - 1)
a. oddSum == 1 + 3 + 5 + ...+ (count-2) && count is odd
b. oddSum == 1 + 3 + 5 + ...+ (count-1) && count is odd
c. oddSum == 1 + 3 + 5 + ...+ count && count is odd
d. oddSum == 1 + 3 + 5 + ...+ (2*max - 1) && count is odd
10. Choose the best loop invariant for the following loop.
// ASSERT: n>1 && sum == 0 && i == 1
while (i <= n ) {
i++;
sum = sum + 5;
}
// ASSERT: sum == 5 * n
a. sum == 5 * (i-1) && i <= n
b. sum == 5 * (i-1) && i <= n+1
c. sum == 5 * (i-1) && i <= n-1
d. sum == 5 * (i-1) && i > n
FILL IN THE BLANKS.
11. Parameter passage by ______________ sends the invoked function a copy
of the actual parameter's value.
12. Parameter passage by ______________is used for parameter alpha below:
void DoSomething ( char& alpha, char beta );
13. Parameter passage by ______________is used for parameter beta below:
void DoSomething ( char& alpha, char beta );
14. Parameter passage by ______________is used for parameter gamma below:
void DoSomething ( char delta, char gamma[ ] );
15. When a main() function is written for the express purpose of
exercising another function that is being developed, this main()
function is called a(n) ______________
16. The ______________sort algorithm rearranges data by repeatedly
searching for the largest value in the unsorted portion of the array
and swapping it into its proper location within the sorted portion
of the array.
17. A(n) ______________is an unrefined function that "fakes" the desired
action to allow testing of the higher-level logic.
18. A(n) ______________ is an assertion used to describe the conditions
that are true when a function completes its execution.
MULTIPLE CHOICE
19. Suppose that myprog.cpp uses features from two modules, ModA (files
moda.h, moda.cpp, and moda.obj) and ModB (files modb.h, modb.cpp, and
modb.obj). If only myprog.cpp is modified which files(s) must be
recompiled?
a. myprog.cpp
b. moda.cpp
c. modb.cpp
d. all of a, b, c above
e. none of a, b, and c above
20. Suppose that myprog.cpp uses features from two modules, ModA (files
moda.h, moda.cpp, and moda.obj) and ModB (files modb.h, modb.cpp, and
modb.obj). If only myprog.cpp is modified and recompiled, which
files(s) must be relinked?
a. myprog.obj
b. moda.obj
c. modb.obj
d. all of a, b, c above
e. none of a, b, and c above
21. Which one of the following cannot be exported by a module?
a. a data type
b. a function
c. a loop
d. a constant
22. Suppose that NextRand is a function that returns a random number
between (but not including) 0.0 and 1.0. Which expression below
yields a random integer from 1 through 9 (inclusive)?
a. int ( NextRand() * 9.0 )
b. int ( NextRand() * 8.0 ) + 1
c. int ( NextRand() * 9.0 ) + 1
d. int ( NextRand() * 8.0 + 1.0)
e. int ( ( NextRand() + 1.0) * 9.0 )
23. Assume that somemod.h and somemod.cpp are files of a module and that
someprog.cpp is a client of the module. Which files(s) must #include
the file somemod.h?
a. someprog.cpp
b. somemod.cpp
c. somemod.h
d. a and b above
e. a, b, and c above
FILL IN THE BLANKS.
24. A(n) ______________is software that uses the features supplied by a
module.
25. A(n) ______________diagram is a picture that emphasizes the items
that are supplied by a particular module.
26. When software companies make available their specification files
without showing any implementation code, they are using an important
software design technique called information ______________
27. With a module, the ______________file contains function definitions
for all function prototypes displayed in the specification file.
28. A(n) ______________is the common name for the variable that is used
to generate one random number after another.
29. In software design, to______________things means to group them
together and hide them.
30. Using a C++ standard library routine, give a function call that
will copy the string in array myString to the array yourString.
______________
31. Using a C++ standard library routine, give a function call that
will convert the character someChar to an uppercase letter if it
was a lowercase letter (and leave it unchanged, otherwise).
______________
TRUE or FALSE
32. A data type consists of a set of values and a collection of
allowable operations on those values.
33. A C++ class member function cannot have the same name as the name
of the class.
34. A C++ class constructor cannot return a function value.
MULTIPLE CHOICE
35. If the designer of a C++ class wishes to allow clients to inspect but
not modify private data, what is the best approach?
a. Provide an access function as a class member.
b. Declare the data to be public, not private.
c. Provide an additional class constructor.
d. Do nothing--it is not acceptable to let clients inspect
private data.
36. Which of the following C++ built-in operations are defined for class
instances?
a. ==
b. .
c. =
d. b and c above
e. a, b, and c above
37. Given the class declaration
class MyClass {
public:
.
.
.
void Func();
private:
int n;
};
what notation does the body of Func use to assign n the value 3?
a. n = 3;
b. MyClass.n = 3;
c. MyClass::n = 3;
d. someObject.n = 3;
e. It can't be done, because n is private.
38. A class invariant is a universal pre- and postcondition for every
member function with the following exception:
a. It is not a precondition for an access function.
b. It is not a postcondition for an access function.
c. It is not a precondition for a class constructor.
d. It is not a postcondition for a class constructor.
e. It is not a precondition for a const member function.
39. Consider the following class declaration
class X {
public:
void F( int );
private:
int n;
};
The following client code, with each line numbered in a comment, has
one or more errors.
X alpha; // 1
alpha.F( ); // 2
alpha.n = 3; // 3
Identify the line(s) with error(s).
a. Line 1
b. Line 2
c. Line 3
d. Lines 1 and 2
e. Lines 2 and 3
QUESTIONS #40-41 BELOW both use the following class declaration
class SomeClass {
public:
void Func( );
private:
int m;
int n;
};
and client code
SomeClass alpha;
SomeClass beta;
40. Considering both pieces of code above, which identifiers are names
of class instances?
a. m and n
b. alpha and beta
c. SomeClass, m and n
d. Alpha, beta, m, and n
e. Func, m, and n
41. Considering both pieces of code above, which identifiers are names of
class members?
a. m and n
b. alpha and beta
c. SomeClass, m and n
d. Alpha, beta, m, and n
e. Func, m, and n
42. A class SomeClass has a member function F that has no parameter list,
returns an int value, and does not modify any of the private data.
Which of the following would be the correct function definition for F?
a. int F( ) const
{ . . . }
b. const int F( )
{ . . . }
c. SomeClass::int F( ) const
{ . . . }
d. const int SomeClass::F( )
{ . . . }
e. int SomeClass::F( ) const
{ . . . }
43. Consider the class declaration
class X {
public:
X();
// POST: Private data initialized to zero
X( int n );
// POST: Private data initialized to n
private:
int priv;
};
and the client code
X gamma(5);
After gamma is created, what is the value of gamma.priv?
a. 0
b. 5
c. n
d. Unknown, but declaration is valid.
e. Declaration is invalid.
44. Suppose that the class declaration of MyClass includes the following
function prototype
Boolean LessThan ( /* in */ MyClass someInstance );
Which of the following tests in the client code correctly compares
two class instances alpha and beta?
a. if ( alpha < beta )
b. if ( alpha.LessThan(beta) )
c. if (LessThan(alpha, beta) )
d. if (alpha.LessThan.beta)
e. if (LessThan(alpha).beta)
45. Suppose that the class declaration of MyClass includes the following
function prototype
Boolean operator< ( /* in */ MyClass someInstance );
Which of the following tests in the client code correctly compares two
class instances alpha and beta?
a. if ( alpha < beta )
b. if ( alpha.LessThan(beta) )
c. if (LessThan(alpha, beta) )
d. if (alpha < someInstance)
e. if ( < alpha.beta)
FILL IN THE BLANKS.
46. A(n) ______________is a C++ class member function that is executed
automatically when a class instance is created.
47. In C++, a(n) ______________is a function that is not a class member
yet has access to the private class members.
48. The official name for the :: operator in C++ is the
______________operator.
49. A(n) ______________operator is one that has multiple meanings.
50. In C++, the ______________constructor is one that has no
parameter list.
TRUE or FALSE
51. An array is an atomic type.
52. If several items are pushed onto a stack and then all of them are
removed, they will come back in the same order in which they were
pushed.
53. Stacks are LIFO structures.
54. Lists are FIFO structures.
MULTIPLE CHOICE
55. Which data structure has an implicit cursor that indicates the
current position?
a. list
b. stack
c. queue
d. a and b above
e. a, b, and c above.
56. Which data structure(s) must store homogeneous data?
a. list
b. array
c. record
d. a and b above
e. b and c above.
57. Assume the C++ class IntList is a list data structure for storing
int values. Let myList be an instance of this class with current
contents (head to tail, left to right) as follows:
(25, -4, 56, 8, 10, 3)
Assume that 56 is the current item. What will be the contents of
myList after execution of the following code segment?
myList.Advance( );
myList.Delete( );
a. (25, -4, 8, 10, 3)
b. (25, -4, 56, 10, 3)
c. (25, -4, 56, 8, 3)
d. (25, -4, 56, 8, 10)
e. none of the above
58. Assume that stk is an instance of a C++ class IntStack for storing
int values, and stk currently contains 15, -6, and 14 from top to
bottom (shown left to right): (15, -6, 14 )
What will be the contents of the stack after execution of the
following code segment?
stk.Push(35);
stk.Push(4);
a. (15, -6, 14, 4, 35)
b. (35, 4, 15, -6, 14)
c. (4, 35, 15, -6, 14)
d. (4, 35, 15)
e. none of the above
59. Assume that q is an instance of a C++ class IntQueue for storing int
values, and q currently contains 15, 20, 10, and 30 from front to rear
(shown left to right): (15, 20, 10, 30)
What will be the contents of the queue after execution of the
following code segment?
q.Dequeue( );
q.Enqueue(90);
a. (90, 20, 10, 30)
b. (15, 20, 10, 90)
c. (90, 15, 20, 10)
d. (20, 10, 30, 90)
e. none of the above
FILL IN THE BLANKS.
60. Assume the C++ class IntList is a list data structure for storing
int values. Let myList be an instance of this class with current
contents (head to tail, left to right) as follows:
(25, -4, 56, 8, 10, 3)
Assume that 56 is the current item. If the code segment
while (myList.CurrentItem( ) != 3)
myList.Advance( );
myList.InsertAfter(44);
is now executed, show the resulting contents of myList. (______________)
61. Assume the C++ class IntList is a list data structure for storing
int values. Let myList be an instance of this class with current
contents (head to tail, left to right) as follows:
(25, -4, 56, 8, 10, 3)
Assume that 56 is the current item. If the code segment
while ( ! myList.EndOfList( ) )
myList.Advance( );
myList.InsertBefore(75);
is now executed, show the resulting contents of myList. (______________)
62. Assume that stk is an instance of a C++ class IntStack for storing
int values, and stk currently contains 15, -6, and 14 from top to
bottom (shown left to right): (15, -6, 14 )
If the code segment
stk.Pop( );
if (stk.Top( ) < 0)
stk.Pop( );
else
stk.Push(10);
is now executed, show the resulting contents of stk. (______________)
63. Assume that stk is an instance of a C++ class IntStack for storing
int values, and stk currently contains 15, -6, and 14 from top to
bottom (shown left to right): (15, -6, 14 )
If the code segment
while ( ! stk.IsEmpty() )
stk.Pop( );
stk.Push(42);
stk.Push(Stk.Top( ) );
is now executed, show the resulting contents of stk.(______________)
64. Assume that q is an instance of a C++ class IntQueue for storing
int values, and q currently contains 15, 20, 10, and 30 from front
to rear (shown left to right): (15, 20, 10, 30)
If the code segment
while (q.Front( ) != 10)
q.Dequeue( );
is now executed, show the resulting contents of q.(______________)
65. Assume that q is an instance of a C++ class IntQueue for storing
int values, and q currently contains 15, 20, 10, and 30 from front
to rear (shown left to right): (15, 20, 10, 30)
If the code segment
while (q.Front( ) != 30) {
q.Enqueue( q.Front( ) );
q.Dequeue( );
}
is now executed, show the resulting contents of q. (______________)
TRUE or FALSE
66. In C++, a struct is a class whose members are all, by default,
public.
67. The built-in operations that are valid for structs are
assignment (=), equality testing (==), and member selection (.).
68. In C++, a struct can be passed as a parameter either by value, or
by reference.
MULTIPLE CHOICE
69. What is the most appropriate data structure for storing a single memo
consisting of the name of the sender and a message of up to 500
characters?
a. an array of arrays
b. an array of structs
c. a struct containing arrays
d. a struct containing structs
70. What is the most appropriate data structure for storing 5000 items
for a hardware store, where the information for each item consists
of the wholesale price, the resale price, and the quantity on hand?
a. an array of arrays
b. an array of structs
c. a struct containing arrays
d. a struct containing structs
71. Using the declarations
struct FirstRec {
int part1;
float part2;
};
FirstRec myRec;
FirstRec yourRec;
which of the following assignment statements is(are) valid?
a. myRec.part1 = yourRec.part1;
b. myRec = yourRec;
c. myRec.part1 = yourRec;
d. a and b above
e. none of the above
QUESTIONS #72-76 BELOW all use the following declarations:
typedef char String9[10]
struct GenInfo {
String9 brand;
String9 model;
};
struct CarRec {
GenInfo style;
float cost;
};
CarRec myCar;
72. Which of the following assignment statements is(are) valid?
a. myCar.cost = 12000.0;
b. myCar.GenInfo = "Ford";
c. strcpy(myCar.GenInfo, "Ford");
d. a and c above
e. none of the above
73. Which of the following assignment statements is(are) valid?
a. myCar.style[3].brand = 'X';
b. myCar.style.brand[3] = 'X';
c. myCar[3].style.brand = 'X';
d. a and c above
e. none of the above
74. What is the type of myCar.style?
a. char
b. CarRec
c. String9
d. GenInfo
e. none of the above
75. What is the type of myCar.style.brand[2]?
a. char
b. float
c. String9
d. GenInfo
e. none of the above
76. If a char occupies 1 byte of memory and a float occupies 4 bytes,
how many bytes of memory does myCar occupy?
a. 1
b. 5
c. 8
d. 14
e. 24
QUESTIONS #77-80 BELOW all use the following declarations:
typedef char String19[20]
struct AddressType {
int number;
String19 street;
};
struct Entry {
String19 name;
AddressType address;
};
Entry phoneBook[1000];
77. Which of the following expressions is (are) valid?
a. phoneBook[72]
b. phoneBook[72].address
c. phoneBook.address[72]
d. a and b above
e. a and c above
78. Which of the following expressions is (are) valid?
a. phoneBook[72].address[4].number
b. phoneBook[72].address.street[4]
c. phoneBook[72].name[4]
d. a and b above
e. b and c above
79. What is the type of phoneBook[1].address.street[1] ?
a. String19
b. Entry
c. char
d. int
e. AddressType
80. If a char occupies 1 byte of memory and an int occupies 2 bytes,
how many bytes of memory does phoneBook occupy?
a. 1042
b. 1402
c. 4042
d. 42,000
e. 42,042
FILL IN THE BLANKS.
81. When an array of structs is sorted, it must be sorted on some
______________of each struct.
QUESTIONS #82-85 BELOW all use the following declarations:
struct PhoneBookEntry {
char name[51];
long phoneNo;
};
PhoneBookEntry phoneBook[8000];
82. Write an expression that denotes the tenth phone book entry. ______________
83. Write an expression that denotes the phone number in the first phone
book entry. ______________
84. Write an expression that denotes the name of the person in the first
phone book entry. ______________
85. Write an expression that denotes the first letter of the name of the
person in the first phone book entry. ______________
Last updated: Nov. 4, 1999