wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

C++ Arrays, Strings, Pointers, and OOP — Worksheet Extraction

Total questions: 99

Worksheet time: 50mins

Name
Class
Date
1.

Which describes an array in C++?

a)

A group of same-type elements in contiguous memory

b)

A single variable

c)

A runtime-resizing container

d)

A file structure

2.

What is the index of the first element in a C++ array?

a)

1

b)

-1

c)

Depends on compiler

d)

0

3.

What happens when accessing arr[5] in an array of size 5?

a)

Out-of-bounds access

b)

Auto resizing occurs

c)

Compiler error

d)

The program stops immediately

4.

Which is a valid array declaration?

a)

int a(5);

b)

int a = [5];

c)

int a{5};

d)

int a[5] = {1,2,3,4,5};

5.

How many elements are in int foo[] = {1,2,3,4,5}?

a)

5

b)

4

c)

6

d)

Undefined

6.

Which defines a two-dimensional array?

a)

int a();

b)

int a[3][4];

c)

int a{3,4};

d)

array a;

7.

What is actually passed to a function when an array is used as a parameter?

a)

Entire array copy

b)

Address of first element

c)

Last index

d)

Array size

8.

What does arr[3] mean?

a)

Declares an array

b)

Declares a pointer

c)

Accesses the 4th element

d)

Creates a reference

9.

What results from exceeding array bounds in C++?

a)

Undefined behavior

b)

Guaranteed crash

c)

Automatic correction

d)

Index loops to zero

10.

What does int foo[5] = {}; do?

a)

Leaves garbage values

b)

Fills with random values

c)

Allocates no memory

d)

Initializes all elements to 0

11.

What is automatically added to a C-string literal?

a)

'+'

b)

'='

c)

'\0'

d)

'&'

12.

Which is NOT allowed for character arrays?

a)

s[0] = 'H';

b)

s[1] = 'i';

c)

s = "Hi";

d)

s[2] = '\0';

13.

Which is a valid C-string initialization?

a)

char s("Hi");

b)

char s[] = {'H','i','\0'};

c)

char s[] = (Hi);

d)

char s = "Hi";

14.

What happens if '\0' is missing in a C-string?

a)

Reads past memory

b)

Converts to string class

c)

Adds null automatically

d)

Becomes empty

15.

Which correctly stores a string in a char array?

a)

string s = {'H','i'};

b)

char s[] = {'H','i'};

c)

char s[] = Hi;

d)

char s[5] = "Hi";

16.

Which is true about C-strings?

a)

Fixed size at compile time

b)

They resize automatically

c)

They don't require null terminator

d)

They are always dynamic

17.

Which function returns a C-string version of a std::string?

a)

getline()

b)

c_str()

c)

read()

d)

fetch()

18.

What does cin.get(str,100,'\n'); do?

a)

Reads a single word

b)

Reads characters until newline

c)

Clears the buffer

d)

Writes to the file

19.

A string literal such as "Hello" is stored as a _____.

a)

Null-terminated character array

b)

dynamic string object

c)

pointer constant

d)

numerical code

20.

Which statement about char arrays is correct?

a)

Their size is fixed once declared

b)

They expand like vectors

c)

They auto-append more memory

d)

They assign strings directly

21.

Which operator is used to access individual characters in a char array?

a)

.

b)

[]

c)

->

d)

*

22.

How can you copy one C-string into another?

a)

str1 = str2;

b)

strcpy(str1, str2);

c)

str1.copy(str2);

d)

str1 = &str2;

23.

Which function returns the length of a C-string (excluding the null character)?

a)

length()

b)

strlen()

c)

size()

d)

count()

24.

What happens if you write past the null character of a C-string?

a)

Undefined behavior

b)

Automatically resizes

c)

Throws a compile-time error

d)

Converts to std::string

25.

Which statement about char s[] = "Hello"; is true?

a)

It creates a string object

b)

The array contains 6 elements including '\0'

c)

The array contains only 5 elements

d)

s can be reassigned directly to another string

26.

What does a pointer store?

a)

A data type

b)

A variable name

c)

A computed value

d)

A memory address

27.

Which pointer type can point to any data type?

a)

void*

b)

int*

c)

float*

d)

double*

28.

A pointer with an uninitialized random address is called a _____.

a)

null pointer

b)

wild pointer

c)

constant pointer

d)

void pointer

29.

A pointer referring to freed or invalid memory is a _____.

a)

null pointer

b)

dangling pointer

c)

safe pointer

d)

double pointer

30.

Which pointer arithmetic is valid?

a)

ptr + 1

b)

ptr / ptr

c)

ptr * ptr

d)

ptr % 3

31.

Which keyword allocates dynamic memory in C++?

a)

malloc

b)

calloc

c)

alloc

d)

new

32.

What is a pointer to a pointer called?

a)

Twin pointer

b)

Root pointer

c)

Double pointer

d)

Multi-pointer

33.

What do smart pointers do?

a)

Automatically free memory

b)

Disable dereferencing

c)

Increase pointer size

d)

Modify memory layout

34.

What is the size of a pointer on a 64-bit system?

a)

2 bytes

b)

4 bytes

c)

8 bytes

d)

Depends on data type

35.

What must be checked before dereferencing a pointer?

a)

That it is valid and initialized

b)

That it is converted to int

c)

That it points to NULL

d)

That its address has changed

36.

Which operator is used to access the value stored at a pointer’s address?

a)

&

b)

*

c)

->

d)

%

37.

What does the & operator do when applied to a variable?

a)

Creates a pointer

b)

Returns the variable’s memory address

c)

Dereferences a pointer

d)

Allocates memory

38.

How do you declare a pointer to an integer variable?

a)

int ptr;

b)

int* ptr;

c)

ptr int*;

d)

pointer ptr;

39.

Which of the following is a correct way to dynamically allocate an array of 10 integers?

a)

int arr[10];

b)

int* arr = new int[10];

c)

int arr = new int(10);

d)

int arr = malloc(10);

40.

What happens when delete is used on a pointer?

a)

The pointer is removed

b)

Memory is freed but pointer still holds the address

c)

Pointer is automatically set to null

d)

Pointer size doubles

41.

What keyword is used to define a class in C++?

a)

object

b)

class

c)

structure

d)

define

42.

Which of the following best describes a class?

a)

A function

b)

A loop

c)

A user-defined data type

d)

A built-in data type

43.

What is an object in C++?

a)

A variable

b)

An instance of a class

c)

A function

d)

A pointer

44.

Which operator is used to access class members through an object?

a)

.

b)

->

c)

::

d)

:

45.

What is the main purpose of a constructor?

a)

Destroy an object

b)

Store methods

c)

Access private data

d)

Initialize objects automatically

46.

Which statement is true about access specifiers?

a)

They define how class members are accessed

b)

They declare variables

c)

They store loops

d)

They include headers

47.

What happens if no access specifier is provided in a class?

a)

Members are public

b)

Members are private

c)

Members are protected

d)

Members are static

48.

Which of the following keywords is used to allow inheritance?

a)

:

b)

inherits

c)

extends

d)

->

49.

What is encapsulation in C++?

a)

Hiding data using private members

b)

Writing multiple constructors

c)

Defining a class inside another class

d)

Overloading operators

50.

What is polymorphism?

a)

Using multiple variables

b)

Hiding data

c)

Having many forms of a function or class

d)

Using multiple headers

51.

What type of function is automatically called when an object goes out of scope?

a)

Constructor

b)

Destructor

c)

Friend function

d)

Virtual function

52.

How do you declare a member function outside the class definition?

a)

void className::functionName();

b)

void functionName::className();

c)

className::void functionName();

d)

functionName::void className();

53.

Which keyword allows a class to access private members of another class?

a)

friend

b)

protected

c)

public

d)

virtual

54.

Which feature allows a base class pointer to call derived class functions?

a)

Encapsulation

b)

Inheritance

c)

Polymorphism

d)

Constructor

55.

What does the private access specifier do?

a)

Members are accessible only within the class

b)

Members are accessible everywhere

c)

Members are accessible to derived classes only

d)

Members cannot store values

56.

What keyword is used to make a function virtual?

a)

override

b)

static

c)

friend

d)

virtual

57.

What happens when a class is inherited privately?

a)

Public members become private

b)

Private members become public

c)

Protected members disappear

d)

No inheritance occurs

58.

What is constructor overloading?

a)

Multiple constructors with different parameters

b)

Having one constructor only

c)

Overriding base constructor

d)

Using destructor as constructor

59.

What symbol is used with the scope resolution operator?

a)

::

b)

->

c)

.

d)

:

60.

Which access specifier allows access only within the same class?

a)

public

b)

private

c)

protected

d)

static

61.

Which access specifier allows access by derived classes?

a)

private

b)

protected

c)

static

d)

const

62.

What does the term “base class” mean?

a)

The class that inherits

b)

The class being inherited from

c)

The final derived class

d)

The class with constructors

63.

Which keyword allows a derived class to override a base class function?

a)

override

b)

const

c)

friend

d)

final

64.

What operator is used to access members through a pointer to an object?

a)

.

b)

::

c)

:

d)

->

65.

What is type conversion in C++?

a)

Changing a class

b)

Converting one data type to another

c)

Creating a variable

d)

Declaring a function

66.

Which of the following is NOT a type of type conversion?

a)

Explicit

b)

Implicit

c)

Automatic

d)

Manual

67.

Implicit type conversion is also known as:

a)

Casting

b)

Coercion

c)

Overriding

d)

Transformation

68.

Explicit type conversion in C++ is called:

a)

Type casting

b)

Overloading

c)

Deriving

d)

Defining

69.

Which cast operator is used for compile-time conversions?

a)

reinterpret_cast

b)

const_cast

c)

dynamic_cast

d)

static_cast

70.

Which cast operator is used for runtime type conversions?

a)

dynamic_cast

b)

static_cast

c)

const_cast

d)

reinterpret_cast

71.

Which cast operator removes const or volatile qualifiers?

a)

static_cast

b)

const_cast

c)

reinterpret_cast

d)

dynamic_cast

72.

Which cast is considered the most dangerous and should be avoided unless necessary?

a)

const_cast

b)

reinterpret_cast

c)

dynamic_cast

d)

static_cast

73.

What happens when you remove const and modify the original variable?

a)

Undefined behavior

b)

Program runs normally

c)

Compile-time error

d)

Type safety ensured

74.

What is a risk of type conversion?

a)

Data loss and undefined behavior

b)

Code optimization

c)

Faster execution

d)

Stronger typing

75.

Which of the following is a safe way to convert a float to an int?

a)

int x = static_cast(3.14);

b)

int x = reinterpret_cast(3.14);

c)

int x = const_cast(3.14);

d)

int x = dynamic_cast(3.14);

76.

What is the result of implicit conversion from int to double?

a)

Truncation of decimal part

b)

Automatic widening without data loss

c)

Compile-time error

d)

Undefined behavior

77.

Which of the following allows checking at runtime if a base pointer actually points to a derived object?

a)

static_cast

b)

dynamic_cast

c)

reinterpret_cast

d)

const_cast

78.

Which cast operator can reinterpret any pointer type as another pointer type?

a)

reinterpret_cast

b)

const_cast

c)

dynamic_cast

d)

static_cast

79.

When should explicit type casting be used instead of implicit conversion?

a)

When you want automatic conversion

b)

When compiler cannot convert types safely

c)

When defining variables

d)

When using default constructors

80.

What is the purpose of exception handling in C++?

a)

Faster execution

b)

Memory optimization

c)

Input validation only

d)

Handling runtime errors safely

81.

Which keyword is used to throw an exception?

a)

throw

b)

raise

c)

error

d)

catch

82.

Which block must follow every try block?

a)

finally

b)

catch

c)

except

d)

raise

83.

What happens when an exception is thrown with no matching catch block?

a)

Program ignores it

b)

Program terminates

c)

Error automatically fixed

d)

Control returns to main

84.

Which type of exception can be thrown?

a)

Any data type

b)

Only int

c)

Only string

d)

Only std::exception

85.

What is the purpose of the catch(...) handler?

a)

Rethrow exception

b)

Catch integer errors only

c)

Catch string errors only

d)

Catch any type of exception

86.

What does the std::exception::what() function return?

a)

The line number

b)

The variable name

c)

A C-string error message

d)

Exception ID

87.

Which statement rethrows the current exception?

a)

throw

b)

raise()

c)

throw current

d)

rethrow()

88.

What is stack unwinding?

a)

Resetting the stack pointer

b)

Reloading memory

c)

Destroying automatic objects during exception propagation

d)

Halting execution

89.

What occurs when multiple catch blocks are used?

a)

First matching type handles the exception

b)

All catch blocks run

c)

Only the last one runs

d)

Catch order does not matter

90.

What symbol begins every preprocessor directive?

a)

@

b)

#

c)

$

d)

%

91.

Which directive defines a constant?

a)

#include

b)

#define

c)

#ifdef

d)

#pragma

92.

Which directive prevents multiple inclusions of a header file?

a)

#error

b)

#line

c)

#undef

d)

#ifdef

93.

What does #include do?

a)

Inserts the content of another file

b)

Creates a variable

c)

Compiles a separate program

d)

Allocates memory

94.

Which directive removes the definition of a macro?

a)

#line

b)

#pragma

c)

#elif

d)

#undef

95.

Which directive checks if a macro is defined?

a)

#ifdef

b)

#ifnot

c)

#exists

d)

#macro

96.

What is the purpose of #pragma?

a)

Create functions

b)

Remove macros

c)

Provide compiler-specific instructions

d)

Convert code to assembly

97.

What does #if 0 typically do?

a)

Disable a block of code

b)

Turn on debug mode

c)

Clear memory

d)

Print a value

98.

Which directive ends an #if conditional block?

a)

#stop

b)

#macroend

c)

#endif

d)

#finish

99.

Which preprocessor directive includes standard library headers like ?

a)

#define

b)

#pragma

c)

#ifdef

d)

#include