Sie sind auf Seite 1von 10

‫ المؤشرات‬/ ++ ‫مراجعة لمادة سي‬

‫ محمد محمود أبوشقير‬.‫د‬

Sample Test
1. Using the following declarations for each part, identify the output.

int x = 20, y = 5, *px = &x, *py;


int a[] = {2, 4, 8, 1, 5, 3}, *pa = a;

(a) cout << (*px)+3; // output = _______________

(b) py = px;
*py += *px * y;
cout << x: // output = _______________

(c) cout << *pa; // output = _______________

(d) cout << *(pa+3); // output = _______________

(e) pa++;
cout << *++pa; // output = _______________

(f) pa += 2;
*pa += 3;
cout << *pa; // output = _______________

2. The pointer rectPtr identifies a dynamically allocated rectangle.

rectangle *rectPtr = new rectangle(4,5), r;

(a) The statements (*rectPtr).area()and rectPtr->area()are equivalent.


(i) True (ii) False

(b) The statements *rectPtr.area()and rectPtr->area()are equivalent.


(i) True (ii) False

(c) Which assignment statement gives r the same dimensions as the dynamically allocated rectangle
(i) r = rectPtr; (ii) r = new rectangle(*rectPtr);
(iii) r = *rectPtr; (iv) *r = *rectPtr;

3. What are the final values of x and y?

int x, y = 25, *px = &x, *py = &y;

*px = 58;
*py += 6; Resulting value of x _________ of y:________

4. What is the result of the following declaration?


‫ المؤشرات‬/ ++ ‫مراجعة لمادة سي‬
‫ محمد محمود أبوشقير‬.‫د‬

int *a = new int(55);

(a) a is an array of 55 integers (b) *a is an integer with initial value 55


(c) a is an integer with initial value 55 (d) a is an array of 55 integers with initial values 55

5. Assume the declaration int *arr;


Which declaration dynamically creates an array of 35 integers?
(a) arr = new int[35]; (b) *arr = int[35]; (c) arr = new int(35); (d) *arr = new[35] of int;

6. Assume we have dynamically allocated an array arr of 50 doubles. Which statement will deallocate these
memory locations?
(a) delete double arr(50); (b) delete [] arr; (c) delete arr[]; (d) delete double[] arr;

7. In class testClass the member function f() doubles the current value of intData and returns a reference to
itself.
class testClass
{
public:
...
testClass& f();
private:
int intData;
};

Which of the following is a valid implementation for f()?

(a) testClass& testClass::f() (b) testClass& testClass::f()


{ {
testClass tmp = this; this->data *= 2;
tmp.data *=2; return *this;
return tmp; }
}

(c) testClass& testClass::f() (d) testClass& testClass::f()


{ {
testClass tmp = *this; testClass tmp = *this;

tmp.data *=2; tmp.data *=2;


return &tmp; return *this;
} }

8. Use the following declaration for each part (a)-(d). Fill in the value for *p and *q.

int m = 30, n = 8, *p = &m, *q = &n;

(a) *p = *q * 2 *p = _____
(b) *q += 8 *q = _____
(c) (*q)++ *q = _____
(d) q=p *p = _____ *q = _____

9. Identify the error that occurs in each of the following statements.


‫ المؤشرات‬/ ++ ‫مراجعة لمادة سي‬
‫ محمد محمود أبوشقير‬.‫د‬
(a) vector<int> *vPtr;
vPtr = new vector<int>[15];

(b) int arr[20], b[10];


b = a;

(c) int *p = new int{1,4,9};

10. Apply the function f() for parts (a) and (b).

template <typename T>


void f(T *arr, int n)
{
T *p = arr, *q = arr+n/2, temp;

for(int i = 0; i < n/2; i++)


{
temp = *p;
*p++ = *q;
*q++ = temp;
}
}

(a) What are the contents of arr after calling f()?

int arr[] = {3, 5, 7, -9, 6};


f(arr, 5);

Contents

(b) What are the contents of strArr after calling f()?

string strArr[] = {"North", "South", "East", "West"};


f(strArr, 4);

Contents

11. Assume that *p is declared as a pointer to a vector of integers.


vector<int> *p;

(a) Which statement dynamically allocates a vector object that initially contains 8 elements?
(i) p = new vector<int>[8]; (ii) *p = vector<int>(8);
(iii) p = new vector<int>(8); (iv) *p = new vector<int>(8);

(b) Which statement inserts the value 5 at the rear of the vector?
(i) (p->push_back)(5) (ii) *p->push_back(5);
(iii) (*p.push_back)(5) (iv) p->push_back(5);

(c) Which statement assigns the value 10 at index 3 in the vector?


(i) p[3] = 10; (ii) *p[3] = 10; (iii) (*p)[3] = 10; (iv) p->[3] = 10;
‫ المؤشرات‬/ ++ ‫مراجعة لمادة سي‬
‫ محمد محمود أبوشقير‬.‫د‬
12. The following is a declaration of class dynStoreMax that contains a single data member ptr, which is a pointer
to a dynamically allocated object of type T. The member function update() takes the argument v of type T and
makes it the new stored value provided it is greater than the current stored value.

template <typename T>


class dynStoreMax
{
public:
dynStoreMax(const T& v = T());
~ dynStoreMax();
dynStoreMax(const dynStoreMax <T>& obj);
dynStoreMax<T>& operator= (const dynStoreMax <T>& rhs);
T getValue() const;
void update(const T& v);
private:
T *ptr;
};

(a) The constructor allocates dynamic memory for the data member and assigns it the value v. Which
statement is part of the implementation for the constructor?
(i) *ptr = new dynStoreMax <T>(v); (ii) ptr = new dynStoreMax <T>(v);
(iii) *ptr = new T (v); (iv) ptr = new T(v);

(b) What is the value of ptr in part (a) if the heap does not have sufficient memory? __________________

(c) What is the name for the function ~dynStoreMax()?___________________________________

(d) Complete the implementation of the function ~dynStoreMax () assuming inline code.
~ dynStoreMax ()
{ ______________________________________________; }

(e) What is the name for the function dynStoreMax (const dynStoreMax <T>& obj)?
______________________________

(f) Which pair of statements provides the implementation for the overloaded assignment operator?
(i) *ptr = rhs.ptr; (ii) ptr = rhs.ptr;;
return *this; return *this;
(iii) *ptr = *rhs.ptr; (iv) *ptr = rhs.*ptr;;
return *this; return *this;

(g) Assume the following declarations. What is the output?

string s = "Jerry", t = "Johnson";


dynStoreMax<string> d(s);

d.update(t);

cout << d.getValue()[4]; // output: _______________________

13. Which statement(s) dynamically allocates an object of type demo whose members have the initial values 1 and
"cat"?
‫ المؤشرات‬/ ++ ‫مراجعة لمادة سي‬
‫ محمد محمود أبوشقير‬.‫د‬

class demo
{
public:
int data;
string name;

demo(int n, const string& str) : data(n), name(str)


{}
};
demo *d;

(a) d = new demo(1, "cat"); (b) d = &demo(1,"cat");

(c) d = new demo; (d) d = new demo;


d->data = 1; d.one = 1;
d->name = "cat"; d.name = "cat";

14. A sequence of statements use miniVector objects vA and vB. Assume the statements execute sequentially
so that you must use the results from all previous statements to determine the size and the capacity of the
miniVector object after executing the current statement.

miniVector<int> vA(4); // vA.vSize = ____ vA.vCapacity = ____

miniVector<int> vB; // vB.vSize = ____ vB.vCapacity = ____

vA.push_back(5); // vA.vSize = ____ vA.vCapacity = ____

vB.push_back(5): // vB.vSize = ____ vB.vCapacity = ____

vA.push_back(6); // vA.vSize = ____ vA.vCapacity = ____

vB.push_back(6): // vB.vSize = ____ vB.vCapacity = ____

15. Correct any syntax error in the declaration of the vector or miniVector object.
(a) vector<vector<int>> table

(b) vector<int, int> > table;

(c) miniVector<string> vString[15];

16. The following is a 4 by 4 table of integer values with the row and column labels in bold.

0 1 2 3
0 5 8 7 -1
1 3 0 2 9
2 -4 7 6 2
3 1 0 7 4

(a) Declare the table as the matrix object mat.

(b) Assuming mat has the integer values above, what is the output from writeVector(mat[1])?
________________________________________
‫ المؤشرات‬/ ++ ‫مراجعة لمادة سي‬
‫ محمد محمود أبوشقير‬.‫د‬
17. The word deque is a abbreviation for ___________________________________________________-

18. The following declaration creates an array with 20 elements.

int arr[25];

The variable arr is a(n) __________ that references __________________.

(a) constant … starting address of the array


(b) pointer variable … element arr[0]
(c) integer variable … element arr[0]
(d) address variable … starting address of the array

19. Assume a programmer does not explicitly provide a copy constructor in the declaration of the dynamic
class dynClass. The runtime system then calls the class constructor in the following declaration of objects
dA and dB.

dynClass dA, dB = dA;

(a) True (b) False

For Questions 21 and 22, add the member functions insert() and erase() to the miniVector class. For their
implementation, use the following declaration for key members in the private section of the class.

class miniVector
{
private:
int vSize; // number of elements in the list
T *vArr; // the dynamic array
}

20. The function insert() adds the new element item at index pos in the vector.

void insert(int pos, const T& item);

Complete the implementation of insert() using inline code.

void insert(int pos, const T& item)


{
int i;

_________________________ // add item to end of vector

// shift elements on the tail of the list to the right


// one position

for (__________; ______________; ________)


{

}
vArr[pos] = item;
‫ المؤشرات‬/ ++ ‫مراجعة لمادة سي‬
‫ محمد محمود أبوشقير‬.‫د‬
}

21. The function erase() removes from the vector the element at index pos.

void erase(int pos);

Complete the implementation of erase() using inline code.

void erase(int pos)


{
int i;

// shift elements on the tail of the list to fill the gap

for (__________; ______________; ________)


{

}
_________________________________________
}
‫ المؤشرات‬/ ++ ‫مراجعة لمادة سي‬
‫ محمد محمود أبوشقير‬.‫د‬

Test Solutions
1. (a) output = 23
(b) output = 120
(c) output = 2
(d) output = 1
(e) output = 8
(f) output = 11

2. (a) (i)
(b) (ii)
(c) (iii)

3. Resulting value of x 58; of y 31

4. (b)

5. (a

6. (b)

7. (b)

8. (a) *p = *q * 2 *p = 16
(b) *q += 8 *q = 16
(c) (*q)++ *q = 9
(d) q=p *p = 30 *q = 30

9. (a) Use (15) and not [15]


(b) Cannot assign array a to array b
(c) Cannot initialize a dynamically allocated array

10. Apply the function f() for parts (a) and (b).

template <typename T>


void f(T *arr, int n)
{
T *p = arr, *q = arr+n/2, temp;

for(int i = 0; i < n/2; i++)


{
temp = *p;
*p++ = *q;
*q++ = temp;
}
}

(a)

Contents 7 -9 3 5 6
‫ المؤشرات‬/ ++ ‫مراجعة لمادة سي‬
‫ محمد محمود أبوشقير‬.‫د‬

(b)

Contents Ease West North South

11. (a) (iii)


(b) (iv)
(c) (iii)

12. (a) (iv)


(b) NULL (0)
(c) Destructor
(d) ~ dynStoreMax )
{ delete ptr;

(e) Copy Constructor


(f) (iii)
(g) Output s

13. (a)

14. miniVector<int> vA(4); // vA.vSize = 4 vA.vCapacity = 4

miniVector<int> vB; // vB.vSize = 0 vB.vCapacity = 0

vA.push_back(5); // vA.vSize = 5 vA.vCapacity = 8

vB.push_back(5): // vB.vSize = 1 vB.vCapacity = 1

vA.push_back(6); // vA.vSize = 6 vA.vCapacity = 8

vB.push_back(6): // vB.vSize = 2 vB.vCapacity = 2

15. (a) vector<vector<int> > table // add blank after <int>

(b) vector<vector<int> > table;

(c) miniVector<string> vString(15);

16. (a) matrix<int> mat(4,4);

(b) 3 0 2 9

17. doubly ended queue

18. (a)

19. (b)
‫ المؤشرات‬/ ++ ‫مراجعة لمادة سي‬
‫ محمد محمود أبوشقير‬.‫د‬

20. void insert(int pos, const T& item)


{
int i;

push_back(item); // add item to end of vector

// shift elements on the tail of the list to the right


// one position

for (i = vSize-1; i >= pos; i--)


{
vArr[i+1] = vArr[i];
}
vArr[pos] = item;
}

21. void erase(int pos)


{
int i;

// shift elements on the tail of the list to fill the gap

for (i = pos+1; i < vSize; i++)


{
vArr[i-1] = vArr[i];
}
pop_back();
}

Das könnte Ihnen auch gefallen