This exam consists of 25 multiple-choice questions, 15 output prediction questions, and 3 programming problems.
Select the best answer for multiple-choice questions.
Predict the correct output for the given C and C++ code snippets.
Write the required programs for the programming section.
You have 1 hour 30 minutes to complete this exam.
What is the correct syntax for defining a variable in C? a) int x = 5;
b) x = int 5;
c) integer x = 5;
d) var x = 5;
What is the correct way to include a standard library in C++? a) import <iostream>
b) #include <iostream>
c) library <iostream>
d) using library <iostream>
Which of the following is NOT a valid C data type? a) int
b) float
c) string
d) char
In C, which function is used to take user input? a) input()
b) scanf()
c) cin >>
d) get()
What does the following line of code do?
int *ptr = &x;
a) Declares a pointer to an integer variable x
. b) Assigns a value to x
. c) Creates an integer reference. d) None of the above.
How many times will the following loop execute?
for (int i = 0; i < 5; i++) {
printf("%d", i);
}
a) 4 b) 5 c) 6 d) Infinite
What will the following condition evaluate to?
int x = 10;
if (x == 10 && x > 5)
a) true
b) false
c) null
d) Compiler error
What is the output of printf("%d", 5 / 2);
in C? a) 2.5
b) 2
c) 3
d) Compiler error
What is the purpose of the return
statement in C++ functions? a) Terminates the program b) Exits the function and optionally returns a value c) Declares a function d) Calls another function
Which function is used to dynamically allocate memory in C? a) malloc()
b) new
c) allocate()
d) memset()
What happens when free(ptr);
is called in C? a) The pointer is deleted from memory. b) The allocated memory is freed but ptr
still points to it. c) The pointer is set to NULL
automatically. d) The program crashes.
Which of the following is a valid way to initialize a pointer in C++? a) int *p = new int(5);
b) int p = &5;
c) int *p; *p = 5;
d) ptr<int> p = 5;
Which keyword is used to define a class in C++? a) struct
b) class
c) object
d) define
What does the public
access specifier do? a) Hides class members from external access. b) Allows members to be accessible outside the class. c) Restricts access to only derived classes. d) Prevents object instantiation.
What is the output of cout << (5 > 2 ? "Yes" : "No");
? a) Yes
b) No
c) true
d) Compiler error
What does the this
pointer refer to in C++? a) A pointer to the current object b) A global variable c) A pointer to the parent class d) A memory allocation function
What does polymorphism allow in C++? a) Multiple functions to have the same name but different parameters b) Defining multiple objects in a class c) Preventing access to class members d) Allocating memory dynamically
How do you declare a destructor in C++? a) void ~ClassName();
b) ClassName::~ClassName();
c) ~ClassName();
d) delete ClassName();
What is the difference between struct
and class
in C++? a) struct
members are private by default, while class
members are public b) class
members are private by default, while struct
members are public c) They are identical d) struct
cannot have member functions
What does encapsulation do in C++? a) Groups data and methods together while restricting access b) Allows inheritance between classes c) Enables polymorphism d) Allocates memory dynamically
What is the correct syntax for inheritance in C++? a) class Derived : Base {}
b) class Base -> Derived {}
c) class Derived extends Base {}
d) inherit Base in Derived {}
Which of the following is NOT an advantage of using pointers? a) Efficient memory usage b) Direct memory manipulation c) Avoids memory leaks automatically d) Enables dynamic memory allocation
What does delete[] arr;
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
What happens if you delete
a pointer that was never allocated with new
? a) Undefined behavior b) The pointer is automatically assigned memory c) The pointer is deleted safely d) The compiler prevents this operation
What does operator overloading allow in C++? a) Using the same operator for different operations b) Allocating memory dynamically c) Preventing memory leaks d) Creating new operators
What will be the output of the following code?
int main() {
int x = 5;
printf("%d", x++);
printf("%d", ++x);
}
Predict the output:
int main() {
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d", *(ptr + 1));
}
What does the following program print?
int main() {
int num = 10;
int *ptr = #
*ptr = *ptr + 5;
printf("%d", num);
}
What is the output of the following?
int main() {
char str[] = "Hello";
printf("%c", *(str + 1));
}
What will this C program output?
int main() {
int i = 1;
while (i < 4) {
printf("%d ", i);
i++;
}
}
Predict the output:
#include <iostream>
using namespace std;
int main() {
int a = 10;
int &b = a;
b++;
cout << a;
}
What will this program print?
#include <iostream>
using namespace std;
void func(int &x) {
x *= 2;
}
int main() {
int num = 5;
func(num);
cout << num;
}
What is the output of this pointer arithmetic example?
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4};
int *ptr = arr;
cout << *(ptr + 2);
}
What does this loop output?
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 3; i++) {
cout << i << " ";
}
}
What will the following C++ program print?
#include <iostream>
using namespace std;
int main() {
int x = 10;
cout << (x > 5 ? "High" : "Low");
}
What is the output of this C++ function?
#include <iostream>
using namespace std;
void change(int *ptr) {
*ptr = 20;
}
int main() {
int a = 10;
change(&a);
cout << a;
}
Predict the output:
#include <iostream>
using namespace std;
int main() {
int x = 8;
cout << x / 2;
}
What will this print?
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10;
cout << (a > b ? a : b);
}
What will be the output of this program?
#include <iostream>
using namespace std;
int main() {
string str = "Hello";
cout << str.length();
}
What does this print?
#include <iostream>
using namespace std;
int main() {
char arr[] = "Exam";
cout << arr[2];
}
Greatest Common Divisor (GCD) Implementation
Write a function in both C and C++ that computes the Greatest Common Divisor (GCD) of two numbers using recursion.
Example:
int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
Dynamic Memory Allocation
Write a C++ program that:
Dynamically allocates an array of n
integers.
Accepts n
values from the user.
Finds and prints the maximum and minimum values in the array.
Frees the allocated memory.
Example:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int *arr = new int[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int max = arr[0], min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
cout << "Max: " << max << " Min: " << min << endl;
delete[] arr;
return 0;
}
Star Triangle Patterns
Write a program in both C and C++ that prints a right-aligned star triangle based on user input for the number of rows.
Example (C):
#include <stdio.h>
int main() {
int n;
printf("Enter number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) printf(" ");
for (int j = 1; j <= i; j++) printf("* ");
printf("\n");
}
return 0;
}
Example (C++):
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) cout << " ";
for (int j = 1; j <= i; j++) cout << "* ";
cout << endl;
}
return 0;
}
Multiple-Choice Questions: 25 × 2 = 50 points
Output Prediction Questions: 15 × 5 = 75 points
Programming Questions: 3 × 10 = 30 points
Total: 155 points
Good luck! 🚀 Let me know if you need additional explanations or practice problems!