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.
| 119601. |
Identify the true statement.(a) writerow( ) takes 1 dimensional data(b) writerows( ) takes 2 dimensional data(c) both are true(d) both are false |
|
Answer» (c) both are true |
|
| 119602. |
What is the syntax for csv.reader( )? |
|
Answer» The syntax for csv.reader( ) is where csv.reader(fileobject,delimiter,fmtparams) file object – passes the path and the mode of the file delimiter – an optional parameter containing the standard dilects like, | etc can be omitted fmtparams – optional parameter which help to override the default values of the dialects like skipinitialspace,quoting etc. Can be omitted |
|
| 119603. |
Write a Python program to modify an existing file? |
||||||||||||||||||
|
Answer» import csv row= [‘3’ , ‘Meena’ , ‘Bangalore’] with open(‘student.csv’ , ‘r’) as readFile: reader= csv.reader(readFile) lines =list(reader) # list( ) – to store each row of data as a list lines[3] =row with open(‘student.csv’ , ‘w’) as writeFile: # returns the writer object which converts the user data with delimiter writer= csv.writer(writeFile) #writerows( )method writes multiple rows to a csv file writer, writerows(lines) readFile.close( ) writeFile.close( ) When we Open the student.csv file with text editor, then it will show:
|
|||||||||||||||||||
| 119604. |
What is the difference between the write mode and append mode? |
|
Answer» Append mode write the value of row after the last line of the “student.csv file:” The ‘w’ write mode creates a new file. If the file is already existing ‘w’ mode over writs it. Where as ‘a’ append mode add the data at the end of the file if the file already exists otherwise creates a new one. |
|
| 119605. |
What is CSV File? |
|
Answer» A CSV file is a human readable text file where each line has a number of fields , separated by commas or some other delimiter. A CSV file is also known as a Flat File. Files in the CSV format can be imported to and exported from programs that store data in tables, such as Microsoft Excel or OpenOfficeCalc. |
|
| 119606. |
To open the file updating data, click ………(a) a(b) b(c) t(d) + |
|
Answer» To open the file updating data, click + |
|
| 119607. |
What will happen when you open a file for writing and file already exists there?(a) creates a new file(b) truncates the file(c) overwrite the file(d) append the contents |
|
Answer» (b) truncates the file |
|
| 119608. |
………… opens a file for exclusive creation.(a) r(b) w(c) x(d) + |
|
Answer» x opens a file for exclusive creation. |
|
| 119609. |
Python has a ......... collector to clean up unreferenced objects. |
|
Answer» Python has a garbage collector to clean up unreferenced objects. |
|
| 119610. |
How will you create csv file using MS-Excel? |
||||||||||||||||||||
|
Answer» Create A CSV File Using Microsoft Excel: To create a CSV file using Microsoft Excel, launch Excel and then open the file you want to save in CSV format. For example, below is the data contained in our sample Excel worksheet:
Item Name, Cost-Rs, Quantity, Profit Keyboard,480, 12,1152 , Monitor, 5200, 10, 10400 Mouse,200,50,2000 Total Profit = 13552 |
|||||||||||||||||||||
| 119611. |
The line // white indicates(a) the first two fields of the row are empty(b) It can be deleted(c) comma not necessary(d) only one field is there and , can be deleted |
|
Answer» (a) the first two fields of the row are empty |
|
| 119612. |
…….. command arranges a list value in ascending order |
|
Answer» sort( ) command arranges a list value in ascending order. |
|
| 119613. |
What will be written inside the file test.csv using the following program import csvD = [[‘Exam’],[‘Quarterly’],[‘Halfyearly’]] csv.register_dialect(‘M’ ,lineterminator = ‘\n’)with open(‘c:\pyprg\chl3\line2.csv’ , ‘w’) as f:wr = csv.writer(f,dialect= ’M’)wr.writerows(D)f.close( )(a) Exam Quarterly Halfyearly(b) Exam Quarterly Halfyearly(c) Q H(d) Exam, Quarterly, Halfearly |
|
Answer» (d) Exam, Quarterly, Halfearly |
|
| 119614. |
What is the output of the following program?import csv d=csv.reader(open(‘c:\PYPRG\chl3\city.csv’))next(d)for row in d:print(row)if the file called “city.csv” contain the following detailschennai,mylaporemumbai,andheri(a) chennai,mylapore(b) mumbai,andheri(c) chennai, mumbai(d) chennai,mylapore,mumbai,andheri |
|
Answer» (b) mumbai,andheri |
|
| 119615. |
Making some changes in the data of the existing file or adding more data is called(a) Editing(b) Appending(c) Modification(d) Alteration |
|
Answer» (c) Modification |
|
| 119616. |
………. is used to remove the ordered Diet. |
|
Answer» Dist( ) is used to remove the ordered Diet. |
|
| 119617. |
Which is a dictionary subclass that saves the order in whcih its contents are added?(a) orderedDist(b) SortDist(c) DistSort(d) Sorting |
|
Answer» (a) orderedDist |
|
| 119618. |
Fill in the blanks.1. ……… is used to arrange a list in descending order.2. To sort by more than one column, we can use ……. with multiple indices. |
|
Answer» 1. sort(reverse) 2. itemgetter |
|
| 119619. |
List literals are written using ……..(a) [ ](b) ( )(c) { }(d) <> |
|
Answer» List literals are written using [ ] |
|
| 119620. |
Which of the following creates an object which maps data to a dictionary?(a) listreader( )(b) reader( )(c) tuplereader( )(d) DictReader( ) |
|
Answer» (d) DictReader( ) |
|
| 119621. |
closing a file will free up the resources that were tied with the file and is done by ……. method.(a) Exit(b) close(c) Quit(d) None of these |
|
Answer» closing a file will free up the resources that were tied with the file and is done by close method. |
|
| 119622. |
Which one of the following is used to add the elements in the list.(a) add(b) insert(c) append(d) update |
|
Answer» append is used to add the elements in the list. |
|
| 119623. |
We can register for new dialects using class of csv module. |
|
Answer» csv.register-dialects |
|
| 119624. |
……. method writes a row of data into the specified file |
|
Answer» writerow( ) method writes a row of data into the specified file. |
|
| 119625. |
To sort second column which option have to be selected?(a) itemgetter(0)(b) itemgetter(1)(c) itemgetter(2)(d) itemgetter(3) |
|
Answer» (b) itemgetter(1) |
|
| 119626. |
CSV means …… files(a) common server values(b) comma separated values(c) correct separator values(d) constructor separated value |
|
Answer» (b) comma separated values |
|
| 119627. |
Which one of the following cannot be used as a column separator?(a) delimiter(b) pipe(c) comma(d) # |
|
Answer» Answer is (d) # |
|
| 119628. |
Which format is not allowed to read data from cav files?(a) quotes(b) pipe(c) comma(d) Asterisk |
|
Answer» Asterisk is not allowed to read data from cav files. |
|
| 119629. |
Which function is used to print the data in dictionary format without order?(a) dictionary(b) print( )(c) dict( )(d) dictprint( ) |
|
Answer» Answer is (c) dict( ) |
|
| 119630. |
The number of parameters in csv.writer( ) …….(a) 2 (b) 3 (c) 4 (d) 5 |
|
Answer» The number of parameters in csv.writer( ) 3 |
|
| 119631. |
(I) csv.writer work with list/tuple(II) csv.Dictwriter work with dictionary(III) csv.DictReader work with list/tuple/dictionary.(a) (I),(II) – True (III) – False(b) (I) – True (II), (III) – False(c) (I),(II),(III) – True(d) (I),(II),(III) – False |
|
Answer» (a) (I),(II) – True (III) – False |
|
| 119632. |
Which of the following is used to display all the characters after double quotes.(a) Quote(b) Quote-all(c) double quotes(d) single quotes |
|
Answer» (b) Quote-all |
|
| 119633. |
If the fields contains double quotes as part of the data, the internal quotation marks need to be(a) same (b) quarter (c) doubled (d) tripled |
|
Answer» Answer is (c) doubled |
|
| 119634. |
Identify the wrong statement.(a) The writerow( ) writes one row at a time(b) The writerows( ) writes all the data at once(c) No such writerows( ) function in csv |
|
Answer» (c) No such writerows( ) function in csv |
|
| 119635. |
Find the correct statement(I) The last record in the file may or may not have an ending line break(II) Header is must with same format as record lines. (a) (I) is true, (II) is False(b) (I) is False, (II) – True(c) (I), (II) – both are true(d) (I), (II) – both are false |
|
Answer» (a) (I) is true, (II) is False |
|
| 119636. |
The file extension to save excel files are(a) xls Civdsx(b) XL(c) exc or XL(d) XL or xlx |
|
Answer» (a) xls Civdsx |
|
| 119637. |
How will you open a new file in Notepad?(a) File → New(b) Ctrl + N(c) both a and b(d) shift + N |
|
Answer» (c) both a and b |
|
| 119638. |
The additoinal argument fieldnames that are used with csv.DictReader and csv. Dictwriter are called as ......... |
|
Answer» dictionary keys |
|
| 119639. |
How many arguments are there in csv.reader( ) functions?(a) 2(b) 3(c) 4(d) 5 |
|
Answer» There are 3 arguments in csv.reader( ) functions. |
|
| 119640. |
Which one of the following cannot be omitted in cav. reader? (a) file object(b) delimiter(c) fmtparams(d) space |
|
Answer» (a) file object |
|
| 119641. |
Any field containing a newline as part of its data should be given in …….. (a) quotes (b) double colon (c) colon (d) double quotes |
|
Answer» (d) double quotes |
|
| 119642. |
Identify the statement which is correct.(a) csv consumes less memory and faster(b) Excel consumes less memory and slower |
|
Answer» (a) csv consumes less memory and faster |
|
| 119643. |
……… file is used to store tabular data such as spreadsheet or database. |
|
Answer» csv file is used to store tabular data such as spreadsheet or database. |
|
| 119644. |
Identify the wrong statement from the following.(a) Each field may or may not be enclosed in double quotes.(b) If the fields are not enclosed with double quotes, then double quotes may not appear inside the fields (c) Fields containing line breaks, double quotes and commas should be enclosed in single quotes.(d) the last field in the record must not be followed by a comma |
|
Answer» (c) Fields containing line breaks, double quotes and commas should be enclosed in single quotes. |
|
| 119645. |
Give the syntax for csv.writer( ) |
|
Answer» The syntax for csv.writer( ) is csv. writerffileobject, delimiter,fmtparams) where fileobject : passes the path and the mode of the file. delimiter : an optional parameter containing the standard dilects like , | etc can be omitted. fmtparams : optional parameter which help to override the default values of the dialects like skipinitialspace,quoting etc. can be omitted. |
|
| 119646. |
What is the difference between reader( ) and DictReader( ) function? |
|
Answer» Reading CSV File Into A Dictionary: To read a CSV file into a dictionary can be done by using DictReader class of csv module which works similar to the reader( ) class but creates an object which maps data to a dictionary. The keys are given by the fieldnames as parameter. DictReader works by reading the first line of the CSV and using each comma separated value in this line as a dictionary key. The columns in each subsequent row then behave like dictionary values and can be accessed with the appropriate key (i.e. fieldname). The main difference between the csv.reader( ) and DictReader( ) is in simple terms csv. reader and csv.writer work with list/tuple, while csv.DictReader and csv.DictWriter work ‘ with dictionary. csv.DictReader and csv.DictWriter take additional argument fieldnames that are used as dictionary keys. |
|
| 119647. |
If the fields of data in csv file has commas, then it should be given with ……..(a) ,(b) ”(c) ‘(d) : |
|
Answer» If the fields of data in csv file has commas, then it should be given with " |
|
| 119648. |
Write a note on open( ) function of python. What is the difference between the two methods? |
|
Answer» Python has a built-in function open() to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. For Example >>> f = openf’sample.txt”) bopen file in current directory andf is file object >>> f = open(‘c:\ \pyprg\ \chl3sample5.csv’) #specifyingfull path You can specify the mode while opening a file. In mode, you can specify whether you want to read ‘r’ , write ‘w’ or append ‘a’ to the file, you can also specify “text or binary” in which the file is to be opened. The default is reading in text mode. In this mode, while reading from the file the data would be in the format of strings. On the other hand, binary mode returns bytes and this is the mode to be used when dealing with non-text files like image or exe files. f = open(“test.txt”) # since no mode is specified the default mode it is used #perform file operations f.close( ) The above method is not entirely safe. If an exception occurs when you are performing some operation with the file, the code exits without closing the file. The best way to do this is using the “with” statement. This ensures that the file is closed when the block inside with is exited. You need not to explicitly call the close() method. It is done internally. |
|
| 119649. |
There are ………. ways to read a csv file.(a) 1(b) 2 (c) 3 (d) 4 |
|
Answer» There are 2 ways to read a csv file. |
|
| 119650. |
Give the program to add new row to the csv file? |
|||||||||||||||||||||
|
Answer» import csv row= [‘6’ , ‘Sajini’Madurai’] with open(‘student.csv’ , ‘a’) as CF: # append mode to add data at the end writer= csv.writer(CF) writer.writerow(row) # writerow( ) method write a single row of data in file CF.close( ) Output
|
||||||||||||||||||||||