MCQs on File Handling | C Programming

1. File Operations (Opening, Reading, Writing, Closing)

  1. Which function is used to open a file in Python?
    • a) open_file()
    • b) open()
    • c) read()
    • d) fopen()
  2. What is the purpose of the close() function in file handling?
    • a) To open a file
    • b) To close a file and release resources
    • c) To write data to a file
    • d) To read data from a file
  3. Which of the following modes opens a file for reading only?
    • a) ‘r’
    • b) ‘w’
    • c) ‘a’
    • d) ‘rw’
  4. Which function is used to read the contents of a file in Python?
    • a) read()
    • b) write()
    • c) open()
    • d) readlines()
  5. Which mode in Python allows both reading and writing to a file?
    • a) ‘r’
    • b) ‘rw’
    • c) ‘w+’
    • d) ‘a+’
  6. How do you write to a file in Python?
    • a) write()
    • b) put()
    • c) save()
    • d) write_file()
  7. If the file does not exist, which mode in Python will create it?
    • a) ‘r’
    • b) ‘a’
    • c) ‘w’
    • d) ‘rb’
  8. Which function is used to open a file for writing in Python?
    • a) fopen()
    • b) open()
    • c) create()
    • d) save()
  9. What will happen if you try to open a file in ‘r’ mode that does not exist?
    • a) It will create a new file
    • b) It will raise an error
    • c) It will open a file with default content
    • d) It will overwrite the existing file
  10. What does the flush() method do in file handling?
    • a) Clears the buffer and writes the data to the file
    • b) Closes the file
    • c) Reads the file content
    • d) Opens the file

2. Types of Files (Text vs. Binary)

  1. Which of the following is true about a text file?
    • a) It can store only numeric data
    • b) It stores data in human-readable format
    • c) It stores data in binary format
    • d) It cannot be edited manually
  2. A binary file stores data in:
    • a) Plain text format
    • b) Human-readable format
    • c) A non-human-readable format
    • d) A compressed format
  3. Which of the following functions is used to open a file in binary mode in Python?
    • a) open(‘filename’, ‘b’)
    • b) open(‘filename’, ‘rb’)
    • c) open(‘filename’, ‘r’)
    • d) open(‘filename’, ‘wb’)
  4. Which of the following is a characteristic of a text file?
    • a) The data is stored in binary format
    • b) It uses characters and is human-readable
    • c) It can only store images
    • d) It can only store encrypted data
  5. Which of the following is an example of a binary file?
    • a) .txt
    • b) .csv
    • c) .exe
    • d) .html
  6. When reading a file in binary mode, the read() function returns:
    • a) A string
    • b) A list of lines
    • c) A sequence of bytes
    • d) An error
  7. Which mode is used to open a file for writing in binary format?
    • a) ‘wb’
    • b) ‘b’
    • c) ‘rwb’
    • d) ‘w’
  8. In binary files, data is written as:
    • a) Unicode characters
    • b) ASCII characters
    • c) A sequence of bytes
    • d) Plain text
  9. Text files use which encoding by default in Python?
    • a) UTF-8
    • b) ASCII
    • c) ISO-8859-1
    • d) UTF-16
  10. When you open a file in binary mode, what does read() return?
    • a) A byte object
    • b) A string object
    • c) A number
    • d) An error

3. File Pointers (fopen, fclose, fread, fwrite, etc.)

  1. Which function in C is used to open a file?
    • a) fopen()
    • b) read()
    • c) close()
    • d) write()
  2. What is the purpose of the fclose() function in C?
    • a) To read data from a file
    • b) To write data to a file
    • c) To close the file and release resources
    • d) To open a file
  3. Which C function is used to read data from a file?
    • a) read()
    • b) fread()
    • c) get()
    • d) open()
  4. The fwrite() function in C is used to:
    • a) Read data from a file
    • b) Write data to a file
    • c) Open a file
    • d) Close a file
  5. The fseek() function is used to:
    • a) Write data to a file
    • b) Move the file pointer to a specific location
    • c) Read data from a file
    • d) Open a file
  6. The ftell() function in C returns:
    • a) The number of bytes in the file
    • b) The current position of the file pointer
    • c) The total size of the file
    • d) The last modification date
  7. Which function is used to move the file pointer back to the beginning of the file in C?
    • a) fseek()
    • b) rewind()
    • c) fopen()
    • d) fclose()
  8. In C, to move the file pointer to the end of the file, we use:
    • a) fseek(file, 0, SEEK_END)
    • b) fseek(file, 0, SEEK_SET)
    • c) rewind(file)
    • d) fclose(file)
  9. Which function in C is used to get the current position of the file pointer?
    • a) ftell()
    • b) fseek()
    • c) fread()
    • d) fwrite()
  10. The feof() function is used to:
    • a) Check if the file is open
    • b) Check if the end of the file has been reached
    • c) Close the file
    • d) Open the file

4. Error Handling in File Operations

  1. Which of the following is a valid way to handle errors in Python while opening a file?
    • a) try/except block
    • b) if/else block
    • c) switch/case block
    • d) throw/catch block
  2. What does the FileNotFoundError in Python indicate?
    • a) The file is empty
    • b) The file is not found
    • c) The file is corrupted
    • d) The file cannot be opened
  3. How do you handle errors when reading a file in Python?
    • a) Use a try/except block
    • b) Use an if/else statement
    • c) Use a for loop
    • d) Use the error() method
  4. What happens if you try to read from a file that is not open in Python?
    • a) It raises a FileNotFoundError
    • b) It raises a ValueError
    • c) It raises an IOError
    • d) It raises a SyntaxError
  5. How can you check for a file’s existence in Python before opening it?
    • a) Use os.path.exists()
    • b) Use the file().exists() method
    • c) Use file.check()
    • d) Use file_exists() function
  6. What will happen if a program tries to open a file without permission?
    • a) It will open in read-only mode
    • b) It will throw a PermissionError
    • c) It will create a new file
    • d) It will ignore the error
  7. How do you catch errors related to file operations in Python?
    • a) try/except
    • b) catch/throw
    • c) if/else
    • d) break/continue
  8. What type of error is raised if you try to write to a file that is opened in read-only mode?
    • a) FileNotFoundError
    • b) IOError
    • c) PermissionError
    • d) ValueError
  9. Which function will close a file if an error occurs while reading from it?
    • a) close()
    • b) fclose()
    • c) flush()
    • d) exit()
  10. If an error occurs during file operations, the program should:
    • a) Ignore the error and continue
    • b) Close the file and stop the program
    • c) Log the error and continue
    • d) Prompt the user for a new file

5. Command Line Arguments

  1. Which module in Python is used to handle command-line arguments?
    • a) sys
    • b) os
    • c) argparser
    • d) commandline
  2. What is the first element in the sys.argv list in Python?
    • a) The name of the script
    • b) The first command-line argument
    • c) The total number of arguments
    • d) The file name
  3. How do you pass arguments to a Python script from the command line?
    • a) Using sys.argv[]
    • b) Using input()
    • c) Using read()
    • d) Using os.path()
  4. What is the purpose of sys.argv in Python?
    • a) To pass file names to a program
    • b) To access environment variables
    • c) To access command-line arguments
    • d) To get user input
  5. How do you pass command-line arguments to a Python script?
    • a) By specifying them after the script name
    • b) By using a configuration file
    • c) By modifying the environment
    • d) By calling input() function
  6. What does sys.argv[1] represent?
    • a) The script name
    • b) The first command-line argument
    • c) The second command-line argument
    • d) The total number of arguments
  7. How would you handle incorrect or missing command-line arguments?
    • a) Raise an exception
    • b) Print a message and exit
    • c) Ignore the error
    • d) Log the error
  8. Which command-line argument type does Python’s argparse module support?
    • a) Integer
    • b) String
    • c) Boolean
    • d) All of the above
  9. Which function is used to parse command-line arguments in Python?
    • a) parse_args()
    • b) read_args()
    • c) process_args()
    • d) get_args()
  10. What is the purpose of command-line arguments in Python?
    • a) To define variables inside the script
    • b) To pass input data into the script from the terminal
    • c) To open and read files
    • d) To close files after reading

Answers

Question NumberAnswer
1b
2b
3a
4a
5c
6a
7c
8b
9b
10a
11b
12c
13b
14b
15c
16c
17a
18c
19a
20a
21a
22c
23b
24b
25b
26b
27b
28a
29a
30b
31a
32b
33a
34c
35a
36b
37a
38c
39a
40c
41a
42a
43a
44c
45a
46b
47b
48d
49a
50b

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top