Skip to main content

Introduction to Programming MCQs with Answers

Practice important Introduction to Programming MCQs with answers and explanations.

Chapter: 36
Q581:
What does the setw manipulator do in C++?
A. Sets the width of the field for the next output.
B. Sets the precision of floating-point numbers.
C. Sets the base for numeric conversion.
D. Inserts a new line.
Correct Answer: A
Chapter: 36
Q582:
How does setfill manipulator work with setw?
A. It determines the width of the field.
B. It fills empty spaces with a specified character.
C. It sets the precision for floating-point numbers.
D. It clears the error flags.
Correct Answer: B
Chapter: 36
Q583:
What is the purpose of the showbase flag in C++ stream manipulations?
A. To show the base of the number in the output.
B. To set the width of the output field.
C. To set the fill character for the output.
D. To set the precision of floating-point numbers.
Correct Answer: A
Chapter: 36
Q584:
Which manipulator skips whitespace characters in input?
A. ws
B. endl
C. flush
D. setfill
Correct Answer: A
Chapter: 36
Q585:
What is the effect of using setprecision(2) with cout?
A. It prints floating-point numbers with two decimal places.
B. It sets the width of the output field to 2.
C. It converts decimal numbers to hexadecimal format.
D. It flushes the output buffer.
Correct Answer: A
Chapter: 36
Q586:
What does cout << setbase(16) << i; do?
A. Prints the integer i in hexadecimal format.
B. Prints the integer i in decimal format.
C. Prints the integer i in octal format.
D. Sets the base of the stream to 10.
Correct Answer: A
Chapter: 36
Q587:
Which function sets the width of the field for the next output?
A. width()
B. setprecision()
C. setfill()
D. setw()
Correct Answer: D
Chapter: 36
Q588:
How are the effects of manipulators like setw and setprecision limited?
A. They apply to all subsequent output operations.
B. They apply only to the next output operation.
C. They apply to all input operations.
D. They apply only to the next input operation.
Correct Answer: B
Chapter: 36
Q589:
What is the purpose of cout.fill('*')?
A. To set the fill character to '*'.
B. To set the width of the output field to 10.
C. To set the precision of floating-point numbers.
D. To insert a newline in the output.
Correct Answer: A
Chapter: 37
Q590:
What is the purpose of overloading the stream insertion operator (<<) in C++?
A. To change the behavior of input operations
B. To change the behavior of output operations
C. To add new operators to the language
D. To modify the behavior of arithmetic operations
Correct Answer: B
Chapter: 37
Q591:
What is the return type of the overloaded stream insertion operator (<<)?
A. int
B. void
C. ostream&
D. istream&
Correct Answer: C
Chapter: 37
Q592:
Which type of operator cannot be overloaded as a member function when dealing with streams?
A. Unary operators
B. Binary operators
C. Stream insertion (<<) and extraction (>>) operators
D. Arithmetic operators
Correct Answer: C
Chapter: 37
Q593:
In the context of operator overloading, which parameter is always passed by reference for operator >>?
A. The first parameter
B. The second parameter
C. Both parameters
D. Neither parameter
Correct Answer: B
Chapter: 37
Q594:
Why are ostream and istream objects passed by reference to the overloaded operators?
A. To allow modification of their state
B. To avoid copying large objects
C. To maintain cascading operations
D. To provide direct access to their internal data
Correct Answer: C
Chapter: 37
Q595:
What must be done if you want to overload operator << for a class?
A. Declare it as a member function
B. Define it inside the class without any special declaration
C. Define it as a non-member function and declare it as a friend of the class
D. Use it as a global function without any special declaration
Correct Answer: C
Chapter: 37
Q596:
What is a typical use of the operator >> function in a class?
A. To output the class's data to the console
B. To read input from a stream and modify the class's data
C. To perform arithmetic operations on the class's data
D. To format and display the class's data
Correct Answer: B
Chapter: 37
Q597:
In the Matrix class example, what does the operator >> function do?
A. Outputs the matrix data to the console
B. Inputs matrix data from the console
C. Adds two matrices together
D. Displays the matrix with formatting
Correct Answer: B
Chapter: 37
Q598:
What does the operator << function do in the Matrix class?
A. Modifies the matrix's internal state
B. Outputs the matrix data with formatting
C. Inputs the matrix data from the user
D. Performs matrix multiplication
Correct Answer: B
Chapter: 37
Q599:
Why is it important to pass the matrix object by reference in the operator >> function?
A. To ensure the function does not modify the matrix
B. To ensure the matrix object is copied for efficiency
C. To allow the function to modify the matrix object
D. To prevent the matrix object from being passed at all
Correct Answer: C
Chapter: 38
Q600:
What does a user-defined manipulator typically return?
A. A string
B. An integer
C. A reference to ostream
D. A reference to istream
Correct Answer: C

Test Your Knowledge

Take a timed quiz on Introduction to Programming

🚀 Start Quiz Now