1a) Define loop and
nested loop.
b) Write down the output
of the following program.
CLS
FOR B = 1 TO 5
READ N$
M$ = M$ + MID$(N$, B, 1)
NEXT B
PRINT M$
DATA NATURE, TECHNO, HAPPY, GOPAL, PUPILS
END
c) Re-write the
following program correcting the bugs:
REM
to check if a word is palindrome
INPUT N$
FOR K = LEN(N$) TO 1
W$ = S$ + MID$(N$, K, 1)
NEXT K
IF N$ = W$ THEN
PRINT "Palindrome"
OTHERWISE
PRINT "Not Palindrome"
END
d) Re-write the
following program using DO……..LOOP UNTIL
CLS
FOR J = 20 TO 2 STEP -2
S = S + J
NEXT J
PRINT "Sum of even numbers"; S
END
e) Write a program using
FUNCTION….END FUNCTION statement to display the longest string among three input
strings.
f) A sequential data
file “EXAM.DAT” contains name, address, phone number and age of some people.
Write a program to display only those records which contains age above 18 and
address is ‘Lumbini’.
Answers SET G
1a) Repeating statements
or statement blocks for certain number of times depending upon the condition is
called loop.
A
loop that lies within another loop is called nested loop.
b) Output:
NEPAL
c) Debugged Program
REM to check if a word is palindeome
INPUT N$
FOR K = LEN(N$) TO 1 STEP -1
W$ = W$ + MID$(N$, K, 1)
NEXT K
IF N$ = W$ THEN
PRINT "Palindrome"
ELSE
PRINT "Not Palindrome"
END IF
END
d) program using
DO……..LOOP UNTIL
CLS
J = 20
DO
S = S + J
J = J - 2
LOOP UNTIL J < 2
PRINT "Sum of even numbers"; S
END
e) Write a program using
FUNCTION….END FUNCTION statement to display the longest string among three
input strings.
DECLARE FUNCTION LON$( A$, B$, C$)
CLS
INPUT "ENTER FIRST STRING"; A$
INPUT "ENTER SECOND STRING"; B$
INPUT "ENTER THIRD STRING"; C$
PRINT "LONGEST STRING="; LON$( A$, B$, C$)
END
FUNCTION LON$( A$, B$, C$)
IF LEN(A$) > LEN(B$) AND LEN(A$) > LEN(C$) THEN
G$ = A$
IF LEN(B$) > LEN(A$) AND LEN(B$) > LEN(C$) THEN
G$ = B$
ELSE
G$ = C$
END IF
LON$ = S$
END FUNCTION
f) A sequential data
file “EXAM.DAT” contains name, address, phone number and age of some people.
Write a program to display only those records which contains age above 18 and
address is ‘Lumbini’.
OPEN “EXAM.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, A$, P#, A
IF UCASE$(A$) = ”LUMBINI” AND A>18 THEN
PRINT N$, A$, P#, A
END IF
WEND
CLOSE#1
END
***
DOWNLOADED FROM SUSHILUPRETI.COM.NP
1a) What is variable?
List the types of numeric data.
b) Study the following program
and answer the following questions:
OPEN “detail.dat” FOR INPUT AS #1
OPEN
“temp.dat” FOR OUTPUT AS #2
CLS
INPUT
“Name of the students “; s$
FOR
i = 1 to 10
INPUT
#1, n$, c, a
IF
s$ <> n$ THEN
WRITE
#2, n$, c, a
END
IF
NEXT
i
CLOSE
#1, #2
KILL
“detail.dat”
NAME
“temp.dat” AS “detail.dat”
END
Questions:
i) What is the main objective of the above program?
ii) Will the program run if the statement KILL
“detail.dat” is removed? Why?
iii) If the data file “detail.dat” contains only 8
records, then the program will run? Why?
iv) Why the file “temp.dat” is necessary in this
program?
c) Write down the output
of the following program.
CLS
FOR X = 2 TO 15 STEP 2
IF X >= 11 THEN EXIT FOR
PRINT X ^ 2 + 1
NEXT X
PRINT "Done"
END
d) Re-write the
following program correcting the bugs:
CLS
REM TO PRINT THE LONGEST STRING
DATA TOURISM, TRANSPORT, SERVICE, AGRICULTURE, TRADE
READ L$
FOR C = 1 TO 5
READ X$
IF LEN(X$) >LEN(L$) THEN X$ = L$
NEXT C
PRINT "LONGEST STRING IS: L$"
END
e) Re-write the
following program using FOR…NEXT
CLS
N = 1
X = 1
BACK:
PRINT N, N ^ 2
N = N + 2
X = X + 1
IF X <= 8 THEN GOTO BACK
END
f) Write a program to
print alternate characters (starting from the first character of the user given
string.
g) Write a program to
ask student’s name, class and marks secured in three subjects. Store the data
in a sequential data file ‘SCHOOL.DAT’ along with total marks. Make a provision
to ask the user to enter another record.
Answers SET H
1a) A variable is an
identifier or reference or name for the memory address that holds data and can
change its value during the execution of a program. The types of numeric data
are:
i) Integer data
ii) Long integer data
iii) Single precision data
iv) Double precision data
b) Questions:
i) What is the main objective of the above program?
Ans: The main objective
of the above program is to delete the records of students according to the
supplied name of the students.
ii) Will the program run if the statement KILL
“detail.dat” is removed? Why?
Ans: No, the program
will not run because if detail.dat file is not removed then the next statement
while renaming the file of temp.dat to detail.dat, it displays the error
message as “File already exists” because detail.dat file is already existed and
it cannot rename temp.dat file to detail.dat.
iii) If the data file “detail.dat” contains only 8
records, then the program will run? Why?
Ans: No, the program
will not run and it displays the error message as “Input past end of file”
iv) Why the file “temp.dat” is necessary in this
program?
Ans: The file temp.dat
is necessary in this program to store the non deleted records.
c) Output:
5
17
37
65
101
Done
d) Debugged Program
CLS
REM TO PRINT THE LONGEST STRING
DATA TOURISM, TRANSPORT, SERVICE, AGRICULTURE, TRADE
READ L$
FOR C = 2 TO 5
READ X$
IF LEN(X$) >LEN(L$) THEN L$ = X$
NEXT C
PRINT "LONGEST STRING IS:"; L$
END
e) program using
FOR…NEXT
CLS
N = 1
FOR X = 1 TO 8
PRINT N, N ^ 2
N = N + 2
NEXT X
END
f) Write a program to
print alternate characters (starting from the first character) of the user
given string.
DECLARE
SUB ALT(S$)
CLS
INPUT "ENTER ANY STRING"; S$
CALL ALT(S$)
END
SUB ALT(S$)
FOR I = 1 TO LEN(S$) STEP 2
PRINT MID$(S$, I, 1);
NEXT I
END SUB
g) Write a program to
ask student’s name, class and marks secured in three subjects. Store the data
in a sequential data file ‘SCHOOL.DAT’ along with total marks. Make a provision
to ask the user to enter another record.
OPEN “STUDENT.DATA” FOR OUTPUT AS #1
DO
CLS
INPUT “ENTER STUDENT’S NAME”; N$
INPUT “ENTER CLASS”; C
INPUT “ENTER MARKS SECURED IN THREE SUBJECTS”; M1. M2, M3
INPUT “ENTER ROLL NUMBER”; R
T = M1 + M2 + M3
WRITE #1, N$, C, M1, M2, M3, T
INPUT “DO YOU WANT TO CONTINUE”; CH$
LOOP WHILE UCASE$(CH$)=”Y”
CLOSE #1
END