Chapter-13: File Handling/Debug/Class 10
1. Debug the following programs and test them on a computer.
REM to add or append
OPEN “C:\SAL.DAT” FOR OUTPUT AS #1DOCLSINPUT “ENTER NAME” ; N$INPUT “ENTER POST” ; P$INPUT “ENTER SALARY” ; SWRITE #2, N$ , P$, SINPUT “ WANTS TO SUPPLY MORE Y/N” ; CHLOOP WHILE UCASE$(CH)= “Y”CLOSE #2,#1ENDCorrected ProgramREM to add or appendOPEN “C:\SAL.DAT” FOR APPEND AS #1CLSDOINPUT “ENTER NAME” ; N$INPUT “ENTER POST” ; P$INPUT “ENTER SALARY” ; SWRITE #1, N$ , P$, SINPUT “ WANTS TO SUPPLY MORE Y/N” ; CH$LOOP WHILE UCASE$(CH$)= “Y”CLOSE #1
END
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 EndAnswer
| Error (bug) | Correction |
|---|---|
| Call Sub average(a,b,c) | Call average(a,b,c) |
| av =(a + b + c) ÷ 3 | av =(a + b + c) / 3 |
| Display ... | Print ... |
| Sub End | End Sub |
After correction the correct program is:
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 Series | CALL Series |
| For Ctr= 10 to 1 | For ctr=10 to 1 step-1 |
| Display X;Y; | PRINT X;Y; |
| End Series | 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 FUNCTIONAnswer:
| Error (bug) | Correction |
|---|---|
| FUNCTION SUM(M,N) | DECLARE FUNCTION SUM(M,N) |
| Display SUM(M,N) | Print SUM(A,B) |
| S=SUM | SUM=S |
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 FUNCTIONAnswer:
| Error (bug) | Correction |
|---|---|
| Declare ....(N$) | Declare...(N) |
| PRINT ...FACTO(N) | PRINT ...FACTO(X) |
| WHILE N=0 | WHILE N<>0 |
| F=FACTO | FACTO=F |
5. Re-write the given program after correcting the bugs.
CDC Model 2077 BSREM 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"
EndAnswer:
| Error(bug) | Correction |
|---|---|
| OUTPUT | APPEND |
| Input #2, Nm$, Cl , Rn | Input #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
ENDAnswer:
| Error (bug) | Correction |
|---|---|
| STD.DOC | "STD.DOC" |
| OUT | OUTPUT |
| INPUT "ENTER NAME" ; N | INPUT "ENTER NAME" ; N$ |
| WRITE 1,N$,A | WRITE #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/77REM 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
ENDAnswer:
| Error (bug) | Correction |
|---|---|
| OPEN "ABC.DAT" FOR INPUT AS #1 | OPEN "ABC.DAT" FOR OUTPUT AS #1 |
| INPUT #1, N$, R, T | WRITE #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
ENDAnswer:
| Error (bug) | Correction |
|---|---|
| WHILE NOT EOF (#2) | WHILE NOT EOF (2) |
| WRITE #2, N$,C,R | INPUT #2, N$,C,R |
| DISPLAY N$,C,R | PRINT N$,C,R |
| NEXT | WEND |
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"
ENDAnswer:
| Error (bug) | Correction |
|---|---|
| OPEN ....OUTPUT | INPUT |
| 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"
ENDAnswer:
| Error(bug) | Correction |
|---|---|
| INPUT #1,R,N$,C | INPUT #1,RN,N$,C |
| WRITE #2,N$,C | WRITE #2,RN,N$,C |
| CLOSE #1 | CLOSE #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 FUNCTIONAnswer:
| 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
ENDAnswer:
| Error(bug) | Correction |
|---|---|
| PRINT SUM (N) | CALL SUM (N) |
| WHILE N =0 | WHILE N<>0 |
| R = R MOD 10 | R = N MOD 10 |
| N=N/10 | N=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
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.
LinkedIn ProfileRelated Posts
Loading related posts…
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...