Font size
WorksheetsProgramming Concepts Worksheet
Total questions: 50
Worksheet time: 25mins
Which of the following correctly uses the enum to define colors and assigns Green to a variable c?
enum Color { Red, Green, Blue }; Color c = Green;
enum Color { Red, Green, Blue }; c = Green;
enum Color { Red = 1, Green = 2, Blue = 3 }; Color c = 2;
Which of the following statements correctly frees dynamically allocated memory pointed to by ptr?
delete ptr;
free(ptr);
All of the others
ptr = NULL;
Which of the following is a valid way to declare a structure in C/C++?
struct Person { int age; char name[50]; };
struct Person { age int; name char[50]; };
struct Person { int age; string name; };
All of the others
Which of the following C/C++ statements correctly initializes an array with the values 1, 2, 3, 4, 5?
int arr[] = {1, 2, 3, 4, 5};
int arr[5] = {1, 2, 3, 4, 5};
int arr[5] = (1, 2, 3, 4, 5);
All of the others
Which of the following is the correct way to declare a boolean variable in C++?
bool isTrue = true;
boolean isTrue = true;
bool isTrue = 1;
bool isTrue = T;
All of the others
How do you access a member function drive using a pointer carPtr to an object of class Car?
carPtr.drive();
carPtr->drive();
(*carPtr).drive();
All of the others
What is the correct syntax to declare an integer variable named count in C++?
int count;
integer count;
num count;
count int;
Question: What does polymorphism allow in OOP?
Objects to have multiple forms
Classes to have multiple inheritance
Functions to be overloaded
All of the others
Question: Which stream class is used for output file operations in C++?
ifstream
ofstream
fstream
stringstream
Question: Which keyword is used to inherit publicly from a base class Animal in C++?
inherits
publicly
public
protected
Question: Which loop structure ensures that the loop body is executed at least once?
for loop
while loop
do...while loop
foreach loop
Question: Which loop construct is particularly useful when the number of iterations is determined by user input during the loop's execution?
for loop
while loop
do-while loop
foreach loop
Question: Which keyword is used to inherit a base class publicly in C++?
inherit
publicly
public
private
Question: Which access specifier allows class members to be accessible from outside the class?
private
protected
public
static
Which of the following control structures is used to execute a block of code multiple times based on a condition?
if
switch
for
break
Which of the following is a valid way to declare a pointer to a character in C++?
char ptr;
char* ptr;
pointer
*char ptr;
Which STL container is best suited for implementing a dynamic array that can change size automatically?
list
map
vector
set
Which loop construct is particularly useful when the number of iterations is determined by user input during the loop's execution?
for loop
while loop
do-while loop
foreach loop
What is the primary benefit of using functions in programming?
Increase the program size
Reduce code reusability
Break complex programs into manageable parts
Make debugging more difficult
What is the purpose of the typedef keyword in C++?
To create a new type that is identical to an existing type
To declare a constant value
To define a macro
To allocate dynamic memory
What is the primary purpose of a compiler in C/C++ programming?
To execute the program
To translate high-level code into machine code
To manage memory allocation
To provide a user interface
What is the purpose of the continue statement in a loop?
To exit the loop immediately
To skip the current iteration and proceed to the next one
To pause the loop
To restart the loop
What is the effect of calling join() on a std::thread object?
It detaches the thread from the main thread
It blocks the calling thread until the thread finishes execution
It terminates the thread immediately
It creates a new thread
Which of the following correctly declares an iterator for a map <string, int>?
map<string, int>::iterator it;
iterator<map<string, int>> it;
map::iterator<string, int> it;
it map<string, int>::iterator;
What is the primary purpose of iterators in STL?
To sort containers
To traverse and access elements within containers
To manage memory allocation
To handle file operations
What is the purpose of the const keyword when declaring a variable in C/C++?
To allow the variable to be modified
To prevent the variable from being modified
To declare a pointer
To allocate dynamic memory
What does the #include
Includes the standard input/output stream library
Includes the integer manipulation library
Includes the string handling library
Includes the file handling library
Which of the following is true about member functions marked as const?
They can modify any member variables
They cannot modify any member variables
They must return an integer
They can only be called by static objects
Which of the following is true about C/C++ templates?
They allow functions and classes to operate with generic types
They are only used with classes
They cannot be overloaded
They must specify all types at compile time
Which of the following best describes type casting in C/C++?
Changing the type of a variable permanently
Converting a variable from one type to another temporarily
Declaring a new type
Assigning a value to a constant
What does the realloc function do in C/C++?
Allocates new memory
Frees allocated memory
Resizes previously allocated memory
Copies memory from one location to another
Which of the following statements correctly declares a function prototype for a function named multiply that takes two double parameters and returns a double?
double multiply(double a, double b) {}
double multiply(double a, double b);
void multiply(double a, double b);
multiply(double a, double b) double;
Which of the following correctly declares an iterator for a map<string, int>?
map<string, int>::iterator it;
iterator<map<string, int>> it;
map::iterator<string, int> it;
it map<string, int>::iterator;
What does the free function do in C/C++?
Allocates memory
Deallocates previously allocated memory
Resizes allocated memory
Copies memory
How can you ensure that a member function in a base class can be overridden in derived classes to achieve runtime polymorphism?
Declare the function as static
Declare the function as virtual
Use the override keyword
Make the function const
How do you open a file named "data.txt" for reading using an ifstream object fin?
fin.open("data.txt", ios::out);
fin.open("data.txt", ios::in);
fin.open("data.txt", ios::app);
fin.open("data.txt", ios::binary);
How can you prevent data races in a multithreaded C++ program?
By using global variables
By using mutexes to synchronize access to shared resources
By detaching all threads
By limiting the number of threads to one
In multithreading, what is the purpose of a mutex?
To create new threads
To synchronize access to shared resources
To terminate threads
To allocate memory dynamically
What is the primary difference between procedural programming and object-oriented programming (OOP)?
Procedural programming uses classes, while OOP does not
OOP focuses on objects combining data and functions, whereas procedural programming focuses on a sequence of actions
Procedural programming is more modular than OOP
OOP cannot reuse code, unlike procedural programming
What is inheritance in OOP?
Encapsulating data and methods within a class
Creating multiple instances of a class
Deriving a new class from an existing class, inheriting its attributes and behaviors
Overloading functions within a class
What is the purpose of a copy constructor in a C++ class?
To initialize an object with default values
To copy the attributes of one object to another, ensuring deep copying if necessary
To destroy an object
To overload the assignment operator
What is an abstract class in C++?
A class that cannot have any member functions
A class that cannot be instantiated and contains at least one pure virtual function
A class that has only static members
A class that inherits from another class
What is the primary advantage of using STL algorithms over writing your own algorithms?
STL algorithms are always faster
STL algorithms provide a consistent and optimized way to perform common operations
STL algorithms require less memory
STL algorithms are easier to understand
What is the purpose of the continue statement in a loop?
To skip the current iteration and continue with the next one
To terminate the loop immediately
To pause the loop temporarily
To restart the loop from the beginning
Which of the following best describes polymorphism in OOP?
The ability to create multiple instances of a class
The ability of different classes to be treated as instances of the same class through inheritance
The ability to hide data within a class
The ability to overload operators
Which of the following best describes a class in C++?
A function that performs a specific task
A blueprint for creating objects, defining attributes and behaviors
An instance of an object
A type of pointer
Which of the following is true about comments in C/C++?
Comments are executed by the compiler.
Comments are used to document and enhance code readability.
Comments can only be single-line.
Comments must be used with a semicolon.
Which of the following is true about C++ std::string compared to C-style strings?
std::string automatically manages memory for string operations.
C-style strings are objects with member functions.
std::string requires manual handling of the null terminator.
C-style strings support operator overloading for concatenation.
How do you open a file named "data.txt" for reading using an ifstream object fin?
fin.open("data.txt", ios::out);
fin.open("data.txt", ios::in);
fin.open("data.txt", ios::app);
fin.open("data.txt", ios::binary);
How do you correctly overload the + operator for a class Vector in C++?
Vector operator+(Vector a, Vector b) { Vector result; result.x = a.x + b.x; result.y = a.y + b.y; return result; }
void operator+(Vector a, Vector b) {}
friend Vector operator+(const Vector &a, const Vector &b);
Vector operator+() { /*...*/ }
