Master Perl’s file handling and I/O operations with these expertly curated MCQs. Learn about file modes, filehandles, efficient methods for handling large files, and working with binary data seamlessly.
File Modes and Filehandles (10 Questions)
Which operator is used to open a file in Perl? A) open B) read C) file D) access
What does the file mode < signify in Perl? A) Write-only B) Read-only C) Append-only D) Read and Write
How do you open a file for appending in Perl? A) open(FILE, '>>filename'); B) open(FILE, '<<filename'); C) open(FILE, '>filename'); D) open(FILE, '|filename');
What is the purpose of the die function in file handling? A) Create a file B) Gracefully close a file C) Terminate program with an error message D) None of the above
Which symbol is used to denote a filehandle in Perl? A) @ B) $ C) % D) None
What is the correct way to close a file in Perl? A) exit(FILE); B) close(FILE); C) file.close; D) stop(FILE);
Which file mode allows both reading and writing? A) > B) < C) +> D) >>
What happens if you try to open a non-existent file in read mode? A) Creates the file B) Returns undef C) Throws an error D) Appends data
What is the purpose of select() in Perl filehandling? A) Close a filehandle B) Set the default output filehandle C) Delete a file D) Change file permissions
How do you check if a filehandle is open? A) defined FILE B) exists FILE C) eof FILE D) open FILE
Reading/Writing Large Files (10 Questions)
What is the purpose of the <> operator in Perl? A) Open a file B) Read from a file C) Write to a file D) Delete a file
Which method is preferred for handling large files in Perl? A) Line-by-line reading B) Read entire file into memory C) Random access D) Reading via binary mode
What does the binmode function do? A) Sets binary mode on a filehandle B) Converts text to binary C) Compresses the file D) Closes the filehandle
How do you process large files efficiently in Perl? A) Use slurp mode B) Read chunks of data C) Always use +> mode D) Open file in write-only mode
What does the function seek() do? A) Reads data line by line B) Moves the file pointer to a specific location C) Closes the filehandle D) Deletes the file
What is the default size of the data chunk in Perl’s read() function? A) 1024 bytes B) 512 bytes C) 8192 bytes D) User-defined
How can you write large amounts of data to a file in Perl? A) Use print function B) Use write function with buffer handling C) Use syswrite D) All of the above
Which function is used to check if the end of the file is reached? A) eof(FILE) B) end(FILE) C) close(FILE) D) finish(FILE)
What happens if you read a large file without using line-by-line processing? A) Memory overflow B) File is closed automatically C) Entire file is stored in memory D) File pointer resets
Which function combines line-by-line reading with error handling? A) readline B) open C) slurp D) read
Working with Binary Files (10 Questions)
Which file mode is recommended for binary files? A) < B) b+ C) binmode D) +<
What does the sysread() function return? A) Number of bytes read B) Line number C) End-of-file status D) File size
What is the correct way to read a binary file? A) Use binmode with read() B) Use <> C) Use print D) Use syswrite
Can you read binary files without setting binmode? A) Yes, but it might cause data corruption B) No C) Yes, but only on Linux systems D) No, unless using <>
How do you write binary data to a file in Perl? A) Use syswrite with binmode B) Use write directly C) Use print D) Both A and C
Which of the following is a key challenge when working with binary files? A) Handling line breaks B) Dealing with non-ASCII data C) File size constraints D) All of the above
What is the significance of byte order when reading binary files? A) Determines storage format B) Affects text conversion C) Impacts file permissions D) Causes no difference
What is the correct syntax to append binary data to an existing file? A) open(FILE, '>>:raw', $filename) B) open(FILE, '>', $filename) C) open(FILE, '+>>', $filename) D) open(FILE, 'raw', $filename)
What is a common use case for binary file handling? A) Reading multimedia files B) Processing encrypted data C) Handling serialized objects D) All of the above
How do you ensure cross-platform consistency for binary files? A) Use binmode B) Encode file in UTF-8 C) Use <> D) Avoid opening in write mode