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

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