Short Answers Section 4.1 Pointers and Dynamic Memory |
new
but the heap is out of memory?
int i = 42;
int k = 80;
int* p1;
int* p2;
p1 = &i;
p2 = &k;
*p1 = *p2;
Draw a picture of memory after this additional statement.
int i = 42;
int k = 80;
int* p1;
int* p2;
p1 = &i;
p2 = &k;
p1 = p2;
Short Answers
Section 4.2
Pointers and Arrays
as Parameters
void exam(const double data[]);
Write two or three sentences to tell me all the information that
you know about the parameter.
function foo (const int * p);
What restriction does the const keyword provide within the
implementation of foo?
Short Answers
Section 4.3
The Bag ADT with
a Dynamic Array
reserve(used + 1);
Describe a problem with this approach, and provide a better solution.
void bag::triple_capacity( )
// Postcondition: The capacity of the bag's dynamic array has been
// tripled. The bag still contains the same items that it previously
// had.
Do not
use the reserve function, do not use realloc, and do not cause a heap leak.
Do make sure that both data and capacity have new values that correctly
indicate the new larger array.
Short Answers
Section 4.6
The Polynomial
polynomial p(3);
p.assign_coef(4.1, 4);
p.assign_coef(2.1, 2);
double slope(const polynomial& p, double x)
// POSTCONDITION: The return value is equal to the slope of the
// polynomial p, evaluated at the point x.
Multiple Choice Section 4.1 Pointers and Dynamic Memory |
int *p; int i; int k; i = 42; k = i; p = &i;After these statements, which of the following statements will change the value of i to 75?
k = 75;
*k = 75;
p = 75;
*p = 75;
int i = 42;
int j = 80;
int *p1;
int *p2;
p1 = &i;
p2 = &j;
*p1 = *p2;
cout << i << j << endl;
What numbers are printed by the output statement?
int i = 1;
int k = 2;
int *p1;
int *p2;
p1 = &i;
p2 = &k;
p1 = p2;
*p1 = 3;
*p2 = 4;
cout << i;
int i = 1;
int k = 2;
int* p1;
int* p2;
p1 = &i;
p2 = &k;
p1 = p2;
*p1 = 3;
*p2 = 4;
cout << k;
int i = 1;
int k = 2;
int* p1;
int* p2;
p1 = &i;
p2 = &k;
*p1 = *p2;
*p1 = 3;
*p2 = 4;
cout << i;
Multiple Choice
Section 4.2
Pointers and Arrays
as Parameters
void goop(int z[ ]);
int x[10];
Which is the correct way to call the goop function with x as the argument:
void goo(int* x)
{
*x = 1;
}
Suppose that a is an int* variable pointing to some integer,
and *a is equal to zero. What is printed if you print *a
after the function call goo(a)?
Multiple Choice
Section 4.3
The Bag ADT with
a Dynamic Array
void quiz( )
{
bag::size_type i; // Line 1
bag b; // Line 2
b.insert(42); // Line 3
i = b.size( ); // Line 4
cout << i; // Line 5
}
When is the bag's dynamic array allocated?
void quiz( )
{
bag::size_type i; // Line 1
bag b; // Line 2
b.insert(42); // Line 3
i = b.size( ); // Line 4
cout << i; // Line 5
}
When is the bag's dynamic array returned to the heap?
void operator =(const foo& source);
In an assignment statement a = b, what will be the actual argument for
the parameter source?
Multiple Choice
Section 4.4
Prescription for a
Dynamic Class
Multiple Choice
Section 4.5
The String ADT
char s[8];
char s[9];
char s[10];
char s[11];
char s[12];
(x = y)
(x == y)
(x != y)
(strcmp(x, y))
Data Structures and Other Objects Using C++
Thank you for visiting
http://www.cs.colorado.edu/~main/questions/chap04q.html
Copyright #169; 2000
Addison-Wesley Computer and Engineering Publishing Group