C++ MCQs with Explanations

Beginning with C++

1. C++ is an extension of the C language. One of the new features added is:

Explanation ≡ Correct answer: option c) Operator overloading
C++ introduced the concept of operator overloading, allowing the same operator to be used with different data types and perform different operations depending on the context. This is a powerful feature that enhances code readability and flexibility.

2. Which of the following statements is FALSE?

Explanation ≡ Correct answer: option d) C++ does not permit the use of printf() for displaying an output.
C++ is a language derived from C, and it introduces cout and cin for input/output operations. While printf() is allowed in C++, it is generally recommended to use cout for output and cin for input in C++ programs.

3. Line commands are inserted into a C++ program using

Explanation ≡ Correct answer: option c) Stars and slashes like /*……*/
C++ supports both single-line comments using // and multi-line comments using /* … */. Comments help add explanatory notes to the code and are ignored by the compiler during the compilation process.

4. For declaring the variables a and b, which of the following declaration statements is incorrect?

Explanation ≡ Correct answer: option c) int a, // Declaration // b;
Option c is incorrect because a comment "// Declaration // b;" is placed in between the declaration of variables "a" and "b". In C++, comments cannot be used in the middle of a declaration statement. The correct way to declare multiple variables on the same line is to separate them with commas, as shown in options a and d.

5. For using functions that convert numbers to text and text to numbers, we must include the following header file:

Explanation ≡ Correct answer: option c) <cstdio>
The header file <cstdio> (or <stdio.h> in C) provides functions for input and output operations in C++. It includes functions like printf() for displaying formatted text and scanf() for reading input. These functions are useful for converting numbers to text and text to numbers using formatting.

6. Which of the following statements provide two lines of output?

Explanation ≡ Correct answer: option b) cout << ”VERY” “\n” << “GOOD” “\n”;
Option b uses the newline escape sequence "\n" to insert a line break, resulting in two lines of output. The text "VERY" is printed on the first line, followed by a line break, and then "GOOD" is printed on the second line.

7. Which of the following statements is invalid in C++?

Explanation ≡ Correct answer: option b) cin >> x; >> y;
Option b is invalid because it tries to use the input stream operator (>>) twice consecutively without a variable in between. In C++, you should read input into different variables in separate statements, as shown in option c.

8. Which of the following statements requires the header file <math.h> to be included?

Explanation ≡ Correct answer: option d) x = sqrt(y);
The function sqrt(y) calculates the square root of y and is defined in the <cmath> (or <math.h> in C) header file. To use this function in a C++ program, you need to include the corresponding header file using the #include directive.

9. Name the header file that is to be used in the #include directive when we use cout for displaying outputs.

Explanation ≡ Correct answer: option c) <iostream>
The <iostream> header file contains the input/output stream classes in C++. It provides functionality for displaying output to the console (cout) and reading input from the console (cin).

10. If "a" is an object of the class B and void print(void) is a member function of B, then which one of the following statements will invoke the function?

Explanation ≡ Correct answer: option d) a.print();
To invoke a member function of an object, you use the dot (.) operator, not the scope resolution (::) operator. So, to call the member function print() for the object "a" of class B, you would use the syntax "a.print();".


Your Score is 10

THE STUDENT FRIENDLY BOOK TO LEARN C++. 
Let us C++ by Famous author Yashavant Kanetkar
Indian writer


Topic wise C++ MCQs Index ≡ (Click to open)
Click on particular topic to visit that page
  1. Principles of Object-Oriented Programming 
  2. Beginning with C++
  3. Tokens, Expressions, and Control Structures
  4. Functions in C++
  5. Classes and Objects
  6. Constructors and Destructors
  7. Operator Overloading
  8. Inheritance
  9. Pointers, Virtual Functions, and Polymorphism
  10. Managing Console I/O Operations
  11. Working with Files
  12. Templates
  13. Exception Handling
  14. Standard Template Library
  15. Manipulating Strings
  16. New Features in ANSI C++ Standards
  17. Object-Oriented System Development

Post a Comment

0 Comments