Professional Documents
Culture Documents
5. Identify the header file needed to read, write, and manipulate the file:
A. Ifstream C. Istream
B. Ofstream D. Fstream
6. If a file named as testdata.bin is to be opened in a binary mode to add data, then the C++
code to be used will be
A. ofstream myfile; myfile.open("testdata.bin" , ios::out | ios::app | ios.binary);
B. ifstream myfile; myfile.open("testdata.bin" , ios::in | ios::app | ios.binary);
C. ofstream myfile; myfile.open("testdata.bin" , ios::out | ios::ate | ios.trunc);
D. ifstream myfile; myfile.open("testdata.bin" , ios::in | ios::ate | ios.trunc);
7. Which of the following options is CORRECT about the output of eof()?
I. It returns true when there is no more data to be read from the input file.
II. It returns false when there is no more data to be read from the input file.
III. It returns true when there is data to be read from the input file.
IV. It returns false when there is data to be read from the input file.
A. I only C. I and IV
B. III only D. II and III
8. If the file is opened for output operations and its contents are deleted and replaced by the new
ones, then the file opening mode used will be
A. ios::ate C. ios::app
B. ios::out D. ios::trunc
SECTION-B(3*8=24)
i. Rewrite the following program segment using conditional operator.
if (a > b) large = a; else large = b;
ii. Write the output of below code.
int main() {
int n ;
for (n = 5; n < 0; n--)
{ cout << n<<endl;
if (n == 3)
break; } return 0;}
iii. Write a C++ program that prints sum of squares of integers from 1 to 10.
iv. How switch and else-if is different from each other, also write syntax of both
v. Differentiate between Binary file and Text file ?
vi. Write down the output of the following code?
vii. What is stream? Write down the purpose of the following functions:
a. getline( ) b. get( )
viii. Trace the errors form the following code or rewrite the code.
int a{10},
i; cout >> ” enter ten numbers
for (i = 1; i < 10: i++)
cin << a{i};
SECTION-C (5*2=10)
1. (a) Write a C++ program that
creates a text file named as Test.txt
writes the following line in the file.
I Love Pakistan closes the file.
(b) . A library charges a fine for every book returned late. For first 5 days the fine is 50
rupees, for 6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the
book after 30 days your membership will be cancelled. Write a program to accept the number
of days the member is late to return the book and display the fine or the appropriate message.
2. (a) ) Write a C++ program that displays the following menu:
Geometry Calculator
Display Area of Circle
Display Area of Rectangle
Quit Enter your choice (1-3):
If user enters 1, the program should ask for the radius of the circle and then display its area. Use
formula: area = πr2 , use 3.14 for π.
If user enters 2, the program should ask for the length and width of the rectangle and then
displays its area, use formula: area = length x width.
Display an error message if the user enters a number outside the range of 1 – 3
(b) Differentiate between While Loop and Do-While Loop.