C++ MCQs with Explanations

1. Functions that are necessary for handling formatted input and output operations are declared in the class

Explanation ≡ Correct answer: a) iostream

The iostream class is the base class for all input and output streams in C++. It contains the functions that are necessary for handling formatted input and output operations. These functions include printf(), scanf(), getline(), and write().

The ios class is a subclass of iostream that provides additional functionality for handling I/O operations. It contains functions for setting and getting the formatting flags, as well as functions for controlling the buffering of I/O operations.

The istream and ostream classes are subclasses of ios that represent input and output streams, respectively. They contain functions for reading and writing data from and to streams.


2. Which of the following statements is TRUE?

Explanation ≡ Correct answer: a) The extraction operator >> always skips white space characters, b) The cout stream is normally connected to the display screen, and d) All the above
a) The extraction operator >> always skips white space characters: When using the extraction operator `>>` to read input from a stream, it automatically skips any leading white space characters, such as spaces, tabs, and newlines, before reading the actual data. b) The cout stream is normally connected to the display screen: By default, the output stream `cout` in C++ is connected to the display screen, and any data written to `cout` will be displayed on the standard output, which is usually the console or terminal. c) The function write ( ) outputs an entire line of text: This statement is not true. The `write()` function in C++ is used to output a specified number of characters to the stream, not an entire line of text. Therefore, the correct statements are a), b), and d).

3. Which of the following statements is FALSE?

Explanation ≡ Correct answer: c) The function getline( ) does not take any arguments
c) The function getline( ) does not take any arguments: This statement is false. The `getline()` function in C++ is used to read a line of text from an input stream and stores it into a string object. It takes at least two arguments - the input stream to read from and the string object to store the line of text. Options a), b), and d) are not correct statements. The `put()` function can be used to output a line of text using a loop construct, the `get()` function can be used to read a character from the keyboard, including blank space and new line characters, and a C++ stream is associated with a particular class (e.g., `istream` or `ostream`).

4. Which of the following statements is illegal?

Explanation ≡ Correct answer: d) cout.put('x')
The statement `cout.put('x');` is illegal. The `cout.put()` function is used to output a single character to the standard output, and it requires a character as an argument, not a character constant. The correct way to use `cout.put()` is by passing a character, like `cout.put('x')`. Options a), b), and c) are valid statements. The `cin.get()` function is used to read a single character from the standard input, and it can take a character constant as an argument. Therefore, the correct answer is d) cout.put('x').

5. If m and n represent integer values, which of the following statements is legal?

Explanation ≡ Correct answer: a) cout.write(text, 10);
a) cout.write(text, 10);: This statement is legal. The `cout.write()` function is used to output a specified number of characters from a character array (C-string) to the standard output. In this case, it outputs 10 characters from the character array `text`. Option b) is not legal because the variable `m` must be of type `streamsize` (which is an alias for `int`) to specify the number of characters to be written. Option c) is not legal because the `write()` function returns the `ostream` object, so chaining two `write()` functions is not possible without additional modifications. Therefore, the correct statement is a) cout.write(text, 10).

6. The ios format function precision() is used to

Explanation ≡ Correct answer: b) specify the number of digits to be displayed after the decimal point
The `precision()` function is used to specify the number of digits to be displayed after the decimal point for floating-point values. It affects the output of floating-point values when using `<<` to write to the output stream. Option a) is not correct because the field size is set using the `width()` function, not `precision()`. Option c) is not correct because the `precision()` function is used for floating-point values, not integers. Option d) is not correct because displaying output in scientific format is controlled using the `scientific` flag, not the `precision()` function. Therefore, the correct answer is b) specify the number of digits to be displayed after the decimal point.

7. The statements

        cout.width(4);
        cout<< 12 << 34 << "\n";
        

will produce the output in the following format

Explanation ≡ Correct answer: d)
The statement `cout.width(4);` sets the width of the output field to 4. The `width()` function in C++ sets the field width for the next output operation. The following two `cout` statements will use the specified width of 4: ``` cout<< 12 << 34 << "\n"; ``` The first output operation `cout<< 12` prints the number 12 with a width of 4, which means it will be right-justified in a field of width 4. Since 12 has only two digits, it will be padded with two spaces on the left to make it a total of 4 characters. The second output operation `cout<< 34` prints the number 34 also with a width of 4, which means it will also be right-justified in a field of width 4. Since 34 has only two digits, it will be padded with two spaces on the left to make it a total of 4 characters. Therefore, the output will be: ` 12 34` (with spaces before 12 and 34 to make them 4 characters wide).

8. Which of the following statements is TRUE?

Explanation ≡ Correct answer: a) We need not specify the precision for each item separately, b) If the specified field width is smaller than the size of the value, C++ expands the size of the field to fit the value, and d) When the function width ( ) is used, the text is printed right-justified
a) We need not specify the precision for each item separately: When setting the precision using the `precision()` function, the specified precision applies to all floating-point values until it is changed. Once the precision is set, it remains in effect until it is modified. b) If the specified field width is smaller than the size of the value, C++ expands the size of the field to fit the value: This statement is true. When using the `width()` function to set the field width for an output operation, if the specified width is smaller than the size of the value, C++ expands the size of the field to fit the value. c) When displaying floating-point numbers using the precision function, the trailing zeros are truncated: This statement is false. The `precision()` function specifies the number of digits to be displayed after the decimal point for floating-point values. It does not truncate trailing zeros. d) When the function width ( ) is used, the text is printed right-justified: This statement is true. The `width()` function is used to set the field width for the next output operation, and by default, the text is printed right-justified in the specified field. Therefore, the correct statements are a), b), and d).

9. Which of the following is a valid statement?

Explanation ≡ Correct answer: c) cout << setf(ios::hex, ios::basefield);
c) cout << setf(ios::hex, ios::basefield);: This statement is valid. The `setf()` function is used to set format flags for the output stream. In this case, it sets the hexadecimal format flag `ios::hex` in the `ios::basefield` format category. Options a) and b) are not valid statements. Option a) is incorrect because `ios::internal` and `ios::basefield` are not valid flags to be passed to the `setf()` function together. Option b) is incorrect because `floatfield` is not a valid flag to be used with `ios::scientific`. Option d) is not correct because option c) is a valid statement. Therefore, the correct statement is c) cout << setf(ios::hex, ios::basefield).

10. For displaying plus sign and trailing zeros for printing positive floating-point numbers, we must use the flag

Explanation ≡ Correct answer: b) ios::showpos only
To display the plus sign for positive floating-point numbers and trailing zeros, you should use the `ios::showpos` flag. This flag sets the positive sign flag for non-negative numbers. Options a) and e) are incorrect. The `ios::showbase` and `ios::showpoint` flags are used for other purposes and do not affect the display of plus signs or trailing zeros for positive floating-point numbers. Option c) is also incorrect because the `ios::showpoint` flag is used to force the decimal point to be shown, which is not related to the display of the plus sign or trailing zeros. Option d) is incorrect because option a) is not relevant to displaying plus signs or trailing zeros. Therefore, the correct answer is b) ios::showpos only.

11. What would be the output of the following code segment?

        cout.setf(ios::showpos);
        cout.precision(2);
        cout << 345.609;
        
Explanation ≡ Correct answer: b) +345.61
The `cout.setf(ios::showpos);` line sets the positive sign flag for non-negative numbers. The `cout.precision(2);` line sets the precision to 2, meaning that the output will have 2 digits after the decimal point. The output of the code segment `cout << 345.609;` is "+345.61". The number 345.609 is rounded to 345.61 with two digits after the decimal point. Since the `ios::showpos` flag is set, the plus sign is displayed for the positive number. Therefore, the correct answer is b) +345.61.

12. Which of the following statements are legal?

        (i) cout << setwidth(5);
        (ii) cout.precision(3);
        (iii) cout << 123.45 << setw(10);
        
Explanation ≡ Correct answer: c) i and iii
(i) `cout << setw(5);`: This statement is incorrect. The correct function name for setting the width is `width()`, not `setwidth()`. The correct statement should be `cout << setw(5);`. (ii) `cout.precision(3);`: This statement is correct. The `precision()` function sets the number of digits to be displayed after the decimal point for floating-point values. (iii) `cout << 123.45 << setw(10);`: This statement is correct. The `setw()` function is used to set the field width for the next output operation. In this case, it sets the field width to 10 before outputting the number 123.45. Therefore, the correct statements are i and iii.


Your Score is 10

THE STUDENT FRIENDLY BOOK TO LEARN C++. THE EXPLANATIONS ARE IN SIMPLE ENGLISH AND THEY PROVIDE EXERCISES TO MAKE YOUR LEARNING EXCELLENT.

Topic wise C++ MCQs Index ≡
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