C++ Basic I/O MCQ

Input and Output (I/O) operations are foundational in C++ programming. Whether it's reading from a file, getting input from the user, or displaying results, understanding I/O is essential. Let's test your knowledge of basic I/O in C++ with this beginner-centric quiz!

1. Which header file is primarily used for I/O operations in C++?

a) <iostream>
b) <io>
c) <input>
d) <output>

Answer:

a) <iostream>

Explanation:

The header file contains definitions for objects like cin, cout, cerr, etc., which are used for I/O operations in C++.

2. What is the primary object used for standard output in C++?

a) cin
b) cout
c) cprint
d) co

Answer:

b) cout

Explanation:

The cout object, defined in the header, is used for standard output operations.

3. Which of the following is the correct way to read an integer input in C++?

a) cout << x;
b) cin << x;
c) cin >> x;
d) cout >> x;

Answer:

c) cin >> x;

Explanation:

The cin object with the extraction operator (>>) is used to take input from the user.

4. How do you display "Hello, World!" on the console?

a) cout << "Hello, World!";
b) cout >> "Hello, World!";
c) cin << "Hello, World!";
d) cin >> "Hello, World!";

Answer:

a) cout << "Hello, World!";

Explanation:

The cout object with the insertion operator (<<) is used for displaying output.

5. Which of the following allows you to format the output in C++?

a)
b)
c)
d)

Answer:

a)

Explanation:

The header provides set of manipulators to format the output in C++.

6. How can you display a newline character in C++?

a) cout << "/n";
b) cout << "\n";
c) cout << "//n";
d) cout << "&n";

Answer:

b) cout << "\n";

Explanation:

The escape sequence \n represents a newline character in C++.

7. If you want to read an entire line of text including spaces, which function would you use?

a) cin.read()
b) cin.getline()
c) cin.get()
d) cin.line()

Answer:

b) cin.getline()

Explanation:

The cin.getline() function is used to read an entire line of text from the standard input, including spaces.

8. Which object is used to report error messages?

a) cin
b) cout
c) cerr
d) cerror

Answer:

c) cerr

Explanation:

The cerr object is used to display error messages on the standard error device, which is typically the screen.

9. What is the purpose of the endl manipulator?

a) Read input
b) Display an end-of-file character
c) Display a newline character and flush the output buffer
d) Display a tab character

Answer:

c) Display a newline character and flush the output buffer

Explanation:

The endl manipulator is used to insert a newline character and flush the output buffer.

10. What would the following code display?

cout << "Hello" << " " << "World!";
a) HelloWorld!
b) Hello World!
c) Hello" "World!
d) Error

Answer:

b) Hello World!

Explanation:

The code uses the insertion operator to concatenate and display multiple strings. The output would be "Hello World!".

Well done on attempting the quiz! Basic I/O operations are essential for creating interactive and dynamic programs. If some questions stumped you, don't worry. Revisit the relevant topics and practice more. The more you code, the clearer these concepts will become. Happy coding!



Comments