Docsity

Computer Science – Data File Handling, Exams of Computer science

Toggle sidebar
bg1
Name of the Chapter- DATA FILE HANDLING
Question
No
Question Content
Learning Objective(if
Provided)
Q1.
Suppose content of ‘mytext.txt’ file is:
What will be the output of the following code?
file = open(“mytext.txt”, ‘r’)
txt = file.read()
print(file.read(10))
(A) The key to
(B) obstacles.
(C) Error
(D No Output
Knowledge
Q2.
Consider the following directory structure:
There are three directories under root directory ‘Admin’.
The current working directory is Final. Identify the
relative path name for file ‘Result1.xlsx’.
(A) .\Result1.xlsx
(B) Admin\Final\First Term\Result1.xlsx
(C) ..\First Term\Result1.xlsx
(D) First Term\Result1.xlsx
Application
Q3.
Which of the following statements is correct regarding
file access modes?
(A) In ‘r’ mode, we can read and write data from/in the
file.
(B) In ‘a’ mode, the existing data will be over-written if
the file exists.
Understanding
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Computer Science – Data File Handling and more Exams Computer science in PDF only on Docsity!

Name of the Chapter- DATA FILE HANDLING Question No Question Content Learning Objective(if Provided) Q1. Suppose content of ‘mytext.txt’ file is: What will be the output of the following code? file = open(“mytext.txt”, ‘r’) txt = file.read() print(file.read(10)) (A) The key to (B) obstacles. (C) Error (D No Output Knowledge Q2. Consider the following directory structure: There are three directories under root directory ‘Admin’. The current working directory is Final. Identify the relative path name for file ‘Result1.xlsx’. (A) .\Result1.xlsx (B) Admin\Final\First Term\Result1.xlsx (C) ..\First Term\Result1.xlsx (D) First Term\Result1.xlsx Application Q3. Which of the following statements is correct regarding file access modes? (A) In ‘r’ mode, we can read and write data from/in the file. (B) In ‘a’ mode, the existing data will be over-written if the file exists. Understanding

(C) In ‘w’ mode, the data in the file is retained and new data will be appended to the end. (D) In ‘a+’ mode, both reading and writing operations can take place and new data is appended to the end of the existing file. Q4. Consider a statement fileobj = open(“myblog.txt”, ‘r’) Which of the following options can be used to print the last line of a text file ‘myblog.txt’? (A) print(fileobj.readlines() -1) (B) disp = fileobj.readlines() print(disp[-1]) (C) print(fileobj.readlines(-1)) (D) print(fileobj.read(-1)) Understanding Q5. Which of the following statements is used to open a text file to add new contents at the end of the already existing text file named ‘mydiary.txt’? (i) with open(“’mydiary.txt”, ‘w’) as fileobj: (ii) fileobj = open(“mydiary.txt”, ‘a’) (iii) with open(“mydiary.txt”,’a’) as fileobj: (iv) with open(“mydiary”, ‘r+’) as fileobj: (A) Only (i) (B) Only (ii) (C) Both (ii) and (iii) (D) Both (i) and (iv) Understanding Q6. “The information is stored in machine-readable format and pickle module is used for reading and writing data from/in the file.” Identify the type of file discussed in the above lines? (A) Text files (B) CSV files (C) Binary file (D) All of the above Understanding Q7. Assume that you are writing in a text file; now you want to identify where the cursor is currently at in the file. Which function will you use for the same? (A) seek() (B) hide() (C) tell() (D) type() Knowledge

Fileobj = open("story.txt", 'r') S1= Fileobj.read(20) S2= Fileobj.tell() Fileobj.seek(34,0) S3= Fileobj.read(20) print(S3) (A) The Tortoise meanwhi (B) slowly but steadily (C) Hare was sleeping (D) he Tortoise in time Q13. Consider a file “online.txt” What will be the output of the following code? fileobj=open('online.txt','r') count = 0 l=fileobj.readline() for i in l: if 'o' in i: count=count+ print(count) fileobj.close() (A) 10 (B) 8 (C) 9 (D) 20 Evaluation Q14. Which function is used to write all the characters? (A) write() (B) writecharacters() (C) writeall() (D) writechar() Understanding Q15. What is the difference between r+ and w+ modes? (A) No difference. (B) In r+ mode, the pointer is initially placed at the beginning of the file and for w+, the pointer is placed at Knowledge

the end. (C) In w+ mode, the pointer is initially placed at the beginning of the file and for r+, the pointer is placed at the end. (D) Depends on the operating system. Q16 Information stored on a storage device with a specific name. is called a A. Array B. Dictionary C. file D. tuple Q17 EOL stands for A. End of Lines B. End Of Line C. End Of List D. End Of Location Q18 Which of the following file types allows to store large data files in the computer memory? A. Text Files B. Binary Files C. CSV Files D. None of these Q19 Which of the following functions transfer the data to file before closing the file? A. flush() B. close() C. open() D. fflush() Q20 Which of the following is not a known file type? A. csv B. pdf C. txt D. txp Q21 Which of the following not a valid file extension for file student? A. student.txt

Q27 The CSV files can be operated by _______software. A. Spreadsheet B. Notepad C. MS Excel D. All of the above Q28 Which of the following parameter needs to be added with open function to avoid blank row followed file each row in CSV file? A. delimiter B. newline C. writer, delimiter D. file object Q29 12. Default EOL character in Python. A. ‘\n’ B. ‘\r’ C. ‘’ D. ‘\t’ Q30 Trying to open a binary file using a text editor will show: A. Garbage values B. ASCII values C. Binary character D. Unicodes Q31 Choose the correct syntax to open a file in write mode. (A) f.open(“file.txt”,’w’) (B) f = open(“file.txt”) (C) f = open(“file.txt”,’w’) (D) f = open(“file.txt”, ‘r’)

KNOWLEDGE AND

UNDERSTANDING

Q32 Choose the correct syntax to open a file in append mode. (A) with open(“file.txt”, “a”) as f : (B) with open(“file.txt”) as “f” (C) with open(“file.txt”, “a”) as f (D) with open(“file.txt”, “w”) as f :

KNOWLEDGE AND

UNDERSTANDING

Q33 Which of the following is not a valid file access mode? (A) a+ (B) r (C) w (D) b+

KNOWLEDGE AND

UNDERSTANDING

Q34 Choose the correct syntax to close a file. (A) f.closed

KNOWLEDGE AND

UNDERSTANDING

(B) f.close() (C) f.closed() (D) f.close Q35 Which method is used to write multiple strings/lines in a text file? (A) write() (B) writeline() (C) writelines() (D) writesingle()

KNOWLEDGE AND

UNDERSTANDING

Q36 Which method is used to write a single string/line in a text file? (A) write() (B) writeline() (C) writelines() (D) writesingle()

KNOWLEDGE AND

UNDERSTANDING

Q37 Which of the following statements is correct? (A) In r+ mode, a file opens for both reading and writing if it already exists. The file pointer placed at the beginning of the file. (B) In w+ mode, a file opens for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, create a new file for reading and writing. (C) Both of the above (D) None of the above

KNOWLEDGE AND

UNDERSTANDING

Q38 By default, in which mode a file open. (A) r (B) w (C) a (D) None of the above

KNOWLEDGE AND

UNDERSTANDING

Q39 While using an open function in which mode a text file will be created, if it does not exist already. (A) r+ (B) w (C) Both of the above (D) None of the above

APPLICATION

Q40 While using an open function in which mode a text file will not be created, if it does not exist already. (A) r+ (B) r (C) Both of the above (D) None of the above

APPLICATION

Q41 Which of the following iterable objects can be passed to the writelines() method for writing multiple lines/strings in a text file.

APPLICATION

(C) string 1 of list lines will overwrite string 2 of list lines in the file ‘readme.txt’. (D) string 2 of list lines will overwrite string 1 of list lines in the file ‘readme.txt’. Q46 What are the file types in Python? (A) Text File (B) TSV File (C) CSV File (D) All of the above Understanding Q47. Which command is used to open a text file in Python in reading mode? (A) F = open( ) (B) F = open(“Data.Txt”,”w”) (C) F = open(“Data.Txt,”r”) (D) All of the Above Analysis Q48 What is the use of File Object? (A) File Object Serves as a link to a file (B) File Objects are used to read and write data to the file (C) A file Object is a reference to a file on disk (D) All of the above Knowledge Q49 Which is the default mode to open a file? (A) read mode (B) r+ mode (C) write mode (D) w+ mode Knowledge Q50 Which mode creates a new file, when open file? (A) w+ mode (B) a mode (C) a+ mode (D) All of the above Understanding

Q51 Which function is used to read the contents from a file? (A) read ( ) (B) write ( ) (C) close ( ) (D) None of these Knowledge Q52 Which of the following command is used to open a file “C:\Data.txt for writing as well as reading mode? (A) f = open(“c:\pat.txt”,”w”) (B) f = open(“c:\pat.txt”,”wb”) (C) f = open(“c:\pat.txt”,”w+”) (D) f = open(“c:\pat.txt”,”wb+”) Application Q53 Which function is used to write a list of strings in a file? (A) writeline ( ) (B) writelines ( ) (C) writelist ( ) (D) writefulline ( ) Understanding Q54 When open a file in append mode, by default new data can be added at the? (A) Beginning of the file (B) End of the file (C) At the middle of the file (D) All of the above Analysis Q55. Find the output of the following code – fp = open("sample.txt", "r") fp.read(8) print(fp.tell()) fp.close() (A) 0 Application

Fileobj.close() What is the output of the above given code? (A) Generates an error : x must be string. (B) It will write 200 at the end of the file. (C) It will write 200 at the beginning of the file. (D)It will delete the existing content and write 200 in the beginning. Q59 Consider a file “online.txt” With educational institutes closed due to the COVID- pandemic, the government has been encouraging online education to achieve academic continuity. Online education allows for learning something beyond the norm. What will be the output of the following code? fileobj=open('online.txt','r') count = 0 l=fileobj.readline() for i in l: if 'o' in i: count=count+ print(count) fileobj.close() (A) 10 (B) 8 (C) 9 Application

(D)

Q60 Consider the content of ‘rhyme.txt’ file: What will be the output of the following code? fileobj=open(‘rhyme.txt','r') count = 0 l=fileobj. readlines() for i in l: if(i[0]=='H'or i[0]==’T’): count=count+ print(count) fileobj.close( ) (A) 4 (B) 2 (C) 3 (D) 1 Analysis Q61 Which of the following file mode will refer to the TEXT mode? (A) rb (B) b (C) b+ (D) w

UNDERSTANDING

KNOWLEDGE

ANALYSIS

(A) read() (B) csv.reader() (C) csv.read() (D) reader()

KNOWLEDGE

ANALYSIS

Q67 The correct syntax to read from Text file is__________ (A) .read() (B) read.(filehandle) (C) read(filehanle).txt (D) None of the above

UNDERSTANDING

KNOWLEDGE

ANALYSIS

Q68 The correct syntax to reads a line of input from Text file is__________ (A) .read() (B) readline.(filehandle) (C) read(filehanle).txt (D) .readline()

UNDERSTANDING

KNOWLEDGE

ANALYSIS

Q69 The correct syntax to reads all lines of input from Text file and returns them in a list is __________ (A) .readlines() (B) readline.(filehandle) (C) read(filehanle).txt (D) .readline()

UNDERSTANDING

KNOWLEDGE

ANALYSIS

Q70 In which file, delimiters are used for line and translations occur? (A) Text file (B) Binary file (C) Both are correct (D) None of the above

UNDERSTANDING

KNOWLEDGE

ANALYSIS

Q71 observe the following code and answer the follow f1=open("mydata","a") ______#blank f1.close() what type of file is mydata? (A) Text file (B) Binary file (C) Both are correct (D) None of the above

UNDERSTANDING

KNOWLEDGE

ANALYSIS

Q72 observe the following code and answer the follow f1=open("mydata","a") ______#blank f1.close() Fill in the blank1 with correct statement to write "abc" in the file "mydata" (A) f1.write(“abc”) (B) write(abc) (C) write.f1(abc) (D) None of the above

UNDERSTANDING

KNOWLEDGE

ANALYSIS

Q73 In which of the following file modes the existing data of the file will not be lost? (A) ab and a+b mode (B) r mode (C) w mode (D) None of the above

UNDERSTANDING

KNOWLEDGE

ANALYSIS

(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) results in an error Q Which function is used to read single line from file? (a) readline() (b) readlines() (c) readstatement( ) (d) readfulline() Knowledge Q81 Which function is used to change the position of the File Handle to a given specific position. a. seek() b. read() c. tail() d. write() Knowledge Q82 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 Understanding Q83 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 mentioned Understanding Q84 How many arguments passed to the tell()? a. 0 b. 1 c. 2 d. variable number of arguments Knowledge Q85 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 mentioned Understanding Q86 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 Knowledge

c) file position remains unchanged d) error Q87 readlines() method return _________ a. String b. List c. Dictionary d. Tuple Knowledge Q88 Which of the following method is used to read a specified number of bytes of data from a data file. a. read( ) b. read(n) c. readlines( ) d. reading(n) Knowledge Q89 Write the output of the following: f=open("test.txt","w+") f.write("File-Handling") f.seek(0) a=f.read(5) print(a) a. File- b. File c. File-H d. No Output Understanding Q90 Write the output of the following: f=open("test.txt","w+") f.write("FileHandling") f.seek(0) a=f.read(-1) print(a) a. File b. Handling c. FileHandling d. No Output Understanding Q Soumya is learning to use word processing software for the first time. She creates a file with some text formatting features using notepad. What type of file is she working on? A. Text file B. CSV file Knowledge