Questions Based on File Handling

1.  Write a program to accept name and marks of three subjects for 10 students in a sequential file. Then display the students marks details with total and average marks.

2.  The details of employee of an organisation are stored on a data file ORG.DAT. They enter the name of the employee, his designation, age and salary drawn. The file is updated every year. If the age of an employee is greater then 60 years, his name is removed from file. Write a program in BASIC using sequential file so that after updating, the records are printed in the proper format.

3.  A) Write a program to create a sequential file named SHARE.DAT containing script name and the opening and closing prices per share as follows:

            Script Name      Opening Price     Closing Price

            TELCO            884.00               877.00

            BATA India       176.00               176.10

            COLGATE          183.00               181.40

            GLAXO India      600.00               600.00

    B) Update the file to include the following:

            SATYAM COMPUTER  596.50               589.60

            TISCO             99.00                95.40

4.  Writes a program to create a sequential file and store name, author and reference number of the book present in a library. Then display the name and author name of the book after inputting the reference number of that book.

5.  Write a program to create a sequential file that stores your weekly school timetable in it. Then will retrieve the information and tell you, what subject do you have in the given period on the given day.

6.  Write a program to create a sequential file containing the names of probable who are likely to get grade one in computer science.

7.  A file name STU.DAT contains serial number (N), roll number (R$), name (N$) and date of birth (D$) of 40 student in a class. Write a program in basic to open the file and update it as per specification given below:

    i)  In serial number 7, a name Amrit Pal Singh is to be corrected as Amit Pal Singh.

    ii) A new name is to be added as - serial number 41, roll no.7554, name Varun Shukla and date of birth 13/3/1984.

8.  Write a program, which will create three separate sequential data file named “NAME”, “ADDRESS” and “PHONE” simultaneously. The files are to contain names, address and phone number of forty-five students.

9.  A sequential file named “STAFF” contains names (N$) and basic salaries (BS) of the employees in a firm. This file is to be used as an input file, which will calculate. DA = 90% of basic, HRA = 15% of (BASIC +DA), PF = 10% of (BASIC + DA), Gross Pay = BASIC + DA + HRA, IT = 20% of Gross Pay, Deduction = PF + IT, Take Home Pay = Gross Pay - Deduction. Write a program which will store data for each employees in another file named “SALARY” in the following format:

            Name      Gross Pay      Deduction    Take-Home-Pay

10. Write a program to open a sequential file to store numbers in it. Use another program to get back the numbers and add them. Print also the average of the numbers.

11. A) Create a sequential file named as “BOOKS” for a library containing title, name of author, reference number for a number of books.

    B) Update the book files to include some new arrivals.

12. Write a program to create a sequential file with ten names that would be input by the users. The user must then be able to find a particular name in the file and output the name on screen else the program must output “NAME NOT FOUND”.

13. A) Write a program to create a sequential file named STUDENT.DAT and numbered as 1 to store roll, name, marks in English, math and science and also the total marks of the students.

    B) Write a program to read only the name, roll and total marks from the file STUDENT.DAT and print under the following heading.

            NAME    ROLL NO.        TOTAL MARKS

14. A) Write a program to create a sequential file containing the following information:

Country % of people owning PC
INDIA

JAPAN

USA

GERMANY

SINGAPORE

UK

MEXICO

1.7  

14.6

36.0

17.4

20.4

21.5

3.5

 

    B) Update the file to include the following:

Country % of people

owning PC

PAKISTAN

ISRAEL

0.2

14.3

15. Write a basic program to create a sequential file “INCOME”. Store names and salaries of 30 employees. Modify the record of the employee whose salary falls below 5000. Print all the records.

16. A) Write a program to create a file “COMPANY.DAT” for a company to keep the records of Name, Designation, Joining Date and Date of Retirement of their 850 employees.

    B) Prepare a list to show the date of retirement in ascending order. I.e. the Name, Designation and Date of Retirement of that employee will appear at the top of the list, who will retire first.

17. A) Write a basic program to create sequential file and store name of 11 cricketers and the country to which they belong.

    B) Write another program to read and print the records from the above file, only after asking the user to enter the name of the cricketers and if that name is present. Repeat the process according to the user’s choice.

18. Suppose an already created file named “MARKS.DAT” containing the names, marks obtained in Physics, Chemistry, Mathematics and English. Using this file prepare a merit list of students Grade wise in the following format:

        Name  Phy  Chem  English  Math  Total  Average  Grade

    The calculation of grades are done on basis of following conditions: Grade A if average>=80, Grade B if average>=60 and <80. Grade C if average>=40 and <60. Grade F if average<40. A student grade is considered as F if he does not score 40 in any subjects.

19. Write a program to create a sequential file name “STUDENT.DAT” to store the unique records of students. Accept Name and Roll then write it to sequential file if the given Roll number is not present in file. If the record of a student is already present then display a proper message. Make your program as a multiple data entry.

20. Suppose an already created file named “STUDENTS.TXT” containing the Names and Roll number of student. Due to some mistake file may contain some duplicate records (on basis of Roll number). Write a programming concept, which will remove duplicate records from file and store only unique records.

21. A sequential file named “STUDENT.DOC” contains the Roll and Name. The data stores in the sequential file is not ordered Roll number wise. Write a programming logic to arrange the data and store it in sequential file.

22. Suppose an already created file named “MARKS.DAT” containing the names of student and marks obtained in Physics, Chemistry, Mathematics and English. Using this file prepare a list of passed students. A student is considered to pass the examination if he score at least 40 in English and at least 50 in other subjects.

23. A created sequential file named “STAFF” containing the name and basic pay of the employee in a firm. This file is to be used in a basic program, which will calculate.

            DA = 10% of basic pay.

            HRA = 30% of basic pay.

            PF = 8.5% of (BASIC + DA).

            Gross pay = BASIC + DA + HRA.

            IT = 30% of gross pay.

            Deduction = PF + IT

            Take home pay = Gross pay - Deduction.

    Write a basic program that will store Name, Gross Pay, Deduction and Take Home Pay for each employee in a file name “SALARY”.

24. A sequential file named “RESULT” containing name and percentage marks for the three semesters. Read the marks of the three semesters and display a list containing name and final marks of each student. Final mark comprises of 30% of each of the first two semesters and 40% of the third.

25. Write a basic program to create a sequential file containing the name of students who get over 90% marks in Computer.

 

Some theory questions on File Handling

1.  What do you mean by DATA FILE?

Ans:A Data file contains group of logically related data in form of records.

2.  What do you mean by RECORD and FIELD in a data file?

Ans:A group of related sets of data is known as Fields or Attributes, so under each field the information's are collected. A group of related fields is known as Record or Row or Tuple.

3.  Write any two objectives to creating a data file?

Ans:1) A Data file contains huge collection of data, which can be used in any program. 2) By a name a Data file can be stored in a secondary storage device, so it can be used for the future.

4.  Write one or two difference between Random file and Sequential file?

Ans:1) In a sequential file we can store records sequentially and stored records can be accessed and processed sequentially. In a Random file we can store records randomly and also accesses them randomly. 2) In sequential file the length of each records are varies, where as in random file records lengths are same because of their fixed field length.

5.  What is the main difference between WRITE # & PRINT # command?

Ans:In a file data/fields are separated by commas. A WRITE # statements automatically insert commas in between fields. But in a PRINT # statement commas are not put unless specially mentioned.

6.  What are the three rules to be followed when giving the data file name.

Ans:1) A file name can be upto 8 characters long, with an optional 3-character extension. 2) The first letter of the file name must be start with an alphabet. 3) No special character allowed in file name.

7.  What is the use of the statement LINE INPUT and write a program segments to illustrate the use of LINE INPUT statement.

Ans:LINE INPUT statement reads an entire line upto 254 characters, including commas, quotes and other delimiters. This input is taken from the keyboard and stored into a string variable.

        10 OPEN “O”, #1, “NAME”

        20 LINE INPUT “ENTER NAME :”; N$

        30 WRITE #1, N$

        40 CLOSE #1

        50 OPEN “I”, #1, “NAME”

        60 LINE INPUT #1, N$

        70 PRINT N$

        80 CLOSE #1

        90 END

8.  What does the LINE INPUT # statement in BASIC do.

Ans:LINE INPUT # statement reads from a sequential file to a string variable an entire line upto 254 characters, ignoring delimiters.

9.  Write the difference between data file and program file.

Ans:1) A Data file contains group of logically related data in form of records, where as a program file is a set of BASIC statement which is used to do a particular task. 2) Data files are automatically create and save in storage device if we execute a file handling program, where as we have to save program file by using SAVE commands. 3) Data file is not part of any particular program it is a separate file any program file can access them. 4) Data file can be used by any program, where as data entered in a program can not be used by other program.

10. What is the main difference between INPUT & INPUT # command?

Ans:1) INPUT statement read data from keyboard and stored to a variable, where as INPUT # statement read data from file and stored to a variable. 2) INPUT statement prompt (?) on screen to enter some data but INPUT # does not produce any prompt and not allow us to enter some data.

11. What is the meaning of the error message INPUT PAST END.

Ans:When we accessed record from sequential file records are read sequentially one by one, but after some time when there is not record left in sequential file the program execution is stopped by displaying this message.

12. What is the use of EOF() or END OF FILE function.

Ans:When there is not record left in sequential file the program execution is stopped by displaying an error message. We can avoid this by checking facility EOF() that repeat the operation as long as the records exist in the file.

13. Why data file is required or needed, justify your answer?  OR  We can store data in program by using READ-DATA, so why we create data file to store the records. OR Distinguished between READ-DATA and Data File concept.

Ans:We can store any volume of data in any BASIC program, this set of records can not be accessed by other programs. But if we stored the records in a file the records can be accessed by any program whenever requires. That is why records should be stored in files.

14. What point to remember when OPEN a file in OUTPUT mode?

Ans:The point is to be remember Never open an existing file in output mode, as it will immediately erased the existing file and a new blank file is created with the same name. All the records stored in file will be lost and can not recalled again. So when a new file is created a new file name must be used.

15. What is the main difference between CLOSE & CLOSE #1 command?

Ans:CLOSE statement with file number close the given file number. While CLOSE statements without any file number close all the files is in use.

16. How does a program file is different from Data file?

Ans:A program file is basically a program, i.e. a set of instruction fed into the computer to do a particular task. While a data file is a collection of records stored in a storage device. A data file is the product of execution of the program file.

17. What are the three modes in sequential file?

Ans:The three modes are: OUTPUT mode for creation, APPEND mode for adding records and INPUT mode for reading.

18. What is the function of channel number in an OPEN statement of filing in BASIC?

Ans:OPEN statement has two identifications one the name and the other is channel number. A file is recognised in the internal memory by the channel.

19. What task does the CLOSE command perform in sequential file?

Ans:CLOSE command closes all open sequential files in Basic.

20. Differentiate between input file mode and output file mode.

Ans:In Input file mode we can only read the data from file. Where as in Output file we can only write the data on to the file.