wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Programming Concepts Worksheet

Total questions: 50

Worksheet time: 25mins

Name
Class
Date
1.

Which of the following correctly uses the enum to define colors and assigns Green to a variable c?

a)

enum Color { Red, Green, Blue }; Color c = Green;

b)

enum Color { Red, Green, Blue }; c = Green;

c)

enum Color { Red = 1, Green = 2, Blue = 3 }; Color c = 2;

2.

Which of the following statements correctly frees dynamically allocated memory pointed to by ptr?

a)

delete ptr;

b)

free(ptr);

c)

All of the others

d)

ptr = NULL;

3.

Which of the following is a valid way to declare a structure in C/C++?

a)

struct Person { int age; char name[50]; };

b)

struct Person { age int; name char[50]; };

c)

struct Person { int age; string name; };

d)

All of the others

4.

Which of the following C/C++ statements correctly initializes an array with the values 1, 2, 3, 4, 5?

a)

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

b)

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

c)

int arr[5] = (1, 2, 3, 4, 5);

d)

All of the others

5.

Which of the following is the correct way to declare a boolean variable in C++?

a)

bool isTrue = true;

b)

boolean isTrue = true;

c)

bool isTrue = 1;

d)

bool isTrue = T;

e)

All of the others

6.

How do you access a member function drive using a pointer carPtr to an object of class Car?

a)

carPtr.drive();

b)

carPtr->drive();

c)

(*carPtr).drive();

d)

All of the others

7.

What is the correct syntax to declare an integer variable named count in C++?

a)

int count;

b)

integer count;

c)

num count;

d)

count int;

8.

Question: What does polymorphism allow in OOP?

a)

Objects to have multiple forms

b)

Classes to have multiple inheritance

c)

Functions to be overloaded

d)

All of the others

9.

Question: Which stream class is used for output file operations in C++?

a)

ifstream

b)

ofstream

c)

fstream

d)

stringstream

10.

Question: Which keyword is used to inherit publicly from a base class Animal in C++?

a)

inherits

b)

publicly

c)

public

d)

protected

11.

Question: Which loop structure ensures that the loop body is executed at least once?

a)

for loop

b)

while loop

c)

do...while loop

d)

foreach loop

12.

Question: Which loop construct is particularly useful when the number of iterations is determined by user input during the loop's execution?

a)

for loop

b)

while loop

c)

do-while loop

d)

foreach loop

13.

Question: Which keyword is used to inherit a base class publicly in C++?

a)

inherit

b)

publicly

c)

public

d)

private

14.

Question: Which access specifier allows class members to be accessible from outside the class?

a)

private

b)

protected

c)

public

d)

static

15.

Which of the following control structures is used to execute a block of code multiple times based on a condition?

a)

if

b)

switch

c)

for

d)

break

16.

Which of the following is a valid way to declare a pointer to a character in C++?

a)

char ptr;

b)

char* ptr;

c)

pointer ptr;

d)

*char ptr;

17.

Which STL container is best suited for implementing a dynamic array that can change size automatically?

a)

list

b)

map

c)

vector

d)

set

18.

Which loop construct is particularly useful when the number of iterations is determined by user input during the loop's execution?

a)

for loop

b)

while loop

c)

do-while loop

d)

foreach loop

19.

What is the primary benefit of using functions in programming?

a)

Increase the program size

b)

Reduce code reusability

c)

Break complex programs into manageable parts

d)

Make debugging more difficult

20.

What is the purpose of the typedef keyword in C++?

a)

To create a new type that is identical to an existing type

b)

To declare a constant value

c)

To define a macro

d)

To allocate dynamic memory

21.

What is the primary purpose of a compiler in C/C++ programming?

a)

To execute the program

b)

To translate high-level code into machine code

c)

To manage memory allocation

d)

To provide a user interface

22.

What is the purpose of the continue statement in a loop?

a)

To exit the loop immediately

b)

To skip the current iteration and proceed to the next one

c)

To pause the loop

d)

To restart the loop

23.

What is the effect of calling join() on a std::thread object?

a)

It detaches the thread from the main thread

b)

It blocks the calling thread until the thread finishes execution

c)

It terminates the thread immediately

d)

It creates a new thread

24.

Which of the following correctly declares an iterator for a map <string, int>?

a)

map<string, int>::iterator it;

b)

iterator<map<string, int>> it;

c)

map::iterator<string, int> it;

d)

it map<string, int>::iterator;

25.

What is the primary purpose of iterators in STL?

a)

To sort containers

b)

To traverse and access elements within containers

c)

To manage memory allocation

d)

To handle file operations

26.

What is the purpose of the const keyword when declaring a variable in C/C++?

a)

To allow the variable to be modified

b)

To prevent the variable from being modified

c)

To declare a pointer

d)

To allocate dynamic memory

27.

What does the #include directive do in a C++ program?

a)

Includes the standard input/output stream library

b)

Includes the integer manipulation library

c)

Includes the string handling library

d)

Includes the file handling library

28.

Which of the following is true about member functions marked as const?

a)

They can modify any member variables

b)

They cannot modify any member variables

c)

They must return an integer

d)

They can only be called by static objects

29.

Which of the following is true about C/C++ templates?

a)

They allow functions and classes to operate with generic types

b)

They are only used with classes

c)

They cannot be overloaded

d)

They must specify all types at compile time

30.

Which of the following best describes type casting in C/C++?

a)

Changing the type of a variable permanently

b)

Converting a variable from one type to another temporarily

c)

Declaring a new type

d)

Assigning a value to a constant

31.

What does the realloc function do in C/C++?

a)

Allocates new memory

b)

Frees allocated memory

c)

Resizes previously allocated memory

d)

Copies memory from one location to another

32.

Which of the following statements correctly declares a function prototype for a function named multiply that takes two double parameters and returns a double?

a)

double multiply(double a, double b) {}

b)

double multiply(double a, double b);

c)

void multiply(double a, double b);

d)

multiply(double a, double b) double;

33.

Which of the following correctly declares an iterator for a map<string, int>?

a)

map<string, int>::iterator it;

b)

iterator<map<string, int>> it;

c)

map::iterator<string, int> it;

d)

it map<string, int>::iterator;

34.

What does the free function do in C/C++?

a)

Allocates memory

b)

Deallocates previously allocated memory

c)

Resizes allocated memory

d)

Copies memory

35.

How can you ensure that a member function in a base class can be overridden in derived classes to achieve runtime polymorphism?

a)

Declare the function as static

b)

Declare the function as virtual

c)

Use the override keyword

d)

Make the function const

36.

How do you open a file named "data.txt" for reading using an ifstream object fin?

a)

fin.open("data.txt", ios::out);

b)

fin.open("data.txt", ios::in);

c)

fin.open("data.txt", ios::app);

d)

fin.open("data.txt", ios::binary);

37.

How can you prevent data races in a multithreaded C++ program?

a)

By using global variables

b)

By using mutexes to synchronize access to shared resources

c)

By detaching all threads

d)

By limiting the number of threads to one

38.

In multithreading, what is the purpose of a mutex?

a)

To create new threads

b)

To synchronize access to shared resources

c)

To terminate threads

d)

To allocate memory dynamically

39.

What is the primary difference between procedural programming and object-oriented programming (OOP)?

a)

Procedural programming uses classes, while OOP does not

b)

OOP focuses on objects combining data and functions, whereas procedural programming focuses on a sequence of actions

c)

Procedural programming is more modular than OOP

d)

OOP cannot reuse code, unlike procedural programming

40.

What is inheritance in OOP?

a)

Encapsulating data and methods within a class

b)

Creating multiple instances of a class

c)

Deriving a new class from an existing class, inheriting its attributes and behaviors

d)

Overloading functions within a class

41.

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

a)

To initialize an object with default values

b)

To copy the attributes of one object to another, ensuring deep copying if necessary

c)

To destroy an object

d)

To overload the assignment operator

42.

What is an abstract class in C++?

a)

A class that cannot have any member functions

b)

A class that cannot be instantiated and contains at least one pure virtual function

c)

A class that has only static members

d)

A class that inherits from another class

43.

What is the primary advantage of using STL algorithms over writing your own algorithms?

a)

STL algorithms are always faster

b)

STL algorithms provide a consistent and optimized way to perform common operations

c)

STL algorithms require less memory

d)

STL algorithms are easier to understand

44.

What is the purpose of the continue statement in a loop?

a)

To skip the current iteration and continue with the next one

b)

To terminate the loop immediately

c)

To pause the loop temporarily

d)

To restart the loop from the beginning

45.

Which of the following best describes polymorphism in OOP?

a)

The ability to create multiple instances of a class

b)

The ability of different classes to be treated as instances of the same class through inheritance

c)

The ability to hide data within a class

d)

The ability to overload operators

46.

Which of the following best describes a class in C++?

a)

A function that performs a specific task

b)

A blueprint for creating objects, defining attributes and behaviors

c)

An instance of an object

d)

A type of pointer

47.

Which of the following is true about comments in C/C++?

a)

Comments are executed by the compiler.

b)

Comments are used to document and enhance code readability.

c)

Comments can only be single-line.

d)

Comments must be used with a semicolon.

48.

Which of the following is true about C++ std::string compared to C-style strings?

a)

std::string automatically manages memory for string operations.

b)

C-style strings are objects with member functions.

c)

std::string requires manual handling of the null terminator.

d)

C-style strings support operator overloading for concatenation.

49.

How do you open a file named "data.txt" for reading using an ifstream object fin?

a)

fin.open("data.txt", ios::out);

b)

fin.open("data.txt", ios::in);

c)

fin.open("data.txt", ios::app);

d)

fin.open("data.txt", ios::binary);

50.

How do you correctly overload the + operator for a class Vector in C++?

a)

Vector operator+(Vector a, Vector b) { Vector result; result.x = a.x + b.x; result.y = a.y + b.y; return result; }

b)

void operator+(Vector a, Vector b) {}

c)

friend Vector operator+(const Vector &a, const Vector &b);

d)

Vector operator+() { /*...*/ }