CS 150 Post-Midterm Review SP'16

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

int ch; ch = 100; int* ptr = &ch; Which of the following statements represents a legal way of obtaining the value that ptr points to?

*ptr

int stringCmp (const char *s1, const char *s2) { while (...) {s1++; s2++;} return *s1 - *s2; } Which while condition makes stringCmp correct?

*s1 == *s2 && *s1 != '\0' && *s2 != '\0'

int SIZE = 4; int a[SIZE] = {1}; cout << a[3] << endl; What is printed (assume all #includes, etc.)

0

int numdata[3] = {1, 2}; What is the value stored in the last element of the array named numdata?

0

int sum(const int a[], size_t size){ int sum = 0; for (int i = -1; i < size -1; i++) sum += a[i + 1]; return sum; } int main() { int b[] = {1, 2, 3}; cout << sum(b, 3) << endl; } What is printed? (#includes OK)

0

What is the range of valid index (or subscript) values for an array of size 7?

0 to 6

int a[] = {6, 1, 9, 5, 12, 3}; int x = 0; for (size_t i = 0; i < 6; i++) if (a[i] < a[x]) x = i; cout << x << endl; What does this loop print?

1

void f(vector<int> v) { v[0] = 42; } vector<int> x = {1, 2, 3}; f(x); cout << x[0] << endl; What is printed? (all #include)

1

Which is true? 1. Recursive solutions may do the same work as iterative solutions 2. Recursive solutions are often easier to implement than their iterative counterparts 3. The performance of recursive functions can always be substantially improved by converting them to iterative solutions

1, 2

What is true about the command line in C++? 1. The first argument is the name of the program. 2. Command line arguments are passed in an array. 3. main is: int main(int argc, char* argv[]).

1, 2 and 3

1. "\n"; 2. '\n' 3. "n" 4. "/n" 5. 'n'

1, 3

1. char s[10] = "Hello"; 2. char s[10] = {'H', 'e', 'l', 'l', 'o'}; 3. char s[10] = {'H', 'e', 'l', 'l', 'o', '\0'}; 4. char s[5] = "Hello"; 5. char s[] = "Hello"; Which contains the c-string "Hello"?

1, 3, 5

int num = 0; int* ptr = &num; num = 5; *ptr = *ptr + 5; cout << num << " " << *ptr << endl; What is the output?

10 10

int tArea(int sideLen){ if(sidelen < 1) return 0; if(sidelen == 1) return 1; int smalerSide = sideLen - 2; int smallerArea = tArea(smallerSide); return smallerArea + sideLen; }

16

int a[] = {6, 1, 9, 5, 12, 3}; int x = 0; for (size_t i = 0; i < 6; i++) if (a[i] % 2 == 1) x += a[i]; cout << x << endl; What does this loop print?

18

const int SIZE = 5; int a[SIZE]; for (size_t i = 0; i < SIZE; i++) { a[i] = 2 * (i - 1); } What value is stored at position (subscript) 2?

2

int* f(int a[], size_t size) { int r = a[0]; return &r; } -- in another function int x[] = {1, 2, 3}; int *p = f(x, 3); *p = 3; What is stored in the array x after this code runs?

3, 2, 3

int numarray[2][3] = { { 3, 2, 3 }}; cout << numarray[0][0]; cout << numarray[1][0]; What is the output of this code snippet?

30

void f(vector<int>& v) { v.at(0) = 42; } vector<int> x = {1, 2, 3}; f(x); cout << x[0] << endl; What is printed? (all #include)

42

class Car { public: double getSpeed() const { return speed; } Car() { speed = 0; } Car(double dspeed) { speed = dspeed; } private: double speed; }; Car c1, c2(5); double sum = = c1.getSpeed() + c2.getSpeed();

5

int cnt = 0; int a[2][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { a[j][i] = cnt; cnt++; } } What is the value of a[1][2] after this code runs?

5

int sum(const int a[], size_t size) int sum = 0; for (size_t i = 1; i < size; i++) sum += a[i]; return sum; } int main() { int b[] = {1, 2, 3}; cout << sum(b, 3) << endl; } What is printed? (all #includes)

5

int func(int n) { if(n < 2) return 1; return n * func(n-1); } int main() {cout << func(3) << endl;}

6

int sum(const int a[], size_t size) { int sum = 0; for (size_t i = 0; i < size; i++) sum += a[i]; return sum; } int main() { int b[] = {1, 2, 3}; unsigned len = sizeof(b) / sizeof(b[0]); cout << sum(b, len) << endl; } What is printed? (all #includes)

6

int sum(const int a[], size_t size){ int sum = 0; for (size_t i = 0; i < size; i++) sum += a[i]; return sum; } int main() { int b[] = {1, 2, 3}; cout << sum(b, 3) << endl; } What is printed? (all #includes)

6

char s[] = "hi\0hey"; What is the result of this code?

7 chars, 'h', 'i', '\0', 'h', 'e', 'y', '\0' stored in s. strlen(s)2

int num = 0; int* ptr = &num; *ptr = 80; num = 90; cout << *ptr << endl; What is the output?

90

Which one of the following is an example of the "substitution principle"?

A derived class object can be used in place of a base-class object

Actor* ac = new Actor; Hero* hr = new Hero; ac = hr; Suppose Hero is derived from Actor. What happens here?

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

What is true about using recursion?

A recursive computation solves a problem by calling itself with simpler input.

double dolphinSpeed[5]; double x = 0.0;

All of these are incorrect.

for (int i = 0; i < argc; i++) { cout << "Arg #" << (i + 1) << "->" << argv[i] << "|"; } ABC.exe is run as ABC A B C. What is printed?

Arg #1: ABC|Arg #2: A|Arg #3: B|Arg #4: C

obj->myfun(); Why is it not possible from this statement alone to determine which member function myfun() will be executed?

Because the object pointed to by obj might be a derived object

What does a derived class inherit from its base class?

Both data and behavior

How can you ensure that a recursive function terminates?

Call the recursive function with simpler inputs and provide a special case for the simplest inputs.

AeroCar, derived from Car, overrides setSpeed(double). In AeroCar's setSpeed function, how can the original setSpeed function be called?

Car::setSpeed(20)

class Alien { int age; public: explicit Alien(int); }; Alien a(2575); Alien b(a); b = 75 * 3; What value does the variable b contain?

Code does not compile because the r-value used in the assignment must be an Alien

void f(const vector<int>& v) { v[0] = 42; } vector<int> x = {1, 2, 3}; f(x); cout << x[0] << endl; What is printed? (all #include)

Compile time error

class Alien { int age; public: Alien(int a); }; Alien a(2575); Alien b(a); b = 75 * 3; What value does the variable b contain?

Compiles and runs. Alien b is 225 eons old

int a[] = {6, 1, 9, 5, 12, 3}; int x = 0; for (size_t i = 0; i < 6; i++) if (a[i] % 2 == 0) x++; cout << x << endl; What does the loop do?

Counts the even elements in a

int a[] = {1, 2, 3}; int b[3]; b = a; This code...

Does not compile because b is not a variable

class Time { long long seconds; public: Time(long sec); Time(Time other); }; Time t1(3600); Time t2(t1); What value does the variable t2 contain?

Does not compile. Illegal constructor that takes an object of the same class

virtual is used with a base-class to tell the compiler to determine the actual function called at run-time. Derived classes can have their own versions of the base-class function. When a base-class defines a virtual function, what should each derived class do?

Each derived class may over-ride the virtual function to provide unique, derived-class behavior

Which of the following is a key requirement to ensure that recursion is successful?

Every recursive call must simplify the computation in some way.

int a[] = {6, 1, 9, 5, 12, 3}; int x = a[0]; for (size_t i = 0; i < 6; i++) if (a[i] > x) x = a[i]; cout << x << endl; What does this loop do?

Finds the largest value in a

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

Inheritance

double* num = new double[10]; What does the new operator do in this statement?

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

double* num = new double(10); What does the new operator do in this statement?

It allocates enough memory for a double value and initializes it with 10

int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; cout << a[2][-2] << endl; What does this print?

It compiles, runs and prints 5

int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; cout << a[0][5] << endl; What does this print?

It compiles, runs and prints 6

string* mw[20]; for (int i = 0; i < 14; i++) { mw[i] = new string("Hello"); } cout << *mw[15] << endl; What is the problem with this code?

It dereferences an uninitialized pointer

string* mw; for (int i = 0; i < 14; i++) { mw = new string("Hello"); } cout << *mw << endl; What is the problem with this code?

It has a memory leak

int* pArray[10]; Which of the following best describes the nature of pArray?

It is an array of ten integer pointers on the stack.

Which statement is true about an uninitialized pointer?

It may point to a memory location that a program does not own

What is true about an accessor member function?

It should be declared with the const keyword.

int* num = new int; *num = 10; cout << num << endl; delete num; *num = *num * 2; cout << num << endl; What is true about this code?

It uses a dangling pointer

class Item { public: Item() {} Item(double p) { price = p; } Item(const string& desc) { description = desc; } private: string description; double price; }; What is the problem with this class?

Item(), Item(double) don't initialize description

The virtual reserved word can be used for which of the following?

Member functions

int* num = NULL; cout << *num << endl; What is the output?

No compilation errors, but when run it results in an illegal memory access ("crash")

int* num = nullptr; cout << *num << endl; What is the output?

No compilation errors, but when run it results in an illegal memory access ("crash")

When a derived function over-rides a base-class function, which of these is true? 1. The derived class must call the base-class function first, followed by any new code 2. The derived-class function may not call the baseclass function again 3. The base-class function must have different parameters from the derived function

None of these

FMRadio myRadio(); myRadio.setFrequency(98.6); cout << myRadio.getFrequency(); Given the FMRadio class (not the struct) version created in the chapter, and assuming that 98.6 is a valid frequency, what should print?

Nothing, because we haven't created an object

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

NumericQuestions contains both a query and an answer string.

Which are true? 1. Base-class constructors can be made virtual. 2. Derived class constructors can be made virtual. 3. Base-class member functions can be made virtual. 4. Derived class member functions can be made virtual.

Only 3 is a true statement.

int a[5]; in.read((char *)a, 4); What does this code do?

Reads 1 int from the file and stores it in a

What is the difference between a heap array and a regular (automatic or static) array?

Regular arrays have a fixed size when declared; heap arrays can have a dynamic size at run-time

class Jewel { public: Jewel() { cost = 0; } Jewel(double c, const string& n) { cost = c; name = n; } void modify(double v) { cost = v; } void modify(const string& v) { name = v; } void print() const { cout << name << " " << cost; } private: double cost; string name; }; Jewel jewel(1000.0, "Sapphire"); jewel.modify("Ruby"); jewel.print(); jewel.modify(500); What is printed?

Ruby 1000.0

int a[] = {6, 1, 9, 5, 12, 3}; int x = 0; for (auto e : a) e = 0; cout << x << endl; What does this loop do?

Sets every element in a to 0

int a[] = {6, 1, 9, 5, 12, 3}; int x = 0; for (auto e : a) e = 0; cout << x << endl; What does this loop do?

Sets the local variable e to 0, but no other effect

int a[] = {6, 1, 9, 5, 12, 3}; int x = 0; for (auto e : a) x += e; cout << x << endl; What does this loop do?

Sums the elements in a

int a[] = {1, 2, 3}; int b[] = {4, 5, 6}; a = b;

Syntax error

The Pet base class has a member function void setName(string newName). The Cat class is derived from the Pet base class, but does not define the setName function. Which is correct?

The Cat class inherits the setName function.

int* num = nullptr; cout << num << endl; What is the output?

The address value 0

int* num = NULL; cout << &num << endl; What is the output?

The address value where num is stored

Infinite recursion can occur because?

The base case is missing one of the necessary termination conditions

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 derivedclass object

What is necessary to avoid the slicing problem for functions with polymorphic parameters?

The function should accept a generic parameter

class Time { Time(); long getTime() const; void setTime(long sec); private: long seconds; }; What are part of the implementation?

The member variable seconds

class Time { public: Time(); long getTime() const; void setTime(long sec); private: long seconds; }; Which are part of the implementation?

The member variable seconds

int ch = 100; cout << &ch << endl; What is displayed when you run this code?

The memory location of ch

Time t1(); cout << t1 << endl; What is printed? (all #includes)

The value 1

Why is the command line argc always at least 1?

There is always at least one argument, the name of the program running

double* deleted; *deleted = 10; cout << *deleted; What is wrong with this code?

There is an uninitialized pointer being used

Elephant, Lion, and Tiger class all are derived from Animal. AfricanElephant and IndianElephant are derived from Elephant. Which of the following types cannot be passed to vaccinate(Elephant& e)?

Tiger, Lion, and Animal objects

int sum(const int a[], unsigned size) { int sum = 0; for (size_t i = 0; i < size; i++) sum += a[i]; return sum; } int main() { int b[] = {1, 2, 3}; unsigned len = sizeof(b); cout << sum(b, len) << endl; } What is printed? (all #includes)

Undefined values (or possible crash)

In order to avoid errors, it is often prudent to use the fully-qualified name of a function that is to be invoked. Within the context of inheritance, what is meant by "fully-qualified name"?

Use the name of the class that defines the function together with :: and the function name.

Which of the following is a common pointer error?

Using a pointer that has not been initialized

The Car class, derived from Vehicle, contains one constructor which does not explicitly call a particular Vehicle constructor. Which is true?

Vehicle default constructor implicitly called

The DMV uses a registration program that has a Vehicle base class. Car and Truck are derived from Vehicle. Which types of objects can be passed to the function register(Vehicle& v)?

Vehicle, Car and Truck objects

A pointer describes which of the following?

Where a certain value in memory is

int a[] = {1, 2, 3}; int b[] = {1, 2, 3}; if (a == b) ... This code...

Will compile, and return false

int num = 0; int* ptr; * ptr = &num; cout << *ptr << endl; What is the output?

Will not compile

int* ptr = &0;; cout << *ptr << endl; What is the output?

Will not compile

void stringCopy (char *p, const char *q) { while ((*p = *q) != '\0') { p++; q++; } }

Yes; the '\0' is copied as the while condition fails

char a[] = {'a', 65.0, 97, 0LL}; cout << a << endl; What is printed (assume all #includes, etc.)

aAa

The application ABC is run like this: ABC Alex Brent Chris Roger 32 33 44 78

argc is 9 and argv[0] is "ABC"

Look at the UML diagram shown here. In C++ terminology, the Button class is a ____________ class.

base

int f() { static int c = '21' ; return c * 2; } This code appears in the file f1.cpp. What is the scope of the variable c?

block scope

Which assigns a character value to the first position in a character array named chars?

chars[0] = 'y';

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

choice contains the memory location of an integer variable

In OOP, a(n) ________________ represents the definition—the blueprint if you like—that is used to construct the components of the program.

class

When a class extends a base class, it must specify that relationship in the class definition. Which of these defines Manager as a class that extends Employee?

class Manager : public Employee {};

string name = "Houdini"; Which of the following is a legal assignment?

const char* cstr = name.c_str();

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?

cout << &ch << endl;

int myarray[15]; Which of these displays the eighth element of myarray?

cout << myarray[7];

What line of code will print the value 4 on the screen? struct S { int a, b; }; vector<S> v; S s = {3, 4}; v.push_back(s);

cout << v[0].b << endl;

char * a = "Hello"; strcpy(a, "Huh?"); What does a point to after running this?

crashes when run

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

delete num;

Look at the UML diagram shown here. In C++ terminology, the ImageButton class is a ____________ class.

derived

In OOP, you use __________________ to enforce the principle of data hiding.

encapsulation

char c = 'c'; int f() { return 21; } This code appears in the file f1.cpp. What is the scope of the variable c?

file scope

class Time { long seconds; public: Time(); long getTime(); void setTime(long sec); }; This class has a semantic error. What is it?

getTime is missing const at end

Computer scientists tend to write addresses and the contents of memory in _________ notation because it is easier to identify the individual bits.

hexadecimal

int myFunc(int n) { //code goes here return myfunc(n - 1) + n * n; }

if(n < 1) return 1;

FMRadio myRadio(98.6, 8); cout << myRadio.getFrequency(); On line 2 of this code, the object myRadio is known as the:

implicit parameter

To create a new class by adding features or data elements to an existing class, you use the OOP technique of ___________________.

inheritance

Which declaration is illegal?

int a[5] = {0, 1, 2, 3, 4, 5};

Which of the following options represents the legal definition of the main() function so it can receive command line arguments?

int main(int argc, char* argv[])

Which line creates an array with 5 elements?

int numcount[5];

int size; cin >> size; Given this code, which of the following is legal?

int* intarray = new int[size];

Which is an incorrect function declaration?

int[] f(int n1, int n2, int n3); (Cannot return an array)

A user-defined type created as a struct (like the FMRadio version 1)

interface is its implementation

static char c = 'c'; int f() { return 21; } This code appears in the file f1.cpp. What is the linkage of the variable c?

internal linkage

char arr[] = "Let Us C++!"; Which of the following is true about arr?

it is an array of size 12, last value is '\0'

When a set of cooperating functions call each other in a recursive fashion, is is called

mutual recursion

int f() { static int c = '21' ; return c * 2; } This code appears in the file f1.cpp. What is the linkage of the variable c?

no linkage

const char * a = "Dog"; const char * b = a; if (strcmp(a, b)) cout << "equal" << endl; else cout << "not equal" << endl; What does this print?

not equal

int a[] = {1, 2, 3}; int b[] = {1, 2, 3}; if (a == b) cout << "equal"; else cout << "not equal"; What is printed (assume all #includes, etc.)?

not equal

In C++, global variables are stored.

on the heap

struct S { int a; long b; }; S x = {42, 793L}; Which line will write the structure variable x to the binary stream out?

out.write((char *)&x, sizeof(x));

The OOP technique used to write programs so that different objects respond differently to the same commands is known as ______________________.

polymorphism

To encapsulate a data member (field) inside your class, you'll always use the modifier:

private

Normally, in a class, the member functions (or methods) are _____________, while the data members (or instance variables) are ___________.

public, private

char s[50]; char *t = "ac"; //make s the string "ac" Which line makes the comment correct?

s[0] = t[0]; s[1] = t[1]; s[2] = t[2];

Reading one item after another from a file is called ____ access.

sequential

class Time { long seconds; public: Time(); long getTime() const; void setTime(long sec) const; }; This class has a semantic error. What is it?

setTime should not have const at end

Infinite recursion can lead to an error known as?

stack overflow or stack fault

The ________ of an object includes all the information about the object; that is, its attributes or characteristics.

state

The three properties of every object are its identity, ___________, and behavior.

state

char c = 'c'; int f() { return 21; } This code appears in the file f1.cpp. What is the duration of the variable c?

static duration

int f() { static int c = '21' ; return c * 2; } This code appears in the file f1.cpp. What is the duration of the variable c?

static duration

char c1 = 'W'; char *str = &c1; if (c1 == 'W') { char c2 = 'X'; str = &c2; } cout << c1 << *str << endl; This code appears to work, but has an very subtle bug. What is it?

str points to a variable that may not exist

The header file to include the standard-library functions for processing array-style strings is

string.h or cstring

You want to open the .gif file fname for reading and writing; which commands should you use?

strm.open(fname, ios::in | ios::out | ios::binary);

Which of these can be used to get the current position for writing to a random access file?

strm.seekp(position)

In order to support polymorphism, the virtual reserved word must be used with _________.

the base-class

The C++ & operator allows you to determine.

the location in memory of an object

The C++ sizeof operator allows you to determine.

the number of bytes used by an object or type

char s[10] = "one "; char t[10] = "two "; strcpy (s, t); strcpy (t, s); strcat (s, t); What is stored in s after this code runs?

two two

const size_t SIZE = 12; int ar[SIZE * 2] afunc(ar, SIZE); Which is the correct function declaration for this call to afunc?

void afunc(int ar[], size_t n);

If you see this code inside main. What is the correct prototype for mystery? vector<int> v; mystery(v);

void mystery(vector<int>);

On most computers, the memory size required to hold a value of type int is generally called a:

word

When you create a class and do not supply any constructors.

you can create objects, but any primitive fields will not be initialized

An FMRadio frequency must be restricted to a certain range. You enforce these invariants through the implementation of:

your class mutators


Ensembles d'études connexes

Chapter 11 - Project Risk Management

View Set