Docsity

Exam 2 Answers for CIS 4930 - Application Development Using C++, Exams of Computer Science

Toggle sidebar
bg1
Instructor: Dr. Kun Suk Kim
Spring 2004
Application Development Using C++
CIS 4930 (Section 7415X)
Exam 2 Answers
CLOSED BOOK
50 Minutes
Name:
NOTE:
1. All answers will be graded on correctness, efficiency, clarity, elegance and other normal criteria
that determine quality.
2. The points assigned to each question are provided in parentheses.
pf3
pf4
pf5

Partial preview of the text

Download Exam 2 Answers for CIS 4930 - Application Development Using C++ and more Exams Computer Science in PDF only on Docsity!

Instructor: Dr. Kun Suk Kim

Spring 2004

Application Development Using C++

CIS 4930 (Section 7415X)

Exam 2 Answers

CLOSED BOOK

50 Minutes

Name:

NOTE:

  1. All answers will be graded on correctness, efficiency, clarity, elegance and other normal criteria that determine quality.
  2. The points assigned to each question are provided in parentheses.
  1. (9) Consider the class inheritance:

class B { public: B(); B(int nn); void f(); viod g(); private: int n; }; class D: public B { public: D(int nn, float dd); void h(); private: double d; };

(a) (3) How many private members does an object of class D have? (C)

A) 0 B) 4 C) 2 D) 1 E) 3

(b) (3) How many public members does an object of class D have? (E)

A) 3 B) 5 C) 2 D) 6 E) 4

(c) (3) Which of the following functions can be invoked by an object of class D? (D)

A) g() B) f() C) h() D) all of the above

  1. (10) Give short answers to the following questions.

(a) (5) What, if anything, is wrong with the following class template?

template class A { public: A(); ... private: ... };

template A::A() { ... }

In the constructor instantiation, A::A() is wrong. It must be A::A(). (b) (5) What is wrong with following function template (other than the fact that it really does almost nothing)?

template int f(int & x) { return x; }

int main() { int y = 3, z; z = f(y); }

Every template argument specified in the template-argument-list must be used in the argument of function template.

  1. (18) Which variable is a local variable? Which variable is a global variable? Which state- ment makes names from a namespace available? (What namespace?) Which are an error? (If so, why?) Which introduce a variable from a namespace? (What namespace?) Which hide a global variable?

#include namespace X { int i, j, k; } int k; void f1() { int i = 0; using namespace X; // (a)(2) makes names from namespace X available i++; // (b)(2) is the local variable i j++; // (c)(2) is X::j k++; // (d)(2) is an error: ambiguous - is this X::k or // the global k? ::k++; // (e)(2) is the global k X::k++; // (f)(2) is explicitly X’s k using X::i; // (g)(2) is an error, i is declared twice using X::k; // (h)(2) hides global k using std::cout; // (i)(2) introduces cout from namespace std }

  1. (23) Give short answers to the following questions.

(a) (6)What is the difference between the iterators defined here:

vector vec; // put 10 values into vec const vector::iterator p = vec.begin(); vector::const_iterator q = vec.begin();

*p=0; // OK to change vec where p points p=vec.end(); // error: l-value specifies const object // can’t change p itself q=vec.end(); // OK to change q *q=0; // error: l-value specifies const object // can’t change vec where q points.

(b) (12) Suppose we are given an STL vector container named vec that holds values of type double. Explain what each of the following mean:

*(vec.end()-vec.size()); // (a)(2) *(vec.end()-1); // (b)(2) vec.back(); // (c)(2) *(vec.rend()-1); // (d)(2) *(vec.begin()+vec.size()-1); // (e)(2) *(vec.rbegin()); // (f)(2)

(a) and (d) evaluate to the first element of the container. (a) starts with an iterator past the end, then subtracts the number of elements from this iterator, which results in an iterator to the first element of the vector. (d) starts with an iterator in front of the first element, then moves one towards the rear resulting in an iterator to the first element of the vector. (b), (c), (e), and (f) evaluate to the last element of the container. (b) starts with an iterator past the end, and moves one towards the front. Dereferencing gives the last element. (c) returns the back element. (e) begins with an iterator to the first element, add one less than the size, giving an iterator to the last element. Derefer- encing gives the last element. Finally, in (f), rbegin() member returns an iterator to the last element (the beginning element in reverse traversal of the vector elements). Dereferencing gives the last element. (c) (5) For a vector, inserting or deleting invalidates iterators in positions after the in- sertion or deletion. What happens to the set (or map) container if we delete in the middle? Insertion does not affect iterators, and deletion only invalidates iterators to the re- moved item.