Anda di halaman 1dari 6

Instructor:Dr.

MengSu

CMPSC122 Exam 1

Print Your Name:

Do all your work on the pages of this examination. If you need more space, you may use the
reverse side of the page, but try to use the reverse of the same page where the problem is stated.
You have 50 minutes. The questions are of varying difficulty, so avoid spending too long on any
one question.

Problem

Score/Points

/30

/10

/10

/30

/12

/8

Total

1.[2 point each] Multiple Choice: Fill in only one of the letters you pick for the best correct answer at the
beginning of each problem. Dont circle on the answer!
1) [
] In a software life cycle, what is produced after the analysis and specification phase?
(a) maintenance
(b) design
(c) source code
(d) testing report
2) [
] typedef is used to
(a) create a name that is an alias for another name.
(b) create new data types.
(c) cast one type to another type.
(d) initialize class members.
3) [

]What is the result of the following?


double x[2] = {5, 2};
double * y = x;
(*y)++;
(a) It increments y
(b) It increments x
(c) It increments x[0]
(d) It increment x[1]
4) [
(a) char
(b)char
(c) char
(d) char

]Assume you have the following declarations of arrays. Which is correct?


A[ ][ ];
C[100][];
B[100, 25];
D[100][25];

5) [
]Assume that each value of type int is stored in 4 bytes, if an array is declared as
int t[10]; and the base address of this array is 100. What is the address of t[3]?
(a) 103
(b) 104
(c) 112
(d) 116
6) [

]For the declarations below, which expression is TRUE if ptr1 and ptr2 point at the same object?
int *ptr1;
int *ptr2;
(a) ptr1 = = ptr2
(b) *ptr1 = = ptr2
(c) &ptr1 = = &ptr2
(d) None of the above is false
7) [
(a) void
(b)void
(c) void
(d) void

]Assume you have the following prototypes of a function. Which is correct?


magic(int A[21][], int size);
magic(int A[size][size], int size);
magic(int A[][], int size);
magic(int A[][21], int size);

8) [
]Given that d is an double number array starting at location 2000, dPtr is a pointer to d, and
each double is stored in 8 bytes of memory, what location does dPtr + 2 point to?
(a) 2002
(b) 2008
(c) 2016
(d) 2024
9) [
]In a class, constructors are
(a) put in private part.
(b) called automatically when an object is initialized.
(c) not able to be overloaded.
(d) non-member functions

10) [
]When an ADT is implemented as a C++ class, which of the following should normally be
true?
(a) Function members are private, data members are public
(b) Function members as well as data members are private
(c) Function members are public, data members are private
(d) Function members as well as data members are public
11) [
]By default, class data members declared without an access identifier
(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.
12) [
]A class may contain multiple constructors if
(a) they have different names.
(b) they have different return types.
(c) they have the same argument list.
(d) they have different argument lists.
13) [
]Given below are some statements about the default (0-argument) constructor:
I. Its return type is the type of the class
II. It has no return type
III. The programmer can define it, but the C++ language doesnt require this
IV. The programmer must define it
Which of these statements are true?
(a). I, and III only
(b). I, II and IV only
(c). II and IV only
(d). II, and III only
14) [
] Consider the following class:
class Foo {
public:
void f1 (int n);
void f2 (const int & n);
void f3 (int n) const;
private:
int m;};
Which of the three member functions can NOT legally alter member variable m ?
a. The function f1
b. The function f2
c. The function f3
d. All three of them
15) [
]Which of the following statement segments assigns the value 0 to the variable i through the
pointer p, and is doing it correctly?
(a) int i, * p = i;
* p = 0;
(b) int i, * p = & i;
* p = 0;
(c) int i, * p = i;
p = 0;
(d) int i, * p = & i;
p = 0;

2. (10%) Explain the features of the ADT (Abstract Data Type). Why the C++ class is designed for
defining the ADT?

3 (10%) Consider a static two dimensional char array S which has 3 rows and 4 columns.
a) Write a declaration for S.

b) Write the names of all the elements in the third row of S.

c) If S has the base address 100 in decimal, what is the address of S[2][1]in decimal?

4. (30%) The following questions refer to the rectangle class Rect declaration in the header file:
(Note: the spaces left are for you to add some new members.)
class Rect {
public:
//spaces
Rect(int len, int wid);
int GetArea()const;// return the area of the rectangle
private:
int length;
int width;
};
// end of the header file
Please do the following in correct C++ syntax
1) Define the constructor Rect(int len, int wid);in the implementation file.

2) Add the default constructor for this class and define it.

3) Write the implementation for member function int GetArea()

4) Declare and implement a new method (function member) bool isItASquare() to check if the
Rect object is a square.

5) Declare and implement a new method (function member) void display()to print a Rect object in
the format [length, width]. For example:
Rect r(5, 3);// r should be printed as [5, 3]on the screen.

6) Write a client main function to declare a rectangle object. Your main function then prints the area of
the rectangle on the screen, and also prints the length and width on the screen only if the rectangle is a
square after checking it by your program.

5(12%) Write your version of C-string function strcat: char* mystrcat(char *s1, const char*
s2) which appends the string s2 to the string s1 and the new string s1 is returned .

6 (8%) Write a C++ function, including the prototype, to accept a two dimensional array and return true
if it is a symmetric two dimensional array, which means that the transpose of it is the same as itself.

Anda mungkin juga menyukai