UCLA PIC 10B,Problem Solving using C++

Summer 2001,

 

Instructor: Ivo Dinov, Asst. Prof. In Mathematics/Neurology

 

Final Exam Study Guide

 

Wednesday, Aug. 15, 2001, 9:30 AM, PPB # 2214

 

Multiple choice questions may have more than one correct answer. Give all correct answers. If an answer to a multiple choice question is in some sense only partially correct, an explanation should be worth points. True - False questions require an explanation. This study guide is complementary to the MidTerm Study Guide (which is also available online at the PIC10B class notes page).

 

I.                Part One

 

1. Define or characterize the template facility for C++.

 

2. True or False: Templates allow only parameterized types for classes.

 

3. True or False: Templates allow only parameterized types for functions.

 

4. Templates may be used to make available a very large collection of _____.

 

††††††† a) programs

††††††† b) classes

††††††† c) file

††††††† d) functions

††††††† e) none of the above. C++ doesn't support templates.

 

5. In the template prefix,

 

††††† template <class T>

 

What kind of variable is the parameter T?

 

††††††† a. T must be a class

††††††† b. T must not be a class

††††††† c. T can be only types built into the C++ language

††††††† d. T can be any type, whether built into C++ or declared by the client programmer.

 

6. True-False. A function template behaves as if the programmer had overloaded the function name for every type used in an instantiation.

 

7.For the following template declaration of the following function,

 

††††† template<class T> void problem_7( T object);

 

††††††††††††††† a. Give a statement that causes the compiler to generate a function that

††††††††††††††† ††† passes int values to the function problem_7.

 

†††††††††††††† b. Give an equivalent function declaration (prototype) for the function

††††††††††††††† ††† generated by this usage.

 

8.Write a function template for a function named minimum. The function will have two parameters of the same type. It returns the smaller of these (either if they are equal.)

 

In carrying this out, give:

 

††††† a. a prototype (declaration) and preconditions for the function template

††††† b. a definition for this function.

††††† c. As a part of your answer, remark on the restriction of this function template to types for which operator< defined.

 

9. Describe a strategy for writing template functions.

 

 

10. What is wrong with the following function template (Other than the fact that it really does almost nothing)?

 

template<class T>

void f( int & x )

{

††† return x;

}

int main()

{

††† int y = 3, z;

††† z = f(y);

}

 

11. How many type parameters may a function template have?

††††††††† a) none, that is not what the parameters in a function template are called.

††††††††† b) 1

††††††††† c) 2

††††††††† d) as many as are needed

 

 

The answer to next question is very compiler dependent.It is based on the PITFALL: Compiler Complications in the text on page 754.Use of this question requires investigation of problems with your compilerís template facility.

 

12. The code for a template function is generated when:

 

††††††††† a) the function template declaration (prototype) appears in the C++ program.

††††††††† b) the function template is encountered in the C++ program.

††††††††† c) the function call is encountered in the C++ program

††††††††† d) at runtime, when the function call is executed.

 

 

13. What problems with templates have you encountered with regard to your compilerís template facility?

 

14. True-False: In writing a class template, the function declarations within the class have special placement and special location.

 

15. True-False: In implementing class template member functions, the functions are themselves templates.

 

16. Given this (incomplete) class template below, complete the template and show how to implement the member function

void f( T x );

 

Hint: What the function does is immaterial. I suggest you use this for the function body.

{

††† //whatever f does

}

 

template<class T>

class Problem

{

public:

††† . . .

††† void f( T x );

private:

††† int x;

};

17. True False: In the implementation of a class template function, the class name before the scope resolution operator is just the class name, with no additional syntax.

 

18. What if anything is wrong with the following code?

template<class T>

class A

{

public:

††† A();

††† ...

private:

††† ...

};

 

template<class T>

A::A()

{

††† ...

}

 

19. What if anything is wrong with the following code?

template<class T>

class A

{

public:

††† A();

††† ...

private:

††† ...

};

 

A<T>::A()

{

††† //...

}

 

20. Discuss briefly, indicating whether mostly true or mostly false and justify:

††††††††††††††† Friends are used exactly the same for template and non-template classes.

 

21.Which of the following template function definitions and invocations will not compile, if any?If not, why not?

 

††††††††† Assume that classes have been declared with appropriate

††††††††† default constructors and assume that

 

//a.

template<class A>

A func( A x, A y){ return A(); }

int main()

{

††† U u1, u2, u3;

††† u2 =func(u2, u3);

}

 

//b.

template<class B>

B func() { return 1; }

int main()

{

††† T t;

††† t = f();

}

//c.

template<class C>

void func(C x, int * y){}

int main()

{

††† T t;

††† int i;

††† func( t, &i );

}

 

//d.

template<class D, class E>

void func(D x, E y){}

 

int main()

{

††† T t;

††† U u;

††† func ( t, u );

}

 

 

II.           Part Two

 

1. A linked list is

††††††††††††††† a) fixed in size

††††††††††††††† b) can vary in size, shrinking or growing as there is need

††††††††††††††† c) can be set, and then not changed other than destroying the list

††††††††††††††† d) none of the above

 

2. Given the structure definition:

 

††††††††††††††† const int STRING_SIZE = 20;

††††† struct ListNode

††††† {

††††††††††† char item[STRING_SIZE];

††††††††††† int count;

††††††††††† ListNode * link;

††††† };

 

††††† ListNode * head = new ListNode;

 

Show two equivalent ways to set the count member in the ListNode variable to which head points.

 

3.Given the structure definition:

 

††††††††††††††† const int STRING_SIZE = 20;

††††† struct ListNode

††††† {

††††††††††† char item[STRING_SIZE];

††††††††††† int count;

††††††††††† ListNode * link;

††††† };

 

††††† ListNode * head = new ListNode;

 

Give code to assign the string "Wilbur's brother Orville" to the member item of the variable to which head points. Hint: you need a function declared in cstring.

 

4. The link field in the last node in a linked list has a special value stored in it. What is it? It has a special name. What is the special name?Why is this done?

 

5. Given the type definitions:

 

††††††††† const int STRING_SIZE = 20;

††† struct ListNode

††† {

††††† char item[STRING_SIZE];

††††† int count;

††††† ListNode * link;

††† };

 

††††††††††††††† What is the output produced by the following code? (As always,you are to assume that

††††††††††††††† the code is embedded in a correct and complete††††††††††† program.

 

††††††††††††††† ListNode * head = new ListNode;

††††††††††††††† strcpy( head->item, "Stop Light");

††††† head->count = 10;

††††† cout << (*head).item << endl;

††††† cout << head->item << endl;

††††† cout << (*head).count << endl;

††††† cout << head->number << endl;

 

6.Given the type definitions:

 

††††††† const int STRING_SIZE = 20;

†† struct ListNode

†† {

††††† char item[STRING_SIZE];

††††† int count;

††††† ListNode * link;

†† };

 

†† ListNode * head = new ListNode[10];

 

Show how to return the store allocated by this statement to the heap manager for reallocation.

 

7. Given the type definitions:

 

††††††† const int STRING_SIZE = 20;

†† struct ListNode

†† {

††††† char item[STRING_SIZE];

††††† int count;

††††† ListNode * link;

†† };

 

†† ListNode * head = new ListNode;

 

Give a typedef statement that hides the pointer operator *. Call the new type identifier NodePtr.

 

8. Which of the following are potential problems when we use the delete operator on a pointer variable.

††††††††††††††† a) inaccessible heap memory

††††††††††††††† b) dangling pointers

††††††††††††††† c) uninitialized pointers

††††††††††††††† d) NULL pointers

 

9. Which of the following are potential problems when we assign pointer variables:

††††††††††††††† a) inaccessible heap memory

††††††††††††††† b) dangling pointers

††††††††††††††† c) uninitialized pointers

††††††††††††††† d) NULL pointers

 

10.Here is a diagram of a three node linked list with a head pointer and a node to be inserted. Show all steps necessary to insert the node pointed to by temp between the second and third nodes, including the search for the node prior to insertion.

 

You are to make clear the sequence of steps by either numbering the steps, or by making several copies of this drawing, with one change per drawing, numbered to show the sequence.

 

You may assume a search routine taking a head node and data has been defined for you.

 

Give code and the drawing to make absolutely clear what you are doing.

 

11.Here is a diagram of a three node linked list with a head pointer and a node to be inserted. Show all steps necessary to delete the second node, pointed to by discard_me. You may assume the necessary search for the node to be deleted is done and the pointer discard_me is already positioned.

You are to make clear the sequence of steps by either numbering the steps, or by making several copies of this drawing, with one change per drawing, numbered to show the sequence.

You may assume that a search routine taking a head pointer and data arguments has been defined for you.

Give code and the drawing to make absolutely clear what you are doing.

 

††††††††††††††††††††††††††††††† NodePtr discard_me;

††††††††††† NodePtr temp_ptr;

 

12. Answer, and explain: For large lists, inserting a new item into a linked list compared to insertion in an array is

††††††††††††††† a) more efficient.

††††††††††††††† b) less efficient.

††††††††††††††† c) about the same.

††††††††††††††† d) depends on the size of the two lists.

 

13.Answer, and explain: For large lists, deleting an item from a linked list compared to deletion from an array is

††††††††††††††† a) more efficient.

††††††††††††††† b) less efficient.

††††††††††††††† c) about the same.

††††††††††††††† d) depends on the size of the two lists.

 

14. A linked list is normally specified by giving a pointer pointing to the first node of a list. An empty list has no nodes at all. What is the value of a head pointer for an empty list?

 

15. Suppose you have the following struct definition and typedef statements in your program:

 

††††††††††††††† struct N

††††† {

††††††††††† double d;

††††††††††† N* next;

††††† };

††††† typedef N* node_ptr;

††††† node_ptr p1;

 

Now suppose further that p1 points to a node of type N in a linked list. Write code that makes p1 point to the next node on the linked list.

 

16.Suppose you have the following struct definition and typedef statements in your program:

 

††††††††††††††† struct N

††††† {

††††††††††† double d;

††††††††††† N* next;

††††† };

††††† typedef N* node_ptr;

††††† node_ptr head, p1, p2;

 

Now suppose further that p1 points to a node of type N in the middle of a linked list (neither the first nor the last node).Write code that deletes the node after the node p1 points to in the linked list. After the code is executed, the linked list should be the same, excepting the specified node is missing.

 

17. The discipline for a stack is:

††††††††††††††† a) data first inserted is the data first out.

††††††††††††††† b) for a misbehaving stack, 30 lashes.

††††††††††††††† c) data last inserted is the data first out.

††††††††††††††† d) depends on the application of the stack.

 

18. Data is removed from a stack in the _____ _______ it was entered.

††††††††††††††† a) same order

††††††††††††††† b) reverse order

††††††††††††††† c) alternating order

††††††††††††††† d) sorted order

†††††††††††††††

 

19. Name the stack operations as implemented in the text. Tell what each does.

 

20. Give pseudocode for inserting a node in front of the head node in a linked list.

 

 

III.      Part Three

 

1. True/False: In C++, inheritance has to do with the gene pool. Explain.

2. What are the benefits of inheritance and Object Oriented Programming?

3. Given the class below, tell what the default constructor initializes the data member to. Name this initialization technique and give another way to write the constructor.

††††† class A

††††† {

††††† public:

††††† A();

††††† private:

††††† int a;

††††† };

†††††

††††† A::A() : a(17)

††††† {

††††† //deliberately empty

††††† }

4. A base/member initialization list is preceded by

††† a) two colons

†† b) a dot (or period)

††† c) a single colon

††† d) a semicolon

5.A base/member initialization list produces results that are exactly ________

††† a) assignment

††† b) redeclaration

††† c) redefinition

††† d) initialization

††† e) output

6. The statement int sum(0); performs ____________

††† a) a declaration of a function

††† b) definition of and intializes an int variable to 0.

††† c) redefines sum

††† d) overloads sum

7. When a derived class inherits from a base class, how is the base class constructor called?

8. Should members data be initialized in an initializer list? If not always, when should an exception be made?

9. In the code for HourlyEmployee that is derived from Employee, the constructor code appears

†† HourlyEmployee::HourlyEmployee(string new_name,†††

††††††††††††††††††††††† string new_number,

††††††††††††††††††††††† string new_wage_rate,

††††††††††††††††††††††† double new_hours

††††††††††††††††††††††† )

: Employee(new_name, new_number), wage_rate(new_wage_rate),

††††††††††††††††††††††††††††††††††† hours(new_hours)

Describe the purpose of the items after the : in this constructor.

10. Given the declarations:

†† class S

†† {

†† public:

†††† S(){}

†††† void doIt(int& x);

†† private:

†††† int a;

†††† int b;

†† };

†† S s;

†† int n;

Write a call to the member function doIt with calling object s and argument n.

11. Given the declarations and definitions:

†† class S

†† {

†† public:

†††† // Assume proper constructors have been defined.

†††† // The declaration that is part of your answer would go here.

†† private:

†††† int a;

†††† int b;

†† };

Give a declaration (prototype) of a member function, a_plus_b for this class that takes no arguments and returns an int value.

12. Given the declarations and definitions:

†† class S

†† {

†† public:

†††† // Assume proper constructors have been defined.

†††† // The declaration that is part of your answer would go here.

†† private:

†††† int a;

†††† int b;

†† };

†† // The definition that is part of your answer would go here.

Give a definition of a member function named a_plus_b for this class that returns the sum of the int members a and b of the class S.

13. Consider the following program:

†† #include <iostream>

†† class base

†† {

†† public:

†††† virtual void member() {std::cout << "base class\n";}

†† };

 

†† class derived : public base

†† {

†† public:

†††† void member() {std::cout << "derived class\n";}

†† };

†† void function( base& x)

†† {

†††††† x.member();

†† }

†† int main()

†† {

††††† base b;

††††† derived d;

††††† function(b);

††††† function(d);

†† }

†† a) Give the output of this code.

††† b) Give the output when the keyword virtual is omitted.

†† c) Explain these results.

14. Describe the 'slicing problem' in inheritance.

15. Why can't we assign a base class object to a derived class object?

16. What is the problem assigning with the (legal) assignment of a derived class object to a base class object?

17. What is the meaning of the keyword protected seen in base classes?

18. What is the meaning of the phrase "public inheritance"?

19. Consider the class definition and main function.

††† //file 15t14.cc

††† #include <iostream>

††† class A

††† {

††† public:

††††††† A();

††† virtual int f();

††† private:

††††††† int a;

††† };

††† A::A():a(0) { std::cout << "constructor A() called \n";}

††† int main()

††† {

†††††† A x;

††† }

At this point the programmer compiles the class definition and main function. She gets the error message:

g++ 15t14.cc

/tmp/cca237091.o: In function `A::A(void)':

/tmp/cca237091.o(.text+0xa): undefined reference to `A virtual table'

Explain what is wrong.

20. Given the following classes:

††† class base

††† {

††† public:

††††† void pb();

††† protected:

††††† int b1

††† private:

††††† int b2;

††† };

 

††† class derived : public base

††† {

††† public:

††††††† void pd();

††† protected:

††††† int d;

††† };

††† // assume that all member and friend functions are

††† // defined here.

††† void outside(){/* . . . */}

††† base bb;

††† derived dd;

List every member that the function mentioned below can access. Several cases will be the same.

a) function base::pb()

b) function derived::pd()

d) function outside()

 

21. Describe and distinguish function overloading, redefinition of functions and overriding of functions.

 

IV.      Part Four

 

1. Write a program to convert from 24 hour time to 12 hour time.You may use class DigtalTime from Chapter 8 as a starting point. You will need to replace the terminating error reporting code with calls to the required exception, TimeFormatError.Provide error messages appropriate to the particular errors as detected.Then insert the try-catch blocks, terminating the program at the error. After this is tested and debuggedprovide the loop to request repetition.In this example we need to show we understand the three most important features of exceptions: throwing, catching, retrowing and processing.