All Exams  >   Humanities/Arts  >   Computer Science for Class 12  >   All Questions

All questions of File Handling for Humanities/Arts Exam

What happens when the following statement is executed and file1.txt is not present in the current directory?
file_obj = open("file1.txt", "r")
(1) Python creates a new file ‘file1.txt’
(2) Raises a FileNotFoundError
(3) Raises an IOError
(4) Does nothing
  • a)
    1
  • b)
    3
  • c)
    4
  • d)
    2
Correct answer is option 'D'. Can you explain this answer?

Anaya Patel answered
  • The code raises a FileNotFoundError because the file ‘file1.txt’ is not present in the current directory.
  • Since the file is being opened in read mode, the file must be available to be opened. If the file would be opened in write mode, then Python would have created a new file with the given name. But that does not happen for the read mode.
  • The IOError occurs when a file exists but it cannot be opened due to some reason.

Which one is the following advantage of using with clause while opening a file.
  • a)
    The file write automatically.
  • b)
    The file read automatically.
  • c)
    The file closed automatically.
  • d)
    The file delete automatically.
Correct answer is option 'C'. Can you explain this answer?

Advantage of Using With Clause While Opening a File: The File Closed Automatically

The with statement in Python is used to wrap the execution of a block of code with methods defined by a context manager. When it comes to file handling, the with statement is particularly useful as it ensures that the file is automatically closed after the block of code is executed. This has several advantages:

1. Automatic File Closure:
When a file is opened using the with statement, the file object is automatically closed at the end of the block. This eliminates the need for explicit calls to the file's close() method, ensuring that the file is always closed properly. By automatically closing the file, potential issues such as resource leaks or data corruption are avoided.

2. Exception Handling:
The with statement also provides a built-in exception handling mechanism. If an exception occurs within the block of code, the file is still closed before the exception is propagated further. This is crucial for preventing file-related errors from causing the program to terminate abruptly and potentially leaving the file in an inconsistent state.

3. Readability and Simplicity:
Using the with statement makes the code more readable and concise. The intention of opening and closing a file is clearly expressed in a single block, making the code easier to understand. It eliminates the need for additional try-finally blocks or explicit checks to ensure the file is closed.

4. Efficient Resource Management:
By automatically closing the file, the with statement ensures efficient resource management. It guarantees that system resources, such as file handles, are released promptly when they are no longer needed. This is especially important when dealing with a large number of files or when working with limited resources.

In summary, the with clause while opening a file provides the advantage of automatically closing the file after the block of code is executed. This ensures proper resource management, simplifies the code, and prevents potential errors or data corruption. By using the with statement, the file handling process becomes more efficient and less error-prone.

Which of the following option is true?
fp.seek(10, 1)
(1) Move file pointer ten characters behind from the current position.
(2) Move file pointer ten characters ahead from the current position.
(3) Move file pointer ten characters ahead from the beginning of a file.
(4) Move file pointer ten characters behind ahead from the end of a file.
  • a)
    1
  • b)
    3
  • c)
    4
  • d)
    2
Correct answer is option 'D'. Can you explain this answer?

Sai Dey answered
This question is related to the Python programming language and file handling. The correct answer is option 'D', which states that the `fp.seek(10, 1)` command moves the file pointer ten characters ahead from the current position.

Here is a detailed explanation:

1. Understanding the `seek()` function:
The `seek()` function in Python is used to change the position of the file pointer within a file. It takes two arguments: the offset and the reference point. The offset specifies the number of bytes to be moved, and the reference point determines from where the offset should be calculated.

2. The given command: `fp.seek(10, 1)`
The `fp.seek(10, 1)` command indicates that the file pointer `fp` needs to be moved ten characters ahead from the current position.

3. Explanation of the options:
a) Option 1: "Move file pointer ten characters behind from the current position."
This option is incorrect because the command `fp.seek(10, 1)` moves the file pointer ahead, not behind.

b) Option 2: "Move file pointer ten characters ahead from the current position."
This option is incorrect because it is essentially the same as the correct answer, option 'D'. Therefore, the correct answer cannot be option 'D'.

c) Option 3: "Move file pointer ten characters ahead from the beginning of a file."
This option is incorrect because the command `fp.seek(10, 1)` moves the file pointer from the current position, not from the beginning of the file.

d) Option 4: "Move file pointer ten characters behind ahead from the end of a file."
This option is incorrect because it does not accurately describe the functionality of the given command.

4. Final conclusion:
Considering the explanations above, it is evident that option 'D' is the correct answer. The command `fp.seek(10, 1)` moves the file pointer ten characters ahead from the current position.

The following code will perform which operation?
object.readline(10)
  • a)
    Read first 10 lines
  • b)
    Read first 10 characters of line
  • c)
    Read first 10 bits in line
  • d)
    Read first 10 words of line
Correct answer is option 'B'. Can you explain this answer?

Gaurav Kumar answered
Concept:
Python File readline() Method:
The readlines() are used to read all of the lines at once and return them as string elements in a list. This method is useful for tiny files since it reads the entire file content to memory and then splits it into individual lines. We may go through the list and use the strip() method to remove the newline 'n' character.
Syntax:
file.readline(size)
Here size is Optional. The number of bytes from the line to return. Default -1, which means the whole line.
Explanation:
object.readline(10)
readline reads a line of the file and returns it in the form of the string. It takes a parameter 10, which specifies the maximum number of bytes that will be read. However, does not reads more than one line, even if 10 exceeds the length of the line.
So it read the first 10 characters of line.
Hence the correct answer is to read the first 10 characters of the line.

Which of the following is an invalid file mode?
  • a)
    r+
  • b)
    rb
  • c)
    ab
  • d)
    rw
Correct answer is option 'D'. Can you explain this answer?

Preethi Sen answered
Invalid File Mode: 'rw'

Explanation:
In computer programming, a file mode is a parameter used when opening or creating a file to specify the intended operations on the file. It determines whether the file should be read from, written to, or both. The file mode is usually represented by a combination of characters that define the permissions and access rights for the file.

The valid file modes in most programming languages include the following:

1. Read Mode ('r'): This mode allows the program to read data from the file. It is used when you want to open a file for reading purposes only. The file must already exist, otherwise, an error will be thrown.

2. Write Mode ('w'): This mode allows the program to write data to the file. If the file does not exist, it will be created. If the file already exists, its contents will be truncated (erased) before writing new data to it.

3. Append Mode ('a'): This mode allows the program to append data to the end of an existing file. If the file does not exist, it will be created. Unlike the write mode, the existing contents of the file are preserved, and new data is added at the end.

4. Binary Mode ('b'): This mode is used to open a file in binary format, which is necessary when dealing with non-text files such as images, audio, or video files. It is often used in conjunction with the read or write modes.

Invalid File Mode: 'rw'

The 'rw' file mode mentioned in option D (rw) is not a valid file mode. A file mode can be either 'r', 'w', or 'a' to specify the read, write, or append operations, respectively. Combining 'r' and 'w' together as 'rw' does not represent a valid file mode. Therefore, option D is the correct answer as it denotes an invalid file mode.

Select the statement that is not true for r+ mode.
(1) If the file does not exist, it gives an error.
(2) Both reading and writing can be performed.
(3) If the file does not exist, it creates a new file.
(4) Existing data is not truncated.
  • a)
    1
  • b)
    3
  • c)
    4
  • d)
    2
Correct answer is option 'B'. Can you explain this answer?

Explanation:

Reading and Writing in r+ Mode:
In the r+ mode, both reading and writing operations can be performed on a file. When a file is opened in r+ mode, the file pointer is positioned at the beginning of the file, and both reading and writing operations can be carried out.

Creating a New File in r+ Mode:
Unlike the statement mentioned in option 3, if the file does not exist in r+ mode, it does not create a new file. Instead, it gives an error indicating that the file does not exist. This error needs to be handled appropriately in the code to avoid any issues.

Existing Data Truncation in r+ Mode:
When a file is opened in r+ mode, the existing data in the file is not truncated. This means that any data present in the file will remain intact, and new data can be written or existing data can be read without affecting the existing content.
Therefore, option 3 is not true for r+ mode as it does not create a new file if the file does not exist. It is important to handle the error appropriately to ensure smooth execution of the program when working with files in r+ mode.

With reference to file handling in Python, which of the following statement(s) is/are true?
a. The file seek points are 0, 1, and 2.
b. The seek() function works differently for text and binary files.
c. When a file is opened in binary mode, the seek() method can have a negative offset value.
d. The offset in the seek() function specifies the number of bits by which the file pointer is to be moved.
  • a)
    b, d
  • b)
    a, c
  • c)
    b, c, d
  • d)
    a, b, c, d
Correct answer is option 'B'. Can you explain this answer?

Aryan Chavan answered
Answer:

File handling in Python allows us to work with files, such as reading from or writing to files. It involves various operations such as opening, reading, writing, and closing files. The following statements are related to file handling in Python:

a. The file seek points are 0, 1, and 2:
- This statement is incorrect. The file seek points are not specifically 0, 1, and 2. The seek() function in Python is used to change the file position to the desired location. It takes an offset argument that specifies the number of bytes to move the file pointer.

b. The seek() function works differently for text and binary files:
- This statement is correct. The seek() function behaves differently for text and binary files. In text mode, it moves the file pointer in terms of characters, while in binary mode, it moves the file pointer in terms of bytes.

c. When a file is opened in binary mode, the seek() method can have a negative offset value:
- This statement is correct. When a file is opened in binary mode, the seek() method can have a negative offset value. It allows us to move the file pointer backward by the specified number of bytes.

d. The offset in the seek() function specifies the number of bits by which the file pointer is to be moved:
- This statement is incorrect. The offset in the seek() function specifies the number of bytes by which the file pointer is to be moved, not the number of bits.

Therefore, the correct statements are:
- b. The seek() function works differently for text and binary files.
- c. When a file is opened in binary mode, the seek() method can have a negative offset value.

These statements highlight the differences in file handling in Python, particularly with regards to the seek() function and working with text and binary files.

A ___________ method is used to position the file object at a particular position in a file.
  • a)
    tell()
  • b)
    read()
  • c)
    input()
  • d)
    seek()
Correct answer is option 'D'. Can you explain this answer?

Hansa Sharma answered
Concept:
Setting Offsets in a File:
The functions we've learned so far are utilized to access data sequentially from a file. However, if we wish to retrieve data at random, Python has the seek() and tell() methods.
The seek() method:
This function is used to place a file object at a certain location within a file.  seek() method is used to position the file object at a particular position in a file.
syntax:
file_object.seek(offset [, reference_point])
Here offset is the number of bytes by which the file object is to be moved. reference_point indicates the starting position of the file object.
Hence the correct answer is seek().

Serialisation is also known as____________.
  • a)
    Pickling
  • b)
    Unpickling
  • c)
    continuation
  • d)
    None of these
Correct answer is option 'A'. Can you explain this answer?

Ruchi Joshi answered
Serialisation is the process of converting an object into a format that can be stored or transmitted, and later reconstructed back into the original object. It allows objects to be easily stored, transferred, or shared across different platforms or applications.

Pickling is a term commonly used in Python to refer to the process of serialisation. It is the process of converting a Python object hierarchy into a byte stream, which can then be stored on disk or transmitted over a network. The byte stream can later be unpickled to reconstruct the original object hierarchy.

Unpickling is the reverse process of pickling, where the byte stream is converted back into the original object hierarchy. It allows the objects to be reconstructed and used as they were before the pickling process.

Continuation is not a term related to serialisation. It refers to the act of continuing something or the state of being continued. It is not directly related to the process of converting objects into a serialised format.

Therefore, the correct answer is option A - Pickling. Pickling is the term commonly used in Python to describe the process of serialisation. It involves converting a Python object hierarchy into a byte stream, which can be stored or transmitted, and later reconstructed back into the original object hierarchy through unpickling.

The following functions are used to access data randomly.
  • a)
    open() and readlines()
  • b)
    seek() and read()
  • c)
    seek() and tell()
  • d)
    tell() and read()
Correct answer is option 'C'. Can you explain this answer?

Gaurav Kumar answered
Concept:
Setting Offsets in a File:
The functions we've learned so far are utilized to access data sequentially from a file. However, if we wish to retrieve data at random, Python has the seek() and tell() methods.
The tell() method:
This function returns a number indicating the file object's current location in the file. The supplied position is the byte location from the start of the file to the current position of the file object.
syntax:
file_object.tell()
The seek() method:
This function is used to place a file object at a certain location within a file. 
syntax:
file_object.seek(offset [, reference_point])
Here offset is the number of bytes by which the file object is to be moved. reference_point indicates the starting position of the file object.
Hence the correct answer is seek() and tell().

Which method is used to rename the file or folder?
  • a)
    filename()
  • b)
    flush()
  • c)
    fname()
  • d)
    rename()
Correct answer is option 'D'. Can you explain this answer?

Roshni Patel answered
Renaming a file or folder using the 'rename()' method

To rename a file or folder, the 'rename()' method is used. This method allows you to change the name of a file or folder to a new name of your choice.

Explanation:

The 'rename()' method is a function provided by programming languages and operating systems to modify the name of a file or folder. When using this method, you provide the current name of the file or folder, as well as the new name that you want to assign to it.

Here is a step-by-step explanation of how to use the 'rename()' method to rename a file or folder:

1. Identify the current name: First, you need to know the current name of the file or folder that you want to rename. This could be the full path to the file or folder or just the name if it is located in the current directory.

2. Choose a new name: Decide on the new name you want to assign to the file or folder. Make sure the new name complies with any naming conventions or restrictions imposed by the operating system or programming language.

3. Use the 'rename()' method: Call the 'rename()' method and provide the current name and the new name as arguments. The method will then update the name of the file or folder accordingly.

4. Verify the renaming: After calling the 'rename()' method, you can verify if the renaming was successful. You can check if the file or folder with the new name exists in the desired location.

Example:

Here is an example of how to use the 'rename()' method in Python to rename a file:

```python
import os

current_name = 'old_file.txt'
new_name = 'new_file.txt'

os.rename(current_name, new_name)
```

In this example, the 'rename()' method from the 'os' module is used to change the name of the file 'old_file.txt' to 'new_file.txt'. After executing this code, the file will be renamed accordingly.

Conclusion:

The 'rename()' method is the correct option (D) for renaming a file or folder. This method is available in various programming languages and operating systems, allowing you to easily change the name of a file or folder to a new name of your choice.

What is the full form of CSV file?
  • a)
    Colon Separated Values
  • b)
    Context Separated Values
  • c)
    Comma Separated Values
  • d)
    Caps Separated Values
Correct answer is option 'C'. Can you explain this answer?

Hansa Sharma answered
Concept:
CSV Full Form:
  • The Comma Separated Value (CSV) format is a text file that contains data. It makes it easier to store data in a table-like format. The CSV extension is used to identify CSV files.
  • CSV files are used to transfer huge databases between applications while adhering to a precise format. CSV files may be opened with any text editor, such as Notepad or Excel.
Characteristics:
  • Each data item is entered on a different line. If the record is too long, it may span numerous lines.
  • The data fields are separated by commas.
  • A set of double quotes contains the fields that contain commas and are separated by double-quotes.
  • The fields containing double quotes are contained in a set of double-quotes.
  • The space characters that appear adjacent to built-in commas are ignored.
Hence the correct answer is Comma Separated Values.

Match the following Set 1 to set 2.
  • a)
    a-i, b- iii, c- ii, d- iv
  • b)
    a-i, b- iii, c- iv, d- ii
  • c)
    a-iii, b-i , c-iv , d-ii
  • d)
    a-iii, b-i , c-ii , d-iv
Correct answer is option 'D'. Can you explain this answer?

Gaurav Kumar answered
Concept:
The seek() method :
This method is used to position the file object at a particular position in a file.
The syntax of seek() is:
file_object.seek(offset [ , reference_point])
In the above syntax, offset is the number of bytes by which the file object is to be moved. reference_point indicates the starting position of the file object. That is, with reference to which position, the offset has to be counted.
  • It can have any of the following values:
    • 0 - beginning of the file
    • 1 - current position of the file
    • 2 - end of file
  • By default, the value of reference_point is 0,
    • i.e. the offset is counted from the beginning of the file.
Explanation:
  • f.seek(0) Move file pointer to the beginning of a File
  • f.seek(5) Move file pointer five characters ahead from the beginning of a file.
  • f.seek(0, 2) Move file pointer to the end of a File
  • f.seek(5, 1) Move file pointer five characters ahead from the current position.
  • f.seek(-5, 1) Move the file pointer five characters behind from the current position.
  • f.seek(-5, 2) Move the file pointer in the reverse direction. Move it to the 5th character from the end of the file.
Hence the correct answer is a-iii, b-i, c-ii , d-iv.

What is the full form of CSV file?
1) Colon Separated Values
2) Context Separated Values
3) Comma Separated Values
4) Caps Separated Values
  • a)
    1
  • b)
    3
  • c)
    4
  • d)
    2
Correct answer is option 'B'. Can you explain this answer?

Hansa Sharma answered
Concept
CSV Full Form:
  • The Comma Separated Value (CSV) format is a text file that contains data. It makes it easier to store data in a table-like format. The CSV extension is used to identify CSV files.
  • CSV files are used to transfer huge databases between applications while adhering to a precise format. CSV files may be opened with any text editor, such as Notepad or Excel.
Characteristics:
  • Each data item is entered on a different line. If the record is too long, it may span numerous lines.
  • The data fields are separated by commas.
  • A set of double quotes contains the fields that contain commas and are separated by double-quotes.
  • The fields containing double quotes are contained in a set of double-quotes.
  • The space characters that appear adjacent to built-in commas are ignored.
Hence the correct answer is Comma Separated Values

You are given the text file, file1.txt whose contents are:
hello everyone
today is a monday
all the best for your exams
 
What will be the output of the following code?
myfile = open("file1.txt", "r")
str = myfile.readline(20)
print(str)
myfile.close()
  • a)
    hello everyone\ntoday
  • b)
    hello everyone
    today
  • c)
    hello everyone
  • d)
    none of the above
Correct answer is option 'C'. Can you explain this answer?

Hansa Sharma answered
  • The readline() function reads the number of bytes specified, up to the end of the first line only.
  • That is, if the number of character exceeds the first line, the readline() function does not read them.
  • The readline() function does not read the \n escape sequence.
  • So, the given code only returns the first line even though it is less than 20 bytes or characters. The \n character is not included within the return value.
Important points:
  • The read() function reads exactly as many bytes as specified.
  • The readlines() function always reads complete lines. If the specified number of characters ends in the middle of a line, the function still reads that complete line up to the \n character. Also, the readlines() function returns the \n character.

Chapter doubts & questions for File Handling - Computer Science for Class 12 2025 is part of Humanities/Arts exam preparation. The chapters have been prepared according to the Humanities/Arts exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Humanities/Arts 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of File Handling - Computer Science for Class 12 in English & Hindi are available as part of Humanities/Arts exam. Download more important topics, notes, lectures and mock test series for Humanities/Arts Exam by signing up for free.

Top Courses Humanities/Arts