

McqMate
These multiple-choice questions (MCQs) are designed to enhance your knowledge and understanding in the following areas: Computer Science Engineering (CSE) , Information Technology Engineering (IT) , Electrical Engineering , Civil Engineering , Mechanical Engineering .
Chapters
201. |
Write the list comprehension to pick out only negative integers from a given list ‘l’. |
A. | [x<0 in l] |
B. | [x for x<0 in l] |
C. | [x in l for x<0] |
D. | [x for x in l if x<0] |
Answer» D. [x for x in l if x<0] | |
Explanation: to pick out only the negative numbers from a given list ‘l’, the correct list comprehension statement would be: [x for x in l if x<0]. |
202. |
=[x+y for x, y in zip(l1, l2)] l3 |
A. | error |
B. | 0 |
C. | [-20, -60, -80] |
D. | [0, 0, 0] |
Answer» D. [0, 0, 0] | |
Explanation: the code shown above returns x+y, for x belonging to the list l1 and y belonging to the list l2. that is, l3=[10-10, 20-20, 30-20], which is, [0, 0, 0]. |
203. |
Write a list comprehension for number and its cube for l=[1, 2, 3, 4, 5, 6, 7, 8, 9]. |
A. | [x**3 for x in l] |
B. | [x^3 for x in l] |
C. | [x**3 in l] |
D. | [x^3 in l] |
Answer» A. [x**3 for x in l] | |
Explanation: the list comprehension to print |
204. |
Write a list comprehension for producing a list of numbers between 1 and 1000 that are divisible by 3. |
A. | [x in range(1, 1000) if x%3==0] |
B. | [x for x in range(1000) if x%3==0] |
C. | [x%3 for x in range(1, 1000)] |
D. | [x%3=0 for x in range(1, 1000)] |
Answer» B. [x for x in range(1000) if x%3==0] | |
Explanation: the list comprehension [x for x in range(1000) if x%3==0] produces a list of numbers between 1 and 1000 that are divisible by 3. |
205. |
Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212]. |
A. | [(2**x) for x in range(0, 13)] |
B. | [(x**2) for x in range(1, 13)] |
C. | [(2**x) for x in range(1, 13)] |
D. | [(x**2) for x in range(0, 13)] |
Answer» A. [(2**x) for x in range(0, 13)] | |
Explanation: the required list comprehension will print the numbers from 1 to 12, each raised to 2. the required answer is thus, [(2**x) for x in range(0, 13)]. |
206. |
What is the data type of (1)? |
A. | tuple |
B. | integer |
C. | list |
D. | both tuple and integer |
Answer» B. integer | |
Explanation: a tuple of one element must be created as (1,). |
207. |
What type of data is: a=[(1,1),(2,4),(3,9)]? |
A. | array of tuples |
B. | list of tuples |
C. | tuples of lists |
D. | invalid type |
Answer» B. list of tuples | |
Explanation: the variable a has tuples enclosed in a list making it a list of tuples. |
208. |
Is the following Python code valid? >>> a,b=1,2,3 |
A. | yes, this is an example of tuple unpacking. a=1 and b=2 |
B. | yes, this is an example of tuple unpacking. a=(1,2) and b=3 |
C. | no, too many values to unpack |
D. | yes, this is an example of tuple unpacking. a=1 and b=(2,3) |
Answer» C. no, too many values to unpack | |
Explanation: for unpacking to happen, the number of values of the right hand side must be equal to the number of variables on the left hand side. |
209. |
Tuples can’t be made keys of a dictionary. |
A. | true |
B. | false |
Answer» B. false | |
Explanation: tuples can be made keys of a dictionary because they are hashable. |
210. |
Which of the following statements create a dictionary? |
A. | d = {} |
B. | d = {“john”:40, “peter”:45} |
C. | d = {40:”john”, 45:”peter”} |
D. | all of the mentioned |
Answer» D. all of the mentioned | |
Explanation: dictionaries are created by specifying keys and values. |
211. |
Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use? |
A. | d.delete(“john”:40) |
B. | d.delete(“john”) |
C. | del d[“john”] |
D. | del d(“john”:40) |
Answer» C. del d[“john”] | |
Explanation: execute in the shell to verify. |
212. |
Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we use? |
A. | d.size() |
B. | len(d) |
C. | size(d) |
D. | d.len() |
Answer» B. len(d) | |
Explanation: execute in the shell to verify. |
213. |
print(list(d.keys())) |
A. | [“john”, “peter”] |
B. | [“john”:40, “peter”:45] |
C. | (“john”, “peter”) |
D. | (“john”:40, “peter”:45) |
Answer» A. [“john”, “peter”] | |
Explanation: the output of the code shown above is a list containing only keys of the dictionary d, in the form of a list. |
214. |
Which of these about a dictionary is false? |
A. | the values of a dictionary can be accessed using keys |
B. | the keys of a dictionary can be accessed using values |
C. | dictionaries aren’t ordered |
D. | dictionaries are mutable |
Answer» B. the keys of a dictionary can be accessed using values | |
Explanation: the values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values. |
215. |
Which of the following is not a declaration of the dictionary? |
A. | {1: ‘a’, 2: ‘b’} |
B. | dict([[1,”a”],[2,”b”]]) |
C. | {1,”a”,2”b”} |
D. | { } |
Answer» C. {1,”a”,2”b”} | |
Explanation: option c is a set, not a dictionary. |
216. |
Which of the following isn’t true about dictionary keys? |
A. | more than one key isn’t allowed |
B. | keys must be immutable |
C. | keys must be integers |
D. | when duplicate keys encountered, the last assignment wins |
Answer» C. keys must be integers | |
Explanation: keys of a dictionary may be any data type that is immutable. |
217. |
Which of the statements about dictionary values if false? |
A. | more than one key can have the same value |
B. | the values of the dictionary can be accessed as dict[key] |
C. | values of a dictionary must be unique |
D. | values of a dictionary can be a mixture of letters and numbers |
Answer» C. values of a dictionary must be unique | |
Explanation: more than one key can have the same value. |
218. |
If a is a dictionary with some key-value pairs, what does a.popitem() do? |
A. | removes an arbitrary element |
B. | removes all the key-value pairs |
C. | removes the key-value pair for the key given as an argument |
D. | invalid method for dictionary |
Answer» A. removes an arbitrary element | |
Explanation: the method popitem() removes a random key-value pair. |
219. |
If b is a dictionary, what does any(b) do? |
A. | returns true if any key of the dictionary is true |
B. | returns false if dictionary is empty |
C. | returns true if all keys of the dictionary are true |
D. | method any() doesn’t exist for dictionary |
Answer» A. returns true if any key of the dictionary is true | |
Explanation: method any() returns true if any key of the dictionary is true and false if the dictionary is empty. |
220. |
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”) |
Answer» B. outfile = open(“c:\\scores.txt”, “w”) | |
Explanation: w is used to indicate that file is to be written to. |
221. |
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”) |
Answer» A. outfile = open(“c:\\scores.txt”, “a”) | |
Explanation: a is used to indicate that data is to be appended. |
222. |
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 mentioned |
Answer» D. all of the mentioned | |
Explanation: the program will throw an error. |
223. |
1 TEXT FILES, READING AND WRITING FILES, FORMAT OPERATOR |
A. | infile.read(2) |
B. | infile.read() |
C. | infile.readline() |
D. | infile.readlines() |
Answer» A. infile.read(2) | |
Explanation: execute in the shell to verify. |
224. |
print(f.closed) |
A. | true |
B. | false |
C. | none |
D. | error |
Answer» A. true | |
Explanation: the with statement when used with open file guarantees that the file object is closed when the with block exits. |
225. |
Which are the two built-in functions to read a line of text from standard input, which by default comes from the keyboard? |
A. | raw_input & input |
B. | input & scan |
C. | scan & scanner |
D. | scanner |
Answer» A. raw_input & input | |
Explanation: python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. these functions are: |
226. |
Which one of the following is not attributes of file? |
A. | closed |
B. | softspace |
C. | rename |
D. | mode |
Answer» C. rename | |
Explanation: rename is not the attribute of file rest all are files attributes. |
227. |
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 mentioned |
Answer» A. tells you the current position within the file | |
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. |
228. |
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 mentioned |
Answer» A. rename(current_file_name, new_file_name) | |
Explanation: this is the correct syntax which has shown below. |
229. |
fo.close() |
A. | compilation error |
B. | syntax error |
C. | displays output |
D. | none of the mentioned |
Answer» C. displays output | |
Explanation: it displays the output as shown below. the method next() is used when a file is used as an iterator, typically in a loop, the next() method is called repeatedly. this method returns the next input line, or raises stopiteration when eof is hit. |
230. |
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 mentioned |
Answer» A. sets the file’s current position at the offset | |
Explanation: sets the file’s current position at the offset. the method seek() sets the file’s current position at the offset. |
231. |
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 mentioned |
Answer» A. truncates the file size | |
Explanation: the method truncate() truncates the file size. following is the syntax for truncate() method: |
232. |
Which is/are the basic I/O connections in file? |
A. | standard input |
B. | standard output |
C. | standard errors |
D. | all of the mentioned |
Answer» D. all of the mentioned | |
Explanation: standard input, standard output and standard error. standard input is the data that goes to the program. the standard input comes from a keyboard. standard output is where we print our data with the print keyword. unless redirected, it is the terminal console. the standard error is a stream where programs write their error messages. it is usually the text terminal. |
233. |
sys.stdout.write('Python\n') |
A. | compilation error |
B. | runtime error |
C. | hello python |
D. | hello python |
Answer» D. hello python | |
Explanation: none output: |
234. |
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 mentioned |
Answer» A. it is used for object serialization | |
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. |
235. |
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 mentioned |
Answer» B. it is used for object deserialization | |
Explanation: 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. |
236. |
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 mentioned |
Answer» B. file object = open(file_name [, access_mode][, buffering]) | |
Explanation: open() function correct syntax with the parameter details as shown below: file object = open(file_name [, access_mode] [, buffering]) |
237. |
fo.close() |
A. | compilation error |
B. | runtime error |
C. | no output |
D. | flushes the file when closing them |
Answer» D. flushes the file when closing them | |
Explanation: the method flush() flushes the internal buffer. python automatically flushes the files when closing them. but you may want to flush the data before closing any file. |
238. |
Correct syntax of file.writelines() is? |
A. | file.writelines(sequence) |
B. | fileobject.writelines() |
C. | fileobject.writelines(sequence) |
D. | none of the mentioned |
Answer» C. fileobject.writelines(sequence) | |
Explanation: 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. |
239. |
Correct syntax of file.readlines() is? |
A. | fileobject.readlines( sizehint ); |
B. | fileobject.readlines(); |
C. | fileobject.readlines(sequence) |
D. | none of the mentioned |
Answer» A. fileobject.readlines( sizehint ); | |
Explanation: 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. |
240. |
In file handling, what does this terms means “r, a”? |
A. | read, append |
B. | append, read |
C. | write, append |
D. | none of the mentioned |
Answer» A. read, append | |
Explanation: r- reading, a-appending. |
241. |
What is the use of “w” in file handling? |
A. | read |
B. | write |
C. | append |
D. | none of the mentioned |
Answer» B. write | |
Explanation: this opens the file for writing. it will create the file if it doesn’t exist, and if it does, it will overwrite it. |
242. |
What is the use of “a” in file handling? |
A. | read |
B. | write |
C. | append |
D. | none of the mentioned |
Answer» C. append | |
Explanation: this opens the fhe file in appending mode. that means, it will be open for writing and everything will be written to |
243. |
Which function is used to read all the characters? |
A. | read() |
B. | readcharacters() |
C. | readall() |
D. | readchar() |
Answer» A. read() | |
Explanation: the read function reads all characters fh = open(“filename”, “r”) content = fh.read(). |
244. |
Which function is used to read single line from file? |
A. | readline() |
B. | readlines() |
C. | readstatement() |
D. | readfullline() |
Answer» B. readlines() | |
Explanation: the readline function reads a single line from the file fh = open(“filename”, “r”) |
245. |
Which function is used to write all the characters? |
A. | write() |
B. | writecharacters() |
C. | writeall() |
D. | writechar() |
Answer» A. write() | |
Explanation: to write a fixed sequence of characters to a file |
246. |
Which function is used to write a list of string in a file? |
A. | writeline() |
B. | writelines() |
C. | writestatement() |
D. | writefullline() |
Answer» A. writeline() | |
Explanation: with the writeline function you can write a list of strings to a file |
247. |
Which function is used to close a file in python? |
A. | close() |
B. | stop() |
C. | end() |
D. | closefile() |
Answer» A. close() | |
Explanation: f.close()to close it and free up any system resources taken up by the open file. |
248. |
Is it possible to create a text file in python? |
A. | yes |
B. | no |
C. | machine dependent |
D. | all of the mentioned |
Answer» A. yes | |
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(). |
249. |
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+ |
Answer» A. wb+ | |
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. |
250. |
Which of the following is not a valid mode to open a file? |
A. | ab |
B. | rw |
C. | r+ |
D. | w+ |
Answer» B. rw | |
Explanation: use r+, w+ or a+ to perform both read and write operations using a single file object. |
Done Studing? Take A Test.
Great job completing your study session! Now it's time to put your knowledge to the test. Challenge yourself, see how much you've learned, and identify areas for improvement. Don’t worry, this is all part of the journey to mastery. Ready for the next step? Take a quiz to solidify what you've just studied.