शिक्षकों के लिए: File Handling (Computer Science, CIE) के लिए इस्तेमाल के लिए तैयार लेसन स्लाइड्स, रिवीज़न नोट्स — इन्हें अपने लेसन में इस्तेमाल करें, या टॉपिक को एक इंटरैक्टिव क्लास एक्टिविटी की तरह चलाएं जिसे आपके शिक्षार्थी लाइव गेम की तरह खेलें।
फ्री प्रीव्यू — 57 में से 8 सवाल। सभी देखने के लिए साइन अप करें।
1.What does the 'r' mode in file handling allow you to do?
Easy
ARead from a file only
BWrite to a new file
CAppend to an existing file
DRead and write to a file
2.Which file mode would you use to add data to the end of an existing file without overwriting it?
Easy
A'w'
B'a'
C'r'
D'x'
3.What is the purpose of the CLOSEFILE command in pseudocode?
Easy
ATo delete the file from the disk
BTo release the file from the program's control
CTo clear all data in the file
DTo open the file for reading
4.A program uses the pseudocode statement: OPENFILE "data.txt" FOR WRITE. What happens if "data.txt" already exists?
Easy
AThe program appends data to the end of the file
BThe program overwrites the existing contents of the file
CThe program raises an error and stops
DThe program reads the existing contents first
5.In the following pseudocode, what is the purpose of the variable endOfFile?
Easy
ATo count the number of lines read
BTo signal when there is no more data to read
CTo store the last line of the file
DTo indicate the size of the file
6.A text file contains employee records with each record spread over four lines: name, department, salary, age. Which pseudocode correctly reads one record?
Medium
AREADFILE "employees.txt", name
READFILE "employees.txt", department
READFILE "employees.txt", salary
READFILE "employees.txt", age
BREADFILE "employees.txt", name, department, salary, age
COPENFILE "employees.txt" FOR READ
READFILE "employees.txt", name, department, salary, age
DREADFILE "employees.txt", record
SPLIT record INTO name, department, salary, age
7.Consider the pseudocode:
OPENFILE "data.txt" FOR READ
WHILE NOT EOF
READFILE "data.txt", line
IF line = "" THEN
BREAK
ENDIF
OUTPUT line
ENDWHILE
CLOSEFILE "data.txt"
What is the purpose of the IF statement checking for an empty line?
Medium
ATo skip blank lines in the file
BTo detect the end of the file
CTo count the number of lines
DTo remove empty lines from the output
8.Which Python statement correctly opens a file named 'students.txt' for appending?
Easy
Afile = open('students.txt', 'a')
Bfile = open('students.txt', 'w')
Cfile = open('students.txt', 'r')
Dfile = open('students.txt', 'append')
और सवाल
9.A text file 'scores.txt' contains numbers, one per line. Which pseudocode correctly calculates the total of all numbers?
Medium
Atotal ← 0
OPENFILE "scores.txt" FOR READ
READFILE "scores.txt", num
WHILE num ≠ ""
total ← total + num
READFILE "scores.txt", num
ENDWHILE
CLOSEFILE "scores.txt"
Btotal ← 0
OPENFILE "scores.txt" FOR READ
READFILE "scores.txt", num
WHILE num ≠ ""
total ← total + CONVERT(num)
READFILE "scores.txt", num
ENDWHILE
CLOSEFILE "scores.txt"
Ctotal ← 0
OPENFILE "scores.txt" FOR READ
WHILE NOT EOF
READFILE "scores.txt", num
total ← total + num
ENDWHILE
CLOSEFILE "scores.txt"
Dtotal ← 0
OPENFILE "scores.txt" FOR READ
READFILE "scores.txt", num
total ← total + CONVERT(num)
CLOSEFILE "scores.txt"
10.What does the TRIM function do in the pseudocode example?
Easy
ARemoves the first character of a string
BRemoves extra spaces and newline characters from the beginning and end of a string
CConverts a string to uppercase
DExtracts a portion of a string
11.Which file mode would you use to create a new file or overwrite an existing one?
Easy
A'r'
B'a'
C'w'
D'rw'
12.In the pseudocode for writing to a file, why is NEWLINE written after each field?
Easy
ATo separate records so they can be read back correctly
BTo make the file appear visually neat
CTo increase file size
DTo ensure the file is closed properly
13.A program reads data from a file named 'data.txt' using the following pseudocode:
OPENFILE "data.txt" FOR READ
WHILE NOT EOF
READFILE "data.txt", line
OUTPUT line
ENDWHILE
CLOSEFILE "data.txt"
The file 'data.txt' contains the numbers 5, 10, and 15 each on a separate line. How many lines will be output?
Easy
A3
B2
C4
D1
14.A text file 'grades.txt' stores student grades in the format: name, grade, name, grade, ... each on separate lines. The following pseudocode is used to read the file and calculate the average grade:
OPENFILE "grades.txt" FOR READ
total = 0
count = 0
WHILE NOT EOF
READFILE "grades.txt", name
READFILE "grades.txt", grade
total = total + grade
count = count + 1
ENDWHILE
CLOSEFILE "grades.txt"
average = total / count
What is the output if the file contains:
Anna
85
Ben
92
?
Medium
A88.5
B85
C92
DError: cannot divide by zero
15.Which Python statement correctly opens a file named 'data.txt' for reading?
Easy
Afile = open("data.txt", "r")
Bfile = open("data.txt", "w")
Cfile = open("data.txt", "a")
Dfile = open("data.txt", "read")
16.A program uses the following pseudocode to write to a file:
OPENFILE "output.txt" FOR WRITE
WRITEFILE "output.txt", "Hello"
WRITEFILE "output.txt", NEWLINE
WRITEFILE "output.txt", "World"
CLOSEFILE "output.txt"
What will the file contain after execution?
Easy
AHello
World
BHelloWorld
CHello World
DHello
World
17.A text file 'scores.txt' contains the following numbers, one per line:
10
20
30
40
Which pseudocode correctly calculates the sum of all numbers?
Medium
Atotal = 0
OPENFILE "scores.txt" FOR READ
WHILE NOT EOF
READFILE "scores.txt", num
total = total + num
ENDWHILE
CLOSEFILE "scores.txt"
Btotal = 0
OPENFILE "scores.txt" FOR READ
FOR i = 1 TO 4
READFILE "scores.txt", num
total = total + num
ENDFOR
CLOSEFILE "scores.txt"
Ctotal = 0
OPENFILE "scores.txt" FOR READ
REPEAT
READFILE "scores.txt", num
total = total + num
UNTIL EOF
CLOSEFILE "scores.txt"
Dtotal = 0
OPENFILE "scores.txt" FOR READ
WHILE NOT EOF
READFILE "scores.txt", num
total = total + 1
ENDWHILE
CLOSEFILE "scores.txt"
18.What is the purpose of the TRIM function in the pseudocode example for reading employee data?
Easy
ATo remove extra spaces and newline characters from the read data
BTo convert the data to uppercase
CTo check if the data is empty
DTo split the data into separate fields
19.Which file mode should be used to add data to the end of an existing file without deleting its current contents?
Easy
AAPPEND
BWRITE
CREAD
DUPDATE
20.Consider the following pseudocode:
OPENFILE "data.txt" FOR READ
WHILE NOT EOF
READFILE "data.txt", line
IF line = "" THEN
BREAK
ENDIF
OUTPUT line
ENDWHILE
CLOSEFILE "data.txt"
What will be output if the file contains:
apple
banana
?
Medium
Aapple
Bapple banana
Capple
banana
Dapple
banana
21.A file 'data.txt' contains the following records, each on a separate line:
ID:101
Name:Alice
Score:85
ID:102
Name:Bob
Score:92
Which pseudocode correctly reads all records and outputs the name of each student?
Medium
AOPENFILE "data.txt" FOR READ
WHILE NOT EOF
READFILE "data.txt", id
READFILE "data.txt", name
READFILE "data.txt", score
OUTPUT name
ENDWHILE
CLOSEFILE "data.txt"
BOPENFILE "data.txt" FOR READ
WHILE NOT EOF
READFILE "data.txt", line
IF line CONTAINS "Name:" THEN
OUTPUT line
ENDIF
ENDWHILE
CLOSEFILE "data.txt"
COPENFILE "data.txt" FOR READ
FOR i = 1 TO 2
READFILE "data.txt", id
READFILE "data.txt", name
READFILE "data.txt", score
OUTPUT name
ENDFOR
CLOSEFILE "data.txt"
DOPENFILE "data.txt" FOR READ
WHILE NOT EOF
READFILE "data.txt", id
READFILE "data.txt", name
READFILE "data.txt", score
IF id CONTAINS "ID" THEN
OUTPUT name
ENDIF
ENDWHILE
CLOSEFILE "data.txt"
22.What does the CLOSEFILE command do?
Easy
AIt closes the file and releases system resources
BIt deletes the file from the disk
CIt saves the file to a new location
DIt clears the contents of the file
23.A program opens a file with OPENFILE "data.txt" FOR WRITE. What happens if the file already exists?
Easy
AThe existing contents are overwritten
BThe new data is appended to the end
CAn error occurs and the file is not opened
DThe file is renamed and a new file is created
24.A text file 'log.txt' contains timestamps and events. Each line has a timestamp followed by a space and an event. Which pseudocode correctly counts the number of events that contain the word 'ERROR'?
Medium
Acount = 0
OPENFILE "log.txt" FOR READ
WHILE NOT EOF
READFILE "log.txt", line
IF line CONTAINS "ERROR" THEN
count = count + 1
ENDIF
ENDWHILE
CLOSEFILE "log.txt"
Bcount = 0
OPENFILE "log.txt" FOR READ
WHILE NOT EOF
READFILE "log.txt", line
IF line = "ERROR" THEN
count = count + 1
ENDIF
ENDWHILE
CLOSEFILE "log.txt"
Ccount = 0
OPENFILE "log.txt" FOR READ
WHILE NOT EOF
READFILE "log.txt", timestamp
READFILE "log.txt", event
IF event = "ERROR" THEN
count = count + 1
ENDIF
ENDWHILE
CLOSEFILE "log.txt"
Dcount = 0
OPENFILE "log.txt" FOR READ
WHILE NOT EOF
READFILE "log.txt", line
IF line INCLUDES "ERROR" THEN
count = count + 1
ENDIF
ENDWHILE
CLOSEFILE "log.txt"
25.A school uses a text file to store student names and marks, one record per line in the format 'Name,Mark'. Which pseudocode correctly reads the file and outputs the name and mark for each student?
Hard
AOPENFILE "students.txt" FOR READ
WHILE NOT EOF
READFILE "students.txt", line
name ← line[1]
mark ← line[2]
OUTPUT name, mark
ENDWHILE
CLOSEFILE "students.txt"
BOPENFILE "students.txt" FOR READ
WHILE NOT EOF
READFILE "students.txt", name
READFILE "students.txt", mark
OUTPUT name, mark
ENDWHILE
CLOSEFILE "students.txt"
COPENFILE "students.txt" FOR READ
WHILE NOT EOF
READFILE "students.txt", line
name ← TRIM(line)
mark ← TRIM(line)
OUTPUT name, mark
ENDWHILE
CLOSEFILE "students.txt"
DOPENFILE "students.txt" FOR READ
WHILE NOT EOF
READFILE "students.txt", line
name ← SUBSTRING(line, 1, COMMAPOS-1)
mark ← SUBSTRING(line, COMMAPOS+1)
OUTPUT name, mark
ENDWHILE
CLOSEFILE "students.txt"
26.A file 'log.txt' contains multiple lines of text. The following pseudocode is intended to count the number of lines that contain the word 'ERROR'. Which line of pseudocode is missing?
OPENFILE "log.txt" FOR READ
count ← 0
WHILE NOT EOF
READFILE "log.txt", line
line ← TRIM(line)
IF line CONTAINS "ERROR" THEN
count ← count + 1
ENDIF
ENDWHILE
CLOSEFILE "log.txt"
OUTPUT count
Medium
AA statement to initialise count to 0 is missing before the loop.
BA statement to close the file is missing after the loop.
CA statement to set endOfFile flag is missing inside the loop when line is empty.
DNo line is missing; the pseudocode is correct.
27.A programmer writes the following Python code to read from a file:
file = open("data.txt", "r")
for line in file:
print(line.strip())
file.close()
What is the main advantage of using a 'for line in file' loop over a 'while not endOfFile' loop with readline()?
Medium
AThe for loop automatically handles closing the file.
BThe for loop is faster because it reads the entire file at once.
CThe for loop automatically stops at the end of the file without needing an explicit check.
DThe for loop can only be used with text files, not binary files.
28.What is the purpose of the TRIM function in the pseudocode for reading employee data from a text file?
Easy
ATo remove any leading and trailing spaces or newline characters from the string.
BTo cut the string to a specified length.
CTo convert the string to uppercase.
DTo check if the string is empty.
29.A text file 'marks.txt' contains student marks, one per line. The following pseudocode reads the file and outputs the average mark. Which line contains an error?
1 OPENFILE "marks.txt" FOR READ
2 total ← 0
3 count ← 0
4 WHILE NOT EOF
5 READFILE "marks.txt", mark
6 total ← total + mark
7 count ← count + 1
8 ENDWHILE
9 CLOSEFILE "marks.txt"
10 average ← total / count
11 OUTPUT average
Medium
ALine 2: total should be initialised to 0.0
BLine 6: mark is a string, so it cannot be added to total without conversion.
CLine 10: average should be calculated inside the loop.
DLine 11: OUTPUT should be PRINT.
30.A program needs to add new records to the end of an existing file without deleting any existing data. Which file mode should be used?
Easy
AREAD
BWRITE
CAPPEND
DUPDATE
31.A text file 'records.txt' stores data in the format: each record occupies 3 lines (ID, Name, Score). The following pseudocode is intended to read all records and output the name of the student with the highest score. Which line contains a logical error?
1 OPENFILE "records.txt" FOR READ
2 maxScore ← -1
3 maxName ← ""
4 WHILE NOT EOF
5 READFILE "records.txt", id
6 READFILE "records.txt", name
7 READFILE "records.txt", score
8 IF score > maxScore THEN
9 maxScore ← score
10 maxName ← name
11 ENDIF
12 ENDWHILE
13 CLOSEFILE "records.txt"
14 OUTPUT maxName
Medium
ALine 2: maxScore should be initialised to 0.
BLine 8: score is a string, so comparison may not work as expected.
CLine 5: id is not used, so it should be omitted.
DLine 14: OUTPUT should be PRINT.
32.In the pseudocode for writing employee data to a file, why is NEWLINE written after each field?
Easy
ATo separate fields within a record so that each field appears on a new line.
BTo mark the end of the file.
CTo add blank lines for readability.
DTo prevent the file from being overwritten.
33.A program uses the following pseudocode to read a file 'data.txt' that contains numbers, one per line. It is supposed to output the sum of all numbers. However, it outputs 0. What is the most likely cause?
OPENFILE "data.txt" FOR READ
sum ← 0
WHILE NOT EOF
READFILE "data.txt", num
sum ← sum + num
ENDWHILE
CLOSEFILE "data.txt"
OUTPUT sum
Medium
AThe variable sum should be initialised to 0.0 instead of 0.
BThe file 'data.txt' does not exist, so the OPENFILE fails silently.
CThe variable num is a string, so the addition concatenates strings instead of adding numbers.
DThe WHILE loop never executes because EOF is true at the start.
34.What does the command OPENFILE do in pseudocode?
Easy
AIt creates a connection to a file so that it can be read from or written to.
BIt displays the contents of a file on the screen.
CIt deletes the file from the storage device.
DIt checks whether a file exists without opening it.
35.Which pseudocode command is used to close a file after processing?
Easy
ACLOSEFILE
BEND
CSTOP
DEXIT
36.In pseudocode, what is the purpose of the TRIM function when reading data from a file?
Easy
AIt removes extra spaces and newline characters from the beginning and end of a string.
BIt cuts the string to a specified length.
CIt converts the string to uppercase.
DIt checks if the string is empty.
37.A text file 'students.txt' stores each student's name and age on separate lines, repeating for all students. Which pseudocode correctly reads all records and outputs the names?
Hard
AOPENFILE "students.txt" FOR READ
endOfFile ← FALSE
WHILE NOT endOfFile DO
READFILE "students.txt", name
name ← TRIM(name)
READFILE "students.txt", age
age ← TRIM(age)
IF name = "" THEN
endOfFile ← TRUE
ELSE
OUTPUT name
ENDIF
ENDWHILE
CLOSEFILE "students.txt"
BOPENFILE "students.txt" FOR READ
WHILE NOT EOF
READFILE "students.txt", line
OUTPUT line
ENDWHILE
CLOSEFILE "students.txt"
COPENFILE "students.txt" FOR READ
REPEAT
READFILE "students.txt", name
OUTPUT name
READFILE "students.txt", age
UNTIL EOF
CLOSEFILE "students.txt"
DOPENFILE "students.txt" FOR READ
FOR i ← 1 TO 10
READFILE "students.txt", name
OUTPUT name
ENDFOR
CLOSEFILE "students.txt"
38.Consider the following pseudocode:
OPENFILE "data.txt" FOR WRITE
WRITEFILE "data.txt", "Hello"
WRITEFILE "data.txt", NEWLINE
WRITEFILE "data.txt", "World"
CLOSEFILE "data.txt"
What will be the contents of 'data.txt'?
Easy
AHello
World
BHelloWorld
CHello World
39.In Python, which mode string should be used to open a file for reading?
Easy
A"r"
B"w"
C"a"
D"rw"
40.What happens if you open a file in write mode ("w") in Python and the file already exists?
Easy
AThe existing contents are erased and the file is overwritten.
BNew data is appended to the end of the file.
CAn error occurs and the file is not opened.
DThe file is opened but no operations are allowed.
41.What is the purpose of the OPENFILE command in pseudocode?
Easy
ATo create a new file and start writing data
BTo prepare a file for reading or writing operations
CTo delete a file from the disk
DTo rename an existing file
42.Which of the following is a valid reason to use the TRIM function when reading data from a file?
Easy
ATo convert the data to uppercase
BTo remove extra spaces and newline characters
CTo check if the file has ended
DTo count the number of characters in the data
43.In pseudocode, the command CLOSEFILE is used to close a file after all operations are complete.
Easy
True or false?
44.A text file 'students.txt' stores student names and grades, one record per line in the format 'Name,Grade'. Which pseudocode correctly reads the file and outputs the name and grade for each student?
Medium
AOPENFILE "students.txt" FOR READ
WHILE NOT EOF
READFILE "students.txt", name
READFILE "students.txt", grade
OUTPUT name & grade
ENDWHILE
CLOSEFILE "students.txt"
BOPENFILE "students.txt" FOR READ
WHILE NOT EOF
READFILE "students.txt", line
name ← line
grade ← line
OUTPUT name & grade
ENDWHILE
CLOSEFILE "students.txt"
COPENFILE "students.txt" FOR READ
WHILE NOT EOF
READFILE "students.txt", line
name ← TRIM(line)
grade ← TRIM(line)
OUTPUT name & grade
ENDWHILE
CLOSEFILE "students.txt"
DOPENFILE "students.txt" FOR READ
WHILE NOT EOF
READFILE "students.txt", line
name ← line[1]
grade ← line[2]
OUTPUT name & grade
ENDWHILE
CLOSEFILE "students.txt"
45.Which of the following are valid file modes in Python? (Select all that apply)
Easy
A"r"
B"w"
C"a"
D"rw"
E"x"
46.Arrange the following steps in the correct order for writing data to a file in pseudocode:
Easy
WRITEFILE "file.txt", data
OPENFILE "file.txt" FOR WRITE
CLOSEFILE "file.txt"
WRITEFILE "file.txt", NEWLINE
47.Match each pseudocode command with its description:
Easy
OPENFILE
READFILE
WRITEFILE
CLOSEFILE
Reads a line from a file
Opens a file for processing
Closes an open file
Writes data to a file
48.A program uses the following pseudocode to append data to a file. What must be the current state of the file 'log.txt'?
OPENFILE "log.txt" FOR APPEND
WRITEFILE "log.txt", "New entry"
CLOSEFILE "log.txt"
Easy
AThe file must already exist
BThe file must not exist
CThe file must be empty
DThe file must be open in another program
49.A text file 'data.txt' contains the following data:
10
20
30
40
Which pseudocode correctly calculates the sum of all numbers in the file?
Medium
AOPENFILE "data.txt" FOR READ
sum ← 0
WHILE NOT EOF
READFILE "data.txt", num
sum ← sum + num
ENDWHILE
CLOSEFILE "data.txt"
BOPENFILE "data.txt" FOR READ
sum ← 0
WHILE NOT EOF
READFILE "data.txt", num
num ← TRIM(num)
sum ← sum + num
ENDWHILE
CLOSEFILE "data.txt"
COPENFILE "data.txt" FOR READ
sum ← 0
WHILE NOT EOF
READFILE "data.txt", num
num ← TRIM(num)
sum ← sum + num
ENDWHILE
DOPENFILE "data.txt" FOR READ
sum ← 0
FOR i ← 1 TO 4
READFILE "data.txt", num
sum ← sum + num
ENDFOR
CLOSEFILE "data.txt"
50.Match each pseudocode command with its correct description.
Easy
OPENFILE
READFILE
WRITEFILE
CLOSEFILE
Closes a file after processing
Reads a line of data from a file
Opens a file for reading, writing or appending
Writes data to a file
51.Complete the pseudocode to open a file named 'data.txt' for reading.
Easy
____ "data.txt" FOR ____
52.A text file 'numbers.txt' contains the numbers 10, 20, 30, 40 each on a separate line. The following pseudocode is executed. What is the value of total after execution?
OPENFILE "numbers.txt" FOR READ
total ← 0
WHILE NOT EOF
READFILE "numbers.txt", num
total ← total + num
ENDWHILE
CLOSEFILE "numbers.txt"
OUTPUT total
Medium
53.What does the acronym EOF stand for in file handling?
Easy
54.A text file 'numbers.txt' contains the numbers 10, 20, 30, 40 each on a separate line. The following pseudocode is executed. What is the value of total after execution?
OPENFILE "numbers.txt" FOR READ
total ← 0
WHILE NOT EOF
READFILE "numbers.txt", num
num ← TRIM(num)
total ← total + num
ENDWHILE
CLOSEFILE "numbers.txt"
OUTPUT total
Medium
55.Explain why it is important to close a file after reading or writing operations.
Medium
56.A text file 'numbers.txt' contains the numbers 10, 20, 30, 40 each on a separate line. The following pseudocode is executed. What is the value of total after execution?
OPENFILE "numbers.txt" FOR READ
total ← 0
WHILE NOT EOF
READFILE "numbers.txt", num
total ← total + num
ENDWHILE
CLOSEFILE "numbers.txt"
OUTPUT total
Medium
A100
B90
C120
D110
57.A text file 'numbers.txt' contains the numbers 10, 20, 30, 40 each on a separate line. The following pseudocode is executed. What is the value of total after execution?
OPENFILE "numbers.txt" FOR READ
total ← 0
WHILE NOT EOF
READFILE "numbers.txt", num
num ← TRIM(num)
total ← total + num
ENDWHILE
CLOSEFILE "numbers.txt"
OUTPUT total
Medium
A100
B110
C90
D120
Unlock all 57 questions & more
इस टॉपिक के हर सवाल, स्लाइड्स, फ्लैशकार्ड और रिवीज़न नोट्स देखने के लिए फ्री अकाउंट बनाएं।