7.4 任务相关知识测验【含答案】

(1)单选题

def save():

f=open("test.txt","wt",encoding="gbk")

f.write("我abc\nxyz")

f.close()

save()

执行后test.txt文件大小是

A  8字节

B  9字节

C  10字节

D  11字节

(2)单选题

def save():

f=open("test.txt","wt",encoding="utf-8")

f.write("我abc\nxyz")

f.close()

save()

执行后test.txt文件长度是

A  8字节

B  9字节

C  10字节

D  11字节

(3)单选题

def save():

f=open("test.txt","wt",encoding="utf-8")

f.write("我abc\nxyz")

f.close()

save()

执行后,哪个read()函数可以读出全部内容

A  def read():
f=open("test.txt","rt")
for s in f.readlines():
print(s,end="")
f.close()

B  def read():
f=open("test.txt","rt",encoding="utf-8")
for s in f.readlines():
print(s,end="")
f.close()

C  def read():
f=open("test.txt","rt",encoding="utf-8") for s in f.readlines(): print(s) f.close()

D  def read():
f=open("test.txt","rt",encoding="utf-8")
s=f.readline()
print(s)
f.close()

(4)单选题

def save():

f=open("test.txt","wt")

f.write("abc\nxyz")

f.close()

def read():

f=open("test.txt","rt")

s=f.readline()

print(len(s))

f.close()

save()

read()

执行结果是

A  3

B  4

C  5

D  7

(5)单选题

def save(s):

f=open("test.txt","wt")

f.write(s["name"]+"\n")

f.write(s["sex"]+"\n")

f.write(s["age"]+"\n")

f.close()

def read():

f=open("test.txt","rt")

name=f.readline().strip("\n")

sex=f.readline().strip("\n")

age=f.readline().strip("\n")

print(name)

print(sex)

print(age)

f.close()

s={"name":"xxx","sex":"male","age":"20"}

save(s)

read()

结果是

A  

B  

C  

D  

(6)单选题

def save(s):

f=open("test.txt","wt")

f.write(s["name"]+"\n")

f.write(s["sex"]+"\n")

f.write(s["age"]+"\n")

f.close()

def read():

f=open("test.txt","rt")

name=f.readline()

sex=f.readline()

age=f.readline()

print(name,sex,age,sep="")

f.close()

s={"name":"xxx","sex":"male","age":"20"}

save(s)

read()

结果是

A  

B  

C  

D  

(7)单选题

一个文本文件test.txt的一行存储5个整数如下:

1 3 56 67 34

数之间用空格隔开,要读出这些整数的函数正确的是:

A  def read():
f=open("test.txt","rt")
while true:
s=f.read()
if s!="":
print(s)
else:
break
f.close()

B  def read():
f=open("test.txt","rt")
while true:
s=f.readline()
if s!="":
print(s)
else:
break
f.close()

C  def read():
f=open("test.txt","rt")
st=f.readlines()
for s in st:
print(s)
f.close()

D  def read():
f=open("test.txt","rt")
s=f.readline()
st=s.split(" ")
for s in st:
print(s)
f.close()

(8)单选题

def test():

f=open("test.txt","wt+")

f.write("abc")

f.seek(3,0)

f.write("xyz")

f.seek(2,0)

s=f.read()

print(s)

f.close()

test()

结果是

A  c xyz

B  bcxyz

C  cxyz

D  bc xyz

(9)单选题

def save(s):

f=open("test.txt","wt")

f.write(s["name"])

f.write(s["sex"])

f.write(s["age"])

f.close()

def read():

f=open("test.txt","rt")

s=f.read()

print(s)

f.close()

s={"name":"xxx","sex":"male","age":"20"}

save(s)

read()

执行结果是

A  xxx male 20

B  xxx male 20

C  xxxmale20

D  错误

(10)单选题

def test():

f=open("test.txt","wt+")

f.write("我abc")

f.seek(3,0)

f.write("xyz")

f.seek(1,0)

s=f.read()

print(s)

f.close()

test()

结果是

A  axyz

B  bxyz

C  我xyz

D  汉字乱码

(11)单选题

def save(s):

f=open("test.txt","wb")

s=s.encode()

f.write(s)

f.close()

def read():

f=open("test.txt","rt")

s=f.read()

print(s)

f.close()

save("我们\nwe")

read()

结果是

A  我们 we

B  我们 we

C  程序错误

D  程序运行没有错误,但是结果是乱码

(12)单选题

def save(s):

f=open("test.txt","wb")

s=s.encode()

f.write(s)

f.close()

def read():

f=open("test.txt","rt",encoding="utf-8")

s=f.read()

print(s)

f.close()

save("我们\nwe")

read()

结果是

A  我们we

B  我们 we

C  我们

D  结果乱码

(13)单选题

def save():

f=open("test.txt","wt")

f.write("abc\nxyz")

f.close()

def read():

f=open("test.txt","rb")

s=f.read()

for x in s:

print(hex(x),end=" ")

f.close()

save()

read()

结果是

A  0x61 0x62 0x63 0xa 0x78 0x79 0x7a

B  0x61 0x62 0x63 0xd 0x78 0x79 0x7a

C  0x61 0x62 0x63 0xd 0xa 0x78 0x79 0x7a

D  0x61 0x62 0x63 0x78 0x79 0x7a

(14)单选题

def save():

f=open("test.txt","wb")

f.write("abc\nxyz".encode())

f.close()

def read():

f=open("test.txt","rb")

s=f.read()

for x in s:

print(hex(x),end=" ")

f.close()

save()

read()

结果是

A  0x61 0x62 0x63 0xd 0x78 0x79 0x7a

B  0x61 0x62 0x63 0x0d 0xa 0x78 0x79 0x7a

C  0x61 0x62 0x63 0x78 0x79 0x7a

D  0x61 0x62 0x63 0xa 0x78 0x79 0x7a

(15)单选题

f=open("test.txt","wb")

f.write("abc")

f.close()

程序正确写入"abc"

A  正确

B  错误

(16)单选题

f=open("test.txt","at")

f.write("abc")

f.close()

新写入的"abc"在文件的尾部

A  正确

B  错误

(17)单选题

f=open("test.txt","wt")

f.write("abc")

f.seek(2,0)

f.write("123")

f.close()

程序时错误的,因为seek只能用在二进制文件操作中

A  正确

B  错误

(18)单选题

f=open("test.txt","rt")

s=f.readline()

print(s)

f.close()

那么s一定以"\n"结尾

A  正确

B  错误

(19)单选题

f=open("test.txt","wt")

f.write("abc")

f.close()

在test.txt文件不存在时会自动创建test.txt

A  正确

B  错误

(20)单选题

f=open("test.txt","rb+")

f.write("abc")

f.close()

文件test.txt不存在时会自动创建test.txt

A  正确

B  错误

获取标准答案请阅读全文

隐藏内容 需要支付:¥10

未经允许不得转载!7.4 任务相关知识测验【含答案】