A program’s fundamental job is to accept some data as an input and produce the required data as output. All languages provide some mechanism for performing Input/Output operations through which user communicates with the program.
- [#1] – Introduction to C++ – Questions
- [#2] – Preparing and Running C++ Program – Questions
- [#3] – C++ Fundamentals – Questions
- [#4] – Data Input and Output – Questions
- [#5] – Control Structures – Questions
- [#6] – Functions – Questions
- [#7] – Arrays and Strings – Questions
- [#8] – Structures and Unions – Questions
- [#9] – Pointers – Questions
- [#10] – Object and Classes – Questions
- [#11] – Constructors and Destructors – Questions
- [#12] – Operator Overloading and Type Conversion – Questions
- [#13] – Inheritance – Questions
- [#14] – Virtual Functions – Questions
- [#15] – C++ Streams and Files – Questions
- [#16] – Templates – Questions
- [#17] – Exception Handling – Questions
A good I/O system must support Input and Output through a variety of devices such as keyboard, printer, monitor, disk etc. and also handle inputting and outputting different types of data. In addition to these properties, I/O system must be capable of handling error conditions that normally occur during I/O operations like incorrect input data etc.
Key Points To Remember –
- A stream refers to flow of data. It acts as an intermediator between wide variety of 1/o devices and programs by providing a consistent and device independent interface to the programmer.
- Streams can be classified as Input Stream and Output Stream. In Input stream, the data flows from the Input device to program and in case of Output Stream, data flows from the program to the output device.
- The cin, cout, cerr, and clog are pre-defined stream objects.
- The cout stream object in conjunction with insertion operator (<<) is used to perform standard input operations.
- The cin stream object in conjunction with extraction operator (>>) is used to perform standard input operations.
- The Insertion or extraction operators can be used more than once in a single 1/O statement. This is known as cascading of operators.
- The stream object cerr is used to display error messages on standard output device which are unbuffered.
- The stream object clog is used to display error message on monitor which are unoffered.
- The header file iostream.h must be included when we use cin and cout statements.
- Manipulators are special functions which are used to display the data in a particular format or to input the data in a desired form.
- The setw(), setfill(), setprecision(), setbase(), dec, oct, hex are some common manipulators which are used to manipulate or format the data. Some of these manipulators require the inclusion of iomanip.h header file in the program.
- The insertion operator (<<) is used to insert text into an output stream.
- The Extraction operator (>>) is used to extract text from an input stream.
Viva Voce Questions –
Here are commonly asked questions with answers:
- Which header file need to be included in the program for using manipulators ?
- What is a buffer ?
- It is a block of memory used to store data temporarily. It acts as a intermediator between the peripheral device and the program. It is used to increase the performance of the I/O operations.
- Can you input a number in hexadecimal form ?
- Yes, using setbase (16) or hex () manipulators.
- How will you display an integer 5237 in a 10 digit field ?
- cout<<setw (10) <<5237;
- What is the default precision for a floating point number to be displayed ?
- The default precision of a floating point number is 6. It can be changed using setprecision () manipulator.
Other Similar Questions –
- Define streams. What are the different types of streams ?
- What is the purpose of cin and cout statements ?
- Why >> operator is called extraction operator ?
- Why << operator is called insertion operator ?
- Explain the casting of insertion and extraction operators ?
- How standard I/O operations in C++ are different from that In C ?
- Explain predefined stream objects cerr and clog ?
- What is the advantage of using streams for 1/O operations in C++ ?
- In which header file the prototype for cin and cout objects are available ?
- What is a manipulator ? Why do we need it ?
- What is the purpose of setw() and endl manipulator ? Explain its use with an example ?
- Explain the following with examples:-
- Which header file needs to be included while using manipulators ?
- Write a program to calculate the volume of a sphere ( v=4/3πr3)
- Write a program to convert inches into centimeter. (1 inch=2.54 centimeters)
- Write a program to calculate the aggregate and percentage marks obtained by the student in three subjects ?
- Write a program to interchange two numbers using third variable ?
- Write a program to solve the algebraic expressions
- y = x2 + (x+1)2
- z = (ax+x) / (ax-x)
- m = x2 + 2xy + y2
- Write a program to find the sum of digits of three digit positive integer number ?
- Write a program to convert an uppercase character to lowercase ?
- Give a statement to display fixed floating point quantity with two decimal precision in a field of 10 places ?
- Write a program that demonstrate the use of setw and setfill manipulator ?
- Write a program to display hexadecimal, octal equivalent of 75, 251 decimal numbers ?
- Write a program that reads the radius of a circle and prints the diameter (2r), circumference (2πr) and area of the circle (πr2). Here π = 3.14159. Display the output in a format up to 2 decimal places of precision ?
- Write a program that inputs a 4 digits number and separate the number into individual digits and then prints the digits separated from one another by 3 spaces each. (Hint: Use modulus operator (%)and setw manipulator).
- Write a program that displays the first three letters of your name in big block letters. For example: Anurag
- Write a program that uses four output statements to print the following pattern of asterisks.
- Write a program that input radius and length of a cylinder and computes and display the area and volume of a cylinder.
Area of cylinder = 2πrh
Volume of cylinder = πr2h
- Write a program that prompts user to enter time elapsed for a certain event in hours, minutes and seconds. The program then outputs the time elapsed in seconds.
- Write a program that prompts uses to input cost of three products as real numbers and then print their sum to the nearest integer.
- Write a program to compute the sum of first ten positive integers, 1+2+…..+ 10 without using loops.
- Write a program that reads six integers and prints the first and the last on first line, second and the fifth on the next line and the third and the fourth on the next line. Sample input and the results are shown below.
Enter six numbers: 26 17 34 7 48 9
Number you entered are:
- Write a program that reads nine integers and prints them three in a line separated by commas as shown below.