[#4] – Data Input and Output – Questions

C-plus-plus-programming-questions-techhyme

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.

Index:

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 –

  1. 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.
  2. 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.
  3. The cin, cout, cerr, and clog are pre-defined stream objects.
  4. The cout stream object in conjunction with insertion operator (<<) is used to perform standard input operations.
  5. The cin stream object in conjunction with extraction operator (>>) is used to perform standard input operations.
  6. The Insertion or extraction operators can be used more than once in a single 1/O statement. This is known as cascading of operators.
  7. The stream object cerr is used to display error messages on standard output device which are unbuffered.
  8. The stream object clog is used to display error message on monitor which are unoffered.
  9. The header file iostream.h must be included when we use cin and cout statements.
  10. Manipulators are special functions which are used to display the data in a particular format or to input the data in a desired form.
  11. 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.
  12. The insertion operator (<<) is used to insert text into an output stream.
  13. The Extraction operator (>>) is used to extract text from an input stream.

Viva Voce Questions – 

Here are commonly asked questions with answers:

  1. Which header file need to be included in the program for using manipulators ?
    • iomanip.h
  2. 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.
  3. Can you input a number in hexadecimal form ?
    • Yes, using setbase (16) or hex () manipulators.
  4. How will you display an integer 5237 in a 10 digit field ?
    • cout<<setw (10) <<5237;
  5. 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 – 

  1. Define streams. What are the different types of streams ?
  2. What is the purpose of cin and cout statements ?
  3. Why >> operator is called extraction operator ?
  4. Why << operator is called insertion operator ?
  5. Explain the casting of insertion and extraction operators ?
  6. How standard I/O operations in C++ are different from that In C ?
  7. Explain predefined stream objects cerr and clog ?
  8. What is the advantage of using streams for 1/O operations in C++ ?
  9. In which header file the prototype for cin and cout objects are available ?
  10. What is a manipulator ? Why do we need it ?
  11. What is the purpose of setw() and endl manipulator ? Explain its use with an example ?
  12. Explain the following with examples:-
    • hex
    • oct
    • setbase()
    • setfill()
    • setprecision()
  13. Which header file needs to be included while using manipulators ?
  14. Write a program to calculate the volume of a sphere ( v=4/3πr3)
  15. Write a program to convert inches into centimeter. (1 inch=2.54 centimeters)
  16. Write a program to calculate the aggregate and percentage marks obtained by the student in three subjects ?
  17. Write a program to interchange two numbers using third variable ?
  18. Write a program to solve the algebraic expressions
    • y = x2 + (x+1)2
    • z = (ax+x) / (ax-x)
    • m = x2 + 2xy + y2
  19. Write a program to find the sum of digits of three digit positive integer number ?
  20. Write a program to convert an uppercase character to lowercase ?
  21. Give a statement to display fixed floating point quantity with two decimal precision in a field of 10 places ?
  22. Write a program that demonstrate the use of setw and setfill manipulator ?
  23. Write a program to display hexadecimal, octal equivalent of 75, 251 decimal numbers ?
  24. 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 ?
  25. 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).
  26. Write a program that displays the first three letters of your name in big block letters. For example: Anurag
  27. Write a program that uses four output statements to print the following pattern of asterisks.
  28. 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
  29. 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.
  30. Write a program that prompts uses to input cost of three products as real numbers and then print their sum to the nearest integer.
  31. Write a program to compute the sum of first ten positive integers, 1+2+…..+ 10 without using loops.
  32. 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:
    26 9
    17 48
    34 7
  33. Write a program that reads nine integers and prints them three in a line separated by commas as shown below.
You may also like:

Related Posts

Leave a Reply