Important Introduction to Programming Short Questions
Chapter: 36
Question 1:
What header files are needed for I/O stream manipulations?
Answer:
For file manipulations, include fstream.h. For formatting and other manipulations, include iomanip.h. The basic I/O operations use iostream.h.
Chapter: 36
Question 2:
How do you check if the end of a file has been reached using cin?
Answer:
Use cin.eof() to check if the end of file has been reached. It returns a boolean indicating if the end-of-file bit is set.
Chapter: 36
Question 3:
What does cin.fail() check for?
Answer:
cin.fail() checks if an input operation has failed due to a formatting error or invalid input. It returns a boolean value indicating if the fail bit is set.
Chapter: 36
Question 4:
What is the purpose of cin.clear()?
Answer:
cin.clear() is used to reset the stream's state flags (e.g., fail, eof) to their normal "good" state, allowing further operations on the stream.
Chapter: 36
Question 5:
Name an example of a non-parameterized manipulator and its function.
Answer:
endl is a non-parameterized manipulator that inserts a new line and flushes the output buffer.
Chapter: 36
Question 6:
How can you print a number in hexadecimal format using manipulators?
Answer:
Use cout << hex << number; to print a number in hexadecimal format. hex is a non-parameterized manipulator that changes the output base to hexadecimal.
Chapter: 36
Question 7:
What does the setw manipulator do?
Answer:
The setw manipulator sets the field width for the next output. It controls the number of characters used to display the next item. For example, cout << setw(4) << number; prints number in a field of width 4.
Chapter: 36
Question 8:
How do you use setprecision to print a float with three decimal places?
Answer:
Use cout << setprecision(3) << float_number; to print float_number with three decimal places. setprecision is a parameterized manipulator that specifies the number of digits after the decimal point.
Chapter: 36
Question 9:
What is the effect of setfill('*') when used with setw(10)?
Answer:
setfill('*') sets the fill character to '*', and setw(10) ensures the next output is printed in a field of 10 characters wide. For example, cout << setfill('*') << setw(10) << number; prints number right-justified in a field of 10 characters, filling extra spaces with '*'.
Chapter: 36
Question 10:
How does setf(ios::left) affect the output format?
Answer:
setf(ios::left) left-justifies the output within the field width. Padding characters appear to the right if necessary.