Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What happens if no arguments are passed to the seek function?(a) file position is set to the start of file(b) file position is set to the end of file(c) file position remains unchanged(d) errorI got this question in semester exam.My doubt is from Files in section Regular Expressions and Files of Python

Answer»

The CORRECT OPTION is (d) error

Best explanation: seek() takes at least one ARGUMENT.

2.

How do you change the file position to an offset value from the start?(a) fp.seek(offset, 0)(b) fp.seek(offset, 1)(c) fp.seek(offset, 2)(d) none of the mentionedThe question was asked during an interview for a job.Enquiry is from Files topic in portion Regular Expressions and Files of Python

Answer» RIGHT answer is (a) fp.seek(offset, 0)

EXPLANATION: 0 indicates that the offset is with RESPECT to the start.
3.

How do you delete a file?(a) del(fp)(b) fp.delete()(c) os.remove(‘file’)(d) os.delete(‘file’)The question was posed to me by my college director while I was bunking the class.This interesting question is from Files topic in portion Regular Expressions and Files of Python

Answer»

Right CHOICE is (c) os.remove(‘file’)

EXPLANATION: os.remove() is USED to delete FILES.

4.

How do you rename a file?(a) fp.name = ‘new_name.txt’(b) os.rename(existing_name, new_name)(c) os.rename(fp, new_name)(d) os.set_name(existing_name, new_name)This question was addressed to me in an online quiz.This question is from Files in chapter Regular Expressions and Files of Python

Answer»

The correct ANSWER is (B) os.RENAME(existing_name, new_name)

Best explanation: os.rename() is used to rename FILES.

5.

How do you get the current position within the file?(a) fp.seek()(b) fp.tell()(c) fp.loc(d) fp.posI have been asked this question during an interview.This intriguing question originated from Files in division Regular Expressions and Files of Python

Answer»

Right ANSWER is (B) fp.tell()

Easy explanation - It gives the current position as an OFFSET from the start of FILE.

6.

How do you close a file object (fp)?(a) close(fp)(b) fclose(fp)(c) fp.close()(d) fp.__close__()I had been asked this question in a job interview.My question is taken from Files topic in portion Regular Expressions and Files of Python

Answer»

Right CHOICE is (c) fp.close()

The EXPLANATION is: close() is a method of the FILE object.

7.

Which of the following is not a valid attribute of a file object (fp)?(a) fp.name(b) fp.closed(c) fp.mode(d) fp.sizeI had been asked this question during an interview for a job.I would like to ask this question from Files in section Regular Expressions and Files of Python

Answer» CORRECT ANSWER is (d) fp.size

Easiest EXPLANATION - fp.size has not been IMPLEMENTED.
8.

How do you get the name of a file from a file object (fp)?(a) fp.name(b) fp.file(name)(c) self.__name__(fp)(d) fp.__name__()I had been asked this question in class test.Query is from Files topic in portion Regular Expressions and Files of Python

Answer»

Right choice is (a) fp.name

Easy EXPLANATION - name is an ATTRIBUTE of the FILE OBJECT.

9.

What is the difference between r+ and w+ modes?(a) no difference(b) in r+ the pointer is initially placed at the beginning of the file and the pointer is at the end for w+(c) in w+ the pointer is initially placed at the beginning of the file and the pointer is at the end for r+(d) depends on the operating systemThis question was addressed to me in semester exam.Query is from Files in chapter Regular Expressions and Files of Python

Answer» CORRECT choice is (b) in R+ the pointer is INITIALLY placed at the beginning of the file and the pointer is at the end for w+

The EXPLANATION: none.
10.

Which of the following is not a valid mode to open a file?(a) ab(b) rw(c) r+(d) w+I got this question by my school principal while I was bunking the class.I need to ask this question from Files topic in section Regular Expressions and Files of Python

Answer»

The CORRECT option is (b) rw

Best explanation: Use r+, w+ or a+ to perform both read and WRITE operations USING a SINGLE FILE object.

11.

Which of the following are the modes of both writing and reading in binary format in file?(a) wb+(b) w(c) wb(d) w+This question was posed to me in an interview for internship.The question is from Files topic in portion Regular Expressions and Files of Python

Answer»

The correct CHOICE is (a) wb+

For explanation: Here is the description below

“w”Opens a file for writing only. Overwrites the file if the file EXISTS. If the file does not exist, CREATES a new file for writing.

“wb” Opens a file for writing only in binary FORMAT. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

“w+” Opens a file for both writing and reading. Overwrites the EXISTING file if the file exists. If the file does not exist, creates a new file for reading and writing.

“wb+” Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

12.

Is it possible to create a text file in python?(a) Yes(b) No(c) Machine dependent(d) All of the mentionedI have been asked this question in semester exam.My doubt stems from Files topic in division Regular Expressions and Files of Python

Answer»

The correct choice is (a) YES

Best explanation: Yes we can create a file in PYTHON. Creation of file is as shown below.

file = OPEN(“newfile.txt”, “w”)

file.write(“hello world in the NEW file\n”)

file.write(“and ANOTHER line\n”)

file.close().

13.

Which function is used to write a list of string in a file?(a) writeline()(b) writelines()(c) writestatement()(d) writefullline()This question was posed to me in an online interview.This question is from Files in section Regular Expressions and Files of Python

Answer»

The correct answer is (a) writeline()

To explain: With the writeline FUNCTION you can WRITE a list of strings to a file

fh = OPEN(“hello.txt”, “w”)

lines_of_text = [“a line of TEXT”, “another line of text”, “a third line”]

fh.writelines(lines_of_text).

14.

Which function is used to close a file in python?(a) Close()(b) Stop()(c) End()(d) Closefile()I had been asked this question during an online exam.My enquiry is from Files topic in chapter Regular Expressions and Files of Python

Answer»

Right CHOICE is (a) Close()

BEST explanation: f.close()to close it and free up any system resources TAKEN up by the open FILE.

15.

Which function is used to write all the characters?(a) write()(b) writecharacters()(c) writeall()(d) writechar()The question was asked in an online interview.This question is from Files in section Regular Expressions and Files of Python

Answer»

Right option is (a) WRITE()

The explanation: To write a fixed sequence of CHARACTERS to a file

fh = open(“HELLO.txt”,”w”)

write(“Hello WORLD”).

16.

Which function is used to read single line from file?(a) Readline()(b) Readlines()(c) Readstatement()(d) Readfullline()The question was posed to me by my college director while I was bunking the class.This question is from Files in division Regular Expressions and Files of Python

Answer»

The CORRECT choice is (b) Readlines()

To explain: The readline function reads a SINGLE LINE from the file FH = open(“filename”, “r”)

CONTENT = fh.readline().

17.

Which function is used to read all the characters?(a) Read()(b) Readcharacters()(c) Readall()(d) Readchar()I had been asked this question in class test.My question is based upon Files topic in division Regular Expressions and Files of Python

Answer»

Correct CHOICE is (a) READ()

The best I can explain: The read function READS all characters fh = OPEN(“FILENAME”, “r”)

content = fh.read().

18.

What is the use of “a” in file handling?(a) Read(b) Write(c) Append(d) None of the mentionedI have been asked this question during an interview.My query is from Files topic in chapter Regular Expressions and Files of Python

Answer»

Right choice is (c) Append

The EXPLANATION is: This opens the fhe FILE in appending MODE. That means, it will be OPEN for writing and EVERYTHING will be written to the end of the file.

fh =open(“filename_here”, “a”).

19.

What is the use of “w” in file handling?(a) Read(b) Write(c) Append(d) None of the mentionedThe question was asked during a job interview.The above asked question is from Files topic in division Regular Expressions and Files of Python

Answer» RIGHT choice is (b) Write

The BEST I can EXPLAIN: This opens the file for writing. It will create the file if it doesn’t exist, and if it does, it will overwrite it.

fh = open(“filename_here”, “w”).
20.

In file handling, what does this terms means “r, a”?(a) read, append(b) append, read(c) write, append(d) none of the mentionedI have been asked this question in exam.I need to ask this question from Files in portion Regular Expressions and Files of Python

Answer»

The CORRECT ANSWER is (a) read, append

The best I can EXPLAIN: R- reading,a-appending.

21.

Correct syntax of file.readlines() is?(a) fileObject.readlines( sizehint );(b) fileObject.readlines();(c) fileObject.readlines(sequence)(d) none of the mentionedI got this question by my school principal while I was bunking the class.I want to ask this question from Files in division Regular Expressions and Files of Python

Answer»

The correct answer is (a) fileObject.READLINES( sizehint );

The best I can explain: The METHOD readlines() reads until EOF using readline() and returns a list containing the lines. If the optional sizehint ARGUMENT is present, INSTEAD of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read.

SYNTAX

Following is the syntax for readlines() method:

fileObject.readlines( sizehint );

Parameters

sizehint — This is the number of bytes to be read from the file.

22.

What is the correct syntax of open() function?(a) file = open(file_name [, access_mode][, buffering])(b) file object = open(file_name [, access_mode][, buffering])(c) file object = open(file_name)(d) none of the mentionedI have been asked this question in an online interview.Enquiry is from Files in division Regular Expressions and Files of Python

Answer»

Correct option is (b) file object = open(file_name [, access_mode][, buffering])

To explain I would say: Open() function correct syntax with the parameter details as shown below:

file object = open(file_name [, access_mode][, buffering])

Here is parameters’ detail:

file_name: The file_name argument is a string value that contains the NAME of the file that you want to access.

access_mode: The access_mode determines the mode in which the file has to be opened, i.e., READ, write, APPEND, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r).

buffering: If the buffering value is set to 0, no buffering will take place. If the buffering value is 1, line buffering will be performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering ACTION will be performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior).

23.

Correct syntax of file.writelines() is?(a) file.writelines(sequence)(b) fileObject.writelines()(c) fileObject.writelines(sequence)(d) none of the mentionedI have been asked this question in a national level competition.I need to ask this question from Files in chapter Regular Expressions and Files of Python

Answer»

Right ANSWER is (c) fileObject.WRITELINES(sequence)

The best I can EXPLAIN: The method writelines() writes a sequence of strings to the file. The sequence can be any iterable OBJECT producing strings, typically a list of strings. There is no return value.

SYNTAX

Following is the syntax for writelines() method:

fileObject.writelines( sequence ).

24.

What is unpickling?(a) It is used for object serialization(b) It is used for object deserialization(c) None of the mentioned(d) All of the mentionedI have been asked this question in unit test.The above asked question is from Files in section Regular Expressions and Files of Python

Answer»

Correct choice is (b) It is USED for object deserialization

The best I can explain: We have been WORKING with simple textual data. What if we are working with objects rather than simple text? For such situations, we can use the pickle module. This module serializes Python objects. The Python objects are converted into byte streams and written to text files. This process is called PICKLING. The inverse OPERATION, reading from a file and reconstructing objects is called deserializing or UNPICKLING.

25.

What is the pickling?(a) It is used for object serialization(b) It is used for object deserialization(c) None of the mentioned(d) All of the mentionedThe question was asked in class test.The above asked question is from Files topic in division Regular Expressions and Files of Python

Answer» RIGHT answer is (a) It is used for object serialization

The explanation: Pickle is the standard MECHANISM for object serialization. Pickle uses a simple stack-based virtual machine that records the instructions used to reconstruct the object. This makes pickle vulnerable to SECURITY risks by malformed or maliciously CONSTRUCTED data, that MAY cause the deserializer to import arbitrary modules and instantiate any object.
26.

What is the use of truncate() method in file?(a) truncates the file size(b) deletes the content of the file(c) deletes the file size(d) none of the mentionedThis question was addressed to me by my school teacher while I was bunking the class.Question is from Files topic in chapter Regular Expressions and Files of Python

Answer»

The CORRECT answer is (a) truncates the FILE SIZE

The best EXPLANATION: The method TRUNCATE() truncates the file size. Following is the syntax for truncate() method:

fileObject.truncate( [ size ])

Parameters

size — If this optional argument is present, the file is truncated to (at most) that size.

27.

What is the use of seek() method in files?(a) sets the file’s current position at the offset(b) sets the file’s previous position at the offset(c) sets the file’s current position within the file(d) none of the mentionedThis question was addressed to me during an interview.My doubt stems from Files topic in portion Regular Expressions and Files of Python

Answer»

Correct choice is (a) sets the file’s current position at the OFFSET

Easiest explanation - Sets the file’s current position at the offset. The method SEEK() sets the file’s current position at the offset.

Following is the syntax for seek() method:

fileObject.seek(offset[, whence])

Parameters

offset — This is the position of the read/write pointer WITHIN the file.

whence — This is OPTIONAL and defaults to 0 which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file’s end.

28.

What is the current syntax of remove() a file?(a) remove(file_name)(b) remove(new_file_name, current_file_name,)(c) remove(() , file_name))(d) none of the mentionedI had been asked this question in homework.My question is taken from Files topic in chapter Regular Expressions and Files of Python

Answer» CORRECT OPTION is (a) REMOVE(file_name)

The EXPLANATION: remove(file_name)
29.

What is the current syntax of rename() a file?(a) rename(current_file_name, new_file_name)(b) rename(new_file_name, current_file_name,)(c) rename(()(current_file_name, new_file_name))(d) none of the mentionedThe question was posed to me during an internship interview.My question is taken from Files topic in portion Regular Expressions and Files of Python

Answer» RIGHT choice is (a) RENAME(current_file_name, new_file_name)

The explanation is: This is the CORRECT syntax which has shown below.

rename(current_file_name, new_file_name)
30.

What is the use of tell() method in python?(a) tells you the current position within the file(b) tells you the end position within the file(c) tells you the file is opened or not(d) none of the mentionedThis question was posed to me in unit test.This question is from Files in division Regular Expressions and Files of Python

Answer»

The correct CHOICE is (a) tells you the current position within the file

Easy EXPLANATION - The TELL() method tells you the current position within the file; in other WORDS, the next read or write will occur at that many bytes from the BEGINNING of the file.

31.

The readlines() method returns ____________(a) str(b) a list of lines(c) a list of single characters(d) a list of integersI had been asked this question in a job interview.My question is based upon Files topic in section Regular Expressions and Files of Python

Answer»

Correct answer is (B) a list of lines

To EXPLAIN: EVERY LINE is stored in a list and returned.

32.

To read the remaining lines of the file from a file object infile, we use ____________(a) infile.read(2)(b) infile.read()(c) infile.readline()(d) infile.readlines()The question was asked in quiz.I'd like to ask this question from Files in chapter Regular Expressions and Files of Python

Answer»

The CORRECT OPTION is (d) infile.readlines()

To explain: Execute in the SHELL to verify.

33.

To read the next line of the file from a file object infile, we use ____________(a) infile.read(2)(b) infile.read()(c) infile.readline()(d) infile.readlines()This question was addressed to me in my homework.This is a very interesting question from Files topic in section Regular Expressions and Files of Python

Answer» CORRECT CHOICE is (c) infile.readline()

Explanation: Execute in the SHELL to VERIFY.
34.

To read the entire remaining contents of the file as a string from a file object infile, we use ____________(a) infile.read(2)(b) infile.read()(c) infile.readline()(d) infile.readlines()The question was asked during an online exam.Asked question is from Files in division Regular Expressions and Files of Python

Answer» CORRECT choice is (B) infile.read()

To EXPLAIN I WOULD say: read FUNCTION is used to read all the lines in a file.
35.

To read two characters from a file object infile, we use ____________(a) infile.read(2)(b) infile.read()(c) infile.readline()(d) infile.readlines()The question was asked by my college director while I was bunking the class.Question is taken from Files topic in chapter Regular Expressions and Files of Python

Answer» RIGHT ANSWER is (a) infile.read(2)

Explanation: Execute in the SHELL to verify.
36.

Which of the following statements are true?(a) When you open a file for reading, if the file does not exist, an error occurs(b) When you open a file for writing, if the file does not exist, a new file is created(c) When you open a file for writing, if the file exists, the existing file is overwritten with the new file(d) All of the mentionedI had been asked this question by my school teacher while I was bunking the class.My question is based upon Files topic in chapter Regular Expressions and Files of Python

Answer» CORRECT ANSWER is (d) All of the mentioned

The explanation is: The program will THROW an ERROR.
37.

To open a file c:\scores.txt for appending data, we use ____________(a) outfile = open(“c:\\scores.txt”, “a”)(b) outfile = open(“c:\\scores.txt”, “rw”)(c) outfile = open(file = “c:\scores.txt”, “w”)(d) outfile = open(file = “c:\\scores.txt”, “w”)This question was posed to me during an interview.My query is from Files topic in division Regular Expressions and Files of Python

Answer» RIGHT ANSWER is (a) outfile = OPEN(“c:\\scores.txt”, “a”)

The EXPLANATION is: a is used to indicate that data is to be APPENDED.
38.

To open a file c:\scores.txt for writing, we use ____________(a) outfile = open(“c:\scores.txt”, “w”)(b) outfile = open(“c:\\scores.txt”, “w”)(c) outfile = open(file = “c:\scores.txt”, “w”)(d) outfile = open(file = “c:\\scores.txt”, “w”)The question was posed to me in exam.This intriguing question originated from Files topic in section Regular Expressions and Files of Python

Answer»

The correct choice is (B) outfile = open(“c:\\scores.txt”, “W”)

The best I can explain: w is used to indicate that file is to be WRITTEN to.

39.

To open a file c:\scores.txt for reading, we use _____________(a) infile = open(“c:\scores.txt”, “r”)(b) infile = open(“c:\\scores.txt”, “r”)(c) infile = open(file = “c:\scores.txt”, “r”)(d) infile = open(file = “c:\\scores.txt”, “r”)This question was posed to me in an online quiz.Question is from Files in chapter Regular Expressions and Files of Python

Answer» CORRECT option is (b) INFILE = OPEN(“c:\\scores.txt”, “r”)

Best EXPLANATION: Execute help(open) to GET more details.
40.

Which of the following lines of code will not show a match?(a) >>> re.match(‘ab*’, ‘a’)(b) >>> re.match(‘ab*’, ‘ab’)(c) >>> re.match(‘ab*’, ‘abb’)(d) >>> re.match(‘ab*’, ‘ba’)This question was addressed to me in an international level competition.I would like to ask this question from Regular Expressions topic in section Regular Expressions and Files of Python

Answer»

The correct CHOICE is (d) >>> re.match(‘ab*’, ‘BA’)

Explanation: In the code shown above, ab* will match to ‘a’ or ‘ab’ or ‘a’ followed by any NUMBER of B’s. HENCE the only line of code from the above options which does not result in a match is:

>>> re.match(‘ab*’, ‘ba’).

41.

Which of the following functions does not accept any argument?(a) re.purge(b) re.compile(c) re.findall(d) re.matchI had been asked this question in an interview for job.My query is from Regular Expressions topic in portion Regular Expressions and Files of Python

Answer»

The CORRECT option is (a) re.purge

Easy explanation - The function re.purge is USED to clear the cache and it does not accept any ARGUMENTS.

42.

Which of the following statements regarding the output of the function re.match is incorrect?(a) ‘pq*’ will match ‘pq’(b) ‘pq?’ matches ‘p’(c) ‘p{4}, q’ does not match ‘pppq’(d) ‘pq+’ matches ‘p’The question was posed to me in an online quiz.Question is taken from Regular Expressions topic in section Regular Expressions and Files of Python

Answer»

Right answer is (d) ‘pq+’ matches ‘p’

EXPLANATION: All of the above statements are correct except that ‘pq+’ match ‘p’. ‘pq+’ will match ‘p’ FOLLOWED by any non-zero number of Q’s, but it will not match ‘p’.

43.

Which of the following functions returns a dictionary mapping group names to group numbers?(a) re.compile.group(b) re.compile.groupindex(c) re.compile.index(d) re.compile.indexgroupI had been asked this question in exam.The question is from Regular Expressions topic in section Regular Expressions and Files of Python

Answer»

Correct answer is (b) re.compile.groupindex

Best explanation: The FUNCTION re.compile.groupindex RETURNS a dictionary MAPPING GROUP NAMES to group numbers.

44.

Which of the codes shown below results in a match?(a) re.match(‘George(?=Washington)’, ‘George Washington’)(b) re.match(‘George(?=Washington)’, ‘George’)(c) re.match(‘George(?=Washington)’, ‘GeorgeWashington’)(d) re.match(‘George(?=Washington)’, ‘Georgewashington’)I had been asked this question in my homework.This intriguing question originated from Regular Expressions topic in chapter Regular Expressions and Files of Python

Answer»

Right option is (c) re.match(‘GEORGE(?=Washington)’, ‘GeorgeWashington’)

BEST explanation: The code SHOWN above demonstrates the use of the function re.match, alongwith the special character ?=. This results in a match only when ‘George’ is immediately followed by ‘Washington’. Also, we have not used the MODULE to IGNORE case. Hence the match is case-sensitive. Therefore the only option which results in a match is:

re.match(‘George(?=Washington)’, ‘GeorgeWashington’)

45.

Which of the following special characters represents a comment (that is, the contents of the parenthesis are simply ignores)?(a) (?:…)(b) (?=…)(c) (?!…)(d) (?#…)This question was addressed to me in homework.The query is from Regular Expressions in division Regular Expressions and Files of Python

Answer»

The CORRECT choice is (d) (?#…)

Easy EXPLANATION - The special character (?#…) represent a comment, that is, the contents of the PARENTHESIS are SIMPLY ignored.

46.

The function of re.search is __________(a) Matches a pattern at the start of the string(b) Matches a pattern at the end of the string(c) Matches a pattern from any part of a string(d) Such a function does not existThe question was asked during a job interview.Query is from Regular Expressions topic in chapter Regular Expressions and Files of Python

Answer»

The correct OPTION is (C) Matches a pattern from any part of a string

The BEST I can EXPLAIN: The re module of PYTHON consists of a function re.search. It’s function is to match a pattern from anywhere in a string.

47.

Which of the following functions results in case insensitive matching?(a) re.A(b) re.U(c) re.I(d) re.XI had been asked this question in exam.This interesting question is from Regular Expressions topic in chapter Regular Expressions and Files of Python

Answer»

The correct ANSWER is (c) re.I

Explanation: The function re.I (that is, re.IGNORECASE) RESULTS in case-insensitive matching. That is, EXPRESSIONS such as [A-Z] will match LOWERCASE characters too.

48.

Which of the following functions clears the regular expression cache?(a) re.sub()(b) re.pos()(c) re.purge()(d) re.subn()This question was posed to me during a job interview.I'm obligated to ask this question of Regular Expressions in section Regular Expressions and Files of Python

Answer»

Right ANSWER is (C) re.purge()

The best I can explain: The function which clears the REGULAR EXPRESSION cache is re.purge(). NOTE that this function takes zero positional arguments.

49.

Choose the function whose output can be: .(a) >>> re.search(‘aaaa’, “alohaaaa”, 0)(b) >>> re.match(‘aaaa’, “alohaaaa”, 0)(c) >>> re.match(‘aaa’, “alohaaa”, 0)(d) >>> re.search(‘aaa’, “alohaaa”, 0)The question was asked in an internship interview.This key question is from Regular Expressions topic in section Regular Expressions and Files of Python

Answer»

The correct option is (a) >>> re.SEARCH(‘aaaa’, “alohaaaa”, 0)

Explanation: The output shown above is that of a search function, whose pattern is ‘aaaa’ and the STRING is that of 8 characters. The only option which matches all these criteria is:

 >>> re.search(‘aaaa’, “alohaaaa”, 0)

50.

The expression a{5} will match _____________ characters with the previous regular expression.(a) 5 or less(b) exactly 5(c) 5 or more(d) exactly 4This question was posed to me in a national level competition.This interesting question is from Regular Expressions in division Regular Expressions and Files of Python

Answer»

The correct choice is (b) EXACTLY 5

The explanation: The character {m} is USED to match exactly m CHARACTERS to the PREVIOUS regular expression. HENCE the expression a{5} will match exactly 5 characters and not less than that.