Search Header Logo
Text file handling (reading name and mark from a file)

Text file handling (reading name and mark from a file)

Assessment

Presentation

•

Computers

•

11th Grade

•

Practice Problem

•

Medium

Created by

Kenneth Tai

Used 3+ times

FREE Resource

2 Slides • 5 Questions

1

Multiple Choice

Which instruction is used to create a text file for storing the data?

1

open(file1,"r")

2

open(file1,"w")

3

file1=open("file.txt","r")

4

file1=open("file.txt","a")

2

Multiple Choice

Which instructions can write three text data into a text file in three separated lines?

1

for i in range(3):

file1.write(data)

2

for i in range(3):

write(data+"\n")

3

for i in range(3):

file1.write(data+"\n")

4

for i in range(3):

file1.writelines(data+"\n")

3

Multiple Choice

Which instruction can read out all data from a text file and store the data in a list?

1

data=file1.read()

2

data = file1.readlines()

3

data = file1.readline()

4

data = file1.readlist()

4

Multiple Choice

Which instruction can remove "\n" from data?

1

data=data.strip()

2

data.strip()=data

3

data=strip().data

4

data=strip(data)

5

Multiple Choice

item=["Peter 50", "Paul 48", "Mary 45"]

Which instructions can use to split the data in items and store the name and the number separately?

1

data=item[1].split()

name=data[0]

age=data[1]

2

data=item.split()

name=data[0]

age=data[1]

3

data=split(item)

name=data[0]

age=data[1]

4

name=data.split()

age=data.split()

6

​Reading out name and mark from a text file

file1=open("name_mark.txt","r")

outputstring=file1.readlines()

print(outputstring)

name=[""]*len(outputstring)     #initialize the name array with size outputstring

mark=[0]*len(outputstring)      #initialize the mark array with size outputstring

for i in range(len(outputstring)):

    outputstring[i]=outputstring[i].strip()

    item=outputstring[i].split()   #split the outputstring into a list item

    print(item)

    name[i]=item[0]             #store first item as name

    mark[i]=int(item[1])        #store second item as mark

file1.close

for i in range(len(outputstring)):

    print(name[i],mark[i])

7

Bubble sort (sorting the data in ascending order)

Algorithm:

•a=[7,4,6,8,1,5]
for i = 0 to len(a) - 2

    for j = 0 to (len(a) – i - 2)

       if a [j] > a[j + 1]

          swap the items
      endif

    next j

next i   

pattern-tertiary

Which instruction is used to create a text file for storing the data?

1

open(file1,"r")

2

open(file1,"w")

3

file1=open("file.txt","r")

4

file1=open("file.txt","a")

Show answer

Auto Play

Slide 1 / 7

MULTIPLE CHOICE