an HCL GUVI product

c++ programming banner

C++ Programming Multiple Choice Questions (MCQs) and Answers

Master C++ Programming with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of C++ Programming. Begin your placement preparation journey now!

Q31

Q31 What is an array in C++?

A

A collection of variables of different types

B

A single variable that can store multiple values

C

A function that returns multiple values

D

A data structure that allows recursion

Q32

Q32 How do you access the third element in an array named 'arr' in C++?

A

arr[2]

B

arr[3]

C

arr(2)

D

arr(3)

Q33

Q33 What are multidimensional arrays in C++?

A

Arrays with only one row or column

B

Arrays with more than one dimension, like 2D or 3D arrays

C

Arrays containing elements of different data types

D

Dynamic arrays

Q34

Q34 What is the size of a character array initialized as char arr[] = "Hello";?

A

4

B

5

C

6

D

7

Q35

Q35 Pseudocode:

SET arr[3] TO [10, 20, 30] PRINT arr[0] What does this print?

A

10

B

20

C

30

D

0

Q36

Q36 Pseudocode:

SET nums[5] TO [1, 2, 3, 4, 5] FOR EACH num IN nums DO PRINT num What does this print?

A

12345

B

54321

C

15

D

Error

Q37

Q37 Pseudocode:

SET arr[3] TO [0] FOR i FROM 0 TO 2 DO SET arr[i] TO i + 1 END PRINT arr[2] What is the output?

A

1

B

2

C

3

D

0

Q38

Q38 Spot the mistake:
int nums[] = {1, 2, 3};
for(int i = 0; i <= 3; i++) {
cout << nums[i];
}

A

Incorrect loop condition

B

Missing array initialization

C

No error

D

Syntax error in cout statement

Q39

Q39 Find the error in this code:
char str[5]; str = "Hello";
cout << str;

A

Invalid assignment to char array

B

No error

C

Syntax error in cout statement

D

Array size mismatch

Q40

Q40 What is a pointer in C++?

A

A variable that stores the address of another variable

B

A reference to another variable

C

A type of array

D

A function parameter

Q41

Q41 What is the purpose of the reference operator (&) in C++?

A

To create a reference variable

B

To access the address of a variable

C

To dereference a pointer

D

To perform bitwise AND

Q42

Q42 How are arrays and pointers related in C++?

A

Pointers can only point to the first element of an array

B

Arrays and pointers are fundamentally different

C

An array name can be used as a pointer to its first element

D

Pointers cannot be used to access array elements

Q43

Q43 Given the code
int arr[] = {1, 2, 3};
int *ptr = arr;
cout << ptr[1];,
what is the output?

A

1

B

2

C

3

D

&arr[1]

Q44

Q44 Pseudocode:

SET arr TO [10, 20, 30] SET ptr TO ADDRESS OF arr[0] PRINT ptr[2] What is the output?

A

10

B

20

C

30

D

ADDRESS OF arr[2]

Q45

Q45 Find the mistake:
int num = 10;
int &ref = num;
ref = nullptr;
cout << num;

A

Assigning nullptr to a reference

B

Syntax error

C

No error

D

Incorrect initialization of the reference

Q46

Q46 What is inheritance in OOP?

A

The process of acquiring properties from one class to another

B

Copying of methods between classes

C

Linking of two classes

D

Creating new classes

Q47

Q47 What does encapsulation in C++ refer to?

A

The process of combining data and functions into a single unit

B

Creating multiple copies of the same function

C

Protecting data by making it private

D

Linking different classes together

Q48

Q48 In C++, what is polymorphism?

A

The ability of different classes to have methods with the same name

B

The capability of a class to have multiple constructors

C

The ability of a function to perform different tasks based on the context

D

The concept of creating many objects from a single class

Q49

Q49 What is operator overloading in C++?

A

Giving additional meaning to C++ operators

B

Creating new operators in C++

C

Using existing operators in different contexts

D

Defining operators only in classes

Q50

Q50 What is the output of this C++ code?
class Test {
public:
int x;
}; Test obj;
obj.x = 5;
cout << obj.x;

A

5

B

0

C

Error

D

Undefined

Q51

Q51 In the code class Base {
};
class Derived : public Base {
};,
which OOP concept is illustrated?

A

Encapsulation

B

Polymorphism

C

Inheritance

D

Abstraction

Q52

Q52 Pseudocode:

CLASS Animal FUNCTION makeSound() PRINT "Sound" What is this an example of?

A

A function in C++

B

A class in C++

C

Polymorphism in C++

D

Encapsulation in C++

Q53

Q53 Pseudocode: CLASS Bird EXTENDS Animal FUNCTION makeSound() PRINT "Chirp" What concept is shown here?

A

Inheritance

B

Polymorphism

C

Encapsulation

D

Abstraction

Q54

Q54 Find the error:
class MyClass { public: MyClass(int x) {
cout << x; }
};
MyClass obj;

A

Missing default constructor in class definition

B

Syntax error

C

No error

D

Incorrect constructor usage

Q55

Q55 Spot the error:
class Test {
public:
int x;
void Test() {
x = 0; }
};

A

Incorrect constructor syntax

B

No error

C

Syntax error in variable declaration

D

Member initialization error

Q56

Q56 In C++, which function is used to open a file?

A

file.open()

B

file.read()

C

file.write()

D

file.close()

Q57

Q57 What is a file pointer in C++?

A

A pointer to the contents of a file

B

A variable that stores the memory address of a file

C

A marker that keeps track of the file's current read/write position

D

A pointer to the file's metadata

Q58

Q58 How do you move the file pointer to a specific location for reading or writing in a file in C++?

A

Using the move() function

B

Using the relocate() function

C

Using the seekg() and seekp() functions

D

Using the transfer() function

Q59

Q59 Identify the issue in this C++ code:
ifstream file;
file.open("data.txt");

A

Incorrect file mode

B

File not closed

C

Missing file path

D

No error

Q60

Q60 What is the purpose of the new operator in C++?

A

To create a new class

B

To allocate memory on the stack

C

To allocate memory on the heap

D

To define a new data type

ad vertical