Important Introduction to Programming Short Questions
Chapter: 36
Question 1:
What is the difference between hex and setbase(16)?
Answer:
hex is a non-parameterized manipulator that sets the base for integer output to hexadecimal. setbase(16) is a parameterized manipulator that performs the same function, setting the base for numeric output to 16.
Chapter: 36
Question 2:
What does ios::showbase do when used with an output stream?
Answer:
ios::showbase causes the output to display the base of the number (e.g., 0x for hexadecimal, 0 for octal). This flag must be set using setf() to be effective.
Chapter: 36
Question 3:
How can you print a number with leading zeros using manipulators?
Answer:
Use cout << setfill('0') << setw(5) << number; to print number with leading zeros to ensure the total field width is 5 characters. setfill('0') sets the fill character to 0.
Chapter: 36
Question 4:
How does cin.width(7) affect input operations?
Answer:
cin.width(7) sets the field width for input operations to 7 characters. This means cin will read up to 7 characters for the next input operation, and any excess characters will be ignored.
Chapter: 36
Question 5:
What is the purpose of ios::fixed in floating-point output?
Answer:
ios::fixed forces floating-point numbers to be displayed in fixed-point notation, rather than scientific notation. It ensures that the output is formatted with a fixed number of digits after the decimal point.
Chapter: 36
Question 6:
What is the effect of the setw manipulator on subsequent output?
Answer:
setw affects only the next piece of output. It does not persist for subsequent outputs; each new setw must be applied separately for its effect to be seen.
Chapter: 36
Question 7:
How do you set the base of numeric output to octal using a manipulator?
Answer:
Use cout << oct << number; to set the base of numeric output to octal. oct is a non-parameterized manipulator that formats the output in base 8.
Chapter: 37
Question 8:
What is the main purpose of overloading stream insertion (<<) and extraction (>>) operators?
Answer:
Overloading these operators allows custom classes to use standard stream operations for input and output, maintaining the spirit of the operators while extending their functionality to user-defined types.
Chapter: 37
Question 9:
Can stream insertion (<<) and extraction (>>) operators be overloaded as member functions of a class?
Answer:
No, they cannot be overloaded as member functions because the left-side operand is typically a stream object (like cout or cin), which is not a member of the class being overloaded. These operators must be overloaded as non-member functions.
Chapter: 37
Question 10:
What must be the return type of the stream insertion (<<) operator function to support cascading operations?
Answer:
The return type must be ostream& to allow chaining of multiple insertion operations, e.g., cout << obj1 << obj2;.