Sample questions
Question 1Medium
The ____ function is used to read a single line from a file and is used when dealing with text files. For reading binary data or other specific segments of a file, you should use the ____ function instead.
- fgets(), fseek()
- fread(), fgets()
- fputs(), fgets()
- fgets(), fread()
Answer: D. fgets(), fread()
Explanation
Although it is possible to specify a maximum length when calling it, the fgets function defaults to reading a single line from the given file resource and is primarily used for text files. The fread function, on the other hand, is used primarily to read binary data. That makes answer D correct.
Step-by-step solution
fgets() reads a single line from a file and is used for text files.fread() is used for reading binary data or specific segments of a file.- So, the first blank is filled with
fgets() and the second with fread(). - This matches option D:
fgets(), fread(). - Final answer:
fgets(), fread().
Why the other options are wrong
- A. fseek() moves the file pointer; it does not read binary data, so the second blank is wrong.
- B. fread() is for binary data or fixed-size reads, not single lines, so the blanks are reversed here.
- C. fputs() writes to a file rather than reading a line, so the first blank is wrong.
Tip: Use fgets() for text, fread() for binary.
Question 2Medium
Which of the following techniques will guarantee a lock safe from any race condition?
- Using
flock() to lock the desired file fopen()'ing a file in the operating system's temporary directory- Creating a temporary file with
tempnam() - Using
mkdir() to create a directory and use it as a lock reference
Answer: D. Using mkdir() to create a directory and use it as a lock reference
Explanation
The correct answer is D. This is a very tough question, and one you're not likely to find in the real exam but that's why you're reading this book! You must remember that flock()
uses a' cooperative' locking mechanism with one big assumption: that all other processes that want to access your file will also use flock(). If they don't, race conditions can arise and the lock is not guaranteed. Curiously, creating a directory with the mkdir function is guaranteed to be an atomic operation, meaning that only one process at any given time can perform it. Therefore, you can create a temporary directory and hold it until you have finished your I/O operations.
Step-by-step solution
- Flock is cooperative: it only protects the file if every process also calls flock.
- A temporary file or a file in the temp folder does not lock anything by itself.
- Mkdir is atomic, so only one process can create a given directory at a time.
- Using mkdir to create a directory as the lock is the guaranteed choice, option D.
Why the other options are wrong
- A. Flock only works if every other process also uses flock, so it is not guaranteed.
- B. Opening a file in the temp directory does not create a lock.
- C. Tempnam makes a unique file name but does not lock anything.
Tip: For a lock that works across processes, use an atomic step such as creating a directory, because flock depends on everyone cooperating.
Question 3Medium
The ..... function is used to read a single character from a file.
- fgetc()
- fgets()
- fget()
- fgetf()
Answer: A. fgetc()
Explanation
fgetc() function is used to read single character from a file.
Step-by-step solution
fgetc() reads one character at a time from a file.fgets() reads a line of text, not just one character.fget() is not a valid function in C standard library.fgetf() does not exist in any standard C functions.- Correct answer is
fgetc().
Why the other options are wrong
- B. fgets() reads a whole line of text, not a single character.
- C. fget() is not a standard function for reading a file in C or PHP.
- D. fgetf() does not exist in the standard functions for reading a file.
Tip: Remember: fgetc() is the correct function for reading a single character from a file.
Question 4Medium
What should you do if your script is having problem recognizing file endings from a text file saved on a platform different from the one you're reading it on?
- Change the
auto_detect_line_endings INI setting - Use a regular expression to detect the last letter of a line
- Use
fpos() - Use
ftok()
Answer: A. Change the auto_detect_line_endings INI setting
Explanation
The auto_detect_line_endings setting makes PHP detect line endings (such as \n, \r\n or \r) when it reads a file, which fixes text files saved on another platform. The other options do not handle line endings.
Step-by-step solution
- Set auto_detect_line_endings to true in php.ini.
- PHP will automatically detect line endings from the saved file.
- This solves recognition issues between platforms.
- Check if the setting is enabled in your environment.
- Correct answer is A.
Why the other options are wrong
- B. A regular expression on the last letter does not fix line endings; the auto_detect_line_endings setting does.
- C. fpos() is not a PHP function for handling line endings.
- D. ftok() creates a System V IPC key from a file; it does not handle line endings.
Tip: Enable auto_detect_line_endings to handle file endings automatically.
Question 5Medium
What does the built-in delete function do?
- It deletes a file
- It deletes a directory
- It removes a database row
- This function does not exist!
Answer: D. This function does not exist!
Explanation
Answer D is correct. There is no function called delete() in PHP. Files are deleted with unlink(), while directories are deleted with rmdir().
Database rows are deleted using the DELETE SQL statement (not a PHP function) and, finally, variables are unset using unset().
Step-by-step solution
- The
delete function is not available in PHP. - Files are removed with
unlink(), directories with rmdir(). - Database rows are deleted using SQL
DELETE statement. - No built-in function named
delete exists in PHP. - Variables are unset using
unset() function. - Correct answer is This function does not exist!
Why the other options are wrong
- A. PHP has no delete function for files; unlink() deletes a file.
- B. PHP has no built-in delete function for directories; rmdir() removes an empty directory.
- C. A database row is removed with an SQL DELETE statement, not a built-in PHP delete function.
Tip: Remember: PHP doesn't have a delete function. Use unlink(), rmdir(), or SQL DELETE as needed.
Question 6Medium
To work with remote files in PHP you need to enable
- allow_url_fopen
- allow_remote_files
- both of above
- none of above
Answer: A. allow_url_fopen
Explanation
This option enables the URL-aware fopen wrappers that enable accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers.
Step-by-step solution
- Allow_url_fopen enables URL-aware fopen wrappers to access remote files.
- This allows PHP to treat URLs as file sources using protocols like http or ftp.
- Other options like allow_remote_files do not exist in PHP configuration.
- Thus, the correct setting is allow_url_fopen.
- Final answer: A (allow_url_fopen).
Why the other options are wrong
- B. allow_remote_files is not a PHP setting; the setting for remote file access is allow_url_fopen.
- C. Both is wrong because allow_remote_files does not exist in the PHP configuration.
- D. None is wrong because allow_url_fopen is the setting that enables remote file access.
Tip: Remember: allow_url_fopen is the correct directive for enabling remote file access in PHP.
Question 7Medium
What is the difference between stat() and fstat()?
- While
stat() works on open file pointers, fstat() works on files specified by pathname - While
fstat() works on open file pointers, stat() works on files specified by pathname fstat() has nothing to do with filesstat() has nothing to do with files
Answer: B. While fstat() works on open file pointers, stat() works on files specified by pathname
Explanation
Answer B is correct. The fstat function works by retrieving statistical information on a file based on an open file pointer, while stat() retrieves the same information based on a pathname.
Step-by-step solution
- fstat() uses an open file descriptor to get file info.
- stat() uses a filename path to get file info.
- fstat() works on open files, not paths.
- stat() works on file paths, not open files.
- So fstat() is for open pointers, stat() is for file names.
- Final answer: While fstat() works on open file pointers, stat() works on files specified by pathname.
Why the other options are wrong
- A. This reverses the two: stat() takes a pathname, while fstat() takes an open file pointer.
- C. fstat() does work with files, since it reads file info from an open file pointer.
- D. stat() does work with files, since it reads file info from a pathname.
Tip: Remember: stat uses paths, fstat uses open files.
Question 8Medium
Which of the following function calls will cause a file pointer to be returned to the beginning of the file?
reset()fseek(-1)fseek(0, SEEK_END)fseek(0, SEEK_SET)
Answer: D. fseek(0, SEEK_SET)
Explanation
The correct answer is D. fseek() is used to move the pointer on an open file. The SEEK_SET constant is used to indicate that the offset provided with the function call should be taken to mean the number of bytes from the beginning of the file. SEEK_SET is also the default value used by fseek() if no starting offset is specified.
Note that the rewind function is equivalent to fseek (0, SEEK_SET).
Step-by-step solution
- fseek(0, SEEK_SET) sets pointer to byte 0 from start of file.
- SEEK_SET means offset starts from beginning of file.
- This moves file pointer back to first byte of file.
- Other options do not reset pointer to start of file.
- Correct function call is fseek(0, SEEK_SET).
Why the other options are wrong
- A. reset() works on arrays, not file pointers, so it does not move a file pointer back to the start.
- B. fseek(-1) is incomplete and gives a negative position, which fails; it does not return to the start.
- C. fseek(0, SEEK_END) moves the pointer to the end of the file, not to the beginning.
Tip: Use fseek(0, SEEK_SET) to reset file pointer to start.
Question 9Medium
Can I generate DLL files from PHP scripts like I can in perl?
- Yes
- No
Answer: B. No
Explanation
No, unfortunately there is no such tool available for PHP.
Step-by-step solution
- The question asks whether PHP can generate DLL files the way Perl can.
- PHP has no such tool available for generating DLL files.
- So the answer is No, option B.
Why the other options are wrong
- A. Yes is wrong because PHP has no tool for generating DLL files.
Tip: Check the premise of a 'can I' question against what the language actually provides.
Question 10Medium
The ...... function checks if the "end-of-file" (EOF) has been reached.
- f_eof()
- f_of()
- feofs()
- feof()
Answer: D. feof()
Explanation
The feof() function checks if the end-of-file (EOF) has been reached. This is a standard function used in C programming to detect when the end of a file stream is encountered.
Step-by-step solution
feof() is a standard C library function.- It returns non-zero if the end-of-file indicator is set.
- This indicates that the input stream has reached EOF.
- Thus,
feof() checks for end of file condition. - The correct answer is
feof().
Why the other options are wrong
- A. f_eof() is not a PHP function; the function is feof().
- B. f_of() is not a PHP function; the function is feof().
- C. feofs() is not a PHP function; the function is feof().
Tip: Remember feof() is the standard function for checking EOF in C.
Practise all 12 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →