Midterm Practice Exam 1

C & C++ Midterm Practice Exam

Instructions:

  • 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.


Part I: Multiple-Choice Questions (Each question is worth 2 points)

Basic Concepts & Syntax

  1. 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;

  2. 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>

  3. Which of the following is NOT a valid C data type? a) int b) float c) string d) char

  4. In C, which function is used to take user input? a) input() b) scanf() c) cin >> d) get()

  5. 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.

Control Structures & Functions

  1. 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

  2. What will the following condition evaluate to?

    int x = 10;
    if (x == 10 && x > 5)

    a) true b) false c) null d) Compiler error

  3. What is the output of printf("%d", 5 / 2); in C? a) 2.5 b) 2 c) 3 d) Compiler error

  4. 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

Memory & Pointers

  1. Which function is used to dynamically allocate memory in C? a) malloc() b) new c) allocate() d) memset()

  2. 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.

  3. 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;

Object-Oriented Programming (C++)

  1. Which keyword is used to define a class in C++? a) struct b) class c) object d) define

  2. 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.

  3. What is the output of cout << (5 > 2 ? "Yes" : "No");? a) Yes b) No c) true d) Compiler error

  4. 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

  5. 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

  6. How do you declare a destructor in C++? a) void ~ClassName(); b) ClassName::~ClassName(); c) ~ClassName(); d) delete ClassName();

  7. 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

  8. 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

  9. 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 {}

  10. 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

  11. 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

  12. 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

  13. 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

Part II: Output Prediction Questions (Each question is worth 5 points)

C Output Questions

  1. What will be the output of the following code?

int main() {
int x = 5;
printf("%d", x++);
printf("%d", ++x);
}
  1. Predict the output:

int main() {
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d", *(ptr + 1));
}
  1. What does the following program print?

int main() {
int num = 10;
int *ptr = &num;
*ptr = *ptr + 5;
printf("%d", num);
}
  1. What is the output of the following?

int main() {
char str[] = "Hello";
printf("%c", *(str + 1));
}
  1. What will this C program output?

int main() {
int i = 1;
while (i < 4) {
printf("%d ", i);
i++;
}
}

C++ Output Questions

  1. Predict the output:

#include <iostream>
using namespace std;
int main() {
int a = 10;
int &b = a;
b++;
cout << a;
}
  1. 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;
}
  1. 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);
}
  1. What does this loop output?

#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 3; i++) {
cout << i << " ";
}
}
  1. What will the following C++ program print?

#include <iostream>
using namespace std;
int main() {
int x = 10;
cout << (x > 5 ? "High" : "Low");
}
  1. 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;
}
  1. Predict the output:

#include <iostream>
using namespace std;
int main() {
int x = 8;
cout << x / 2;
}
  1. What will this print?

#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10;
cout << (a > b ? a : b);
}
  1. What will be the output of this program?

#include <iostream>
using namespace std;
int main() {
string str = "Hello";
cout << str.length();
}
  1. What does this print?

#include <iostream>
using namespace std;
int main() {
char arr[] = "Exam";
cout << arr[2];
}

Part III: Programming Questions (Each question is worth 10 points)

  1. 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);
    }
  1. Dynamic Memory Allocation

  • Write a C++ program that:

    1. Dynamically allocates an array of n integers.

    2. Accepts n values from the user.

    3. Finds and prints the maximum and minimum values in the array.

    4. 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;
    }
  1. 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;
    }

Scoring System:

  • 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!

robot