Time Allowed: 1 hour 30 minutes
Instructions:
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 small coding problem. Solve it by writing the answer clearly and concisely.
1. What is the primary advantage of using design patterns in software development?
a) They make the code execution faster.
b) They enforce the use of only one specific programming language.
c) They provide reusable solutions to commonly occurring problems in software design.
d) They replace the need for testing software.
2. In C++, which of the following correctly declares an array of five pointers to char
?
a) char *b[5];
b) char b[*5];
c) char (*b)[5];
d) char b[5*];
3. Given the class definition below, which design pattern is being implemented?
class Singleton {
private:
static Singleton* instance;
Singleton() {} // Private constructor
public:
static Singleton* getInstance() {
if (!instance) instance = new Singleton();
return instance;
}
};
a) Factory Pattern
b) Singleton Pattern
c) Observer Pattern
d) Adapter Pattern
4. Which of the following statements about pointers is correct?
a) int *p;
declares p
as a pointer to an integer.
b) p = &var;
stores the value of var
in p
.
c) p++
decreases the address stored in p
.
d) *p
gives the memory address stored in p
.
5. What is the purpose of malloc()
in C?
a) It is used to allocate memory dynamically during program execution.
b) It declares a pointer variable.
c) It automatically frees memory after use.
d) It prevents memory leaks.
6. What will the following statement output?
printf("%d", strcmp("apple", "banana"));
a) 0
b) A positive integer
c) A negative integer
d) A compiler error
7. Which Qt class is used to create a main application window?
a) QCoreApplication
b) QWidget
c) QMainWindow
d) QApplication
8. Which of the following correctly declares a function pointer?
a) int (*funcPtr)(int, int);
b) int *funcPtr(int, int);
c) int funcPtr(int*, int*);
d) (*int funcPtr)(int, int);
9. What does the delete
operator do in C++?
a) Deallocates memory previously allocated by new
.
b) Removes an element from an array.
c) Deletes a pointer variable from the stack.
d) Declares a destructor for a class.
10. The Adapter Pattern
is used to:
a) Create an interface to allow incompatible interfaces to work together.
b) Allow an object to notify other objects about state changes.
c) Ensure that only one instance of a class is created.
d) Define a blueprint for creating objects.
11. What is the output of the following program?
#include <iostream>
using namespace std;
void func(int &x) {
x *= 2;
}
int main() {
int a = 5;
func(a);
cout << a << endl;
return 0;
}
Answer:
12. What is the output of the following program?
#include <iostream>
using namespace std;
int main() {
char *words[] = {"Hello", "World", "C++"};
cout << words[1] << endl;
return 0;
}
Answer:
13. What will be the output of the following?
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40};
int *ptr = arr;
cout << *(ptr + 2) << endl;
return 0;
}
Answer:
Write a C++ function that dynamically allocates an integer array of size n
, fills it with values from 1
to n
, and then prints the values before freeing the memory.
Write your answer below (can be handwritten if taking the test physically).
#include <iostream>
using namespace std;
void allocateAndPrintArray(int n) {
int *arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
cout << arr[i] << " ";
}
cout << endl;
delete[] arr;
}
Once you complete the Practice Exam, upload your answers, and I will provide feedback on what you got right and wrong, explanations, and areas to focus on.