wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

C++ Midterm 2

Total questions: 70

Worksheet time: 35mins

Name
Class
Date
1.

How can you modify an array using a range-based for loop?

a)

By using a counter variable within the loop

b)

By declaring the range variable as a reference using &

c)

By using the range variable directly

d)

By using a while loop instead

2.

What is the default value of elements in a global array?

a)

NULL

b)

Undefined

c)

-1

d)

0

3.

What will happen if an array index goes out of bounds in C++?

a)

The compiler will catch the error at compile time

b)

The behavior is undefined and may corrupt memory

c)

The array will resize automatically

d)

 

The program will terminate immediately with an error message

4.

Which loop type is specifically designed to iterate through an array without using an index variable?

a)

While loop

b)

Do-while loop

c)

Regular for loop

d)

Range-based for loop

5.

How can you initialize an array at the time of declaration?

a)

int numbers[5] = 99;

b)

int numbers{99, 98, 97, 96, 95};

c)

int numbers[] = (99, 98, 97, 96, 95);

d)

int numbers[5] = {99, 98, 97, 96, 95};

6.

What does the address operator & do in C++?

a)

Gets the address of a variable

b)

Dereferences a pointer

c)

Allocates memory dynamically

d)

Declares a pointer variable

7.

What does a pointer variable store?

a)

The address of another variable

b)

A memory block

c)

 

A reference to an array

d)

 

A value of an integer

8.

What will the following code output?

a)

The value stored in num

b)

A random memory address

c)

The address of num

d)

Compilation error

9.

What keyword was introduced in C++11 to represent a null pointer?

a)

null

b)

NULL

c)

nullptr

d)

void*

10.

How can an array name be used in pointer arithmetic?

a)

As a dynamically allocated pointer

b)

As a constant pointer

c)

As a dereferenced pointer

d)

As a reference

11.

Given the array int vals[] = {4, 7, 11};, what does *(vals + 2) return?

a)

4

b)

11

c)

Compilation error

d)

7

12.

What happens when a pointer is incremented?

a)

It moves to the next memory address based on the data type size

b)

It decreases its stored value

c)

It causes a segmentation fault

d)

It points to the first element of the array

13.

What does the following statement do?

a)

Assigns a null value to the pointer

b)

Declares a pointer variable but does not allocate memory

c)

Allocates memory for an integer dynamically

d)

Creates a pointer to a constant

14.

What is the correct way to release dynamically allocated memory?

a)

free(ptr);

b)

deallocate(ptr);

c)

delete ptr;

d)

release ptr;

15.

What does the following function do?

a)

Swaps the values of x and y

b)

Swaps memory addresses

c)

Copies values of x and y

d)

Returns the sum of x and y

16.

What is a pointer to a constant?

a)

A pointer whose address cannot be changed

b)

A pointer that is dynamically allocated

c)

A pointer that is initialized with nullptr

d)

A pointer that points to a constant value

17.

Why should a function not return a pointer to a local variable?

a)

It leads to a syntax error

b)

The local variable will be deallocated after the function exits

c)

Pointers cannot be returned from functions

d)

The value returned will be a null pointer

18.

What will be the output of the following code?

a)

15

b)

Garbage value

c)

10

d)

5

19.

Which of the following correctly declares a pointer to a float?

a)

*float p;

b)

float *p;

c)

float p*;

d)

pointer float p;

20.

What does the new operator do in C++ when used with pointers?

a)

Initializes a variable

b)

Allocates memory in execution time

c)

Deallocates memory

d)

Allocates memory in compilation time

21.

Which header file is required for character testing functions like isalpha() and isdigit() in C++?

a)

<cstdlib>

b)

<iostream>

c)

<cstring>

d)

<cctype>

22.

What is the primary characteristic of a C-string in C++?

a)

It is an object of the string class

b)

It is always dynamically allocated

c)

It must be explicitly converted to an integer to be used in mathematical operations

d)

It is a sequence of characters stored in contiguous memory and terminated by a null character

23.

Which function is used to convert a string to an integer in C++?

a)

strconvert()

b)

strtoi()

c)

stoi()

d)

to_string()

24.

Which of the following functions appends one C-string to another?

a)

strlen()

b)

strcmp()


c)

strcpy()

d)

strcat()

25.

What does the function toupper(ch) do?

a)

Removes spaces from a string

b)

Appends a character to the end of a string

c)

Converts a lowercase letter to uppercase, or returns the input unchanged

d)

Converts an uppercase letter to lowercase, or returns the input unchanged

26.

What is the purpose of a struct in C++?

a)

To allocate dynamic memory

b)

To define a new function

c)

To group multiple variables of different types under one name

d)

To create a class with private members

27.

How do you access a structure member in C++?

a)

Using the -> operator

b)

Using the * operator

c)

Using the & operator

d)

Using the . operator

28.

Which of the following correctly initializes a structure variable?

a)

None of the suggested

b)

Both ways are ok

c)

Student s = {123, "John"};

d)

Student s; s.studentID = 123; s.name = "John";

29.

Which operator is used to access structure members through a pointer?

a)

& (ampersand)

b)

-> (arrow)

c)

. (dot)

d)

* (asterisk)

30.

What happens when you pass a structure by value to a function?

a)

The function cannot access structure members

b)

The function will generate an error

c)

The function modifies the original structure

d)

A copy of the structure is created

31.

1. What is the correct way to access the `gpa` of the 4th element in an array of `Student` structures named `stuList`?

a)

gpa.stuList[3]

b)

stuList.gpa[3]

c)

stuList[3].gpa

d)

stuList[3]->gpa

32.

Given a nested structure, which of the following accesses the city of a student named `s`?

33.

Which is the preferred way to pass a structure to a function if the structure should not be modified and memory should be conserved?

a)

Pass by reference

b)

Pass by value

c)

Pass by pointer

d)

Pass by const reference

34.

What will happen if you try to compare two structure variables directly using `==`?

a)

It will compare the memory addresses

b)

It results in a compiler error

c)

It only compares the first member

d)

It compares all their members automatically

35.

Which of the following correctly declares a function that returns a structure of type `Student`?

a)

Student getStudentData();

b)

void getStudentData();

c)

struct getStudentData(Student);

d)

Student getStudentData(Student s);

36.

How do you correctly access the `studentID` using a pointer to a `Student` structure?

a)

(*stuPtr).studentID

b)

studentID->stuPtr

c)

*stuPtr.studentID

d)

studentID.stuPtr

37.

Consider the function below. Which statement is true about `tempStu`?

a)

It returns a copy of the structure safely

b)

It returns a reference to a local variable, which is unsafe

c)

The structure must be dynamically allocated

d)

It cannot return a structure

38.

What is the purpose of using an array of structures instead of parallel arrays?

a)

It allows dynamic memory allocation only

b)

It avoids using loops

c)

It allows grouping related data together

d)

It uses more memory

39.

Which of the following can be used to assign the address of a structure `stu1` to a structure pointer `stuPtr`?

a)

stuPtr = stu1;

b)

stuPtr->stu1 = address;

c)

stuPtr = &stu1;

d)

stu1 = &stuPtr;

40.

If a function needs to change a structure’s member values, how should the structure be passed?

a)

By value

b)

Structures can't be passed to functions

c)

By pointer or non-const reference

d)

By const reference

41.

What is the main characteristic of object-oriented programming?

a)

It makes use of only global variables

b)

It focuses on the process/actions that occur in a program

c)

It does not support encapsulation

d)

It is based on data and functions that operate on it

42.

What is an instance of a class called?

a)

Object

b)

Attribute

c)

Method

d)

Structure

43.

Which of the following is NOT a limitation of procedural programming?

a)

Function hierarchies make modifications challenging

b)

It enhances data security by restricting access to functions

c)

Changing data structures may require modifying multiple functions

d)

Programs become difficult to understand and maintain

44.

What access specifier allows class members to be accessed only within the class?

a)

Private

b)

Public

c)

Protected

d)

Internal

45.

What does the const keyword do when used with a member function?

a)

It makes the function run faster

b)

It ensures the function does not modify any member variables

c)

It allows the function to modify only private members

d)

It prevents the function from being called

46.

Which of the following statements about access specifiers is true?

a)

Private members can be accessed by any function

b)

Access specifiers can appear multiple times in a class

c)

Public members can only be accessed inside the class

d)

If no access specifier is declared, the default is public

47.

What is the purpose of a constructor in C++?

a)

To overload operators in a class

b)

To initialize an object when it is created

c)

To destroy objects when they go out of scope

d)

To allocate memory dynamically

48.

Which of the following is true about a default constructor in C++?

a)

It is automatically provided by the compiler if no constructor is defined

b)

It must be declared as private

c)

It must take at least one parameter

d)

It cannot be overloaded

49.

Which symbol is used to define a destructor in C++?

a)

&

b)

!

c)

*

d)

~

50.

Which statement is true about method overloading in C++?

a)

It allows create method with different number and type of arguments

b)

It can be performed for several default constructor

c)

Methods cannot be overloaded in C++

d)

Overloaded methods must be defined outside a class

51.

Which of the following statements about destructors is TRUE?

a)

Destructors must be explicitly called to deallocate memory

b)

Destructors do not have parameters

c)

Destructors can return values

d)

A class can have multiple destructors

52.

What is an accessor function used for in a class?

a)

To allocate memory dynamically

b)

To modify private data members

c)

To provide read-only access to private data members

d)

To delete objects from memory

53.

Which of the following is NOT a characteristic of an object?

a)

Behavior

b)

State

c)

Identity

d)

Hierarchy

54.

What is an overloaded function in C++?

a)

A function that has the same name but different parameters

b)

A function that returns multiple values

c)

A function that can only be used in inheritance

d)

A function that is called multiple times in a loop

55.

Why is encapsulation important in object-oriented programming?

a)

It prevents code duplication

b)

It restricts direct access to object data, improving security

c)

It allows multiple inheritance

d)

It eliminates the need for constructors

56.

What is an instance variable in a class?

a)

A variable shared among all objects of a class

b)

A variable that is local to a function inside a class

c)

A variable that cannot be accessed outside the class

d)

A member variable in a class, where each object has its own copy

57.

What is the purpose of a static member variable?

a)

To prevent changes to a variable once it is declared

b)

To ensure each object gets its own copy of the variable

c)

To allow a function to access private data members

d)

To share a single variable among all objects of a class

58.

How is a static member function different from a non-static member function?

a)

It can access both static and non-static member variables

b)

It requires an object to be called

c)

It cannot be defined inside the class

d)

It can only access static member variables of a class

59.

How do you define a static member function in a class?

a)

By overloading the function

b)

By defining it in the constructor of the class

c)

By declaring the function outside the class

d)

By using the static keyword before the return type

60.

What is a friend function in C++?

a)

A function that can access only public members of a class

b)

A function that belongs to two or more classes

c)

A function that only works inside a namespace

d)

A function that can access private members of a class, but is not a member of the class itself

61.

What does memberwise assignment do in C++?

a)

Assigns one object to another by copying all member values from one object to another

b)

Creates a deep copy of an object to prevent memory leaks

c)

Copies the address of an object rather than its values

d)

Prevents objects from being assigned to each other

62.

What is the purpose of a copy constructor in C++?

a)

To allocate dynamic memory for an object

b)

To create a deep copy of an object

c)

To free dynamically allocated memory in an object

d)

To initialize an object with default values

63.

What happens if a class does not define a copy constructor?

a)

The compiler generates a constructor that performs a deep copy

b)

The compiler generates a default copy constructor that performs a shallow copy

c)

The class cannot be copied

d)

The program will not compile

64.

What problem can arise when copying an object that contains a pointer to dynamically allocated memory?

a)

The copied object may point to the same memory location, causing unintended changes

b)

The program automatically allocates new memory for the copied object

c)

The copied object gets twice the memory allocation

d)

The original object is deleted when the copy is created

65.

How can a deep copy be implemented to avoid pointer-related issues?

a)

By using a friend function to copy object data

b)

By preventing object copying using private access specifiers

c)

By using the default copy constructor provided by the compiler

d)

By manually allocating new memory in the copy constructor and copying data

66.

Which of the following best describes the relationship established by inheritance in C++?

a)

"has a" relationship

b)

"inherits a" relationship

c)

"uses a" relationship

d)

"is a" relationship

67.

What does an object of a derived class contain?

a)

Only accessible members from both classes

b)

Only members defined in the derived class

c)

Only public members of the base class

d)

All members declared in the derived and base class

68.

When an object of a derived class is created, which constructor is called first?

a)

Base class constructor

b)

Destructor of the derived class

c)

Derived class constructor

d)

It depends on the order of declaration

69.

What is required when a base class has no default constructor and a derived class needs to be instantiated?

a)

Pass arguments to the base class constructor from the derived class constructor

b)

Ignore the base constructor

c)

Redefine the base constructor in the derived class

d)

Declare the derived class as abstract

70.

What happens when a function is redefined in the derived class and called via a base class object?

a)

A runtime error occurs

b)

The base class function is called

c)

The compiler chooses randomly

d)

The derived class function is called