Chapter-13: File Handling/Debug/Class 10

1. Debug the following programs and test them on a computer.

  1. REM to add or append

OPEN “C:\SAL.DAT” FOR OUTPUT AS #1
DO
CLS
INPUT “ENTER NAME” ; N$
INPUT “ENTER POST” ; P$
INPUT “ENTER SALARY” ; S
WRITE #2, N$ , P$, S
INPUT “ WANTS TO SUPPLY MORE Y/N” ; CH
LOOP WHILE UCASE$(CH)= “Y”
CLOSE #2,#1
END
Corrected Program
REM to add or append
OPEN “C:\SAL.DAT” FOR APPEND AS #1
CLS
DO
INPUT “ENTER NAME” ; N$
INPUT “ENTER POST” ; P$
INPUT “ENTER SALARY” ; S
WRITE #1, N$ , P$, S
INPUT “ WANTS TO SUPPLY MORE Y/N” ; CH$
LOOP WHILE UCASE$(CH$)= “Y”
CLOSE #1 
END


  1. REM to copy data from “SAL.DAT” to “TEMP.DAT”

OPEN “SAL.DAT” FOR INPUT AS #1

OPEN “TEMP.DAT” FOR OUTPUT AS #2

CLS

DO

INPUT #2,N$,P$,S

WRITE # 2, N$ , P$, S

LOOP WHILE NOT EOF( )

CLOSE #2,#1

END

Corrected Program

REM to copy data from “SAL.DAT” to “TEMP.DAT”

OPEN “SAL.DAT” FOR INPUT AS #1

OPEN “TEMP.DAT” FOR OUTPUT AS #2

CLS

DO

INPUT #1 , N$ , P$ , S

WRITE # 2 , N$ , P$ , S

LOOP WHILE NOT EOF( 1 )

CLOSE #1 , #2

END

1. Re-write the given program after correcting the bugs.

1. Re-write the given program after correcting the bugs.

REM Sub program to find average of three numbers Declare sub average (a, b, c) Input "Enter any three numbers" , a, b ,c Call sub average(a, b, c) End Sub average(a, b, c) av =(a + b + c) ÷ 3 Display "Average of three numbers is" ; av Sub End
Answer
Error (bug)Correction
Call Sub average(a,b,c)Call average(a,b,c)
av =(a + b + c) ÷ 3av =(a + b + c) / 3
Display ...Print ...
Sub EndEnd Sub

After correction the correct program is:
REM Sub program to find average of three numbers Declare sub average (a, b, c) Input "Enter any three numbers" , a, b ,c Call average(a, b, c) End Sub average(a, b, c) av =(a + b + c) / 3 Print "Average of three numbers is" ; av End Sub

2. Re-write the given program after correcting the bugs.

SEE (Supplementary/Upgrade) 2073 (2017)

DECLARE SUB Series() CLS EXECUTE Series END SUB Series REM program to print 4 8 12 20 .... 20th terms X = 4 Y = 4 FOR ctr = 10 to 1 DISPLAY X;Y; X=X+Y Y=X+Y Next ctr End Series

Answer
Error (bug)Correction
Execute SeriesCALL Series
For Ctr= 10 to 1For ctr=10 to 1 step-1
Display X;Y;PRINT X;Y;
End SeriesEND SUB
After correction the correct program is:
DECLARE SUB Series() CLS CALL Series END SUB Series REM program to print 4 8 12 ... 20th terms X = 4 Y = 4 FOR ctr = 10 to 1 step-1 PRINT X;Y; X=X+Y Y=X+Y Next ctr END SUB

3. Re-write the given program after correcting the bugs.

SEE 2072 (2016)
FUNCTION SUM(M ,N) REM to print sum of two numbers A=6 B=7 DISPLAY sum(M ,N) END FUNCTION SUM(M,N) S= M + N S=SUM END FUNCTION
Answer:
Error (bug)Correction
FUNCTION SUM(M,N)DECLARE FUNCTION SUM(M,N)
Display SUM(M,N)Print SUM(A,B)
S=SUMSUM=S
After correction the correct program is:
DECLARE FUNCTION SUM(M ,N) REM to print sum of two numbers A=6 B=7 PRINT SUM(A,B) END FUNCTION SUM(M,N) S=M + N SUM=S END FUNCTION

4. Re-write the given program after correcting the bugs.

SEE 2074 (2018)
REM to find factorial of a given number DECLARE FUNCTION FACTO (N$) CLS INPUT "Enter a number" ; X PRINT “The factorial is “; FACTO(N) END FUNCTION FACTO(N) F=1 WHILE N=0 F=F*N N=N-1 WEND F=FACTO END FUNCTION
Answer:
Error (bug)Correction
Declare ....(N$)Declare...(N)
PRINT ...FACTO(N)PRINT ...FACTO(X)
WHILE N=0WHILE N<>0
F=FACTOFACTO=F
After correction the correct program is:
REM to find factorial of a given number DECLARE FUNCTION FACTO (N) CLS INPUT "Enter a number" ; X PRINT “The factorial is “; FACTO(X) END FUNCTION FACTO(N) F=1 WHILE N<>0 F=F*N N=N-1 WEND FACTO = F END FUNCTION

5. Re-write the given program after correcting the bugs.

CDC Model 2077 BS
REM to add record in an existing file. CLS Open "Record.dat" for output as #1 AA: Input “Enter Name, Class and Roll No” ; Nm$, Cl , Rn Input #2, Nm$, Cl , Rn Input "more records"; y$ If ucase$(y$)= "Y" then goto AA Close "Record.dat" End
Answer:
Error(bug)Correction
OUTPUTAPPEND
Input #2, Nm$, Cl , RnInput #1, Nm$, Cl , Rn
Close "Record.dat"Close #1

After correction the correct program is:

REM to add record in an existing file. CLS Open "Record.dat" for append as #1 AA: Input “Enter Name, Class and Roll No” ; Nm$, Cl , Rn Print #1, Nm$, Cl , Rn Input "more records"; y$ If ucase$(y$)= "Y" then goto AA Close #1 End

6. Re-write the given program after correcting the bugs.

REM to store name and age in a sequential data file "STD.DOC" OPEN STD.DOC FOR OUT AS #1 INPUT "ENTER NAME" ; N INPUT "ENTER AGE";A WRITE 1,N$,A CLOSE #1 END
Answer:
Error (bug)Correction
STD.DOC"STD.DOC"
OUTOUTPUT
INPUT "ENTER NAME" ; NINPUT "ENTER NAME" ; N$
WRITE 1,N$,AWRITE #1,N$,A

After correction the correct program is:

REM to store name and age in a sequential data file "STD.DOC" OPEN "STD.DOC" FOR OUTPUT AS #1 INPUT "ENTER NAME" ; N$ INPUT "ENTER AGE";A WRITE #1,N$,A CLOSE #1 END

7. Re-write the given program after correcting the bugs.

Vidhyarthi Publication Model Set 2076/77
REM to create a new data file CLS OPEN "ABC.DAT" FOR INPUT AS #1 DO INPUT "Enter Name, Roll No and Total"; N$,R,T INPUT #1, N$, R, T INPUT "Supply more records Y/N";Ch$ LOOP WHILE UCASE(Ch$)="Y" CLOSE #1 END
Answer:
Error (bug)Correction
OPEN "ABC.DAT" FOR INPUT AS #1OPEN "ABC.DAT" FOR OUTPUT AS #1
INPUT #1, N$, R, TWRITE #1, N$, R, T
LOOP WHILE UCASE(Ch$)="Y"LOOP WHILE UCASE$(ch$)="Y"

After correction the correct program is:

REM to create a new data file CLS OPEN "ABC.DAT" FOR OUTPUT AS #1 DO INPUT "Enter Name, Roll No and Total"; N$,R,T WRITE #1, N$, R, T INPUT "Supply more records Y/N";Ch$ LOOP WHILE UCASE$(Ch$)="Y" CLOSE #1 END

8. Re-write the given program after correcting the bugs.

SEE (Supplementary/Upgrade) 2074 (2018)
Rem To print only class 10 record from "student.dat" CLS OPEN "I",#2,"student.dat" WHILE NOT EOF (#2) WRITE #2, N$,C,R IF C=10 THEN DISPLAY N$,C,R END IF NEXT CLOSE #2 END
Answer:
Error (bug)Correction
WHILE NOT EOF (#2)WHILE NOT EOF (2)
WRITE #2, N$,C,RINPUT #2, N$,C,R
DISPLAY N$,C,RPRINT N$,C,R
NEXTWEND

After correction the correct program is:

Rem To print only class 10 record from "student.dat" CLS OPEN "I",#2,"student.dat" WHILE NOT EOF (2) INPUT #2, N$,C,R IF C=10 THEN PRINT N$,C,R END IF WEND CLOSE #2 END

10. Re-write the given program after correcting the bugs.

[SEE (Supplementary/Upgrade) 2075 (2019)]
REM program to read data from the data file. OPEN "STD.DAT" FOR OUTPUT AS # 1 CLS WHILE NOT EOF (#1) WRITE #1, N$,R, C, P PRINT N$, R,C, P WEND CLOSE "STD.DAT" END
Answer:
Error (bug)Correction
OPEN ....OUTPUTINPUT
WHILE....(#1)WHILE NOT EOF(1)
WRITE #1,...INPUT #1,....
CLOSE "STD.DAT"CLOSE #1

After correction the correct program is:

REM program to read data from the data file. OPEN "STD.DAT" FOR INPUT AS # 1 CLS WHILE NOT EOF (1) INPUT #1,N$, R, C, P PRINT N$, R,C, P WEND CLOSE #1 END

11. Re-write the given program after correcting the bugs.

REM TO delete a record from a data file OPEN "STD.DAT" FRO INPUT AS #1 OPEN "TEMP.DAT" FOR OUTPUT AS #2 INPUT "ENTER ROLL NUMBER TO DELETE";R DO WHILE NOT EOF(1) INPUT #1,R,N$,C IF R<>RN THEN WRITE #2,N$,C END IF LOOP CLOSE #1 KILL "STD.DAT" NAME "TEMP.DAT" AS "SECRET.DAT" END
Answer:
Error(bug)Correction
INPUT #1,R,N$,CINPUT #1,RN,N$,C
WRITE #2,N$,CWRITE #2,RN,N$,C
CLOSE #1CLOSE #1,#2
NAME "TEMP.DAT" AS "SECRET.DAT"NAME "TEMP.DAT" AS "STD.DAT"

After correction the correct program is:

REM TO delete a record from a data file OPEN "STD.DAT" FRO INPUT AS #1 OPEN "TEMP.DAT" FOR OUTPUT AS #2 INPUT "ENTER ROLL NUMBER TO DELETE";R DO WHILE NOT EOF(1) INPUT #1,RN,N$,C IF R<>RN THEN WRITE #2,RN,N$,C END IF LOOP CLOSE #1,$2 KILL "STD.DAT" NAME "TEMP.DAT" AS "STD.DAT" END

12. Re-write the given program after correcting the bugs.

SEE 2075 (2019)
DECLARE FUNCTION reverse$(N$) INPUT "Any string";N$ X$=reverse$(N$) PRINT N$ END FUNCTION reverse(N$) L=LEN$(N$) FOR X=L TO 1 STEP-1 A$=MID$(N$,X,1) B$=B$+A$ NEXT X B$=reverse$(N$) END FUNCTION
Answer:
Error(bug)Correction
PRINT N$PRINT X$
FUNCTION reverse(N$)FUNCTION reverse$(N$)
L=LEN$(N$)L=LEN(N$)
B$=reverse$(N$)reverse$=B$

After correction the correct program is:

DECLARE FUNCTION reverse$(N$) INPUT "Any string";N$ X$=reverse$(N$) PRINT X$ END FUNCTION reverse$(N$) L=LEN(N$) FOR X=L TO 1 STEP-1 A$=MID$(N$,X,1) B$=B$+A$ NEXT X reverse$=B$ END FUNCTION

13. Re-write the given program after correcting the bugs.

SEE 2073 (2017)
DECLARE SUB SUM (N) INPUT "Any Number"; N PRINT SUM (N) END SUB SUM (N) S = 0 WHILE N = 0 R = R MOD 10 S = S+R N = N/10 WEND PRINT "Sum of digits"; s END
Answer:
Error(bug)Correction
PRINT SUM (N)CALL SUM (N)
WHILE N =0WHILE N<>0
R = R MOD 10R = N MOD 10
N=N/10N=INT(N/10)

After correction the correct program is:

DECLARE SUB SUM (N) INPUT "Any Number"; N PRINT SUM (N) END SUB SUM (N) S = 0 WHILE N <> 0 R = R MOD 10 S = S+R N = INT(N/10) WEND PRINT "Sum of digits"; s END

 


Netra Koirala

Netra Koirala

Computer Science Educator

Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.

Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.

Featured Post

Grade 10 Computer Science: Specification Grid & Model Questions

Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...

Followers