This exam covers pointers, memory management, function pointers, pointer arithmetic, and design patterns.
Part I consists of multiple-choice questions. Select the most appropriate answer.
Part II consists of output prediction questions. Determine what the given code will output.
Part III consists of a coding problem. Solve it by writing the answer clearly and concisely.
You have 1 hour 30 minutes to complete this exam.
1. What does dereferencing a pointer mean?
a) Changing the pointer’s address.
b) Accessing the value stored at the memory address the pointer holds.
c) Allocating new memory for the pointer.
d) Creating a pointer to another pointer.
2. Which of the following correctly allocates memory for an integer in C++?
a) int p = malloc(sizeof(int));
b) int *p = new int;
c) int *p;
d) int p = *new int;
3. What is a memory leak?
a) When memory is freed before a program terminates.
b) When a pointer is initialized to NULL.
c) When dynamically allocated memory is not freed, causing memory usage to grow indefinitely.
d) When a pointer is deleted twice.
4. What will happen if you dereference a NULL pointer?
a) The program will terminate with an error.
b) The program will continue without issues.
c) The pointer will be set to a default value.
d) It will return an uninitialized value.
5. How can you prevent a dangling pointer?
a) Never delete dynamically allocated memory.
b) Set the pointer to NULL after deleting the allocated memory.
c) Always allocate memory before defining a pointer.
d) Use global variables instead of pointers.
6. Which of the following is NOT true about function pointers?
a) A function pointer can store the address of a function.
b) Function pointers can be passed as arguments to functions.
c) Function pointers must be initialized with NULL
.
d) Function pointers allow dynamic function calls.
7. Which design pattern is used to convert an existing interface into a different one?
a) Factory Pattern
b) Singleton Pattern
c) Adapter Pattern
d) Observer Pattern
8. What is pointer arithmetic used for?
a) Performing arithmetic operations on pointers themselves.
b) Moving a pointer to point to different elements in an array.
c) Converting pointers to integers.
d) Storing functions in pointers.
9. What does `` do in C++?
a) Deletes a single integer.
b) Deletes an array allocated with new[]
.
c) Deletes a structure pointer.
d) Deletes a function pointer.
10. What happens if you ``?
a) Undefined behavior.
b) The pointer is automatically assigned memory.
c) The pointer is deleted safely.
d) The compiler prevents this operation.
11. What will be the output of the following program?
#include <iostream>
using namespace std;
int main() {
int arr[] = {5, 10, 15, 20};
int *p = arr;
cout << *(p + 2) << endl;
return 0;
}
Answer:
12. What will be the output of the following program?
#include <iostream>
using namespace std;
void modify(int *p) {
*p = *p * 2;
}
int main() {
int a = 7;
modify(&a);
cout << a << endl;
return 0;
}
Answer:
13. What is the output of the following pointer arithmetic code?
#include <iostream>
using namespace std;
int main() {
int num = 100;
int *p = #
cout << *p + 2 << endl;
return 0;
}
Answer:
Write a C++ function that:
Dynamically allocates an array of integers of size n
.
Fills the array with values from 1
to n
.
Prints the values stored in the array.
Frees the allocated memory.
Write your answer below (can be handwritten if taking the test physically).
This Practice Exam is designed to test your understanding of pointers, memory management, function pointers, and design patterns in C++. Once you complete the exam, let me know, and I will evaluate your answers!