Podcast
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
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.
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.
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.
It's never safe to de-reference the null pointer.
In the context of garbage collection, garbage is free store memory that has:
In the context of garbage collection, garbage is free store memory that has:
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.
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.
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.
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.
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?
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?
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?
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?
Where does C++ store global variables?
Where does C++ store global variables?
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?
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?
If p is a pointer of type char* and p == nullptr, then evaluating &p is always an error.
If p is a pointer of type char* and p == nullptr, then evaluating &p is always an error.
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.
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.
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.
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.
What is a class in C++?
What is a class in C++?
Which of the following follows the Last In, First Out (LIFO) principle?
Which of the following follows the Last In, First Out (LIFO) principle?
What happens when a local variable goes out of scope?
What happens when a local variable goes out of scope?
What is a dangling pointer?
What is a dangling pointer?
What is the best way to prevent memory leaks?
What is the best way to prevent memory leaks?
What is the difference between referencing and de-referencing?
What is the difference between referencing and de-referencing?
Where is free store memory located?
Where is free store memory located?
What is the correct syntax for defining a constructor in C++?
What is the correct syntax for defining a constructor in C++?
Which keyword is used to deallocate memory from the free store in C++?
Which keyword is used to deallocate memory from the free store in C++?
What is the purpose of the private keyword in a class?
What is the purpose of the private keyword in a class?
Local variables are allocated in the heap.
Local variables are allocated in the heap.
A destructor has the same name as the class but starts with a tilde (~)
A destructor has the same name as the class but starts with a tilde (~)
Stack memory is managed manually, while free store memory is managed automatically.
Stack memory is managed manually, while free store memory is managed automatically.
Pointers can be used to store the memory addresses of variables.
Pointers can be used to store the memory addresses of variables.
The switch statement can only evaluate integer and character values.
The switch statement can only evaluate integer and character values.
Pointer arithmetic allows arithmetic operations such as addition and subtraction on memory addresses.
Pointer arithmetic allows arithmetic operations such as addition and subtraction on memory addresses.
If a class has no constructor, C++ automatically provides a default constructor.
If a class has no constructor, C++ automatically provides a default constructor.
The delete keyword should only be used for objects allocated with new.
The delete keyword should only be used for objects allocated with new.
Public members of a class can be accessed from outside the class.
Public members of a class can be accessed from outside the class.
An array in C++ can dynamically change its size at runtime.
An array in C++ can dynamically change its size at runtime.
A std::vector automatically manages memory allocation and deallocation.
A std::vector automatically manages memory allocation and deallocation.
Accessing an array out of its bounds in C++ results in a compile-time error.
Accessing an array out of its bounds in C++ results in a compile-time error.
Vectors use contiguous memory allocation, just like arrays.
Vectors use contiguous memory allocation, just like arrays.
The push_back() function in a vector increases the vector’s capacity every time it is called.
The push_back() function in a vector increases the vector’s capacity every time it is called.
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.
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.
A struct can have member functions just like a class.
A struct can have member functions just like a class.
Structs are more commonly used for data structures, while classes are used for object-oriented programming.
Structs are more commonly used for data structures, while classes are used for object-oriented programming.
A class can inherit from a struct, but a struct cannot inherit from a class.
A class can inherit from a struct, but a struct cannot inherit from a class.
Local variables are stored in stack memory.
Local variables are stored in stack memory.
Global variables are stored in the free store (heap).
Global variables are stored in the free store (heap).
Dynamic memory allocated using new is stored in free store (heap).
Dynamic memory allocated using new is stored in free store (heap).
Static memory allocation is done at runtime.
Static memory allocation is done at runtime.
The delete keyword deallocates memory from the stack.
The delete keyword deallocates memory from the stack.
A dangling pointer occurs when a pointer still points to a memory location that has been freed.
A dangling pointer occurs when a pointer still points to a memory location that has been freed.
Using a dangling pointer can lead to undefined behavior.
Using a dangling pointer can lead to undefined behavior.
A memory leak occurs when allocated memory is not properly freed.
A memory leak occurs when allocated memory is not properly freed.
Setting a pointer to nullptr after deleting it helps prevent dangling pointers.
Setting a pointer to nullptr after deleting it helps prevent dangling pointers.
What is the difference between an array and a vector in C++?
What is the difference between an array and a vector in C++?
How do you declare a static array of size 10 in C++?
How do you declare a static array of size 10 in C++?
How do you add an element to a vector named vec?
How do you add an element to a vector
What is the time complexity of accessing an element in an array or vector using an index?
What is the time complexity of accessing an element in an array or vector using an index?
When would you use a struct instead of a class in C++?
When would you use a struct instead of a class in C++?
What is one advantage of using classes over structs in object-oriented programming?
What is one advantage of using classes over structs in object-oriented programming?
What is the difference between stack and heap memory?
What is the difference between stack and heap memory?
Where is the static variable stored in memory?
Where is the static variable stored in memory?
What happens if you forget to delete dynamically allocated memory?
What happens if you forget to delete dynamically allocated memory?
How do you prevent a dangling pointer?
How do you prevent a dangling pointer?
What is the difference between a dangling pointer and a memory leak?
What is the difference between a dangling pointer and a memory leak?
How do you allocate memory dynamically for an array of 5 integers?
How do you allocate memory dynamically for an array of 5 integers?
How do you correctly free the memory allocated in an array?
How do you correctly free the memory allocated in an array?
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;
}
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; }
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.
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.
Consider this code fragment:
double* p = new double(1.35);
double** x = &p;
Which statement correctly de-allocates the double that p points to?
Consider this code fragment:
double* p = new double(1.35); double** x = &p;
Which statement correctly de-allocates the double that p points to?
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?
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?
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?
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?
What is the difference between a struct and a class?
What is the difference between a struct and a class?
What is the return type of a constructor?
What is the return type of a constructor?
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.
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.
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?
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?
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.
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.
What is a constructor initializer list?
What is a constructor initializer list?
A class must have exactly one constructor
A class must have exactly one constructor
What does b point to after this code fragment runs?
int* a = new int(5);
int* b = a;
a = nullptr;
What does b point to after this code fragment runs?
int* a = new int(5); int* b = a; a = nullptr;
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?
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?
Consider these two statements:
i) Getters are usually const.
ii) Setters are usually const.
Which one of these statements is most accurate?
Consider these two statements:
i) Getters are usually const. ii) Setters are usually const.
Which one of these statements is most accurate?
A const method in a class can modify the public variables of the class, but not its private variables.
A const method in a class can modify the public variables of the class, but not its private variables.
How many inputs does an ordinary copy constructor take?
How many inputs does an ordinary copy constructor take?
The following single-line code fragment calls the default constructor for the programmer-defined class Folder:
Folder root;
The following single-line code fragment calls the default constructor for the programmer-defined class Folder:
Folder root;
What is the name for a destructor for a class called Page?
What is the name for a destructor for a class called Page?
In a C++ class, a common use of destructors is to de-allocate resources allocated by constructors.
In a C++ class, a common use of destructors is to de-allocate resources allocated by constructors.
Getters are usually non-mutating methods.
Getters are usually non-mutating methods.
Consider these two statements:
i) Setters are usually declared const.
ii) Getters are usually declared const.
Which one of these statements is most accurate?
Consider these two statements:
i) Setters are usually declared const. ii) Getters are usually declared const.
Which one of these statements is most accurate?
In C++, all constructors must have an initializer list.
In C++, all constructors must have an initializer list.
Private variables defined in a class cannot be modified.
Private variables defined in a class cannot be modified.
Consider these two statements:
i) A { corresponds to a “push” onto the call stack
ii) A } corresponds to a “pop” on the call stack
Consider these two statements: i) A { corresponds to a “push” onto the call stack ii) A } corresponds to a “pop” on the call stack
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)?
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)?
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)?
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)?
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)?
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)?
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)?
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)?
What type of memory is allocated at compile time?
What type of memory is allocated at compile time?
Which of the following correctly defines a derived class in C++?
Which of the following correctly defines a derived class in C++?
What access specifier makes base class members inaccessible in a derived class?
What access specifier makes base class members inaccessible in a derived class?
What is the primary purpose of the this pointer in C++?
What is the primary purpose of the this pointer in C++?
What happens when a base class method is overridden in a derived class?
What happens when a base class method is overridden in a derived class?
In a UML class diagram, what does a solid line with a hollow triangle arrowhead represent?
In a UML class diagram, what does a solid line with a hollow triangle arrowhead represent?
A class can inherit from multiple base classes in C++.
A class can inherit from multiple base classes in C++.
The this pointer can be used in static member functions.
The this pointer can be used in static member functions.
Virtual functions allow for dynamic (runtime) polymorphism in C++.
Virtual functions allow for dynamic (runtime) polymorphism in C++.
What is the difference between public, protected, and private inheritance?
What is the difference between public, protected, and private inheritance?
Why do we use the this pointer in C++?
Why do we use the this pointer in C++?
What is method overriding, and how does it relate to polymorphism?
What is method overriding, and how does it relate to polymorphism?
Which of the following correctly defines an abstract method in C++?
Which of the following correctly defines an abstract method in C++?
Which statement about an abstract class is true?
Which statement about an abstract class is true?
What does it mean to instantiate a class?
What does it mean to instantiate a class?
Why should classes that are meant to be inherited have a virtual destructor?
Why should classes that are meant to be inherited have a virtual destructor?
Which of the following best describes the Liskov Substitution Principle (LSP)?
Which of the following best describes the Liskov Substitution Principle (LSP)?
Which of the following correctly defines a namespace alias?
Which of the following correctly defines a namespace alias?
An abstract class can have non-pure virtual functions.
An abstract class can have non-pure virtual functions.
A class with at least one pure virtual function cannot be instantiated.
A class with at least one pure virtual function cannot be instantiated.
The global namespace in C++ refers to any namespace declared inside a function.
The global namespace in C++ refers to any namespace declared inside a function.
Function overloading means defining multiple functions with the same name but different return types.
Function overloading means defining multiple functions with the same name but different return types.
The Single Definition Rule (SDR) ensures that a function or variable can only be defined once in a program.
The Single Definition Rule (SDR) ensures that a function or variable can only be defined once in a program.
What is an abstract base class?
What is an abstract base class?
Why should a class intended for inheritance have a virtual destructor?
Why should a class intended for inheritance have a virtual destructor?
What is a function signature in C++?
What is a function signature in C++?
What is the difference between a declaration and a definition in C++?
What is the difference between a declaration and a definition in C++?
What is the purpose of a namespace alias?
What is the purpose of a namespace alias?
What is the purpose of the throw keyword in C++?
What is the purpose of the throw keyword in C++?
Which keyword is used to handle exceptions in C++?
Which keyword is used to handle exceptions in C++?
What is a runtime error in C++?
What is a runtime error in C++?
What happens when an exception is thrown but not caught?
What happens when an exception is thrown but not caught?
How does the call stack behave when an exception is thrown?
How does the call stack behave when an exception is thrown?
The try block is mandatory when using throw in C++.
The try block is mandatory when using throw in C++.
A destructor is always called before an exception is propagated up the call stack.
A destructor is always called before an exception is propagated up the call stack.
A catch block can catch multiple types of exceptions.
A catch block can catch multiple types of exceptions.
Global variable flags can be used to signal an error state without using exceptions.
Global variable flags can be used to signal an error state without using exceptions.
An exception can only be caught in the same function where it was thrown.
An exception can only be caught in the same function where it was thrown.
What is the purpose of the try block in C++?
What is the purpose of the try block in C++?
How does the call stack behave when an exception occurs?
How does the call stack behave when an exception occurs?
What happens if an exception is thrown inside a destructor?
What happens if an exception is thrown inside a destructor?
What is the advantage of using global variable flags for error handling?
What is the advantage of using global variable flags for error handling?
Flashcards
Pointer Comparison
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
Accessing Array Elements
The expression *(arr + 2)
accesses the element at index 2 of array arr
.
Variable address
Variable address
When a C++ program runs multiple times, the memory address of a variable may be different each time.
Dereferencing Nullptr
Dereferencing Nullptr
Signup and view all the flashcards
Garbage (in Garbage Collection)
Garbage (in Garbage Collection)
Signup and view all the flashcards
If-else-if vs. Switch
If-else-if vs. Switch
Signup and view all the flashcards
Purpose of Makefiles
Purpose of Makefiles
Signup and view all the flashcards
Blackbox vs. Whitebox Testing
Blackbox vs. Whitebox Testing
Signup and view all the flashcards
new Operators Return Type
new Operators Return Type
Signup and view all the flashcards
Location of Global Variables
Location of Global Variables
Signup and view all the flashcards
Memory Leak
Memory Leak
Signup and view all the flashcards
Address of a Null Pointer
Address of a Null Pointer
Signup and view all the flashcards
Memory Usage Determination
Memory Usage Determination
Signup and view all the flashcards
Pointer Scope
Pointer Scope
Signup and view all the flashcards
Class Definition
Class Definition
Signup and view all the flashcards
Stack Principle
Stack Principle
Signup and view all the flashcards
Local Variable Scope
Local Variable Scope
Signup and view all the flashcards
Dangling Pointer
Dangling Pointer
Signup and view all the flashcards
Preventing Memory Leaks
Preventing Memory Leaks
Signup and view all the flashcards
Referencing vs. De-referencing
Referencing vs. De-referencing
Signup and view all the flashcards
Free Store Location
Free Store Location
Signup and view all the flashcards
Constructor Syntax
Constructor Syntax
Signup and view all the flashcards
Deallocating Memory
Deallocating Memory
Signup and view all the flashcards
Private Keyword
Private Keyword
Signup and view all the flashcards
Local Variable Allocation
Local Variable Allocation
Signup and view all the flashcards
Default Constructor
Default Constructor
Signup and view all the flashcards
The delete usage
The delete usage
Signup and view all the flashcards
Public Access
Public Access
Signup and view all the flashcards
Memory Management of Vector
Memory Management of Vector
Signup and view all the flashcards