CSE 240 FINAL EXAM MOCK TEST WITH
ANSWER RATIONALES 2026
◉ Which C/C++ operations will acquire memory from heap? Select
all that apply.
Answer: - new
- malloc
◉ You do not need to garbage-collect the memory, if it comes from
(Select all that apply)
Answer: - global memory
- stack memory
- static memory
◉ What is the key difference between a static variable and a global
variable?
Answer: They have different visibility.
◉ Given the snippet of code:
int x = 5;
int bar(int j)
{
,int *k = 0, m = 5;
k = &m;
return (j+m);
}
void main(void)
{
static int i =0;
i++;
i = bar(i) + x;
}
Which variables obtain their memory from the stack? Select all that
apply.
Answer: k, m, j
◉ What is the best way of deleting an array created by "p = new
StructType[size];" in C++?
Answer: delete[] p;
◉ What memory must be garbage-collected by a destructor?
Answer: heap memory created in the constructors in the same class
◉ What is the best way of deleting a linked list of objects in C++?
,Answer: Use a loop to delete every object in the linked list.
◉ What is the best way of deleting all the nodes in a binary tree
pointed to by the root pointer?
Answer: Traverse the tree and delete each node individually.
◉ In the implementation of the getMax() function in the PriQueue
class, we need to read and write the variables "front" and "rear",
which are defined as protected members in the base class Queue.
Are the functions in the derived class allowed to access the protected
members in the base class?
Answer: Yes, always.
◉ If a class needs to inherit from multiple parent classes, the parent
classes must
Answer: be inherited as virtual classes.
◉ Defining a virtual function in class means that the function
Answer: can be redefined in its child classes.
◉ If you declare a pointer to the object of the parent class and use
the pointer to access the object of a child class, you can access
Answer: the members that exist in the parent class.
, ◉ Assume that Publication is the root class of an inheritance tree.
You want to form a linked list of different publications in the
inheritance tree, including Book, Report, Newspaper, etc. What is the
best way to create a linked list using PublListNode and Publication
classes?
Answer: The PublListNode class contains the Publication class.
◉ What type casting mechanism should be used if you want to
change pointer type for pointing to a different object in an
inheritance hierarchy?
Answer: dynamic_cast
◉ It does not cause memory (storage) overhead to create multiple
Answer: names (alias) for a variable.
◉ Given the following snippet of C++ code:
string name = "Hello";
ofstream myFile;
myFile.open(myFile);
myFile << name;myFile.close();
What does it do?
Answer: It saves the word Hello into a file in the file system.
ANSWER RATIONALES 2026
◉ Which C/C++ operations will acquire memory from heap? Select
all that apply.
Answer: - new
- malloc
◉ You do not need to garbage-collect the memory, if it comes from
(Select all that apply)
Answer: - global memory
- stack memory
- static memory
◉ What is the key difference between a static variable and a global
variable?
Answer: They have different visibility.
◉ Given the snippet of code:
int x = 5;
int bar(int j)
{
,int *k = 0, m = 5;
k = &m;
return (j+m);
}
void main(void)
{
static int i =0;
i++;
i = bar(i) + x;
}
Which variables obtain their memory from the stack? Select all that
apply.
Answer: k, m, j
◉ What is the best way of deleting an array created by "p = new
StructType[size];" in C++?
Answer: delete[] p;
◉ What memory must be garbage-collected by a destructor?
Answer: heap memory created in the constructors in the same class
◉ What is the best way of deleting a linked list of objects in C++?
,Answer: Use a loop to delete every object in the linked list.
◉ What is the best way of deleting all the nodes in a binary tree
pointed to by the root pointer?
Answer: Traverse the tree and delete each node individually.
◉ In the implementation of the getMax() function in the PriQueue
class, we need to read and write the variables "front" and "rear",
which are defined as protected members in the base class Queue.
Are the functions in the derived class allowed to access the protected
members in the base class?
Answer: Yes, always.
◉ If a class needs to inherit from multiple parent classes, the parent
classes must
Answer: be inherited as virtual classes.
◉ Defining a virtual function in class means that the function
Answer: can be redefined in its child classes.
◉ If you declare a pointer to the object of the parent class and use
the pointer to access the object of a child class, you can access
Answer: the members that exist in the parent class.
, ◉ Assume that Publication is the root class of an inheritance tree.
You want to form a linked list of different publications in the
inheritance tree, including Book, Report, Newspaper, etc. What is the
best way to create a linked list using PublListNode and Publication
classes?
Answer: The PublListNode class contains the Publication class.
◉ What type casting mechanism should be used if you want to
change pointer type for pointing to a different object in an
inheritance hierarchy?
Answer: dynamic_cast
◉ It does not cause memory (storage) overhead to create multiple
Answer: names (alias) for a variable.
◉ Given the following snippet of C++ code:
string name = "Hello";
ofstream myFile;
myFile.open(myFile);
myFile << name;myFile.close();
What does it do?
Answer: It saves the word Hello into a file in the file system.