c++ final

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Which of the following options denotes the newline character?

'\n'

Given the following declaration of the variables p1 and p2, which code fragment prints "Hello" if the value of x in the variable p1 is larger than the value of x in the variable p2? struct Point2D { double x; double y; }; Point2D p1; Point2D p2;

-- if (p1.x > p2.x) { cout << "Hello"; }

Study the functions below: int myfun1(int n) { if (n < 1) { return 0; } return myfun2(n * 2 - 1); } int myfun2(int n) { if (n == 1) { return 1;} return n + myfun2(n - 2); } What is the output for the statement cout << myfun1(-7)?

0

What is the output of the following code snippet? double* temperature = NULL; cout << temperature << endl;

0

What is the output of the following code snippet? // class CashRegister { public: void set_item_count(int count); void view() const; private: int item_count; }; void CashRegister::view() const { cout << item_count << endl; } void CashRegister::set_item_count(int count) { item_count = count; } int main() { CashRegister reg1, reg2; reg1.set_item_count(15); reg2.set_item_count(10); reg1.view(); reg2.view(); return 0; } //

15 10

Consider the following recursive function: 1. int triangle_area(int side_length) 2. { 3. if (side_length <= 0) { return 0; } 4. if (side_length == 1) { return 1; } 5. int smaller_side_length = side_length - 2; 6. int smaller_area = triangle_area(smaller_side_length); 7. return smaller_area + side_length; 8. } What is returned when this function is called with a value for side_length of 7?

16

In the following code snippet, what is the return value when the parameter values are m = 10 and n = 4? int foo(int m, int n) { if ((m == n) { return n; } else if (m < n) { return foo(m, n - m); } else { return foo(m - n, n); } }

2

What is the output of the following code snippet? #include <vector> #include <string> #include <iostream> using namespace std; vector<string> substrings(string str) { vector<string> vec; if (str == "" || str.length() <= 1) { vec.push_back(str); return vec; } string first_char = str.substr(0, 1); string rest = str.substr(1, str.length() - 1); vector<string> subs = substrings(rest); int size = subs.size(); for (int i = 0; i < size; i++) { string s = first_char.substr(0, 1); s.append(subs[i]); subs.push_back(s); } return subs; } int main() { vector<string> vec = substrings("432"); for (int i = 0; i < vec.size(); i++) { cout << vec[i] << endl; } return 0; } //.

2 32 42 432

What is the output of the following code snippet? #include <string> #include <iostream> using namespace std; string reverse(string str, int start, int end) { if (start >= end) { return str; } char ch = str[start]; str[start] = str[end]; str[end] = ch; return reverse(str, start + 1, end - 1); } string string_reverse(string str) { int index = str.length() / 2; string str1 = reverse(str, 0, index - 1); string str2 = reverse(str, index, str.length() - 1); string str3 = str1.substr(0, index); string str4 = str2.substr(index, str.length() - index); return str3.append(str4); } int main() { cout << string_reverse("1234") << endl; return 0; }

2143

What is the value of the return statement for the following code snippet, where a = {3, 4, 5, 6, 7, 8} and n = 5? int foo(int a[], int n) { if (n == 0) { return a[n]; } return { return foo(a, n - 1) + a[n]; } }

33

What is the value of the return variable m in the following code snippet, if a = {1, 2, 3, 4, 5, 6} and n = 5? int myfunction(int a[], int n) { if (n == 1) { return a[0]; } int m = myfunction(a, n - 1); if (a[n] > m) { return a[n - 1]; } else { return m; } }

5

What is the output of the following code snippet? int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int* ptr = arr; ptr = ptr + 5; cout << *ptr << endl;

6

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; }

6

Suppose myfunction is defined as follows. How many calls to myfunction result, including the first call, when myfunction(9) is executed? int myfunction(int n) { if (n <= 2) { return 1; } return n * myfunction(n - 1); }

8

What is the output of the following code snippet? #include <iostream> using namespace std; int find(int a, int b) { int val; if (a > b) { val = a; } else { val = b; } return val; } int find(int a[], int size) { int val; if (size == 1) { val = a[0]; } else { val = find(a[size - 1], find(a, size - 1)); } return val; } int main() { int a[7] = { 6, 5, 4, 3, 9, 2, 1 }; cout << find(a, 7); return 0; }

9

Which symbol in C++ indicates inheritance?

:

Class Manager inherits from class Employee. Which of the following statements are true?

A Manager constructor can pass data to an Employee constructor

Suppose the class Manager is derived from the class Employee. Consider these statements: Employee* pe = new Employee; Manager* pm = new Manager; pe = pm; What happens at the "pe = pm" assignment?

A derived-class pointer is assigned to a base-class pointer

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

A recursive function calls itself.

Consider the code snippet below int important_calculation(int size) { int the_answer = 42; int* ptr = new int[size]; ptr[0] = the_answer; return the_answer; }

A. There is a new without a matching delete. B. There is an uninitialized pointer that is used. CorrectC. Both b and c. D. There is memory that is never reclaimed.

In the given code snippet, what type of member function is view()? // class CashRegister { public: void view() const; private: int item_count; double total_price; }; void CashRegister::view() const { cout << item_count << endl; cout << total_price << endl; } //

Accessor member function

Study the following class interface for the class AeroPlane: // class AeroPlane { public: void set_new_height(double new_height); void view() const; void view_new_height() const; AeroPlane(); AeroPlane(double new_height); AeroPlane(double new_height, double new_speed); AeroPlane(int new_height, int new_speed); private: double height; double speed; }; // Which of the following constructors is called for the object declaration AeroPlane c1(10, 100)?

AeroPlane(int new_height, int new_speed)

In the following code snippet, which constructor is called for the object declaration Building bldg(500)in the code below? // class Building { public: Building(); Building(int new_height); void set_height(int new_height); void view() const; private: int height; }; Building::Building() { set_height(0); } Building::Building(int new_height) { set_height(new_height); } int main() { Building bldg(500); return 0; } //

Building(int new_height)

How can a derived class override a base class function?

By providing a new implementation for a function with the same name and parameter types

The AeroCar class inherits from the Car class and overrides the set_speed(double new_speed) function. In the AeroCar set_speed function, how can the Car set_speed function be called?

Car::set_speed(new_speed)

Which of the statements following the code snippet is true? // class CashRegister { public: CashRegister(); CashRegister(int count); void set_item_count(int count); void view() const; private: int item_count; }; CashRegister::CashRegister() { set_item_count(0); } CashRegister::CashRegister(int count) { set_item_count(count); }

CashRegister() is the default constructor because it has no parameters.

In the following code snippet, which constructor is called for the object declaration CashRegister reg(5)? // class CashRegister { public: CashRegister(); CashRegister(int count); void set_item_count(int count); void view() const; private: int item_count; }; CashRegister::CashRegister() { set_item_count(0); } CashRegister::CashRegister(int count) { set_item_count(count); } int main() { CashRegister reg(5); return 0; } //

CashRegister(int count)

Consider the following code snippet: class Question { public: Question() { text = ""; answer = ""; } virtual void set_text(string s) { cout << "Question : set_text function" << endl; } private: string text; string answer; }; class ChoiceQuestion : public Question { public: ChoiceQuestion() {} void set_text(string s) { cout << "ChoiceQuestion : set_text function" << endl; } }; int main() { Question* q1 = new Question; ChoiceQuestion* cq1 = new ChoiceQuestion; q1 = cq1; q1->set_text("Which function?"); return 0; } Which of the following is true about the statement "q1->set_text("Which function?");"?

ChoiceQuestion::set_text(string new_text) is called by q1

Which of the following statements is correct about the public interface for the Car class? . class Car { public: void start(); private: double speed; void stop(); };

CorrectA. All of the listed items. B. The code snippet includes the public interface for the class but lacks the definition of the member functions. C. This interface does not contain the stop() function. D. This interface contains the start() function.

The technique for hand-tracing objects puts public member functions on the front of an index card, and the private data members on the back. Why?

Data members are hidden from outside client code and this simulates the principle of encapsulation

Using pictures can help determine the structure of data and connections of data within a program. How are pointers usually represented in a diagram?

Data storage locations are represented as boxes; pointers are drawn as arrows with the tip pointing to the boxes

The reserved word virtual is used with a base-class member function to alert the C++ compiler that it must determine the actual member function to call at run-time. Thus derived classes can have their own (possibly completely different) versions of the base-class function. When a base-class defines a member function to be virtual, what should each derived class do?

Each derived class should over-ride the virtual base-class function so that it defines a function with the same name that is unique to the derived class

Which of the following hides the implementation details of the data members and member functions within a class?

Encapsulation

Which of the following statements is true about data members of a class definition?

Every object of the defined class has its own set of data members, with possibly different values.

What is the output of the following code snippet? class Employee { public: Employee(string name) { this->name = name; } void Employee_info() { cout << "My name is " << name << "."; } private: string name; }; class Manager : public Employee { public: Manager(string name) : Employee(name) { } void Employee_info() { cout << "I am a Manager."; Employee::Employee_info(); } }; int main() { Manager manager = Manager("Susan Allen"); manager.Employee_info(); return 0; }

I am a Manager. My name is Susan Allen.

Which of the following statements is true with regard to type tags? I. If a new class is added to the hierarchy, the type-tag code will likely need to be revised II. Virtual functions provide a cleaner, more extendible mechanism III. Type tags are never used by programmers

I, II

Consider the two functions that are defined here. Which of the options are true about these functions? I. The function f1 is a recursive solution, and the function f2 is an iterative solution II. The two functions execute at about the same speed III. For a string of length n, the function f1 makes about n/2 recursive calls but the function f2 performs about n/2 iterations string f1(string str) { string val = ""; if (str == "" || str.length() <= 1) { return str; } string first = str.substr(0, 1); string middle = str.substr(1, str.length() - 2); string last = str.substr(str.length() - 1, 1); return return last + f1(middle) + first; } string f2(string str) { int len = str.length(); int mid = len / 2; for (int i = 0; i < mid; i++) { char ch = str[i]; int pos = len - i - 1; str[i] = str[pos]; str[pos] = ch; } return str; }

I, II, and III

Recursive functions may involve degenerate inputs. Which of the following can be considered as a degenerate input? I. An empty string II. A shape without any area III. A negative value for time

I, II, and III

What is the relationship between a base class and a derived class called?

Inheritance

What does the new operator do in the following statement? double* some_num = new double[20];

It allocates an array of size 20, and yields a pointer to the starting element.

Which of the following is a benefit of encapsulation?

It guarantees that an object cannot be accidentally put into an inconsistent state.

What is polymorphism?

It is the ability to implement a function or operator in different ways under different contexts

What does class aggregation mean?

It means that an object of one class acts as the data member of an object of another class.

Which of the following statements is correct about an accessor member function?

It returns the value of a data member of an object but does not modify any data member values.

Study the following code snippet: class Employee { public: Employee(); Employee(string new_name); Employee(double new_salary); Employee(string new_name, double new_salary); private: string name; double salary; }; class Manager : public Employee { public: Manager(); Manager(string new_department, string name, double salary); private: string department; }; Which among the following is the legal way of implementing the constructor of the Manager class that passes parameters to a base-class constructor?

Manager::Manager(string new_department, string new_name, double new_salary) : Employee(new_name, new_salary) { department = new_department; }

What is wrong with the following code snippet? void myfun(char* p) { *p = toupper(*p); } int main() { string myword = "YouHadMeAtHello"; for (int i = 0; i < 10; i++) { myfun(myword[i]); } cout << myword << endl; return 0; }

Must use c_str() to access individual characters of the string variable myword

What is the output of the following code snippet? class Building { public: Building() { name = ""; height = 0; } Building(string n, double h) { name = n; height = h; } void set_name(string n) { name = n; } void set_height(double h) { height = h; } string get_name() const { return name; } double get_height() const { return height; } private: string name; double height; }; class SkyScraper : public Building { public: SkyScraper() { width = 0; } SkyScraper(double w) { width = w; } SkyScraper(string n, double h, double w) : Building(n, h) { width = w; } void set_width(double w) { width = w; } void print_data() const; private: double width; }; void SkyScraper::print_data() const { cout << "Name: " << get_name() << "; Height: " << get_height() << "; Width: " << width << endl; } int main() { SkyScraper bldg1; SkyScraper bldg2(100); SkyScraper bldg3("World Trade Tower", 3000, 100); bldg2.print_data(); return 0; }

Name: ; Height: 0; Width: 100

What is looked for in the problem description when determining the name of a class?

Nouns

Suppose that we have a Question class that contains two data members - a query and an answer string. The NumericQuestion class inherits from Question. Which of the following is true?

NumericQuestions contains both a query and an answer string.

What is the output of the following code snippet? #include <iostream> using namespace std; class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Car::Car(double speed) { this->speed = speed; } void Car::start() { speed = 1; } void Car::accelerate(double speed) { this->speed = this->speed + speed; } void Car::stop() { speed = 0; } double Car::get_speed() const { return speed; } int main() { Car* c1 = new Car(90); c1->start(); c1->accelerate(90); cout << "Speed: " << c1->get_speed() << endl; c1->stop(); return 0; } //

Speed: 91

Study the functions below: int myfun1(int n) { if (n < 1) { return 0; } return myfun2(n * 2 - 1); } int myfun2(int n) { if (n == 1) { return 1;} return n + myfun2(n - 2); } What does the function myfun1 compute for positive n?

Sum of the first n odd integers

What does the function f do? struct Point2D { double x; double y; }; struct Triangle { Point2D v1; Point2D v2; Point2D v3; }; void f(Triangle& t) { int temp = 12.5; temp = t.v1.x; t.v1.x = t.v1.y; t.v1.y = temp; } int main() { Triangle mytri; mytri.v1.x = 1.0; mytri.v1.y = 22.5; f(mytri); }

Swaps values of x and y in vertex 1 of an argument of type Triangle

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?

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

The Manager class inherits from the Employee base class. The Manager class overrides the get_salary()function. What is wrong with the following definition of get_salary() in the Manager class? double Manager::get_salary() const { double base_salary = get_salary(); return base_salary + bonus; }

The call to get_salary should be written as Employee::get_salary();

What is the output of the following code snippet? // #include <iostream> using namespace std; class Cheetah { public: void set_speed(double new_speed); private: double speed; }; void Cheetah::set_speed(double new_speed) { speed = new_speed; } int main() { Cheetah ch1; ch1.set_speed(144); cout << "Cheetah speed: " << ch1.speed; return 0; } //

The code snippet does not compile.

What does this function do? bool foo(string s) { if (s.length() <= 1) { return true; } char first = s[0]; char last = s[s.length() - 1]; if (first != last) { string shorter = s.substr(1, s.length() - 1); return foo(shorter); } else { return false; } }

The code snippet returns false if the string is a palindrome.

What does this function do? bool myfunction(string s) { if (s.length() <= 1) { return true; } char first = s[0]; char last = s[s.length() - 1]; if (first == last) { string shorter = s.substr(1, s.length() - 1); return myfunction(shorter); } else { return false; } }

The code snippet returns true only for strings of any length consisting of the same character.

How should base-class data members be referenced and used from derived classes?

The derived class should use base-class accessors and mutators as a way to access or update base-class data members

Consider the constructor for a derived class. Because a derived-class constructor can only initialize the data members of the derived class, how can it initialize base-class data members via a constructor other than the default constructor?

The derived-class constructor can supply arguments to the base-class constructor via the base-class initializer

Describe how the two arrays index and data are related to one another after the code snippet below executes: int* index[5]; int data[10] = {4, 8, 1, 3, 5, 9, 3, 2, 6, 0}; int i = 0; int* p = &data[9]; for (int i = 0; i < 5; i++) { index[i] = p; p--; }

The elements of index point to the elements of data as follows: index[0] points to data[9] index[1] points to data[8] etc. for all five elements of index

What does it mean for a function to have a polymorphic parameter?

The function accepts an object as a parameter - the object may be a base-class object or a derived-class object

What is displayed when you execute the following code snippet? int ch = 100; cout << &ch << endl;

The memory location of ch

Consider the following code snippet: int* num = new int; *num = 10; cout << num << endl; delete num; *num = *num * 2; cout << num << endl; Which of the following statements is correct?

The num pointer is being used after the memory it is pointing to has been deleted.

Consider the code snippet below, which uses two pre-defined objects Time and ExtTime: int main() { Time* test_time[SIZE]; int rand_hrs, rand_mins, rand_secs; ZoneType rand_zone; for (int i = 0; i < SIZE; i++) { rand_hrs = rand() % 3600; rand_mins = rand() % 60; rand_secs = rand() % 60; rand_zone = (ZoneType)(rand() % 8); if (i % 2) { test_time[i] = new ExtTime(rand_hrs, rand_mins, rand_secs, rand_zone); } else { test_time[i] = new Time(rand_hrs, rand_mins, rand_secs); } } for (int i = 0; i < SIZE; i++) { test_time[i]->write(); cout << endl; } } In order for pointers to objects of two different classes, Time and ExtTime, to be put into the same array (test_time), what must be true?

The objects must be related to one another via inheritance

Consider the code snippet below: int main() { Shape* shapes[NUM_OBJECTS]; shapes[0] = new Circle(0, 0, 100, 150); shapes[1] = new Rectangle(200, 200, 50, 100); shapes[2] = new Circle(300, 50, 250, 250); shapes[3] = new Rectangle(100, 350, 200, 150); for (int i = 0; i < NUM_OBJECTS; i++) { shapes[i]->draw(draw_area); } for (int i = 0; i < NUM_OBJECTS; i++) { delete shapes[i]; } } In order for pointers to the two different objects Circle and Rectangle to be put into the same array (shapes), what must be true?

The objects must be related to one another via inheritance

A word of any length is a palindrome if its characters are the same forward and in reverse. Which of the following simplifications is helpful in designing a recursive solution to the problem of checking to see if a word is a palindrome?

The word obtained by removing the first and last letters is a palindrome, and the first and last letters are the same.

Consider the member function call shpcrt.add_product(5, 59.75); Which of the following describes the role of 5 and 59.75 in this call?

They are explicit parameters.

What is the reason for including the following code snippet in the header file Car.h? #ifndef CAR_H #define CAR_H class Car { public: Car(); Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed1; }; #endif

To define the interface for the Car class

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)?

Vehicle, Car and Truck objects

Consider the following code snippet: class Home { public: Home() { address = ""; landmark = ""; } virtual void set_address(string address) { cout << "Home : set_address function" << endl; } private: string address; string landmark; }; class Villa : public Home { public: Villa() {} void set_address(string address) { cout << "Villa : set_address function" << endl; } }; int main() { Home* q1 = new Home; Villa* cq1 = new Villa; q1 = cq1; q1->set_address("Which function?"); return 0; } Which of the following is true about the statement "q1->set_address("Which function?");"?

Villa::set_address(string addr) is called by q1

What is the output of the following code snippet? void myfun(char* p) { *p = toupper(*p); } int main() { char myword[20] = "YouHadMeAtHello"; for (int i = 0; i < 10; i++) { myfun(&myword[i]); } cout << myword << endl; return 0; }

YOUHADMEATHello

Which location of the array arr does ptr point to right after you assign an array to a pointer variable, as shown in the following code snippet? int arr[10]; int* ptr = arr;

arr[0]

What is the output of the code snippet given below? int arr[5] = {1, 2, 3, 4, 5}; int* ptr = arr; cout << "arr[0] contains a value of " << *ptr << endl;

arr[0] contains a value of 1

A word-unit palindrome is a sentence that produces the same words forward or backward. For example, "Fall leaves after leaves fall" is a word-unit palindrome. Suppose you wish to implement a recursive function that can check sentences to see if they are word-unit palindromes. What is the first step?

break the input into smaller parts that can themselves be inputs to the problem

Consider the following class interfaces: class Car { public: Car(); Car(double new_speed); double get_speed() const; private: double speed; }; class AeroCar : public Car { public: AeroCar(); AeroCar(double new_height, double new_speed); void display_data() const; private: double height; }; int main() { Car c1; Car c2(10); AeroCar ac1; Aerocar ac2(10, 20); c1 = ac1; c1 = c2; ac1 = ac2; ac1 = c1; return 0; }

c1 = ac1;

Based on the following code snippet, which of the following function calls are legal? Assume that car is a Car object and aero is an AeroCar object. class Car { public: Car(); void set_speed(double new_speed); double get_speed() const; void display() const; private: double speed; }; class AeroCar : public Car { public: AeroCar(); void set_speed(double new_speed); void set_height(double new_height); double get_height() const; private: double height; };

car.get_speed() and aero.get_speed()

Consider the code snippet below. int ch = 100; Which of the following is a legally correct way of obtaining the memory location of ch and printing it (the memory location) to standard output, based on the given code snippet?

cout << &ch << endl;

Some programmers are careful about using this within member functions to make clear the distinction between

data members and other variables

What is the legal statement to reclaim the memory allocated below? int* num = new int; *num = 10;

delete num;

Two quantities a and b are said to be in the golden ratio if (a+b)/a is equal to a/b. 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 + 1/(1 + 1/.... Which function below is a correct recursive implementation of this continued fraction?

double golden(int number) { if (number <= 1) { return 1.0;} return 1.0 + 1.0 / golden(number - 1); }

In the following class definition, which of the data members or member functions are hidden from the class users? . class Car { public: Car(); void start(); void stop(); private: double speed; void set_speed(double new_speed); };

double speed and void set_speed(double new_speed)

What is output of the code snippet given below? string name = "Oscar DeGama"; cout << name[7] << name[8] << name[9] << endl;

eGa

One way to determine the run-time efficiency of a recursive function is to

estimate the shape and size of its call tree

When tracing the execution of a recursive function, what can remove confusion about the current point of execution when debugging the program?

examination of the call stack

You are given the class definition for CashRegister, and you are writing client code to set up an array of pointers to CashRegister objects. Given the declaration of the array below, which code snippet correctly allocates CashRegister objects from the heap for the entire array? // CashRegister* all_registers[20]; //

for (int i = 0; i < 20; i++) { all_registers[i] = new CashRegister; }

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]; //

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

When designing classes to solve a specific problem, one simple approach to discovering classes and member functions is to

identify nouns and verbs, which correspond to classes and functions, respectively

Which of the following is a legal statement to dynamically allocate memory for an array whose size is not known until run time? int size; // Assume that size is set at run time

int* intarray = new int[size];

One remarkably simple formula for calculating the value of pi is the so-called Madhava-Leibniz series: pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - .... Consider the recursive function below to calculate this formula: double computePI(int number) { if (number <= 1) { return 1.0;} int oddnum = 2 * number - 1; return computesign(number) * 1.0 / oddnum + computePI(number - 1); } In this recursive function, what is the role of the helper function computesign?

it makes sure the sign (positive or negative) alternates as each term of the series is computed

Suppose you are to write a recursive function to calculate the "area" of a triangle shown as follows, where the area of each [] square is 1: [][][][][][][][][] [][][][][][][] [][][][][] [][][] [] Assume the base of the triangle (top edge) always has an odd number of [] squares, and each subsequent line has two fewer squares (and hence the triangle always ends with a point that has just one [] square). Consider the recursive function below: 1. int triangle_area(int side_length) 2. { 3. if (side_length <= 0) { return 0; } 4. if (side_length == 1) { return 1; } 5. int smaller_side_length = side_length - 1; 6. int smaller_area = triangle_area(smaller_side_length); 7. return smaller_area + side_length; 8. } Will this function correctly compute the area of triangles as shown above for odd side length?

no, because line 5 should be int smaller_side_length = side_length - 2;

Which location of the array num does ptr point to right after you assign an array to a pointer variable, as shown in the following code snippet? int num[5]; int* ptr = num;

num[0]

Discovering aggregation relationships between classes can help the process of implementation because

one object may need to contain objects of another class as data elements in order to simplify the problem solution

What is true about the statement given below? int* ptr_num;

ptr_num contains the memory location of an integer variable.

Which statement, if executed immediately after the given one, creates orphaned heap memory? CashRegister* register_pointer = new CashRegister;

register_pointer = new CashRegister;

In 1735 Leonard Euler proved a remarkable result, which was the solution to the Basel Problem, first posed in 1644 by Pietro Mengoli. This result gave a simple expression for pi. The formula states that pi^2/6 is equal to the limit, as n goes to infinity, of the series 1/1 + 1/2^2 +... + 1 / n^2. Which statement below is the recursive case for a recursive implementation that approximates this infinite series?

return 1.0 / (number * number) + computePI(number - 1);

Two quantities a and b are said to be in the golden ratio if (a+b)/a is equal to a/b. 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 square root (also called an infinite surd): golden ratio = sqrt(1+sqrt(1+sqrt(1+sqrt(1+sqrt(1... If the function double golden (int) is a recursive implementation of this function, what should be the recursive call in that function?

return sqrt (1.0 + golden(number - 1));

Consider a situation where the function reverse_string should reverse a string that is passed as an argument. Which of the following options correctly completes the reverse_substring function? string reverse_substring(string str, int start, int end) { if (start >= end) { return str; } char ch = str[start]; str[start] = str[end]; str[end] = ch; return // complete this statement } string reverse_string(string str) { return reverse_substring(str, 0, str.length() - 1); }

reverse_substring(str, start + 1, end - 1);

Suppose that you declare an array int num[10]. Assuming the function declaration statement given below, what would you use to pass the array to the given function? int sum_array(int arr[]) { }

sum_array(num)

Consider the following class interfaces: class Teacher { public: Teacher(); Teacher(string new_name); string get_name() const; private: string name; }; class MathsTeacher : public Teacher { public: MathsTeacher(); MathsTeacher(string new_qualification, string new_name); void display_data() const; private: string qualification; }; int main() { Teacher t1, t2("John"); MathsTeacher mt1, mt2("TopLevel", "Sarah"); t1 = mt1; t1 = t2; mt1 = mc2; mt1 = t1; return 0; } Which one of the preceding assignment statements in the function main() would slice away the data of the derived class object?

t1 = mt1

Which of the following code snippets is legal for implementing the CashRegister class member function named add_item?

void CashRegister::add_item(double price) { item_count = item_count + 1; total_price = total_price + price; }


Ensembles d'études connexes

Unclassified Antidysrhythmic Drugs

View Set

Chem 2 (Quiz 2): Chapter 15 (Acids and Bases)

View Set

Prepu | Chapter 69: Next Generation | NGN

View Set

REVIEW QUESTIONS KINESIOLOGY (INTRO, SHOULDER GIRDLE, & SHOULDER JOINT)

View Set

6.3: "Indigenous Response to Industrialization"

View Set

Chapter 8: Political Parties, Candidates, and Campaigns

View Set

Numbers 22 - Flashcard MC questions - Ted Hildebrandt

View Set

HESI Exam Fundamental of Nursing

View Set

WEEK 9 >>>Chapter 43 Liver, Biliary Tract, and Pancreas Problems Pancreatic Cancer

View Set