栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Python

断言、with-as 篇,python 入门教程之每日 5 or 6 道题 | Python 技能树题库

Python 更新时间:发布时间: 百科书网 趣学号

本篇博客主要为 https://bbs.csdn.net/skill/python 频道练习题模块补充题目,暂定每天提供 5 or 6 道测试题,后面可能会更多哦~。

本篇博客对【进阶语法】→**【断言】**进行出题。

以下题目,默认将正确答案,放置在选项 A 位置

文章目录
  • 知识点:python 断言 与 with-as
    • 第 1 题:
    • 第 2 题:
    • 第 3 题:
    • 第 4 题:
    • 第 5 题:
    • 试题仓库地址如下:

知识点:python 断言 与 with-as 第 1 题:

题目难度:1 星
题干(问题描述):
下面哪一个选项的代码,能输出橡皮擦的博客首页。

选项 A:

num = 10
assert num == 10, "我是断言输出的错误"
print("橡皮擦博客首页", "https://dream.blog.csdn.net/")

选项 B:

num = 9
assert num == 10, "我是断言输出的错误"
print("橡皮擦博客首页", "https://dream.blog.csdn.net/")

选项 C:

num = 9
assert num > 10, "我是断言输出的错误"
print("橡皮擦博客首页", "https://dream.blog.csdn.net/")

选项 D:

num = 11
assert num < 10, "我是断言输出的错误"
print("橡皮擦博客首页", "https://dream.blog.csdn.net/")

正确答案:A

第 2 题:

题目难度:1 星
题干(问题描述):
下面使用 with...as 实现读取文件内容,书写正确的是?

选项 A:

with open("文件路径","r") as f:
	f.read()

选项 B:

with open("文件路径","w") as f:
	f.read()

选项 C:

with open("文件路径","a") as f:
	f.read()

选项 D:

with open("文件路径","wb") as f:
	f.read()

正确答案:A

第 3 题:

题目难度:2 星
题干(问题描述):
下述代码期望实现手动编写一个类,使其对象可供 with...as 语句操作,请选出正确的自定义类,可以让下述代码正常运行。

def get_obj():
    return MyWithAsClass()
with get_obj() as obj:
    print("with 上下文管理")

选项 A:

class MyWithAsClass:
    def __enter__(self):
        print("__enter__")

    def __exit__(self, type, value, trace):
        print("__exit__")

选项 B:

class MyWithAsClass:
    def __enter__(self):
        print("__enter__")

    def __exit__(self):
        print("__exit__")

选项 C:

class MyWithAsClass:
	def __init__(self):
		pass
    def __enter__(self):
        print("__enter__")

选项 D:

class MyWithAsClass:
    def __init__(self):
        print("__enter__")

    def __exit__(self, type, value, trace):
        print("__exit__")

正确答案:A

第 4 题:

题目难度:1 星
题干(问题描述):
下述 with 上下文管理器能正常运行的是?

选项 A:

class MyWithAsClass:
    def __enter__(self):
        print("__enter__")

    def __exit__(self, type, value, trace):
        print("__exit__")


def get_obj():
    return MyWithAsClass()


with get_obj():
    print("with 上下文管理")

选项 B:

class MyWithAsClass:
    def __enter__(self):
        print("__enter__")

    def __exit__(self, type, value, trace):
        print("__exit__")


def get_obj():
    return MyWithAsClass()


with get_obj() as:
    print("with 上下文管理")

选项 C:

class MyWithAsClass:
    def __enter__(self):
        print("__enter__")

    def __exit__(self, type, value, trace):
        print("__exit__")


def get_obj():
    return MyWithAsClass()


as get_obj():
    print("with 上下文管理")

选项 D:

class MyWithAsClass:
    def __enter__(self):
        print("__enter__")

    def __exit__(self, type, value, trace):
        print("__exit__")


def get_obj():
    return MyWithAsClass()


as get_obj():
    print("with 上下文管理")

正确答案:A

第 5 题:

题目难度:1 星
题干(问题描述):
with...as 语句经常配合文件操作使用,请选出下述对文件打开模式说明,对应关系不匹配的内容。

选项 A:

  • r+:打开一个文件,可读可写,如文件不存在,创建新文件。
  • w+:打开一个文件,可读可写,如文件不存在,创建新文件。

选项 B:

  • r:默认模式,以只读方式打开文件。
  • w:打开一个文件只用于写入,文件存在覆盖,不存在创建。

选项 C:

  • a:打开一个文件追加内容,如文件存在内容,则直接追加数据,如文件不存在,创建新文件进行写入。
  • rb:以二进制格式打开一个只读文件。

选项 D:

  • wb:以二进制格式打开一个可写文件,如文件存在内容,则直接追加数据,如文件不存在,创建新文件进行写入。
  • ab:以二进制格式打开一个可追加文件。

正确答案:A

试题仓库地址如下:

https://codechina.csdn.net/hihell/question

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/274880.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号