C++ MCQs with answers Page - 152

Here, you will find a collection of MCQ questions on C++. Go through these questions to enhance your preparation for upcoming examinations and interviews.

To check the correct answer, simply click the View Answer button provided for each question.

Have your own questions to contribute? Click the button below to share your MCQs with others!

+ Add Question

A

Admin • 833.24K Points
Coach

Q. Which of the following is the correct way to initialize a string in C++?

  • (A) string str = 'Hello';
  • (B) string str = "Hello";
  • (C) string str = H e l l o;
  • (D) string str = [Hello];

A

Admin • 833.24K Points
Coach

Q. Which of the following is the correct way to define a function that returns an integer and takes two integers as parameters?

  • (A) int func(int, int) { }
  • (B) void func(int, int) { }
  • (C) int func(int a, int b) { }
  • (D) int func(int a, b) { }

A

Admin • 833.24K Points
Coach

Q. Which of the following is the correct way to declare a reference in C++?

  • (A) int &ref;
  • (B) int ref&;
  • (C) reference int ref;
  • (D) int ref[];

A

Admin • 833.24K Points
Coach

Q. What is the purpose of the 'virtual' keyword in C++?

  • (A) To create a constant variable
  • (B) To define a static function
  • (C) To allow function overriding in derived classes
  • (D) To declare a global variable

A

Admin • 833.24K Points
Coach

Q. What will be the output of the following code?

int x = 5;
int y = 2;
cout << x % y;

  • (A) 3
  • (B) 2
  • (C) 1
  • (D) 5

A

Admin • 833.24K Points
Coach

Q. Which of the following is used to handle dynamic memory allocation in C++?

  • (A) new and delete
  • (B) malloc and free
  • (C) alloc and dealloc
  • (D) dynamic and free

A

Admin • 833.24K Points
Coach

Q. Which of the following is the correct way to create an object of a class in C++?

  • (A) class obj;
  • (B) obj = new class;
  • (C) class obj = new class();
  • (D) ClassName obj;

A

Admin • 833.24K Points
Coach

Q. Which of the following C++ data types is used to store a single character?

  • (A) int
  • (B) char
  • (C) string
  • (D) float

A

Admin • 833.24K Points
Coach

Q. Which of the following is the correct syntax to open a file in C++?

  • (A) ofstream file('filename.txt');
  • (B) ofstream file("filename.txt");
  • (C) ifstream file('filename.txt');
  • (D) ifstream file("filename.txt");

A

Admin • 833.24K Points
Coach

Q. Which of the following is the correct way to define a default constructor in C++?

  • (A) ClassName() {}
  • (B) void ClassName() {}
  • (C) ClassName() : { }
  • (D) void ClassName() : { }