Suppose inf is an available ifstream and c is an available character. Consider the following code fragment.
do c = inf.get(); while (!inf.eof() && isspace(c));
Which of the following accurately describes the effect of executing this fragment?
Compilers follow the "maximal munch rule" (read as many characters as can possibly be assembled into a meaningful syntactic construct). Therefore, what happens with the following?
int a = 5; int b = 10; cout << (a+++++b) << " "; cout << a << " " << b << endl;
Under the declaration,
int n = 10;
which of the following loop constructs executes statement n times?
Consider the following code fragment.
bool b = false; int f( int x ) { ... } cout << f(b) << endl;
Which of the following is true?
The "maximal munch rule" for compilers is to read as many characters as can possibly be assembled into a meaningful syntactic construct. Consider the following code fragment.
int a = 5; int b = 10; cout << (a+++b) << " "; cout << a << " " << b << endl;
If the C++ compiler follows the maximal munch rule, what is printed as a result of the code fragment?
One reason for using an assert statement such as
assert( 0 <= i && i < 10 );
within code is to
Which of the following descriptions of C++ are true?
Execution of the conditional statement
if( x = expr ) ;
has what effect?
What, if anything, is wrong with the following code fragment?
bool *bp; int x; bp = &x;
The parameters in the main part of a program often take one of the two following forms.
Version 1: int main( int argc, char **argv ) Version 2: int main( int argc, char *argv[] )
What is the difference between the two?
Consider the following declarations.
int (*f)(int); int *g(int);
Which of the following correctly characterizes f and g?
int A[100], *p, *q; for( p = A, q = p + 99; p < A+100; p++, q-- ) swap( p, q );
Execution of this code fragment has what effect on the array A?
char *str1 = "word"; char *str2; str2 = str1;
Which of the following correctly characterizes the effect of executing this fragment?
Thing *ptr = new Thing; ptr = NULL;
Assume that Thing is a user-defined class, and consider the following function.
Thing f(Thing& A) { Thing B; B.x = A.x; return B; }
Where in this code will a copy constructor be used?
Assume that Thing is a user-defined class, and consider the following code fragment, where B is an instance of Thing.
Thing A = B
Which of the following is a class member that is used in this code fragment?
Consider the following class outline for a user-defined implementation of real numbers.
class RealNumber { ... RealNumber( float x ); RealNumber( float x, float y=0 ); };
What, if anything, is wrong with the outline?
class RealNumber { ... private: RealNumber(void) {} };
Which of the following is true about such an implementation?
Consider the class outline below.
class Stack { ... explicit Stack( int n ); };
What is the purpose of the explicit in this outline?
In a template definition
template<class T> ...
the template parameter T ranges over
Consider the following template specification.
template<int n> ...
In use, the template parameter n can be replaced by
Consider the following templated array copy function and arrays.
template<class T> void copy( T a[], T b[], int n ) { for(int i = 0; i < n; i++ ) a[i] = b[i]; } char C[10], CC[10]; int I[10]; float F[10];
Which of the following calls to copy would produce a compile time error?
Consider the following templated swap function and data types.
template<class T> void swap( T& a, T& b ){ T tmp = a; a = b; b = tmp; } char c1, c2; int i1, i2; float A[10];
Which of the following calls to swap produces a compile time error?
What, if anything, is wrong with the following function declaration?
template<class A, class B> A f( B x );
Consider the following outline of a templated sorting function.
template<class T> void sort( T a[], int n ) { ... }
For a given sorting algorithm S, which of the following is true about using this outline to implement S?
Consider the following outline of a templated array class.
template<class T> class Array { ... }
Which of the declarations below is in obvious error with respect to using such a class?
Consider the following templated function.
template<class T> T Apply( T x, T (*ff)(T) ){ return ff(x); }
Which of the following is true concerning its use?
template<class S, class T> void copy( S a[], T b[], int n ) { for(int i = 0; i < n; i++ ) a[i] = b[i]; } char C[10]; int I[10]; float F[10];
Consider the following templated swap function.
template<class T> void swap( T& a, T& b ){ T tmp = a; a = b; b = tmp; }
Determining the right choice of type parameter T occurs at which of the following times?
Which of the following is true about using this outline to implement a general container class?
template<class Func> void mystery( Func f, int n, typename Func::result_type& x ) { for( int i = 0; i < n; i++ ) x += f(i); }
If the first actual parameter of a call to mystery is a function object f that properly defines result_type and if n0 and x0 are the values of the second and third actual parameters, which of the following accurately describes the result of executing that call?
If templates were removed from C++, which of the following would be true?
Which of the following declarations are in error with respect to using such a class?
Consider the following inheritance declarations.
class Base { virtual void f(int); virtual void f(float); }; class Derived: public Base { void f(int); }; Derived D; Base B, *p = &D;
In the context of these declarations, the two functions that are used in the two calls D.f(3.1415); and p-f(3.1415); are, respectively,
class Base { public: int x; private int y; }; class Derived: public Base { public: int z; }; Derived D;
Under these declarations, which of the following statements, if any, will fail to compile?
Consider the function defined as follows.
int f( int n ) { if( n == 0 ) return 0; if( (n & 1) == 0 ) return f(n/2); return f(n/2) + 1; }
The value returned by the call f( 10 ); is
Consider the following definition of a recursive function ff.
int ff( int n, int m ) { if( n == 0 ) return 0; return ff( n - 1, m ) + m; }
If the values of n and m are nonnegative, what is returned by ff( n , m )?
int ff( int n ) { if( n == 0 ) return 1; return 2 * ff( n - 1 ); }
If n > 0, what is returned by ff( n )?
Consider the following definition of a recursive function f.
int f( int x ) { if( x == 0 ) return 1; return x * f( x - 1 ); }
The inputs for which f will terminate are all x such that x is
bool f( int x ) { if( (x & 1) == 1 ) return (x == 1); return f( x >> 1 ); // right shift }
The value returned by the call f(x)will determine whether the input x is
Consider the following recursive definition of a function to compute Fibonacci numbers.
int Fibonacci( int n ) { if( n == 0 ) return 0; if( n == 1 ) return 1; return F(n-1) + F(n-2); }
Why is the function Fibonacci problematic to use?
What does f compute?
int ff( int n, int m ) { if( n == 0 ) return m; return ff( n - 1, m + 1 ); }
int f( int x ) { if( x == 0 ) return 1; return x * f( x ); }
For which inputs x will the call f(x)terminate?
Assume List is a linked list of integers.
List mystery( int a, int b, List L ) { if( L is empty ) return L; if(a == first(L)) return prepend( b, mystery(a, b, tail(L)) ); return prepend( first(L), mystery( a, b, tail(L) ) ); }
The call mystery(a, b, L)returns a linked list just like L except that
Assuming non-negative arguments, what is the purpose of the following recursive function?
int ff( int n, int m ) { if( n == 0 ) return m; return ff( n-1, m*n ); }
Consider the following definition of a recursive function what?
int what( int (*f)(int), int x, int n ) { if( n == 0 ) return x; return f( what( f, x, n - 1 ) ); }
If n is a non-negative integer, then the call what( f, x, n )returns
Consider the following recursive definition of a function f.
f
int f(int n) { if( n == 0 ) return 1; else return f(n / 2); }
The asymptotic running time of the call f(n) is
int f(int n) { if (n == 0) return 1; else return f(n >> 1); }
Consider the following definition of a recursive function, power, that will perform exponentiation.
int power( int b, int e ) { if( e == 0 ) return 1; if( e % 2 = 0 ) return power( b * b, e/2 ); return b * power( b * b, e/2 ); }
Asymptotically in terms of the exponent e, the number of calls to power that occur as a result of the call power(b,e) is
for( int i = 0; i < n; i++ ) for( int j = 0; j < n; j++ ) for( int k = 0; k < n; k++ ) body;
If body executes in O(1) time, then the asymptotic running time of this code fragment is
for( int i = 0; i < n; i++ ) for( int j = 0; j < n/5; j++ ) body;
for( int i = 0; i < n; i += 2 ) for(int j = i; j > 0; j -= 3 ) body
for( int i = 1; i < n; i *= 2 ) body;
If body executes in O(1) time, then the asymptotic running time of the code fragment above is
for( int i = 0; i < n; i += 2 ) for( int j = 0; j < n; j += 3 ) for( int k = 0; k < n; k += 4 ) body;
for( int i = n; i > 0; i /= 2 ) body;
Consider the execution of the following.
list<int> A(10);
Which of the following accurately describes what is created?
list<int> A(10,20);
stack<int> S; S.push(123); S.push(456);
Now consider the following statement.
cout << S.pop() << endl;
Which of the following most accurately describes what goes wrong when that statement is executed?
Execution of the code fragment
stack<int,vector<int> > S;
does which of the following?
For an STL iterator it, execution of the statement
it
++it;
Increase by 1 the size of the container pointed to by it.
li st<int>::const_iterator it; for( it = L.begin(); it != L.end(); ++it ) *it = 0;
What is syntactically wrong with this fragment?
The STL contains a general purpose search function:
find( first, last, x );
What does find return if it finds item x?
What does find return if it fails to find item x?
Consider the following statement using the STL sort() routine.
sort( A.begin(), A.end(), f );
Which of the following most accurately describes the result of executing this statement?
stack<int,list<int> > S;
For an STL iterator it and an STL container A, the expression
it != A.end()
is used for which of the following purposes?
What search method does find use?
Consider the following statement using the STL remove routine.
remove( a, b, x );
What of the following accurately describes what the variables a and b represent?
vector<int> A(10,20);
list<int>::iterator it; for( it = L.end(); it != L.begin(); --it ) cout << *it << endl;
Which of the following most accurately describes what is wrong with it?
Suppose that both A and B are STL containers holding integers. Consider execution of the following code fragment.
ostream_iterator it( cout, " " ); set_intersection( A.begin(), A.end(), B.begin(), B.end(), it );
Which of the following most accurately describes the results.
it--;
If A is an STL vector, then the effect of executing the statement
A.push_back( x );
is to
Consider the following program segment.
vector<int> A(10); A.resize(0); A.push_back(5000);
At the end of an execution of this fragment, the size of vector A is
Consider the following code fragment, where L is a linked list of integers.
for( it = L.begin(); it != L.end(); ++it ) *it += 10;
Execution of this fragment has the effect of
What, if anything, is wrong with the statement
sort( L.begin(), L.end() );
where L is a linked list?
A[i] = x;
Consider the following code fragment concerning the STL list class.
list<int> L(10); L[3] = 555;
The fragment will produce a compile time error because
Assume that L is an STL list object. Consider the following statement.
while( L.size() > 0 ) { ... }
Which of the following most accurately describes what is wrong with that statement?
Consider the following program fragment.
vector<int> A(10); A.push_back( 5000 );
Rotate( list<int>& L, int r ) { for( int i = 0; i < r; i++ ) { L.push_back( L.front() ); L.pop_front(); } }
What, if anything, is wrong with using this fragment to rotate a list L?
Consider the following two ways of creating a vector filled with 1,2,...,1000.
// method I vector<int> A(1000); iota( A.begin(), A.end(), 1 ); // method II vector<int> A; for( int i = 1; i <= 1000; i++ ) A.push_back( i );
Which of the following is true about these two methods?
Consider the following recursive definition of a function search that performs search in a binary tree.
bool search(Type a, Tree T) { if (empty(T)) { return false; } else if(a == data(T)) { return true; } else { bool bl = search(a, left(L)); bool br = search(a, right(L)); return bl || br; } }
Which of the following describes a problem with the function when executed?
Consider the following pseudo-code, which indicates part of a standard binary tree algorithm.
print( node ) { print data; if( there is a left child ) print( left child ); if( there is a right child ) print( right child ); }
Which of the following is the standard name for this algorithm?
print( node ) { if( there is a left child ) print( left child ); print data; if( there is a right child ) print( right child ); }
Suppose S is a binary search tree and x is not an element of S. Consider the following code fragment.
S.remove( x ); S.insert( x );
If T is the tree that results from executing the fragment, what is the relationship between S and T?
Consider the following outline of a memoized function.
int f( int x ) { ... }
How many steps does it take to make 10000 calls to f on input x = 1000?
Consider the following definition of a function hash.
size_t hash( const char *str ) { size_t h = 0; for( ; *str != '0'; str++ ) h += *str; }
What, if anything, is wrong with hash as a hash function for null-terminated strings?
size_t hash( const char *str ) { size_t h = 0; for( ; *str != '0'; str++ ) h = h*256 + *str; }
Suppose a user-defined hash function has the following prototype.
size_t hash( const Thing& x );
Suppose further that it produces values of size up to 32 bits. To use this function with a hash table of size m, the user should