CMPT 135 Concept Review

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Suppose a and b are both valid non-null pointers of type double*.

i) If a == b, then it must be the case that *a == *b ii) If a != b, then it must be the case that *a != *b

  • i) and ii) are both false
  • i) and ii) are both true
  • i) is true and ii) is false (correct)
  • i) is false and ii) is true

Consider this code fragment:

int arr[5] = {8, 2, 6, 4, 15}; cout << ???; Select all the following statements that, when put in place of ??? in the code fragment, will print the third element of arr.

  • &(arr + 2)
  • (&arr + 2)
  • *(arr + 2) (correct)
  • *(&arr + 2)
  • *arr[2]

Suppose n is a variable of type int, and consider this program:

#include

int main() { int n = 5; std::cout << &n; } If you run this program multiple times on the same computer, it will always output the same thing.

False (B)

It's never safe to de-reference the null pointer.

<p>True (A)</p> Signup and view all the answers

In the context of garbage collection, garbage is free store memory that has:

<p>not been de-allocated, but can no longer be accessed (D)</p> Signup and view all the answers

Consider these statements:

i) Every C++ if-else-if structure can be re-written as a logically equivalent switch statement. ii) Every C++ switch statement can be re-written as a logically equivalent if-else-if structure.

<p>i) is false and ii) is true (A)</p> Signup and view all the answers

Consider these statements:

i) In this course, when we use a makefile it is to compile a C++ program. ii) In this course, when we use a makefile it is to run a C++ program.

<p>i) is true and ii) is false (D)</p> Signup and view all the answers

Consider these two statements:

i) Blackbox testing is a kind of whitebox testing. ii) Whitebox testing is a kind of blackbox testing.

Which one of these statements is most accurate?

<p>i) and ii) are both false (C)</p> Signup and view all the answers

Consider these two statements:

i) new int(5) returns a value of type int* ii) new int[5] returns a value of type int*

Which one of these statements is most accurate?

<p>i) and ii) are both true (C)</p> Signup and view all the answers

Where does C++ store global variables?

<p>just static memory (D)</p> Signup and view all the answers

Suppose p is a pointer to a value that was allocated on the free store using new.

What kind of error results if p is de-allocated before the value it points to is de-allocated?

<p>memory leak (D)</p> Signup and view all the answers

If p is a pointer of type char* and p == nullptr, then evaluating &p is always an error.

<p>False (B)</p> Signup and view all the answers

Consider these statements:

i) The maximum amount of free store memory used by a program can always be determined at compile-time. ii) The maximum amount of call stack memory used by a program can always be determined at compile-time.

<p>i) and ii) are both false (B)</p> Signup and view all the answers

Consider these statements:

i) A pointer variable in free store memory can point to a value in stack memory. ii) A pointer variable in stack memory can point to a value in free store memory.

<p>i) and ii) are both true (A)</p> Signup and view all the answers

What is a class in C++?

<p>A blueprint for creating objects (B)</p> Signup and view all the answers

Which of the following follows the Last In, First Out (LIFO) principle?

<p>Stack (B)</p> Signup and view all the answers

What happens when a local variable goes out of scope?

<p>It is automatically deallocated. (B)</p> Signup and view all the answers

What is a dangling pointer?

<p>A pointer that has been deleted or deallocated but still points to memory (A)</p> Signup and view all the answers

What is the best way to prevent memory leaks?

<p>Always deallocate memory using delete or delete[] (C)</p> Signup and view all the answers

What is the difference between referencing and de-referencing?

<p>Referencing creates a pointer, de-referencing accesses the value stored at a pointer (A)</p> Signup and view all the answers

Where is free store memory located?

<p>In the heap (C)</p> Signup and view all the answers

What is the correct syntax for defining a constructor in C++?

<p>className() (B)</p> Signup and view all the answers

Which keyword is used to deallocate memory from the free store in C++?

<p>delete (B)</p> Signup and view all the answers

What is the purpose of the private keyword in a class?

<p>It restricts access to class members from outside the class. (C)</p> Signup and view all the answers

Local variables are allocated in the heap.

<p>False (B)</p> Signup and view all the answers

A destructor has the same name as the class but starts with a tilde (~)

<p>True (A)</p> Signup and view all the answers

Stack memory is managed manually, while free store memory is managed automatically.

<p>False (B)</p> Signup and view all the answers

Pointers can be used to store the memory addresses of variables.

<p>True (A)</p> Signup and view all the answers

The switch statement can only evaluate integer and character values.

<p>True (A)</p> Signup and view all the answers

Pointer arithmetic allows arithmetic operations such as addition and subtraction on memory addresses.

<p>True (A)</p> Signup and view all the answers

If a class has no constructor, C++ automatically provides a default constructor.

<p>True (A)</p> Signup and view all the answers

The delete keyword should only be used for objects allocated with new.

<p>True (A)</p> Signup and view all the answers

Public members of a class can be accessed from outside the class.

<p>True (A)</p> Signup and view all the answers

An array in C++ can dynamically change its size at runtime.

<p>False (B)</p> Signup and view all the answers

A std::vector automatically manages memory allocation and deallocation.

<p>True (A)</p> Signup and view all the answers

Accessing an array out of its bounds in C++ results in a compile-time error.

<p>False (B)</p> Signup and view all the answers

Vectors use contiguous memory allocation, just like arrays.

<p>True (A)</p> Signup and view all the answers

The push_back() function in a vector increases the vector’s capacity every time it is called.

<p>False (B)</p> Signup and view all the answers

The only difference between a struct and a class in C++ is that members of a struct are public by default, while members of a class are private by default.

<p>True (A)</p> Signup and view all the answers

A struct can have member functions just like a class.

<p>True (A)</p> Signup and view all the answers

Structs are more commonly used for data structures, while classes are used for object-oriented programming.

<p>True (A)</p> Signup and view all the answers

A class can inherit from a struct, but a struct cannot inherit from a class.

<p>False (B)</p> Signup and view all the answers

Local variables are stored in stack memory.

<p>True (A)</p> Signup and view all the answers

Global variables are stored in the free store (heap).

<p>False (B)</p> Signup and view all the answers

Dynamic memory allocated using new is stored in free store (heap).

<p>True (A)</p> Signup and view all the answers

Static memory allocation is done at runtime.

<p>False (B)</p> Signup and view all the answers

The delete keyword deallocates memory from the stack.

<p>False (B)</p> Signup and view all the answers

A dangling pointer occurs when a pointer still points to a memory location that has been freed.

<p>True (A)</p> Signup and view all the answers

Using a dangling pointer can lead to undefined behavior.

<p>True (A)</p> Signup and view all the answers

A memory leak occurs when allocated memory is not properly freed.

<p>True (A)</p> Signup and view all the answers

Setting a pointer to nullptr after deleting it helps prevent dangling pointers.

<p>True (A)</p> Signup and view all the answers

What is the difference between an array and a vector in C++?

<p>An array has a fixed size and does not automatically manage memory. A vector is a dynamic array that resizes automatically and manages memory itself.</p> Signup and view all the answers

How do you declare a static array of size 10 in C++?

<p>int arr[10];</p> Signup and view all the answers

How do you add an element to a vector named vec?

<p>vec.push_back(value);</p> Signup and view all the answers

What is the time complexity of accessing an element in an array or vector using an index?

<p>O(1) (Constant time) because elements are stored in contiguous memory.</p> Signup and view all the answers

When would you use a struct instead of a class in C++?

<p>When you need a simple data structure with public access by default, such as a Point {x, y} in a 2D space.</p> Signup and view all the answers

What is one advantage of using classes over structs in object-oriented programming?

<p>Classes allow better encapsulation by default (private members), making them more suited for data hiding.</p> Signup and view all the answers

What is the difference between stack and heap memory?

<p>Stack: Stores local variables, is automatically managed, and has limited size. Heap: Stores dynamically allocated memory, must be manually managed (new/delete).</p> Signup and view all the answers

Where is the static variable stored in memory?

<p>Static variables are stored in the static/global memory section and persist for the program's lifetime.</p> Signup and view all the answers

What happens if you forget to delete dynamically allocated memory?

<p>A memory leak occurs because the allocated memory is not returned to the system.</p> Signup and view all the answers

How do you prevent a dangling pointer?

<p>Set the pointer to nullptr after delete. Avoid using a pointer after deleting it. Use smart pointers like std::unique_ptr.</p> Signup and view all the answers

What is the difference between a dangling pointer and a memory leak?

<p>A dangling pointer points to freed memory. A memory leak happens when memory is allocated but never freed.</p> Signup and view all the answers

How do you allocate memory dynamically for an array of 5 integers?

<p>int* arr = new int[5];</p> Signup and view all the answers

How do you correctly free the memory allocated in an array?

<p>delete[] arr;</p> Signup and view all the answers

Which of these functions has a memory leak?

int f(int a) { double* p = new double(2.11); p = new double(-6.38); delete p; return 3 * a; }

int g(int a) { double* p = new double(2.11); delete p; return 3 * a; }

<p>just f has a memory leak (B)</p> Signup and view all the answers

Consider these two statements:

i) If a program has a memory leak, then it must also have a dangling pointer problem. ii) If a program has a dangling pointer problem, then it must also have a memory leak.

<p>i) and ii) are both false (A)</p> Signup and view all the answers

Consider this code fragment:

double* p = new double(1.35); double** x = &p;

Which statement correctly de-allocates the double that p points to?

<p>delete *x; (B)</p> Signup and view all the answers

Consider this function:

double* f(double x) { int result = x+3; return &return; }

Consider these two statements:

i) Calling f results in a memory leak. ii) Calling f results in a dangling pointer.

Which one of these statements is most accurate?

<p>i) is false and ii) is true (D)</p> Signup and view all the answers

Consider these two statements:

i) A mutating method in a class is a const method. ii) A const method in a class is mutating.

Which one of these statements is most accurate?

<p>i) and ii) are both false (D)</p> Signup and view all the answers

What is the difference between a struct and a class?

<p>by default, the members of a struct are public, while the members of a class are private (D)</p> Signup and view all the answers

What is the return type of a constructor?

<p>constructor's have no return type (D)</p> Signup and view all the answers

Consider these two statements:

i) new and delete can be used to allocate and de-allocate free store memory. ii) new and delete can be used to allocate and de-allocate stack memory.

<p>i) is true and ii) is false (C)</p> Signup and view all the answers

Consider this function:

int f(int a) { int a = 5; int * p = &a; return a + 1; }

Consider these two statements:

i) Calling f results in a memory leak. ii) Calling f results in a dangling pointer.

Which one of these statements is most accurate?

<p>i) and ii) are both false (A)</p> Signup and view all the answers

Consider this code:

char f(char a) { char* c = new char('t'); char* d = c; return a + *c + *d; }

Consider these two statements:

i) Calling f results in a memory leak. ii) Calling f results in a dangling pointer.

<p>i) is true and ii) is false (C)</p> Signup and view all the answers

What is a constructor initializer list?

<p>a list of variable names in a class and their initial values (C)</p> Signup and view all the answers

A class must have exactly one constructor

<p>False (B)</p> Signup and view all the answers

What does b point to after this code fragment runs?

int* a = new int(5); int* b = a; a = nullptr;

<p>the int that was created by new (A)</p> Signup and view all the answers

Consider these two statements:

i) new and delete can be used to allocate and de-allocate free store memory ii) new and delete can be used to allocate and de-allocate stack memory

What is most accurate?

<p>i) is true and ii) is false (C)</p> Signup and view all the answers

Consider these two statements:

i) Getters are usually const. ii) Setters are usually const.

Which one of these statements is most accurate?

<p>i) is true and ii) is false (D)</p> Signup and view all the answers

A const method in a class can modify the public variables of the class, but not its private variables.

<p>False (B)</p> Signup and view all the answers

How many inputs does an ordinary copy constructor take?

<p>exactly 1 (A)</p> Signup and view all the answers

The following single-line code fragment calls the default constructor for the programmer-defined class Folder:

Folder root;

<p>True (A)</p> Signup and view all the answers

What is the name for a destructor for a class called Page?

<p>~Page (D)</p> Signup and view all the answers

In a C++ class, a common use of destructors is to de-allocate resources allocated by constructors.

<p>True (A)</p> Signup and view all the answers

Getters are usually non-mutating methods.

<p>True (A)</p> Signup and view all the answers

Consider these two statements:

i) Setters are usually declared const. ii) Getters are usually declared const.

Which one of these statements is most accurate?

<p>i) is false and ii) is true (A)</p> Signup and view all the answers

In C++, all constructors must have an initializer list.

<p>False (B)</p> Signup and view all the answers

Private variables defined in a class cannot be modified.

<p>False (B)</p> Signup and view all the answers

Consider these two statements: i) A { corresponds to a “push” onto the call stack ii) A } corresponds to a “pop” on the call stack

<p>i) and ii) are both true (A)</p> Signup and view all the answers

Consider these two statements: i) Blackbox tests can be created without seeing the implementation of a function. ii) Whitebox tests can be created without seeing the implementation of a function. Which one of these statements most accurately describes the truth values of i) and ii)?

<p>i) is true and ii) is false (D)</p> Signup and view all the answers

Consider these two statements: i) Only constructors can have initialization lists. ii) A class can have multiple constructors and multiple destructors. Which one of these statements most accurately describes the truth values of i) and ii)?

<p>i) is true and ii) is false (D)</p> Signup and view all the answers

Consider these two statements: i) Immutable objects have no setters. ii) A non-const getter will always cause a compiler error. Which one of these statements most accurately describes the truth values of i) and ii)?

<p>i) is true and ii) is false (C)</p> Signup and view all the answers

Consider these two statements: i) For a child class to be able to re-implement a method it inherits from a parent class, the method must be declared virtual in the parent class. ii) For a child class to be able to re-implement a method it inherits from a parent class, the method must be declared virtual in the child class. Which one of these statements most accurately describes the truth values of i) and ii)?

<p>i) is true and ii) is false (D)</p> Signup and view all the answers

What type of memory is allocated at compile time?

<p>Static Memory (B)</p> Signup and view all the answers

Which of the following correctly defines a derived class in C++?

<p>class Derived : public Base {} (A)</p> Signup and view all the answers

What access specifier makes base class members inaccessible in a derived class?

<p>private (C)</p> Signup and view all the answers

What is the primary purpose of the this pointer in C++?

<p>It stores the address of the current object. (A)</p> Signup and view all the answers

What happens when a base class method is overridden in a derived class?

<p>Only the derived class method is called when using a base class pointer/reference and the method is virtual. (C)</p> Signup and view all the answers

In a UML class diagram, what does a solid line with a hollow triangle arrowhead represent?

<p>Inheritance (C)</p> Signup and view all the answers

A class can inherit from multiple base classes in C++.

<p>True (A)</p> Signup and view all the answers

The this pointer can be used in static member functions.

<p>False (B)</p> Signup and view all the answers

Virtual functions allow for dynamic (runtime) polymorphism in C++.

<p>True (A)</p> Signup and view all the answers

What is the difference between public, protected, and private inheritance?

<p>public: The base class’s public members remain public, and protected members remain protected. protected: The base class’s public and protected members become protected in the derived class. private: The base class’s public and protected members become private in the derived class.</p> Signup and view all the answers

Why do we use the this pointer in C++?

<p>The this pointer helps to reference the current instance of an object, allowing us to return the object itself or resolve naming conflicts between member variables and parameters.</p> Signup and view all the answers

What is method overriding, and how does it relate to polymorphism?

<p>Moverriding occurs when a derived class provides a new definition for a function declared as virtual in the base class. It enables runtime polymorphism, meaning the derived class’s version of the function is called even when accessed through a base class pointer.</p> Signup and view all the answers

Which of the following correctly defines an abstract method in C++?

<p>A method declared with = 0 in a class (C)</p> Signup and view all the answers

Which statement about an abstract class is true?

<p>It must contain at least one pure virtual function (B)</p> Signup and view all the answers

What does it mean to instantiate a class?

<p>Create an object from a class (C)</p> Signup and view all the answers

Why should classes that are meant to be inherited have a virtual destructor?

<p>To prevent memory leaks when deleting derived objects via base class pointers (A)</p> Signup and view all the answers

Which of the following best describes the Liskov Substitution Principle (LSP)?

<p>A subclass must be replaceable by its base class without altering the correctness of the program (B)</p> Signup and view all the answers

Which of the following correctly defines a namespace alias?

<p>namespace ns = std::nested_namespace; (A)</p> Signup and view all the answers

An abstract class can have non-pure virtual functions.

<p>True (A)</p> Signup and view all the answers

A class with at least one pure virtual function cannot be instantiated.

<p>True (A)</p> Signup and view all the answers

The global namespace in C++ refers to any namespace declared inside a function.

<p>False (B)</p> Signup and view all the answers

Function overloading means defining multiple functions with the same name but different return types.

<p>False (B)</p> Signup and view all the answers

The Single Definition Rule (SDR) ensures that a function or variable can only be defined once in a program.

<p>True (A)</p> Signup and view all the answers

What is an abstract base class?

<p>An abstract base class is a class that contains at least one pure virtual function (= 0). It cannot be instantiated and serves as a blueprint for derived classes.</p> Signup and view all the answers

Why should a class intended for inheritance have a virtual destructor?

<p>If a base class destructor is not virtual, deleting a derived object through a base class pointer will call only the base destructor, leading to resource leaks.</p> Signup and view all the answers

What is a function signature in C++?

<p>A function signature consists of its name, return type, and parameter list. It helps in function overloading by distinguishing functions with the same name.</p> Signup and view all the answers

What is the difference between a declaration and a definition in C++?

<p>A declaration tells the compiler that a function or variable exists, while a definition allocates memory or provides implementation.</p> Signup and view all the answers

What is the purpose of a namespace alias?

<p>It shortens long or deeply nested namespace names for easier usage.</p> Signup and view all the answers

What is the purpose of the throw keyword in C++?

<p>To explicitly raise an exception (B)</p> Signup and view all the answers

Which keyword is used to handle exceptions in C++?

<p>catch (A)</p> Signup and view all the answers

What is a runtime error in C++?

<p>An error that occurs during program execution (B)</p> Signup and view all the answers

What happens when an exception is thrown but not caught?

<p>The program terminates (A)</p> Signup and view all the answers

How does the call stack behave when an exception is thrown?

<p>The stack unwinds, calling destructors for local objects before propagating the exception (A)</p> Signup and view all the answers

The try block is mandatory when using throw in C++.

<p>False (B)</p> Signup and view all the answers

A destructor is always called before an exception is propagated up the call stack.

<p>True (A)</p> Signup and view all the answers

A catch block can catch multiple types of exceptions.

<p>True (A)</p> Signup and view all the answers

Global variable flags can be used to signal an error state without using exceptions.

<p>True (A)</p> Signup and view all the answers

An exception can only be caught in the same function where it was thrown.

<p>False (B)</p> Signup and view all the answers

What is the purpose of the try block in C++?

<p>The try block is used to enclose code that may generate exceptions, allowing the program to handle errors using a catch block.</p> Signup and view all the answers

How does the call stack behave when an exception occurs?

<p>When an exception is thrown, the call stack unwinds, meaning local objects in the current scope are destroyed before propagating the exception up to the next function in the stack.</p> Signup and view all the answers

What happens if an exception is thrown inside a destructor?

<p>If an exception is thrown inside a destructor and another exception is already active, the program will terminate. This is why destructors should not throw exceptions.</p> Signup and view all the answers

What is the advantage of using global variable flags for error handling?

<p>Global variable flags provide a lightweight way to indicate an error state without using exceptions, making them useful for embedded systems or performance-critical applications.</p> Signup and view all the answers

Consider these two statements: i) An abstract class is a class with at least one abstract method. ii) The methods of an abstract class must all be public.

<p>i) is true and ii) is false (D)</p> Signup and view all the answers

Consider these two statements:

i) A class diagram shows the inheritance relations between classes in a class hierarchy. ii) For any two classes A and B, either A inherits from B, or B inherits from A.

<p>i) is true and ii) is false (C)</p> Signup and view all the answers

Consider these two statements:

i) A class can inherit from 0 other classes. ii) A class could have 10 other classes inherit from it.

<p>i) and ii) are both true (B)</p> Signup and view all the answers

Consider these two statements:

i) If class A publicly inherits from class B, then private methods in B will be inherited as private in A. ii) If class A publicly inherits from class B, then public methods in B will be inherited as public in A.

<p>i) and ii) are both true (C)</p> Signup and view all the answers

Consider the following code fragment:

 class Flag;

Consider these two statements:

i) The fragment declares a class named Flag. ii) The fragment defines a class named Flag.

<p>i) is true and ii) is false (C)</p> Signup and view all the answers

Consider these two statements:

i) Constructors must have an initialization list. ii) Destructors can take 1 or more input parameters.

<p>i) and ii) are both false (D)</p> Signup and view all the answers

Consider these two statements:

i) Destructors are virtual by default. ii) Destructors are abstract by default.

<p>i) and ii) are both false (B)</p> Signup and view all the answers

Consider these two statements:

i) Setters can be virtual. ii) Getters can be virtual.

<p>i) and ii) are both true (D)</p> Signup and view all the answers

Consider these two classes:

class Person { public: virtual int get_age() const = 0; virtual ~Person() { } };

class Student : public Person { int age; int grade; public: Student(int a, int g) : age(a), grade(g) { }

int get_age() const { return age; }
int get_grade() const { return grade; }

};

Suppose p is a pointer of type Person* and s is a pointer of type Student*, and both p and s point to valid objects in a correct program. Which of these statements cause a compiler error?

s = p; // statement A p = s; // statement B

<p>A causes a compiler error, but B doesn't (A)</p> Signup and view all the answers

Consider this code fragment:

class Person { int age; public: void print_age() { cout << ???; } }

What can ??? be replaced by so that the print_age() correctly prints age.

Select all the choices that work.

<p>age (B), this-&gt;age (D)</p> Signup and view all the answers

Consider these two statements:

i) A single using statement can give access to a exactly one function in a namespace. ii) A single using statement can give access to every function in a namespace.

<p>i) and ii) are both true (A)</p> Signup and view all the answers

Consider these two statements:

i) In C++, std is a namespace. ii) In C++, cout is a namespace.

<p>i) is true and ii) is false (B)</p> Signup and view all the answers

All virtual methods must have an implementation in the class where they are defined.

<p>False (B)</p> Signup and view all the answers

Consider these two statements:

i) All virtual methods must also be abstract. ii) All abstract methods must also be public.

<p>i) and ii) are both false (C)</p> Signup and view all the answers

Consider these two statements:

i) Every C++ function must be defined exactly once. ii) Every C++ function must be declared exactly once.

<p>i) is true and ii) is false (B)</p> Signup and view all the answers

Suppose a function throws an exception that is not caught by a try/catch block anywhere in the program. Which one of these statements is true?

<p>The program eventually crashes due to the exception reaching main. Since main does not catch the program, the exception is thrown out of main, and this crashes the program. (D)</p> Signup and view all the answers

Consider these two statements:

i) Recursive functions generally use less memory than equivalent non-recursive functions. ii) Recursive functions generally run faster than equivalent non-recursive functions.

<p>i) and ii) are both false (B)</p> Signup and view all the answers

Consider this function:

void hi() { hi(); cout << "Hello!\n"; }

What is printed when hi() is called? Assume the compiler applies no optimizations.

<p>nothing is printed (B)</p> Signup and view all the answers

Consider these two statements:

i) Constructors can be const. ii) Destructors can be const.

Which one of these statements is most accurate?

<p>i) and ii) are both false (C)</p> Signup and view all the answers

If function f always throws a std::runtime_error exception, what value does example() return?

int example() { int n = 5; try { f(); // always throws a std::runtime_error exception n++; } catch (std::runtime_error e) { n--; } return n; }

<p>4 (@)</p> Signup and view all the answers

Consider this function:

void f() { f(); }

What happens when you call f()? Assume the compiler applies no optimizations.

<p>it eventually crashes due to running out of stack memory (C)</p> Signup and view all the answers

Suppose class A inherits from class B. Select all the true statements.

<p>A is a subclass of B (B), B is the parent of A (C)</p> Signup and view all the answers

A function is tail-recursive if the last thing it does it recursively call itself. Select all the tail-recursive functions.

<p>int h(int n) { if (n &lt;= 1) { return 1; } else { return h(n-2); } } (A), void a() { cout &lt;&lt; &quot;carrot&quot;; a(); } (@)</p> Signup and view all the answers

The following single-line code fragment calls the default constructor for the programmer-defined class Folder:

Folder root;

<p>True (A)</p> Signup and view all the answers

Suppose class Student inherits from the class Person, and p is a pointer of type Person* that points to a valid Person object. Suppose function f is defined like this:

void f(Student* s) { // ... }

Does calling f(p) always result in a compile-time error?

<p>yes (B)</p> Signup and view all the answers

Consider these two statements:

i) Any piece of C++ code that uses loops can be re-written into code that does the same thing using recursion instead (without any loops). ii) Any piece of C++ code that uses recursion can be re-written into code that does the same thing using loops instead (without any recursion).

<p>i) and ii) are both true (D)</p> Signup and view all the answers

Consider these two statements:

i) Only virtual methods can be inherited. ii) Only public methods can be inherited.

<p>i) and ii) are both false (B)</p> Signup and view all the answers

Consider using this function to compute fib(50):

int fib(int n) { if (n == 1) { return 1; } else if (n == 2) { return 1; } else { return fib(n - 1) + fib(n - 2); } }

Which one of these statements is true?

<p>it calls fib so many times that it runs very slowly (B)</p> Signup and view all the answers

Consider this function:

int p(int a, int n) { if (n < 0) cmpt::error("n must be >= 0");

if (a == 0 && n == 0) return 1;
if (a == 0 && n != 0) return 0;
if (a != 0 && n == 0) return 1;
if (a == 1) return 1;

return a * p(a, n - 1);

}

Is p tail recursive?

<p>no (A)</p> Signup and view all the answers

Consider this function:

int index_of_max(const vector& v) { int mi = 0; for(int i = 1; i < v.size(); i++) { if (v[i] > v[mi]) { mi = i; // line A } } return mi; }

If v has 10,000 randomly chosen and randomly ordered ints, how many times would you expect line A to be called?

<p>about 10 (C)</p> Signup and view all the answers

Suppose v is a non-empty vector of size n. Does this pseudocode correctly calculate the max value of v?

m = 0 for i = 1 to n - 1 do if v[i] > m then m = v[i] end end

<p>no (A)</p> Signup and view all the answers

Consider these two statements:

i) Every C++ function must be defined exactly once. ii) Every C++ function must be declared exactly once

<p>i) is true and ii) is false (D)</p> Signup and view all the answers

Given values v[0], v[1], ..., v[a-1], and a target value b, linear search finds an index c that satisfies which expression?

<p>v[c] == b (A)</p> Signup and view all the answers

Suppose v is a vector that contains n > 0 randomly chosen and randomly ordered ints. v has no duplicates.

If there is a 50% chance that the target value x is somewhere in v, what is the expected average number of comparisons linear search does to either find x, or determine that x is not in v?

<p>3n/4 (B)</p> Signup and view all the answers

Insertion sort works by dividing a vector into two parts, sorting the two parts, and then combining the two sorted parts together.

<p>False (B)</p> Signup and view all the answers

When sorting the same large vector of randomly ordered ints:

<p>mergesort uses about twice as much memory as insertion sort (A)</p> Signup and view all the answers

Suppose A is sorted vector of size m > 0, and B is a sorted vector of size n > 0.

About how many comparisons are needed to merge A and B into a new vector?

<p>m + n (B)</p> Signup and view all the answers

Suppose you have a vector of 1000 different ints in ascending sorted order. If you use binary search to look for a given element in the vector, about how many comparisons are done in the worst case?

<p>10 (B)</p> Signup and view all the answers

About how many comparisons are needed to test if a vector of 50 ints is in ascending sorted order?

<p>50 (C)</p> Signup and view all the answers

Suppose v is a vector of n randomly ordered ints with no duplicates.

In the worst case, about how many comparisons does linear insertion sort do to sort v?

<p>n^2 (D)</p> Signup and view all the answers

About how many comparison would insertion sort do to sort a list of 500 elements?

<p>250000 (D)</p> Signup and view all the answers

The following is a correct specification of the standard merge function (as it might be used by mergesort):

// Pre-condition: // v.size() == w.size() // Post-condition: // returns a new vector that contains all the elements in v and w (even duplicates) // in ascending sorted order. vector merge(const vector& v, const vector& w)

<p>False (B)</p> Signup and view all the answers

When sorting the same large vector of randomly ordered ints:

<p>mergesort is usually much faster than insertion sort (B)</p> Signup and view all the answers

Suppose v is a vector of n randomly ordered ints with no duplicates.

In the worst case, about how many comparisons does mergesort do to sort v?

<p>n log n (D)</p> Signup and view all the answers

It is currently unknown if there is an O(n^k) worst-case algorithm for solving the traveling sales problem, where k is a fixed positive integer constant.

<p>True (A)</p> Signup and view all the answers

Select all the expressions that are in O(n)

<p>7500 + 5000n (A), n (C), 10^6 (D)</p> Signup and view all the answers

In the worst case, how many comparisons does mergesort do on a vector of n items? Choose the tightest expression.

<p>O(nlogn) (D)</p> Signup and view all the answers

The tightest O-notation expression describing the the sum of O(n) and O(n^2) and O(n^3)

<p>O(n^3) (C)</p> Signup and view all the answers

Consider these statements:

i) sorting is an NP-complete problem ii) the traveling salesman problem is an NP-complete problem

<p>i) is false and ii) is true (B)</p> Signup and view all the answers

Suppose algorithm A has a running time of O(n). If A takes 5 seconds to process 5000 items, about how long would you expect A to take to process 10,000 items?

<p>10 (B)</p> Signup and view all the answers

Mergesort sorts a vector by dividing it into two separate parts, independently sorting each part, and then merging the two sorted parts.

<p>True (A)</p> Signup and view all the answers

What is the worst-case running-time for the fastest algorithm that solves the traveling salesman problem, where n is the number of cities?

<p>unknown (A)</p> Signup and view all the answers

Suppose A is a vector of n ints. If you run mergesort on A followed by 1000 calls to binary search, what is the O-notation expression that best describes the total worst-case running time of those operations?

<p>O(nlogn) (D)</p> Signup and view all the answers

What computational problem was Alan Turing the first one to solve in the 1930s?

<p>the halting problem (B)</p> Signup and view all the answers

Select all the expressions that are in O(n^2)

<p>100+3n+n^2 (C), n (D), (n+1)^2 (@)</p> Signup and view all the answers

Consider this function:

template <typename T> void swap(T& a, T& b) { T temp = a; a = b; b = temp; }

For this to work correctly, if the type T is a user-define class then T must have a default constructor.

<p>False (B)</p> Signup and view all the answers

In the worst case, how many comparisons does linear insertion sort do on a vector of n items? Choose the tightest expression.

<p>O(n^2) (B)</p> Signup and view all the answers

In the worst case, how many comparisons does linear search do on a vector of n items? Choose the tightest expression.

<p>O(n) (B)</p> Signup and view all the answers

For linear search, what is the usual key operation that is counted when determining it's run-time performance?

<p>== (A)</p> Signup and view all the answers

In the worst case, how many comparisons does binary search do on a vector of n items? Choose the tightest expression.

<p>O(logn) (C)</p> Signup and view all the answers

Write a cout statement that prints the address of variable a

<p>cout &lt;&lt; &amp;a;</p> Signup and view all the answers

Suppose p is a pointer to an int. Write a cout statement that prints the int p points to

<p>cout &lt;&lt; *p;</p> Signup and view all the answers

the following code fragment does not compile: int x = new int(5); cout << x;

<p>True (A)</p> Signup and view all the answers

Suppose m is a pointer to a double on the free store. Write a statement that deletes the memory m points to

<p>delete m;</p> Signup and view all the answers

Suppose arr is a pointer to a double array on the free store. Write a statement that deletes the memory arr points to

<p>delete[] arr;</p> Signup and view all the answers

True or false: the following code fragment does not compile: string s = "cat"; string t = "dog"; string* a = &s; string* b = &t; a = b; b = a;

<p>False (B)</p> Signup and view all the answers

True or false: the following code fragment causes a memory leak if executed: string s = "cold"; string* p = &s; p = nullptr;

<p>False (B)</p> Signup and view all the answers

True or false: the following function compiles, and has no memory leak or other run-time error: void f() { int a = 5; int* p = &a; cout << a; delete p; }

<p>False (B)</p> Signup and view all the answers

it is an error to delete a pointer whose value is nullptr

<p>False (B)</p> Signup and view all the answers

Suppose arr is an array of 10 int values all initialized to 0. What does cout << arr[10] print?

<p>unknown: arr[10] is out of bounds, and so could print anything, or maybe even crash</p> Signup and view all the answers

every object has at least one (possibly empty) constructor.

<p>True (A)</p> Signup and view all the answers

every object has at least one (possibly empty) destructor.

<p>True (A)</p> Signup and view all the answers

initialization lists can be used with any method in an object.

<p>False (B)</p> Signup and view all the answers

a default constructor takes no inputs.

<p>True (A)</p> Signup and view all the answers

by default, methods and variables in a class are private.

<p>True (A)</p> Signup and view all the answers

an object’s destructor is called automatically when the object goes out of scope, or is deleted.

<p>True (A)</p> Signup and view all the answers

a class can define more than one constructor

<p>True (A)</p> Signup and view all the answers

a class can define more than one destructor.

<p>False (B)</p> Signup and view all the answers

if you create a class called Fraction to represent fractions, then you can define a custom operator+ for adding Fraction objects

<p>True (A)</p> Signup and view all the answers

all objects are classes, but not all classes are objects

<p>False (B)</p> Signup and view all the answers

What is the general name (not g++!) of the program that converts a C++ source code file (e.g. a .cpp file) into object code?

<p>a compiler</p> Signup and view all the answers

What is the general name (not g++!) of the program that converts a C++ object code file into an executable file?

<p>a linker</p> Signup and view all the answers

What is the usual file name extension for C++ header files?

<p>.h</p> Signup and view all the answers

Write a complete C++ program that prints "Hello, world!" on cout and does not have a using statement.

<p>#include <iostream> int main() { std::cout &lt;&lt; &quot;Hello, world!&quot;; }</p> Signup and view all the answers

in the worst case, linear search has to do 1000 comparisons when searching through a vector of n=1000 numbers.

<p>True (A)</p> Signup and view all the answers

binary search only works on sorted data

<p>True (A)</p> Signup and view all the answers

it’s usually faster to do a linear search on a vector of n numbers than it is to first sort that data and then do a binary search on it.

<p>True (A)</p> Signup and view all the answers

What is the name we use for a class, such as PQueue, where all the methods are public and virtual, and at least one method is =0?

<p>abstract base class</p> Signup and view all the answers

Explain what =0 at the end of some method headers means here.

<p>it means that the corresponding method has no implementation.</p> Signup and view all the answers

Explain what the virtual keyword means here

<p>it means that classes that inherit from PQueue are allowed to implement their own version of that method.</p> Signup and view all the answers

why would a class include a virtual destructor?

<p>to allow classes that derive from it to implement their own destructor if they need to</p> Signup and view all the answers

Define a new class named Heap that derives (i.e. inherits) from PQueue. You don't need to implement any methods or variables: just show how to do the inheritance in the class header line.

<p>class Heap : public PQueue { // ...<br /> };</p> Signup and view all the answers

Flashcards

Pointer Comparison

If a and b are valid non-null double* pointers and a == b, then *a == *b is always true. If a != b, *a != *b may not be true.

Accessing Array Elements

The expression *(arr + 2) accesses the element at index 2 of array arr.

Variable address

When a C++ program runs multiple times, the memory address of a variable may be different each time.

Dereferencing Nullptr

Accessing the memory location pointed to by a null pointer will produce a runtime error

Signup and view all the flashcards

Garbage (in Garbage Collection)

Memory that has not been de-allocated, but can no longer be accessed by the program.

Signup and view all the flashcards

If-else-if vs. Switch

A switch statement can be re-written with if-else-if statements, but the reverse isn't always true due to the limitations with switch cases

Signup and view all the flashcards

Purpose of Makefiles

Makefiles in this course are used to compile the C++ program.

Signup and view all the flashcards

Blackbox vs. Whitebox Testing

Testing based on internal structure is whitebox testing, external behavior alone is blackbox testing.

Signup and view all the flashcards

new Operators Return Type

new int(5) and new int[5] both return a pointer of type int* representing the address of allocated memory.

Signup and view all the flashcards

Location of Global Variables

Global variables in C++ are stored in static memory.

Signup and view all the flashcards

Memory Leak

Allocating memory with new but failing to deallocate it with delete results in memory leak.

Signup and view all the flashcards

Address of a Null Pointer

Taking the address of a pointer (&p) is valid even if the pointer itself is null.

Signup and view all the flashcards

Memory Usage Determination

The maximum memory used by a program (free store/call stack) is often determined at runtime, not compile-time.

Signup and view all the flashcards

Pointer Scope

Pointers in either free store or stack memory can point to values in the other memory area.

Signup and view all the flashcards

Class Definition

A class is a blueprint for creating objects (instances).

Signup and view all the flashcards

Stack Principle

Stack follows the Last In, First Out (LIFO) principle.

Signup and view all the flashcards

Local Variable Scope

Local variables are automatically removed from memory (deallocated) when they go out of scope.

Signup and view all the flashcards

Dangling Pointer

A dangling pointer is a pointer that points to memory that has been deallocated or freed.

Signup and view all the flashcards

Preventing Memory Leaks

Deallocate memory using delete or delete[] to prevent memory leaks.

Signup and view all the flashcards

Referencing vs. De-referencing

Referencing creates a pointer to a variable, while de-referencing accesses the value at the pointer's address.

Signup and view all the flashcards

Free Store Location

Free store memory is located in the heap.

Signup and view all the flashcards

Constructor Syntax

className() is the syntax defining constructor. It shares the same name as the class.

Signup and view all the flashcards

Deallocating Memory

The keyword delete is used to deallocate memory from the free store.

Signup and view all the flashcards

Private Keyword

The private keyword restricts access to class members from outside the class.

Signup and view all the flashcards

Local Variable Allocation

Local variables are allocated to stack memory.

Signup and view all the flashcards

Default Constructor

If a class lacks a constructor, C++ provides a default constructor automatically.

Signup and view all the flashcards

The delete usage

The delete keyword should exclusively be used for memory allocated with new.

Signup and view all the flashcards

Public Access

Public members of a class are accessible from outside the class.

Signup and view all the flashcards

Memory Management of Vector

A std::vector automatically manages memory allocation and deallocation.

Signup and view all the flashcards

More Like This

CMPT 214 - AWK in Programming Principles
21 questions
CMPT 141.3 Practice Exam
22 questions
CMPT 270: MVC Architecture Quiz
18 questions

CMPT 270: MVC Architecture Quiz

NonViolentSerpentine2856 avatar
NonViolentSerpentine2856
CMPT 270: Custom JComponents and Graphics
8 questions
Use Quizgecko on...
Browser
Browser