Font size
WorksheetsC++ Arrays, Strings, Pointers, and OOP — Worksheet Extraction
Total questions: 99
Worksheet time: 50mins
Which describes an array in C++?
A group of same-type elements in contiguous memory
A single variable
A runtime-resizing container
A file structure
What is the index of the first element in a C++ array?
1
-1
Depends on compiler
0
What happens when accessing arr[5] in an array of size 5?
Out-of-bounds access
Auto resizing occurs
Compiler error
The program stops immediately
Which is a valid array declaration?
int a(5);
int a = [5];
int a{5};
int a[5] = {1,2,3,4,5};
How many elements are in int foo[] = {1,2,3,4,5}?
5
4
6
Undefined
Which defines a two-dimensional array?
int a();
int a[3][4];
int a{3,4};
array
What is actually passed to a function when an array is used as a parameter?
Entire array copy
Address of first element
Last index
Array size
What does arr[3] mean?
Declares an array
Declares a pointer
Accesses the 4th element
Creates a reference
What results from exceeding array bounds in C++?
Undefined behavior
Guaranteed crash
Automatic correction
Index loops to zero
What does int foo[5] = {}; do?
Leaves garbage values
Fills with random values
Allocates no memory
Initializes all elements to 0
What is automatically added to a C-string literal?
'+'
'='
'\0'
'&'
Which is NOT allowed for character arrays?
s[0] = 'H';
s[1] = 'i';
s = "Hi";
s[2] = '\0';
Which is a valid C-string initialization?
char s("Hi");
char s[] = {'H','i','\0'};
char s[] = (Hi);
char s = "Hi";
What happens if '\0' is missing in a C-string?
Reads past memory
Converts to string class
Adds null automatically
Becomes empty
Which correctly stores a string in a char array?
string s = {'H','i'};
char s[] = {'H','i'};
char s[] = Hi;
char s[5] = "Hi";
Which is true about C-strings?
Fixed size at compile time
They resize automatically
They don't require null terminator
They are always dynamic
Which function returns a C-string version of a std::string?
getline()
c_str()
read()
fetch()
What does cin.get(str,100,'\n'); do?
Reads a single word
Reads characters until newline
Clears the buffer
Writes to the file
A string literal such as "Hello" is stored as a _____.
Null-terminated character array
dynamic string object
pointer constant
numerical code
Which statement about char arrays is correct?
Their size is fixed once declared
They expand like vectors
They auto-append more memory
They assign strings directly
Which operator is used to access individual characters in a char array?
.
[]
->
*
How can you copy one C-string into another?
str1 = str2;
strcpy(str1, str2);
str1.copy(str2);
str1 = &str2;
Which function returns the length of a C-string (excluding the null character)?
length()
strlen()
size()
count()
What happens if you write past the null character of a C-string?
Undefined behavior
Automatically resizes
Throws a compile-time error
Converts to std::string
Which statement about char s[] = "Hello"; is true?
It creates a string object
The array contains 6 elements including '\0'
The array contains only 5 elements
s can be reassigned directly to another string
What does a pointer store?
A data type
A variable name
A computed value
A memory address
Which pointer type can point to any data type?
void*
int*
float*
double*
A pointer with an uninitialized random address is called a _____.
null pointer
wild pointer
constant pointer
void pointer
A pointer referring to freed or invalid memory is a _____.
null pointer
dangling pointer
safe pointer
double pointer
Which pointer arithmetic is valid?
ptr + 1
ptr / ptr
ptr * ptr
ptr % 3
Which keyword allocates dynamic memory in C++?
malloc
calloc
alloc
new
What is a pointer to a pointer called?
Twin pointer
Root pointer
Double pointer
Multi-pointer
What do smart pointers do?
Automatically free memory
Disable dereferencing
Increase pointer size
Modify memory layout
What is the size of a pointer on a 64-bit system?
2 bytes
4 bytes
8 bytes
Depends on data type
What must be checked before dereferencing a pointer?
That it is valid and initialized
That it is converted to int
That it points to NULL
That its address has changed
Which operator is used to access the value stored at a pointer’s address?
&
*
->
%
What does the & operator do when applied to a variable?
Creates a pointer
Returns the variable’s memory address
Dereferences a pointer
Allocates memory
How do you declare a pointer to an integer variable?
int ptr;
int* ptr;
ptr int*;
pointer
Which of the following is a correct way to dynamically allocate an array of 10 integers?
int arr[10];
int* arr = new int[10];
int arr = new int(10);
int arr = malloc(10);
What happens when delete is used on a pointer?
The pointer is removed
Memory is freed but pointer still holds the address
Pointer is automatically set to null
Pointer size doubles
What keyword is used to define a class in C++?
object
class
structure
define
Which of the following best describes a class?
A function
A loop
A user-defined data type
A built-in data type
What is an object in C++?
A variable
An instance of a class
A function
A pointer
Which operator is used to access class members through an object?
.
->
::
:
What is the main purpose of a constructor?
Destroy an object
Store methods
Access private data
Initialize objects automatically
Which statement is true about access specifiers?
They define how class members are accessed
They declare variables
They store loops
They include headers
What happens if no access specifier is provided in a class?
Members are public
Members are private
Members are protected
Members are static
Which of the following keywords is used to allow inheritance?
:
inherits
extends
->
What is encapsulation in C++?
Hiding data using private members
Writing multiple constructors
Defining a class inside another class
Overloading operators
What is polymorphism?
Using multiple variables
Hiding data
Having many forms of a function or class
Using multiple headers
What type of function is automatically called when an object goes out of scope?
Constructor
Destructor
Friend function
Virtual function
How do you declare a member function outside the class definition?
void className::functionName();
void functionName::className();
className::void functionName();
functionName::void className();
Which keyword allows a class to access private members of another class?
friend
protected
public
virtual
Which feature allows a base class pointer to call derived class functions?
Encapsulation
Inheritance
Polymorphism
Constructor
What does the private access specifier do?
Members are accessible only within the class
Members are accessible everywhere
Members are accessible to derived classes only
Members cannot store values
What keyword is used to make a function virtual?
override
static
friend
virtual
What happens when a class is inherited privately?
Public members become private
Private members become public
Protected members disappear
No inheritance occurs
What is constructor overloading?
Multiple constructors with different parameters
Having one constructor only
Overriding base constructor
Using destructor as constructor
What symbol is used with the scope resolution operator?
::
->
.
:
Which access specifier allows access only within the same class?
public
private
protected
static
Which access specifier allows access by derived classes?
private
protected
static
const
What does the term “base class” mean?
The class that inherits
The class being inherited from
The final derived class
The class with constructors
Which keyword allows a derived class to override a base class function?
override
const
friend
final
What operator is used to access members through a pointer to an object?
.
::
:
->
What is type conversion in C++?
Changing a class
Converting one data type to another
Creating a variable
Declaring a function
Which of the following is NOT a type of type conversion?
Explicit
Implicit
Automatic
Manual
Implicit type conversion is also known as:
Casting
Coercion
Overriding
Transformation
Explicit type conversion in C++ is called:
Type casting
Overloading
Deriving
Defining
Which cast operator is used for compile-time conversions?
reinterpret_cast
const_cast
dynamic_cast
static_cast
Which cast operator is used for runtime type conversions?
dynamic_cast
static_cast
const_cast
reinterpret_cast
Which cast operator removes const or volatile qualifiers?
static_cast
const_cast
reinterpret_cast
dynamic_cast
Which cast is considered the most dangerous and should be avoided unless necessary?
const_cast
reinterpret_cast
dynamic_cast
static_cast
What happens when you remove const and modify the original variable?
Undefined behavior
Program runs normally
Compile-time error
Type safety ensured
What is a risk of type conversion?
Data loss and undefined behavior
Code optimization
Faster execution
Stronger typing
Which of the following is a safe way to convert a float to an int?
int x = static_cast
int x = reinterpret_cast
int x = const_cast
int x = dynamic_cast
What is the result of implicit conversion from int to double?
Truncation of decimal part
Automatic widening without data loss
Compile-time error
Undefined behavior
Which of the following allows checking at runtime if a base pointer actually points to a derived object?
static_cast
dynamic_cast
reinterpret_cast
const_cast
Which cast operator can reinterpret any pointer type as another pointer type?
reinterpret_cast
const_cast
dynamic_cast
static_cast
When should explicit type casting be used instead of implicit conversion?
When you want automatic conversion
When compiler cannot convert types safely
When defining variables
When using default constructors
What is the purpose of exception handling in C++?
Faster execution
Memory optimization
Input validation only
Handling runtime errors safely
Which keyword is used to throw an exception?
throw
raise
error
catch
Which block must follow every try block?
finally
catch
except
raise
What happens when an exception is thrown with no matching catch block?
Program ignores it
Program terminates
Error automatically fixed
Control returns to main
Which type of exception can be thrown?
Any data type
Only int
Only string
Only std::exception
What is the purpose of the catch(...) handler?
Rethrow exception
Catch integer errors only
Catch string errors only
Catch any type of exception
What does the std::exception::what() function return?
The line number
The variable name
A C-string error message
Exception ID
Which statement rethrows the current exception?
throw
raise()
throw current
rethrow()
What is stack unwinding?
Resetting the stack pointer
Reloading memory
Destroying automatic objects during exception propagation
Halting execution
What occurs when multiple catch blocks are used?
First matching type handles the exception
All catch blocks run
Only the last one runs
Catch order does not matter
What symbol begins every preprocessor directive?
@
#
$
%
Which directive defines a constant?
#include
#define
#ifdef
#pragma
Which directive prevents multiple inclusions of a header file?
#error
#line
#undef
#ifdef
What does #include do?
Inserts the content of another file
Creates a variable
Compiles a separate program
Allocates memory
Which directive removes the definition of a macro?
#line
#pragma
#elif
#undef
Which directive checks if a macro is defined?
#ifdef
#ifnot
#exists
#macro
What is the purpose of #pragma?
Create functions
Remove macros
Provide compiler-specific instructions
Convert code to assembly
What does #if 0 typically do?
Disable a block of code
Turn on debug mode
Clear memory
Print a value
Which directive ends an #if conditional block?
#stop
#macroend
#endif
#finish
Which preprocessor directive includes standard library headers like
#define
#pragma
#ifdef
#include
