+ All Categories
Home > Documents > Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web...

Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web...

Date post: 26-Mar-2018
Category:
Upload: dinhtram
View: 214 times
Download: 0 times
Share this document with a friend
34
CS 215 final exam, Practice Answer all 29 questions, front and back, clearly and neatly in the space provided. Circle the correct answer for multiple choice questions. Name and section: 1 Multiple Choice 1. (1 point) The new operator allocates objects in a region of memory called the: A. node B. stack C. heap D. queue E. global 2.(1 point) Which of the following methods adds an item to a vector? A. push_back B. pop back C. push_ front D. add E. operator+ 3.(1 point) What is the output of the following code? int i = 11; while (i > 0 && (i % 2 == 1)) { cout << i << “ ”; i = i / 2; } cout << endl; A. 11 5.5 B. 5 2 1 0 C. 11 5.5 2.25 1.125 . . .
Transcript
Page 1: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

CS 215 final exam, PracticeAnswer all 29 questions, front and back, clearly and neatly in the space provided. Circle the correct answer for multiple choice questions.

Name and section:

1 Multiple Choice

1. (1 point) The new operator allocates objects in a region of memory called the:A. node B. stack C. heap D. queue E. global

2. (1 point) Which of the following methods adds an item to a vector?A. push_back B. pop back C. push_ front D. add E. operator+

3. (1 point) What is the output of the following code?

int i = 11;while (i > 0 && (i % 2 == 1)){

cout << i << “ ”; i = i / 2;

}cout << endl;

A. 11 5.5

B. 5 2 1 0

C. 11 5.5 2.25 1.125 . . .

D. 5 2E. 11 5

4. (1 point) Which of the following correctly loops over the items in the vector scores?

A. for (size t i = 0, i <= scores.size, i++)

B. for (size t i = 1; i <= scores.size(); i++)C. for (size t i = 0; i < scores.size(); i++)

D. for (size t i = 1, i < scores.size, i++)

Page 2: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

E. for (size t i = 0; i <= scores.size(), i++)

5. (1 point) If a function requires 8n+2+n2/10 operations, what is its order of

complexity?A. O(8n) B. O(n) C. O(n2/10) D. O(n2) E. O(n log n)

6. (1 point) Which method checks whether a stream has encountered an error?A. err B. error C. fail D. eof E. close

7. (1 point) Which method allows using a stream again after encountering an error?A. good B. clear C. ignore D. eof E. open

8. (1 point) What is the result of the following code?

int array[5] = { 1, 2, 4, 8, 16 };int *p = array;p = p + 2;

A. The second line has an error.

B. The third line has an error.

C. p points to the number 2

D. p points to the number 3

E. p points to the number 4

9. (1 point) What is the term for the situation where a base-class pointer points to a derived-class object?

A. abstraction B. polymorphism C. overloading D. slicing E. inheritance

10. (1 point) What is the term for redefining a method in a derived class?A. overloading B. overriding C. inheritance D. polymorphism E.

slicing

11. (1 point) Which of the following lines correctly calls a base-class method from a derived- class method of the same name?

A. method()

B. super.method()

C. Base.method()

D. Base::method()

E. Base method()

Page 3: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

2 Short Answer (27 points)

12. (3 points) Write an ordinary for loop that does the same thing as this range-based for loop (prints every element of the vector):

vector<int> vec; for (int i : vec)

cout << i << endl;

Solution:

// Either:for (size_t i = 0; i < vec.size(); i++) // int i is okay cout

<< vec[i] << endl// or:for (vector<int>::iterator i = vec.begin(); i != vec.end(); i++)

cout << *i << endl;

13. (4 points) Provide a recursive definition for the number of digits in a positive integer. That is, digits(7) = 1, digits(99) = 2, and digits(100) = 3. Hint: divide by 10.

You may assume integer division as in C++. You do not have to write code, but merely describe the base case and recursive case mathematically. For example:

• Base case: factorial(0) = 1

Page 4: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

• Recursion: factorial(n) = n ∗ factorial(n − 1)

Solution:

• Base case: digits(1 . . . 9) = 1 or digits(0) = 0 (2pt)• Recursion: digits(n) = 1 + digits(n/10) (2pt)

Page 5: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

14. (4 points) To avoid infinite recursion, what must be true of the base case, what must be true of the recursive case, and what must be true of the relationship between them?

Solution: You must check for the base case before doing any recursion (or: the base case must come before the recursive case): 2pt.

The base case must not make any recursive calls: 1pt.

The recursive case must call the function on a smaller input (or, an input that is closer to the base case): 2pt.

(To a maximum of 4 points; just A and C are enough for full credit)

Page 6: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

15. (4 points) Draw the singly-linked list structure that results from executing the follow- ing operations in order. Remember to indicate the head pointer and any null pointers.

list<int> values;

values.push_back(9);values.push_front(2);

values.push_front(6);values.push_back(3);

Solution:list1 pt for correct order of nodes1 pt for head pointer 1 pt for links1 pt for null pointer in the last node (can be a slash, word “null”, arrow to “null”, etc.)

Page 7: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

16. (6 points) Consider the following variable definitions, and assume that both Cat andDog are derived classes of Animal.

Cat garfield; Dog brian; Animal pet; Cat *mine; Dog *yours; Animal *hers;

For each of the following, is it (A) illegal, (B) legal but causes slicing, or (C) completely legal without slicing.

(a) mine = &pet;

(b) brian = *yours;

(c) yours = *brian;

(d) pet = garfield;

(e) hers = &brian;

(f) *hers = *mine;

Page 8: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

Solution:

(a) A(d) B

(b) C(c) A(e) C(f) B

Page 9: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

3Long Answer (28 points)

17. (6 points) Explain the difference between the . (dot) and -> (arrow) operators. Show an example of using each to call the length method on a string. Declare and initialize all variables you use.

Solution: The dot operator takes an object on its left hand side, while the arrow operator takes a pointer to an object.

string s = "Hello";cout << s.length() << endl;

string *p = new string("World"); // or &s cout << p->length() << endl;

2 points for explaining the difference 1 point for string declaration1 point for call with dot (syntax, etc.) 1 point for string pointer declaration1 point for call with arrow (syntax, etc.)

18. (a) (3 points) What is the meaning of the virtual keyword, and how does it relate to inheritance and overriding?

Page 10: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

Solution: When calling an overridden method polymorphically (using a base- class pointer or reference to a derived-class object), if that method is virtual, the derived-class version is called. If it is not, the base-class version is called.1pt for mentioning polymorphism, static versus dynamic type, or base-class pointers or references,1 pt for calling base versus derived class method1 pt for correct details (virtual means use the method from the dynamic type, which may be a derived class)

(b) (2 points) Where should you write the virtual keyword to mark a method as virtual? With the base class, the derived class, or both? In the class definition, the method’s implementation, or both?

Solution: In the definition (1pt) of both classes (1pt). -0 points for only the base class: it works (virtual is inherited), but including it in the derived classes makes the code more clear to people reading the code.

Page 11: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

(c) (3 points) Give an example of a few lines of code that behave differently depending on whether or not a method is virtual, and explain the difference in behavior.

Solution:Vehicle *veh = new Car(); veh->drive();If drive is virtual in class Vehicle, the second line calls Car::drive; if not, it calls Vehicle::drive.1 pt for an example with a base and a derived class, and a pointer or reference 2 pt for describing correct results.

Page 12: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

19. (8 points) Explain at least three benefits of using classes in a program. Explain how classes provide those benefits, and why they are benefits.

Solution: Classes help organize the program into operations that work on sets of related data. They help to avoid global variables, by collecting the data a set of functions works on into a single data structure.

Classes also promote encapsulation: they provide because the only access to private data is through the methods, outside code does not depend on the exact imple- mentation, but only on the interface (the methods). In particular, this means the programmer can make changes and improvement to the implementation without affecting the users of the class. It also means that classes can make and enforce guarantees about their contents (for example, that the price can never go below 0).RUBRIC: Please make a spreadsheet with scores for this problem. Student learning outcome (e): An understanding of professional issues and responsibilities.

• 8 pts: Exceeds standards.

– Student clearly shows full understanding of the benefits of encapsulation and object-oriented programming. Student clearly and completely states three advantages of classes, provides justification for their answer, and explicitly refers to encapsulation or to separating interface from imple- mentation.

• 6 pts: Meets standards.

Page 13: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

– Student shows good understanding of the benefits of encapsulation and object-oriented programming. Student clearly states three advantages of classes, at least one of which refers (perhaps indirectly) to encapsulation.

• 4 pts: Partially meets standards.

– Student shows some understanding of the benefits of object-oriented pro- gramming. Student states at least two advantages of classes, though one of those may be superficial or subjective (“it looks better”). Student refers to interface and implementation in passing, if at all.

• 2 pts: Does not meet standards.

– Student does not demonstrate understanding of object-oriented program- ming. Student states only one advantage, or only superficial or subjective advantages. Student does not refer to interface and implementation.

• 0 pts: Substantially lacking

– Answer is irrelevant, missing, or does not address the question.

Page 14: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

20. (6 points) Members of a class can go into one of three sections: public, private, and protected. For each of the three sections, list which parts of the program can access those sections, and what kind of members should go in that section.

Solution: The public section is accessible by the rest of the program. It usually holds methods that make up the interface of the class..

The private section is accessible only by the class itself. It usually holds data members (and/or internal helper methods).

The protected section is accessible to the class itself and to its derived classes. It usually holds helper methods that can be used by derived classes. (-0 for holding data members that should be accessible to derived classes: violates encapsulation, but it does happen).

For each section: 1 point each for where it can be used, 1 point for what goes there (either a description or an example).

Page 15: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

4Code (29 points)

Provide complete code for each example. Remember to declare all variables. You do not have to write a main function or include header files.

21. Suppose you have a function bool isvowel(char c) that tests whether a single char- acter is a vowel and returns true or false.

(a) (5 points) Write a function that takes a string parameter and returns true if it contains at least one vowel.

Solution:bool has_vowel(string in) { // call by reference also okay

// Iterator or range-based loop also okay. for (size_t i = 0; i < in.size(); i++) {

if (isvowel(in[i])) return true;

// or isvowel(in[i]) == true}return false;

}

1 pt: parameter and return types 1 pt: loops over the string1 pt: correct call to isvowel1 pt: returns true when it finds a vowel1 pt: returns false at the end (not inside the loop).

(b) (5 points) Write a function that takes a string and returns true if it consists onlyof vowels.

Solution:bool all_vowels(string in) {

for (size_t i = 0; i < in.size(); i++) {if (!isvowel(in[i]))

return false;// or isvowel(in[i]) == false

}return true;

}

1 pt: answer is similar to part (a)2 pt: returns false when it finds a non-vowel

Page 16: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

2 pt: returns true at the end (not inside the loop).

Page 17: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

22. Consider the following class definition:

class Animal{

public:// NOTE: no default constructor! Animal(string thegenus, string thespecies); virtual void eat();

private:string genus; string species;

}

(a) (5 points) Define (not implement) a Carnivore class that inherits from Animal. It should have a data member that counts the number of teeth, and a constructor that takes the genus, species, and number of teeth. It should override the eat method.

Solution:class Carnivore : public Animal { public:

Carnivore(string genus, string species, int teeth); virtual void eat();

private:int num_teeth;

};1 pt for inheritance syntax (including public) 1 pt for constructor syntax and parameters 1 pt for eat1 pt for num teeth1 pt for not repeating base-class data members.

(b) (4 points) Write the implementation of Carnivore’s constructor.

Solution:Carnivore::Carnivore(string genus, string species, int teeth)

: Animal(genus, species) // , num_teeth(teeth){

num_teeth = teeth; // or above, as in comment// NOT: int num_teeth = teeth;

}1 pt for general syntax (no return type, Carnivore::, etc.) 1 pt for calling base class constructor in the initializer list

Page 18: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

1 pt for setting the new data member (not a local variable) 1 pt for not setting inherited members directly.

Page 19: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

What is the output of the program below?#include <iostream>

using namespace std;

class Bike {public:

double getGears() const;Bike();Bike(int gears);~Bike();

private:int gears;

};

Bike::Bike(){cout << "Def ";gears = 0;

}

Bike::Bike(int inputGears) {cout << "Triple ";gears = inputGears;

}

Bike::~Bike() {cout << "Fold ";

}

double Bike::getGears() const {cout << "Get ";return gears;

}

int main() {Bike b1;if (true) {

Bike b2;}Bike b3 (0.0);cout << " Bikes Rule ";return 0;

}

A. Def Def Bikes Rule Fold Triple Fold FoldB. Def Def Fold Bikes Rule Fold Fold

C. Def Def Fold Fold Bikes Rule Fold

D. Def Def Fold Triple Bikes Rule Fold Fold

Page 20: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

Design the class Goldfish, which models a creature that is intelligent enough toremember capacity characters at a time.

class Goldfish{public:

Goldfish(int capacity);~Goldfish();void remember(char c);void forget();// Clears m_memory using dots('.')void printMemory() const; // Prints the content of m_memory

private:char* m_memory; // Pointer to memory.int m_amount; // # of chars remembered.int m_capacity; // # of chars this fish can remember.

};

1) Define the constructor. Dynamically allocate capacity-many characters tom_memory. Initialize m_memory with dot('.')s. If capacity is not positive,then set it to 3 by default. Remember to set m_capacity.

2. Implement remember. Store the character c into m_memory. If you alreadyhave m_capacity characters memorized, then discard the oldest characterin m_memory to open up a free slot. (This is an example of LRU (LeastRecently Used) replacement.)

3.Implement forget. Clear memory by filling dot('.')s into memory.

3.Implement the deconstructor.

To clarify, here is how a Goldfish object can be used:

int main() { Goldfish nemo(3); nemo.remember('a'); nemo.printMemory(); // prints "a.. " nemo.remember('b'); nemo.remember('c'); nemo.printMemory(); // prints "abc" nemo.remember('d'); nemo.printMemory(); // prints "bcd"

nemo.forget(); nemo.printMemory(); // prints "..." return 0;

}

Page 21: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

1. A set of parallel vectors can be converted into a vector of objects, where the object hasA) a data member that is a vector of vectorsB) a data member that stores the length of the vectorsC) a member function to operate on each of the original vectorsD) a data member that matches the data type of each of the original vectors

Ans: DTitle: What must be in an object that will replace a set of parallel vectors?Difficulty: MediumSection Ref: 9.7

2. Suppose the vectors defined below are known to be parallel vectors. Which object definition could be used in a vector of objects to replace the parallel vectors?

vector <string> name;vector <int> age;vector <double> gpa;A) class Item

{public:...private: string name; int age; double gpa;};

B) class Item{public:...private: string name[10]; int age[10]; double gpa[10];};

C) class Item{public:...private: vector <string> name; vector <int> age; vector <double> gpa;};

D) class Item{public:...private: string <vector> description; int <vector> age; double <vector> gpa;};

Page 22: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

Ans: ATitle: Which object definition can be used to convert to a vector of objects?Difficulty: MediumSection Ref: 9.8

3. Which statement, if executed immediately after the given one, creates orphaned heap memory?CashRegister* register_pointer = new CashRegister;A) delete register_pointer;B) register_pointer = new CashRegister;C) register_pointer->item_count++;D) delete CashRegister;

Ans: BTitle: Which statement creates orphaned heap memory?Difficulty: MediumSection Ref: 9.9

4. The destructor is a special member function and is invoked under which circumstance?A) When a pointer variable is set to nullB) When a heap object is explicitly created with the new commandC) When a variable of that object is declaredD) When a heap object is explicitly deleted with the delete command

Ans: DTitle: When is the destructor invoked?Difficulty: EasySection Ref: 9.9

5. You are given the class definition for CashRegister. One of the member functions of this class is clear(). Furthermore, you have set up and allocated an array of pointers to CashRegister objects. Given the declaration of the array all_registers below, which code snippet correctly calls the clear() function on every object pointed to from the all_registers array? ?

CashRegister* all_registers[20];

A) for (int i = 0; i < 20; i++){ all_registers[i].clear();}

B) for (int i = 0; i < 20; i++){ all_registers.clear()}

C) for (int i = 0; i < 20; i++){ all_registers[i]->clear();}

D) for (int i = 0; i < 20; i++)

Page 23: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

{ all_registers->clear();}

Ans: CTitle: Which code snippet correctly invokes a member function for every object in an array of pointers to objects?Difficulty: MediumSection Ref: 9.7

6. The Department of Motor Vehicles uses a vehicle registration program that declares a Vehicle class as a base class. The Car class and the Truck class both inherit from the Vehicle class. Which types of objects can be passed to the function register(Vehicle& v)?

A) Only Vehicle objectsB) Only Car and Truck objectsC) Vehicle, Car and Truck objectsD) It is impossible to know without examining the implementation of the Car and Truck classes

Ans: CTitle: Which of these object types can be passed to (this function with a base class parameter)?Difficulty: MediumSection Ref: 10.1

7. Consider the following declaration for the Car class:class Car{public: Car(); void set_speed(double new_speed); double get_speed() const;private: double speed;};The AeroCar class inherits from the Car class. For the AeroCar class to override the set_speed function, what must be done?

A) The AeroCar class must define the function void set_speed(double new_speed)B) The AeroCar class must define the function void override_set_speed(double

new_speed)C) The AeroCar class must define the function void override(string set_speed, double

new_speed);D) The AeroCar class cannot override the set_speed function.

Ans: ATitle: What must be done for the AeroCar class to override (its base class's) set_speed function?Difficulty: MediumSection Ref: 10.2

8. What is the output of the following code snippet?

Page 24: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

class Cat{public: Cat(); void set_speed(double new_speed); double get_speed() const;private: double speed;};Cat::Cat(){ speed = 0;}void Cat::set_speed(double new_speed){ speed = new_speed;}double Cat::get_speed() const{ return speed;}class Cheetah : public Cat{public: Cheetah(){Cat();} void set_speed(double new_speed); void add_speed(double new_speed); double get_speed() const;};void Cheetah::set_speed(double new_speed){ Cat::set_speed(10 * new_speed);}void Cheetah::add_speed(double new_speed){ Cat::set_speed(Cat::get_speed() + new_speed);}double Cheetah::get_speed() const{ return Cat::get_speed();}int main(){ Cheetah cheetah1; cheetah1.set_speed(10); cheetah1.add_speed(250); cout << "Speed: " << cheetah1.get_speed(); return 0;}

The output is:

A) Speed: 150B) Speed: 250

Page 25: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

C) Speed: 350D) Speed: 450

Ans: CTitle: What is the output of this snippet (that calls overridden member functions)?Difficulty: HardSection Ref: 10.2

9. Which of the following statements is correct about a recursive function?

A) A recursive function must call another function.B) A recursive function calls itself.C) A recursive function must be simple.D) A recursive function must never call another function.

Ans: BTitle: Which is correct about a recursive function?Difficulty: EasySection Ref: 11.1

10. What is the output of the following code snippet?int myfunction(int n){ if (n < 2) { return 1; } return n * myfunction(n - 1);}int main(){ cout << myfunction(3) << endl; return 0;}

A) 24B) 6C) 2D) 120

Ans: BTitle: What is output of snippet (with recursive function)?Difficulty: MediumSection Ref: 11.1

11. Suppose you need to write a recursive function power(double x, int n) that calculates x to the power of n. Which of the following would be a correct way to implement the function power?

A) Call power(x, n - 1) and multiply by n.B) Call power(x, n) and multiply by (n – 1).C) Call power(x - 1, n) and multiply by x.D) Call power(x, n - 1) and multiply by x.

Page 26: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

Ans: DTitle: Which is a correct way to implement this recursive function?Difficulty: MediumSection Ref: 11.3

12. Two quantities a and b are said to be in the golden ratio if (a+b)

a is equal to ab . Assuming a and b are

line segments, the golden section is a line segment divided according to the golden ratio: The total length (a + b) is to the longer segment a as a is to the shorter segment b. One way to calculate the golden ratio is through the continued fraction:

golden ratio =

1+ 1

1+ 1

1+ 11+ .. . . Which function below is a correct recursive implementation of this

continued fraction?

A) double golden(int number){ if (number < 1) { return 0.0;}

return 1.0 + 1.0 / golden(number - 1); }B) double golden(int number)

{ if (number <= 1) { return 0.0;}

return 1.0 + 1.0 / golden(number - 1); }C) double golden(int number)

{ if (number <= 1) { return 1.0;}

return 1.0 + 1.0 / golden(number + 1); }D) double golden(int number)

{ if (number <= 1) { return 1.0;}

return 1.0 + 1.0 / golden(number - 1); }

Ans: DTitle: Which recursive function correctly implements the continuing fraction for the golden ratio?Difficulty: HardSection Ref: 11.2

13. Consider the following recursive function:1. void myfun(string word) 2. {3. if (word.length() == 0) { return; }4. myfun(word.substr(1, word.length()));5. cout << word[0];6. }

Page 27: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

What changes about this function if lines 4 and 5 are swapped?

A) nothingB) creates infinite recursionC) prints the characters of the string in both forward and reverse orderD) reverses the order in which the characters of the string are printed

Ans: DTitle: What changes about the output if lines of the function are swapped?Difficulty: EasySection Ref: 11.1

14. The swap function below contains an error. What is it?void swap(int x, int y){ int temp = x; x = y; y = temp;}

A) It is not legal to have three statements on a single lineB) Both parameter variables must be passed by referenceC) The array being sorted must be a parameter variableD) The swap is not correctly implemented by the assignment statements

Ans: BTitle: What is the error in the swap function?Difficulty: EasySection Ref: 12.1

14. Suppose you are given an array of objects for which there is no reasonable ordering, i.e., the objects are fruits (it is difficult to compare apples to oranges). How best should this array be searched?

A) binary searchB) sequential searchC) binary search followed by sequential searchD) divide-and-conquer

Ans: BTitle: How should an unordered collection be searched?Difficulty: EasySection Ref: 12.6

15. Can you delete a node from a singly linked list if only the pointer to that node is provided?

A) No, the node cannot be deleted with the given information. B) Yes, copy the data from the next node into this node and delete the next node.C) Yes, copy the data from the previous node into this node and delete the previous node.D) Yes, simply delete this node.

Ans: A

Page 28: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

Title: Can you delete a singly-linked list node with only the pointer to that node?Difficulty: MediumSection Ref: 13.2

16. Suppose that the node_a variable is a pointer to node A in a doubly-linked list. Also assume the code snippet below:

Node* before = node_a->previous;Node* after = node_a->next;Which of the following code snippets will correctly remove node A from the doubly linked list?

A) before->previous = after;after->next = before;delete node_a;

B) before->next = after;after->previous = before;delete node_a;

C) delete node_a;before->next = after;after->previous = before;

D) delete node_a;before->previous = after;after->next = before;

Ans: BTitle: Which snippet will correctly remove node A from the doubly-linked list?Difficulty: MediumSection Ref: 13.2

17. What is the output of the following code snippet?int size = 2;queue<string> q;q.push("1");q.push("2");q.push("3");q.push("4");int i = 0;while (i< size){ q.pop(); cout << q.front() << endl; ++i;}

A) 12

B) 23

C) 34

D) 2

Page 29: Multiple Choice - cs215uky.wikispaces.comcs215uky.wikispaces.com/file/view/Final exam...  · Web viewCS 215 final exam, Practice. Answer all 29 questions, front and back, clearly

4

Ans: BTitle: What is output of snippet (with queue operations)?Difficulty: MediumSection Ref: 13.4

18. What does the following declaration establish?list<double>::iterator pos;

A) a variable named iterator that is a list that can hold values of type doubleB) a variable named pos that is a list holding values of type doubleC) a variable named pos that marks a position in a list holding values of type doubleD) a variable named pos that is a pointer to values of type double

Ans: C Title: What does the given declaration (iterator) mean?Difficulty: MediumSection Ref: 13.1

19. What does the following code snippet do?list<double>mylist;list<double>::iterator pos;// . . .for (pos = mylist.begin(); pos != mylist.end(); pos++){ cout << *pos << endl;}

A) Writes out each element of the list mylist in order from beginning to endB) Writes out each element of the list mylist in order from the end to the beginningC) Writes out the first element of the list mylistD) Writes out the last element of the list mylist

Ans: ATitle: What does the code snippet do (lists, iterators)?Difficulty: EasySection Ref: 13.1


Recommended