WorksheetsC++ Midterm 2
Total questions: 70
Worksheet time: 35mins
How can you modify an array using a range-based for loop?
By using a counter variable within the loop
By declaring the range variable as a reference using &
By using the range variable directly
By using a while loop instead
What is the default value of elements in a global array?
NULL
Undefined
-1
0
What will happen if an array index goes out of bounds in C++?
The compiler will catch the error at compile time
The behavior is undefined and may corrupt memory
The array will resize automatically
The program will terminate immediately with an error message
Which loop type is specifically designed to iterate through an array without using an index variable?
While loop
Do-while loop
Regular for loop
Range-based for loop
How can you initialize an array at the time of declaration?
int numbers[5] = 99;
int numbers{99, 98, 97, 96, 95};
int numbers[] = (99, 98, 97, 96, 95);
int numbers[5] = {99, 98, 97, 96, 95};
What does the address operator & do in C++?
Gets the address of a variable
Dereferences a pointer
Allocates memory dynamically
Declares a pointer variable
What does a pointer variable store?
The address of another variable
A memory block
A reference to an array
A value of an integer
What will the following code output?
The value stored in num
A random memory address
The address of num
Compilation error
What keyword was introduced in C++11 to represent a null pointer?
null
NULL
nullptr
void*
How can an array name be used in pointer arithmetic?
As a dynamically allocated pointer
As a constant pointer
As a dereferenced pointer
As a reference
Given the array int vals[] = {4, 7, 11};, what does *(vals + 2) return?
4
11
Compilation error
7
What happens when a pointer is incremented?
It moves to the next memory address based on the data type size
It decreases its stored value
It causes a segmentation fault
It points to the first element of the array
What does the following statement do?
Assigns a null value to the pointer
Declares a pointer variable but does not allocate memory
Allocates memory for an integer dynamically
Creates a pointer to a constant
What is the correct way to release dynamically allocated memory?
free(ptr);
deallocate(ptr);
delete ptr;
release ptr;
What does the following function do?
Swaps the values of x and y
Swaps memory addresses
Copies values of x and y
Returns the sum of x and y
What is a pointer to a constant?
A pointer whose address cannot be changed
A pointer that is dynamically allocated
A pointer that is initialized with nullptr
A pointer that points to a constant value
Why should a function not return a pointer to a local variable?
It leads to a syntax error
The local variable will be deallocated after the function exits
Pointers cannot be returned from functions
The value returned will be a null pointer
What will be the output of the following code?
15
Garbage value
10
5
Which of the following correctly declares a pointer to a float?
*float p;
float *p;
float p*;
pointer float p;
What does the new operator do in C++ when used with pointers?
Initializes a variable
Allocates memory in execution time
Deallocates memory
Allocates memory in compilation time
Which header file is required for character testing functions like isalpha() and isdigit() in C++?
<cstdlib>
<iostream>
<cstring>
<cctype>
What is the primary characteristic of a C-string in C++?
It is an object of the string class
It is always dynamically allocated
It must be explicitly converted to an integer to be used in mathematical operations
It is a sequence of characters stored in contiguous memory and terminated by a null character
Which function is used to convert a string to an integer in C++?
strconvert()
strtoi()
stoi()
to_string()
Which of the following functions appends one C-string to another?
strlen()
strcmp()
strcpy()
strcat()
What does the function toupper(ch) do?
Removes spaces from a string
Appends a character to the end of a string
Converts a lowercase letter to uppercase, or returns the input unchanged
Converts an uppercase letter to lowercase, or returns the input unchanged
What is the purpose of a struct in C++?
To allocate dynamic memory
To define a new function
To group multiple variables of different types under one name
To create a class with private members
How do you access a structure member in C++?
Using the -> operator
Using the * operator
Using the & operator
Using the . operator
Which of the following correctly initializes a structure variable?
None of the suggested
Both ways are ok
Student s = {123, "John"};
Student s; s.studentID = 123; s.name = "John";
Which operator is used to access structure members through a pointer?
& (ampersand)
-> (arrow)
. (dot)
* (asterisk)
What happens when you pass a structure by value to a function?
The function cannot access structure members
The function will generate an error
The function modifies the original structure
A copy of the structure is created
1. What is the correct way to access the `gpa` of the 4th element in an array of `Student` structures named `stuList`?
gpa.stuList[3]
stuList.gpa[3]
stuList[3].gpa
stuList[3]->gpa
Given a nested structure, which of the following accesses the city of a student named `s`?
Which is the preferred way to pass a structure to a function if the structure should not be modified and memory should be conserved?
Pass by reference
Pass by value
Pass by pointer
Pass by const reference
What will happen if you try to compare two structure variables directly using `==`?
It will compare the memory addresses
It results in a compiler error
It only compares the first member
It compares all their members automatically
Which of the following correctly declares a function that returns a structure of type `Student`?
Student getStudentData();
void getStudentData();
struct getStudentData(Student);
Student getStudentData(Student s);
How do you correctly access the `studentID` using a pointer to a `Student` structure?
(*stuPtr).studentID
studentID->stuPtr
*stuPtr.studentID
studentID.stuPtr
Consider the function below. Which statement is true about `tempStu`?
It returns a copy of the structure safely
It returns a reference to a local variable, which is unsafe
The structure must be dynamically allocated
It cannot return a structure
What is the purpose of using an array of structures instead of parallel arrays?
It allows dynamic memory allocation only
It avoids using loops
It allows grouping related data together
It uses more memory
Which of the following can be used to assign the address of a structure `stu1` to a structure pointer `stuPtr`?
stuPtr = stu1;
stuPtr->stu1 = address;
stuPtr = &stu1;
stu1 = &stuPtr;
If a function needs to change a structure’s member values, how should the structure be passed?
By value
Structures can't be passed to functions
By pointer or non-const reference
By const reference
What is the main characteristic of object-oriented programming?
It makes use of only global variables
It focuses on the process/actions that occur in a program
It does not support encapsulation
It is based on data and functions that operate on it
What is an instance of a class called?
Object
Attribute
Method
Structure
Which of the following is NOT a limitation of procedural programming?
Function hierarchies make modifications challenging
It enhances data security by restricting access to functions
Changing data structures may require modifying multiple functions
Programs become difficult to understand and maintain
What access specifier allows class members to be accessed only within the class?
Private
Public
Protected
Internal
What does the const keyword do when used with a member function?
It makes the function run faster
It ensures the function does not modify any member variables
It allows the function to modify only private members
It prevents the function from being called
Which of the following statements about access specifiers is true?
Private members can be accessed by any function
Access specifiers can appear multiple times in a class
Public members can only be accessed inside the class
If no access specifier is declared, the default is public
What is the purpose of a constructor in C++?
To overload operators in a class
To initialize an object when it is created
To destroy objects when they go out of scope
To allocate memory dynamically
Which of the following is true about a default constructor in C++?
It is automatically provided by the compiler if no constructor is defined
It must be declared as private
It must take at least one parameter
It cannot be overloaded
Which symbol is used to define a destructor in C++?
&
!
*
~
Which statement is true about method overloading in C++?
It allows create method with different number and type of arguments
It can be performed for several default constructor
Methods cannot be overloaded in C++
Overloaded methods must be defined outside a class
Which of the following statements about destructors is TRUE?
Destructors must be explicitly called to deallocate memory
Destructors do not have parameters
Destructors can return values
A class can have multiple destructors
What is an accessor function used for in a class?
To allocate memory dynamically
To modify private data members
To provide read-only access to private data members
To delete objects from memory
Which of the following is NOT a characteristic of an object?
Behavior
State
Identity
Hierarchy
What is an overloaded function in C++?
A function that has the same name but different parameters
A function that returns multiple values
A function that can only be used in inheritance
A function that is called multiple times in a loop
Why is encapsulation important in object-oriented programming?
It prevents code duplication
It restricts direct access to object data, improving security
It allows multiple inheritance
It eliminates the need for constructors
What is an instance variable in a class?
A variable shared among all objects of a class
A variable that is local to a function inside a class
A variable that cannot be accessed outside the class
A member variable in a class, where each object has its own copy
What is the purpose of a static member variable?
To prevent changes to a variable once it is declared
To ensure each object gets its own copy of the variable
To allow a function to access private data members
To share a single variable among all objects of a class
How is a static member function different from a non-static member function?
It can access both static and non-static member variables
It requires an object to be called
It cannot be defined inside the class
It can only access static member variables of a class
How do you define a static member function in a class?
By overloading the function
By defining it in the constructor of the class
By declaring the function outside the class
By using the static keyword before the return type
What is a friend function in C++?
A function that can access only public members of a class
A function that belongs to two or more classes
A function that only works inside a namespace
A function that can access private members of a class, but is not a member of the class itself
What does memberwise assignment do in C++?
Assigns one object to another by copying all member values from one object to another
Creates a deep copy of an object to prevent memory leaks
Copies the address of an object rather than its values
Prevents objects from being assigned to each other
What is the purpose of a copy constructor in C++?
To allocate dynamic memory for an object
To create a deep copy of an object
To free dynamically allocated memory in an object
To initialize an object with default values
What happens if a class does not define a copy constructor?
The compiler generates a constructor that performs a deep copy
The compiler generates a default copy constructor that performs a shallow copy
The class cannot be copied
The program will not compile
What problem can arise when copying an object that contains a pointer to dynamically allocated memory?
The copied object may point to the same memory location, causing unintended changes
The program automatically allocates new memory for the copied object
The copied object gets twice the memory allocation
The original object is deleted when the copy is created
How can a deep copy be implemented to avoid pointer-related issues?
By using a friend function to copy object data
By preventing object copying using private access specifiers
By using the default copy constructor provided by the compiler
By manually allocating new memory in the copy constructor and copying data
Which of the following best describes the relationship established by inheritance in C++?
"has a" relationship
"inherits a" relationship
"uses a" relationship
"is a" relationship
What does an object of a derived class contain?
Only accessible members from both classes
Only members defined in the derived class
Only public members of the base class
All members declared in the derived and base class
When an object of a derived class is created, which constructor is called first?
Base class constructor
Destructor of the derived class
Derived class constructor
It depends on the order of declaration
What is required when a base class has no default constructor and a derived class needs to be instantiated?
Pass arguments to the base class constructor from the derived class constructor
Ignore the base constructor
Redefine the base constructor in the derived class
Declare the derived class as abstract
What happens when a function is redefined in the derived class and called via a base class object?
A runtime error occurs
The base class function is called
The compiler chooses randomly
The derived class function is called
