最新PYTHON测试题

合集下载

python基础测试题

python基础测试题

python基础测试题一、选择题1、以下哪个是 Python 中的合法变量名?()A 123varB var 123C my_varD @var答案:C解析:在 Python 中,变量名只能包含字母、数字和下划线,且不能以数字开头。

选项 A 以数字开头,选项 B 中间有空格,选项 D 以特殊字符@开头,都不符合变量名的规则,只有选项 C 是合法的变量名。

2、以下代码的输出结果是什么?()```pythonx = 5y = 2print(x // y)```B 2C 3D 7答案:B解析:在Python 中,`//`是整除运算符,返回商的整数部分。

5 除以 2 的商为 25,取整数部分为 2,所以输出结果是 2。

3、以下哪个方法可以将字符串转换为整数?()A `str()`B `int()`C `float()`D `complex()`答案:B解析:`int()`函数用于将一个数字或字符串转换为整数。

`str()`函数将对象转换为字符串,`float()`函数将对象转换为浮点数,`complex()`函数用于创建复数。

4、以下哪个是 Python 中用于创建列表的符号?()A `{}`B `()`D `<>`答案:C解析:在 Python 中,使用方括号``来创建列表。

花括号`{}`用于创建字典,小括号`()`用于创建元组等。

5、以下代码的输出结果是什么?()```pythonmy_list = 1, 2, 3, 4, 5print(my_list1:4)```A `2, 3, 4`B `1, 2, 3`C `2, 3, 4, 5`D `1, 2, 3, 4`答案:A解析:列表的切片操作`start:end` 会返回从索引`start` (包含)到索引`end` (不包含)的子列表。

在这个例子中,`my_list1:4` 会返回索引 1 到索引 3 的元素,即`2, 3, 4` 。

二、填空题1、 Python 中的注释可以使用_____(单行注释)和_____(多行注释)。

python考试最新试题及答案解析图文复习知识点试卷试题

python考试最新试题及答案解析图文复习知识点试卷试题

python考试最新试题及答案解析图文复习知识点试卷试题一、选择题1.我们学习了使用Python程序设计语言解决问题,Python程序设计语言属于()。

A.高级语言B.自然语言C.汇编语言D.机器语言2.算法用Python程序实现,以下代码中哪处语句存在语法错误()A.B.C.D.3.下列 Python 表达式的值为偶数的是()A.12*3%5 B.len(“Welcome”)C.int(3.9)D.abs(-8)4.下列属于计算机编程语言的有()①Python ②Visual Basic ③Java ④C++⑤CPUA.①②④⑤B.②③④⑤C.①②③④D.①③④5.在python中,运行下列程序,正确的结果是()x=1while x>0:x=x+1print( x)A.1 B.1 2 3 4 5 6 7 8 9…………………C.无输出D.1006.在Python中以下语句正确的是()。

A.51jb= "51jb" B.for= "51jb" C.j,b=b,j D.//这是一段测试代码7.利用Python求100以内所有能被3整除的正整数,代码如下:则下列程序第3行的横线应该填()A.i=i*3 B.i=i%3 C.i%3==0 D.i/3==18.以下python程序段运行后,x和st的值分别是()x= 123 + 678st = "123" + "678"A.123678 "123678" B.123678 "801" C.801 "123678" D.801 "801"9.以下Python程序段执行后,输出结果为()。

A.m不能被3整除B.29不能被3整除C.29能被3整除D.m能被3整除10.下列Python表达式中,能正确表示不等式方程|x|>1解的是()A.x>1 or x<-1 B.x>-1 or x<1 C.x>1 and x<-1 D.x>-1 and x<1 11.已知列表list1=[10,66,27,33,23],则python表达式max(list1)的值为()A.10 B.66 C.5 D.2312.下列选项中,可作为Python变量名的是()A.int B.Abc C.float D.Complex13.关于python程序设计语言,下列说法不正确的是( )。

试题python基础试题含答案

试题python基础试题含答案

试题python基础试题含答案一、选择题(每题2分,共20分)1. 以下哪个是Python中的有效变量名?A. 2abcB. abc1C. abc-defD. 1abc答案:B2. 在Python中,哪个关键字用于定义类?A. functionB. classC. defD. return答案:B3. 以下哪个函数用于计算两个数的乘积?A. multiply()B. add()C. divide()D. subtract()答案:A4. 以下哪个模块用于操作文件和目录?A. osB. sysC. mathD. json答案:A5. 在Python中,哪个关键字用于定义函数?A. functionB. classC. defD. return答案:C6. 以下哪个语句用于遍历列表中的每个元素?A. for i in range(len(list)):B. for i in range(1, len(list)+1):C. for i in list:D. for i in range(list):答案:A7. 以下哪个函数用于随机生成一个整数?A. random.randint()B. random.random()C. random.randrange()D. random.choice()答案:A8. 以下哪个模块用于进行数学计算?A. mathB. osC. sysD. json答案:A9. 以下哪个关键字用于定义变量?A. intB. floatC. defD. var答案:D10. 以下哪个语句用于插入一个元素到列表的指定位置?A. list.insert(index, element)B. list.add(index, element)C. list.append(element)D. list.remove(element)答案:A二、填空题(每题2分,共20分)1. 在Python中,定义类的关键字是______。

Python自动化测试面试题大全2024版:面向测试开发工程师的实用指南!习题集与答案解析

Python自动化测试面试题大全2024版:面向测试开发工程师的实用指南!习题集与答案解析

Python自动化测试(2024版)_习题及答案解析(答案见尾页)一、选择题1. Python自动化测试的目的是什么?A. 提高软件质量B. 减少测试用例数量C. 提高开发效率D. 以上全部2. 下面哪个不是Python自动化测试中的基本框架?A. unittestB. pytestC. noseD. pygame3. 以下哪种测试方法不属于单元测试?A. 功能测试B. 性能测试C. 接口测试D. 所有选项都是4. 在Python中,如何编写一个简单的单元测试类?A. class TestCase:def test_method(self):passB. class TestCase:def test_method1():passdef test_method2():passC. class TestCase:def test_method(self):passD. class TestCase:def test_method(self):pass5. 下列哪个库在Python中常用于接口测试?A. requestsB. unittestC. pytestD. all of the above6. 以下哪个模块在Python中提供性能测试的功能?A. timeB. timeitC. unittestD. all of the above7. 以下哪种测试用例设计方法不属于等价类划分法?A. 等价类划分法B. 边界值分析法C. 决策表法D. 所有选项都是8. 以下哪个函数在Python中用于生成随机数?A. random.randint()B. random.random()C. time.time()D. string.ascii_letters9. 以下哪个模块在Python中常用于处理文件和目录操作?A. osB. timeC. randomD. all of the above10. 以下哪个模块在Python中常用于网络请求?A. requestsB. timeC. randomD. all of the above11. 单元测试的核心思想是保证代码的每个部分能够独立工作。

Python(二级)考试题+答案

Python(二级)考试题+答案

Python二级考试题一、单项选择题(每题1.5分,共30分)1.下列哪个关键字可以起到终止循环的作用(C)A、continueB、whileC、breakD、with2.已知列表num=[8,9,2,7,3,5,6,4],运行print(num[::2]),结果是(D)A、8,9,2B、[8,9,2]C、8,2,3,6D、[8,2,3,6]3.运行下列代码,可知a的取值范围为(A)import randoma=random.randint(1,100)A、1≤a≤100B、1≤a≤99C、0≤a≤99D、1<a≤1004.下列哪个自定义列表名称符合规则(C)A、%32B、5alrightC、_numD、not5.执行下列代码的结果为(B)list_1=[37,23,66,54,27,19,88,32]print(len(list_1))A、[37,23,66,54,27,19,88,32]B、8C、len(list_1)D、76.运行下列程序,当输入Tom时,运行结果为(B)name=["Tom","Jerry","Tony","Lisa","Paul"]a=input("输入要查询的姓名:")if a not in name:print("yes!")else:print("no!")A、“yes!”B、“no!”C、程序报错D、True7.下面哪个指令是清空列表内容(D)A、list.insert(数据,索引)B、list.sort()C、del list[数据]D、list.clear()8.运行下列代码的结果是(C)list_1=[37,23,66,54,27,19,88,32]if36in list_1:print("happy!")else:print("unhappy!")A、happy!B、TrueC、unhappy!D、False9.运行下列代码,不可能出现的结果是(B)for a in range(1,8):print(a)A、5B、8C、1D、710.如果要使列表list_1由[9,32,44,5,27,88,10]变为[88,44,32,27,10,9,5],需要用到的操作是(D)A、list_1.insert()B、list_1.sort()C、max(list_1)D、list_1.sort(reverse=True)11.下列对于循环嵌套描述正确的是(D)A、for循环只能嵌套for循环B、for循环只能嵌套while循环C、while循环只能嵌套while循环D、for循环和while循环之间可以互相嵌套12.执行下列代码的结果是(C)tuple=("小明","小红","小白","小亮","小黄")del tuple[2]print(tuple)A、("小明","小白","小亮","小黄")B、("小明","小红","小亮","小黄")C、程序报错D、("小明","小红","小白","小黄")13.想要查询列表list_1=[99,32,23,55,45,65,12]中一共有多少个数据,可以用到的指令是(C)A、list_1.sort()B、max(list_1)C、len(list_1)D、list_1.clear()14.下列集合名称的命名哪一个不符合规范的(A)A、continueB、_jxy3C、A5B6D、setup15.以下哪个指令是找出列表中所有数据中最小值的指令(D)A、list.remove()B、max(list)C、len(list)D、min(list)16.下列哪个指令可以判断a是否为奇数(B)A、a//2=1B、a%2=1C、a/2=1D、a**2=117.运行代码print("good"+"morning")后,正确的结果是(A)A、goodmorningB、good morningC、“good morning”D、good,morning18.如果想让最后的运行结果为11,请问代码第一行s=x中的x应该设为多少(B)s=xfor a in range(1,5):s=s+aprint(s)A、0B、1C、2D、319.如果想从字符串poem=“天生我材必有用”中截取“我必用”三个字,可以采用下列哪项操作(B)A、print(poem[::2])B、print(poem[2::2])C、print(poem[2:])D、print(poem[1:5:2])20.运行下列代码,当输入87时,运行结果为(B)a=int(input("请输入任意整数:"))if a>=10and a<100:if a%2==0:print("成双成对")else:print("独善其身")else:print("天下无双")A、天下无双B、独善其身C、87D、成双成对二、多项选择题(每题2分,共10分)1.下列四组容器哪一组是元组(A、D)A、num=(1,2,3,4,5,6)B、tuple=[1,2,3,4,5,6]C、tuple={1,2,3,4,5,6}D、list=(“a”,”b”,”c”)2.运行下列代码,不可能出现的结果有(B、C、D)for a in range(10,20):if a%2==0:print(a)A、12B、13C、15D、203.执行下列代码,如果要使结果为“XXXX”,那么输入哪些数字可以实现该结果(A、B、C、D)a=int(input("请输入一个自然数:"))for s in range(1,5):if a>=s:print("X",end="")else:print("Y",end="")A、4B、5C、6D、74.以下代码运行结果为15的有哪些(A、B、C、D)A、s=0for a in range(1,6):s=s+aprint(s)B、s=0for a in range(1,8):if a>=6:continues=s+aprint(s)C、s=0for a in range(1,8):s=s+aif a>=5:breakprint(s)D、list_1=[9,27,16,29,33,15,22,35]print(list_1[5])5.超市新进一批水果,将各种水果种类输入电脑系统中生成了列表fruits=["苹果","香蕉","葡萄","菠萝","蛋挞"],但由于工作疏忽,将“柠檬”错输成“蛋挞”,下列哪段代码可以帮助工作人员修正错误(A、B)A、fruits=["苹果","香蕉","葡萄","菠萝","蛋挞"]fruits=fruits[:4]fruits.append("柠檬")print(fruits)B、fruits=["苹果","香蕉","葡萄","菠萝","蛋挞"]fruits[4]="柠檬"print(fruits)C、fruits=["苹果","香蕉","葡萄","菠萝","蛋挞"] fruits=fruits[:4]print(fruits)D、fruits=["苹果","香蕉","葡萄","菠萝","蛋挞"] fruits.insert(4,"柠檬")print(fruits)三、判断题(每题1分,共10分)1.Python的元组中所有的数据必须是同一类型的(错)2.Python的集合中可以出现重复的元素(错)3.执行下面的代码,运行结果为5(对)a={1,1,2,2,3,3,4,4,5,5}print(len(a))4.Python的集合中的元素排列是有序的(错)5.元组中的数据不可以被添加和删除,但可以被替换(错)6.运行下列代码,结果为56(对)s=0for a in range(10,18):if a%2==1:s=s+aprint(s)7.在列表的切片操作中,如果从第一个数据开始截取,那么起始索引可以被省略(对)8.已知字符串string=“两个黄鹂鸣翠柳”,print(string[1:3])结果为“个黄”(对)9.已知字符串a=“Python”,print(a.lower())结果为”PYTHON”(错)10.根据下面的代码可知,如果明天不下雨,可能做的事情有找小明踢球和在家看电视(错)if明天不下雨:if小明有空:print("找小明踢球")elif小红有空:print("找小红看电影")else:print("在家看电视")四、编程题(共50分)1.报名查询系统(20分)六年级一班组织英语口语大赛,同学们都踊跃报名,目前英语老师已经拿到了一份报名名单,请帮英语老师设计一款程序,输入学生姓名后能够查询该姓名是否已在名单中,如果在的话,输出”已成功报名”,否则就将该学生姓名添加进名单并输出新名单。

python合格考试题目及答案

python合格考试题目及答案

python合格考试题目及答案一、单项选择题(每题2分,共10题)1. Python中用于定义类的关键字是?A. classB. functionC. structD. interface答案:A2. 在Python中,以下哪个是正确的字符串格式化方法?A. "Hello, %s" % "world"B. "Hello, {0}".format("world")C. "Hello, {1}".format("world")D. "Hello, " + "world"答案:B3. Python中的全局变量声明应该使用哪个关键字?A. globalB. localC. staticD. public答案:A4. 下列哪个函数用于计算一个数字的绝对值?A. abs()B. max()C. min()D. sum()答案:A5. 在Python中,以下哪个选项是正确的列表推导式?A. [x for x in range(10)]B. (x for x in range(10))C. [x if x % 2 == 0 for x in range(10)]D. {x for x in range(10)}答案:A6. Python中,以下哪个是正确的字典推导式?A. {x: x2 for x in range(5)}B. {x: x2 for x in range(5)}.items()C. {x: x2 for x in range(5)}.keys()D. {x: x2 for x in range(5)}.values()答案:A7. 在Python中,以下哪个是正确的元组解包操作?A. a, b = (1, 2)B. a = b = (1, 2)C. a, *b = (1, 2, 3)D. a, b = 1, 2答案:A8. Python中,以下哪个函数用于将字符串转换为小写?A. str.lower()B. str.upper()C. str.capitalize()D. str.swapcase()答案:A9. 在Python中,以下哪个选项是正确的列表元素添加方法?A. list.append()B. list.add()C. list.insert()D. list.extend()答案:A10. Python中,以下哪个是正确的异常处理结构?A. try...exceptB. try...finallyC. try...except...finallyD. All of the above答案:D二、多项选择题(每题3分,共5题)1. Python中,以下哪些是合法的标识符?A. _my_variableB. 2variableC. my-variableD. my$variable答案:A2. Python中,以下哪些是正确的文件操作模式?A. 'r'B. 'w'C. 'a'D. 'b'答案:A, B, C3. Python中,以下哪些是合法的切片操作?A. list[1:5]B. list[1:5:2]C. list[5:1:-1]D. list[:]答案:A, B, C, D4. Python中,以下哪些是正确的条件语句?A. if...elif...elseB. if...elseC. if...elifD. switch...case答案:A, B, C5. Python中,以下哪些是正确的循环结构?A. for...inB. while...elseC. for...elseD. do...while答案:A, B, C三、编程题(每题10分,共2题)1. 编写一个Python函数,计算并返回一个整数列表中所有偶数的和。

Python练习题(附答案)

Python练习题(附答案)

Python练习题(附答案)一、单选题(共57题,每题1分,共57分)1.Python中,用于获取用户输入的命令为( )A、inputB、readC、getD、for正确答案:A2.关于Python的分支结构,以下选项中描述错误的是( )A、分支结构使用if保留字B、Python中if-elif-else语句描述多分支结构C、分支结构可以向已经执行过的语句部分跳转D、Python中if-else语句用来形成二分支结构正确答案:C3."给出如下代码TempStr ="Hello World" 可以输出“World”子串的是( )"A、print(TempStr[–5:0])B、print(TempStr[–5:])C、print(TempStr[–5: –1])D、print(TempStr[–4: –1])正确答案:B4.以下不能创建一个字典的语句是 ( )A、dict3 = {[1,2,(2,3)]: “uestc”}B、dict2 = { 3 : 5 }C、dict1 = {}D、dict4 = {(1,2,3): “uestc”}正确答案:A5.关于lambda函数,以下选项中描述错误的是( )A、lambda函数也称为匿名函数B、lambda函数将函数名作为函数结果返回C、定义了一种特殊的函数D、lambda不是Python的保留字正确答案:D6.下列说法中正确的是( )。

A、continue能结束循环,而break只能结束本次循环B、break能结束循环,而continue只能结束本次循环C、break用在for语句中,而continue用在while语句中D、break用在while语句中,而continue用在for语句中正确答案:B7.关于函数的返回值,以下选项中描述错误的是( )A、函数可以有return,也可以没有B、return可以传递0个返回值,也可以传递任意多个返回值C、函数可以返回0个或多个结果D、函数必须有返回值正确答案:D8.下列不合法的Python变量名是( )A、Python2B、Hello_WorldC、N.xD、sum正确答案:C9.下列表达式的值为True的是( )A、not(1==1 and 0!=1)B、1==1 and 2!=1C、3>2>2D、(2**=3)<(2*=3)正确答案:B10.关于Python循环结构,以下选项中描述错误的是( )A、遍历循环中的遍历结构可以是字符串、文件、组合数据类型和range()函数等B、continue用来结束当前当次语句,但不跳出当前的循环体C、Python通过for、while等保留字构建循环结构D、continue结束整个循环过程,不再判断循环的执行条件正确答案:D11.在Python函数中,用于获取用户输入的是( )A、get()B、input()C、Eval()D、print()正确答案:B12.面代码的执行结果是( ) print(pow(3,0.5)*pow(3,0.5)==3)A、pow(3,0.5)*pow(3,0.5)==3B、FalseC、3D、True正确答案:B13.优先级最高的运算符为( )。

Python模拟习题(附答案)

Python模拟习题(附答案)

Python模拟习题(附答案)一、单选题(共57题,每题1分,共57分)1.下列不合法的Python变量名是( )A、Hello$WorldB、N_xC、Python2D、sum正确答案:A2.以下选项不属于 Python 整数类型的是( )A、十进制B、二进制C、十二进制D、八进制正确答案:C3.以下选项中,不是Python IDE的是( ) -A、PyCharm -B、Jupyter Notebook-C、Spyder-D、R studio正确答案:D4.使用( )关键字来创建python自定义函数。

A、functionB、funcC、procedureD、def正确答案:D5.以下语句的运行结果是>>>Python = “ Python”>>> print (“ study” + Python)A、语法错误B、“study“PythonC、study PythonD、studyPython正确答案:C6.下列( )语句在Python中是非法的A、x = (y = z + 1)B、x, y = y, xC、x += yD、x = y = z = 1正确答案:A7.关于列表数据结构,下面描述正确的是( )A、不支持 in 运算符B、必须按顺序插入元素C、可以不按顺序查找元素D、所有元素类型必须相同正确答案:C8.以下选项中,对CSV格式的描述正确的是( )A、CSV文件以英文特殊符号分隔元素B、CSV文件以英文逗号分隔元素C、CSV文件以英文空格分隔元素D、CSV文件以英文分号分隔元素正确答案:B9.关于Python的无限循环,以下选项中描述错误的是( )A、无限循环通过while保留字构建B、无限循环也称为条件循环C、无限循环一直保持循环操作,直到循环条件不满足才结束D、无限循环需要提前确定循环次数正确答案:D10."下面代码实现的功能描述为( )Def fact(n): if n==0: return 1 else:Return n*fact(n-1)Num =eval(input("请输入一个整数:")) print(fact(abs(int(num))))"A、接受用户输入的整数N,输出N的阶乘值B、接受用户输入的整数N,判断N是否是素数并输出结论C、接受用户输入的整数N,判断N是否是水仙花数D、接受用户输入的整数N,判断N是否是完数并输出结论正确答案:A11.下列函数中,用于返回元组中元素最小值的是( )A、minB、maxD、len正确答案:A12.关于函数的关键字参数使用限制,以下选项中描述错误的是( )A、关键字参数必须位于位置参数之前B、关键字参数顺序无限制C、不得重复提供实际参数D、关键字参数必须位于位置参数之后正确答案:A13.给出下面代码: k=10000 while k>1: print(k) k=k/2 上述程序的运行次数是( )A、1000B、14C、13D、15正确答案:B14.字符串是一个字符序列,例如,字符串s,从右侧向左第2个字符用( )索引?A、s[0:-2]B、s[2]C、s[:-2]D、s[-2]正确答案:D15.下列运算符的使用错误的是( )A、-10 % -3B、3 * ‘abc’C、[1, 2, 3] + [4, 5, 6]D、1 + ‘a’正确答案:D16.下面代码的执行结果是( )>>> def area(r, pi = 3.14159): return pi * r * r>>> area(3.14, 4)A、39.4384B、出错C、50.24正确答案:A17."下面代码的输出结果是( ) for a in ‘mirror’: print(a, end="") if a == ‘r’: break"A、miB、mirrorC、mirD、Mirror正确答案:C18.使用( )关键字声明匿名函数A、funcB、functionC、defD、lambda正确答案:D19.下面代码的输出结果是>>> hex(255)A、‘0bff’B、‘0xff’C、'0offD、'0eff正确答案:B20.已知x=2,语句x*=x+1执行后,x的值是( )A、6B、3C、2D、4正确答案:A21.已知x=10,y=20,z=30;以下语句执行后x,y,z的值是()。

Python练习题与参考答案

Python练习题与参考答案

Python练习题与参考答案一、单选题(共57题,每题1分,共57分)1.关于Python字符编码,以下选项中描述错误的是( )A、ord(x)和chr(x)是一对函数B、chr(x)将字符转换为Unicode编码C、Python可以处理任何字符编码文本D、Python默认采用Unicode字符编码正确答案:B2.关于return语句,以下选项中描述正确的是( )A、return只能返回一个值B、函数必须有一个return语句C、函数中最多只有一个return语句D、函数可以没有return语句正确答案:D3.( )函数是指直接或间接调用函数本身的函数A、递归B、闭包C、lambdaD、匿名正确答案:A4.关于 Python 语言的注释,以下选项中描述错误的是( )A、Python 语言有两种注释方式:单行注释和多行注释B、Python 语言的单行注释以#开头C、Python 语言的单行注释以单引号 ' 开头D、Python 语言的多行注释以 ' ' '(三个单引号)开头和结尾正确答案:C5.面代码的输出结果是( )>>> oct(–255)A、‘0d–377’B、‘0o–377’C、‘–0d377’D、‘–0o377’正确答案:D6.下面代码的输出结果是( ) >>>f=lambda x,y:y+x>>>f(10,10)A、10,10B、10C、20D、100正确答案:C7.Python 3.x 版本的保留字总数是( )A、27B、33C、16D、29正确答案:B8.关于Python语言的注释,以下选项中描述错误的是( )A、Python语言的单行注释以单引号 ' 开头B、Python语言有两种注释方式:单行注释和多行注释C、Python语言的多行注释以'''(三个单引号)开头和结尾D、Python语言的单行注释以#开头正确答案:A9.以下代码中calculate( )函数属于哪个分类def calculate(number):Result = 0 i = 1While i <= number: result = result + i i += 1Return resultResult = calculate(100) print('1~100的累积和为:', result)A、无参无返回值函数B、有参有返回值函数C、无参有返回值函数D、有参无返回值函数正确答案:B10.以下选项对应的方法可以辅助用于从CSV文件中解析二维数据的是( )A、count()B、strip()C、format()D、center()正确答案:B11.关于open()函数的文件名,以下选项中描述错误的是( )A、文件名可以是相对路径B、文件名不能是一个目录C、文件名对应的文件可以不存在,打开时不会报错D、文件名可以是绝对路径正确答案:C12.下面( )不是Python合法的标识符A、40XLB、selfC、int32D、name正确答案:A13.下列Python保留字中,不用于表示分支结构的是( )A、elifB、inC、ifD、else正确答案:B14.以下不属于Python语言保留字的是( )A、TrueB、passC、doD、while正确答案:C15.以下关于元组的描述正确的是A、元组中的元素允许被修改B、创建元组tup:tup = ();C、创建元组tup:tup = (50);D、元组中的元素允许被删除正确答案:B16.下列 for 语句中,在 in 后使用不正确的是( ) for var in ( ) : print(var)A、(1)B、range(0, 10, 5)C、[1, 2, 3, 4, 5]D、set(‘str’)正确答案:A17.假设a=9,b=2,那么下列运算中,错误的是()A、a+b的值是11B、a%b的值是1C、a**b的值是18D、a//b的值是4正确答案:C18.关于字符串下列说法错误的是( )A、字符串以\0标志字符串的结束B、在三引号字符串中可以包含换行回车等特殊字符C、既可以用单引号,也可以用双引号创建字符串D、字符应该视为长度为1的字符串正确答案:A19.以下的布尔代数运算错误的是( )A、(False and x) == FalseB、(True or x) == TrueC、not (a and b) == not (a) and not (b)D、(True or False) == True正确答案:C20.下列Python语句正确的是 ( )A、max = x > y ? x : yB、if (x > y) print xC、min = x if x < y: else yD、while True : pass正确答案:D21.( )模块是python标准库中最常用的模块之一。

Python模拟试题与答案

Python模拟试题与答案

Python模拟试题与答案一、单选题(共50题,每题1分,共50分)1、下列关于Python的说法中,错误的是()A、Python是从ABC语言发展起来的B、Python是一门高级的计算机语言C、Python是一门只面向对象的语言D、Python是一种代表简单主义思想的语言正确答案:C2、"下面代码的输出结果是( ) a = [1,2,3] if isinstance(a,float): print("{} is float". format(a)) else: print("{} is not float".format(a))"A、a isB、[1, 2, 3] is not floatC、a is floatD、出错正确答案:B3、下列( )类型数据是不可变化的:A、字典B、元组C、集合D、列表正确答案:B4、关于列表的说法,描述错误的是:A、list是一个有序集合,没有固定大小B、list可以存放Python中任意类型的数据C、使用list时其下标可以是负数D、list是不可变数据类型正确答案:D5、当需要在字符串中使用特殊字符时,Python使用( )作为转义字符的起始符号A、%B、/C、#D、\正确答案:D6、给定字典d,以下选项中对d.get(x, y)的描述正确的是( )A、返回字典d中键为x的值,如果不存在,则返回yB、返回字典d中键值对为x:y的值C、返回字典d中键为y的值,如果不存在,则返回yD、返回字典d中值为y的值,如果不存在,则返回x正确答案:A7、以下关于Python自带数据结构的运算结果中正确的是( )项?A、basket = [‘apple’, ‘banana’, ‘apple’, ‘orange’] ; fruit = set(basket);Fruit2 = set([‘apple’, ‘melo’]); len(fruit | fruit2) 的结果是5。

python笔试题及答案

python笔试题及答案

python笔试题及答案1. 题目:请写出一个Python函数,该函数接收一个整数列表作为参数,并返回列表中所有偶数的和。

```pythondef sum_even_numbers(numbers):return sum(num for num in numbers if num % 2 == 0)```答案:函数`sum_even_numbers`通过列表推导式筛选出列表中的偶数,并使用内置函数`sum`计算这些偶数的总和。

2. 题目:如何使用Python生成斐波那契数列的前10个数字?```pythondef fibonacci(n):a, b = 0, 1result = []for _ in range(n):result.append(a)a, b = b, a + breturn result```答案:函数`fibonacci`初始化两个变量`a`和`b`分别代表斐波那契数列的前两个数字0和1。

然后通过循环,每次迭代将`a`的值添加到结果列表中,并更新`a`和`b`的值为下一个斐波那契数。

3. 题目:给定一个字符串,请编写一个Python函数,该函数返回字符串中每个单词的长度。

```pythondef word_lengths(s):return [len(word) for word in s.split()]```答案:函数`word_lengths`使用字符串的`split`方法将字符串分割成单词列表,然后通过列表推导式计算每个单词的长度。

4. 题目:编写一个Python函数,该函数接收一个整数n,返回一个包含从1到n的所有整数的列表。

```pythondef create_list(n):return list(range(1, n + 1))```答案:函数`create_list`使用`range`函数生成一个从1到n的整数序列,然后将其转换为列表。

5. 题目:请编写一个Python函数,该函数接收一个字符串列表作为参数,并返回一个新列表,其中包含原列表中每个字符串的第一个字符。

python理论考试题目及答案

python理论考试题目及答案

python理论考试题目及答案Python理论考试题目及答案一、选择题1. 在Python中,以下哪个是正确的字符串定义?- A. `str = "Hello"`- B. `str = ('Hello')`- C. `str = 'Hello'`- D. `str = {"Hello"}`答案:C2. Python中的函数定义使用哪个关键字?- A. `function`- B. `procedure`- C. `def`- D. `func`答案:C3. 在Python中,以下哪个操作符用于比较两个值是否不相等? - A. `==`- B. `!=`- C. `=~`- D. `===`答案:B4. 下面哪个是Python中的合法标识符?- A. `class`- B. `my-variable`- C. `3things`- D. `class_name`答案:D5. 在Python中,以下哪个是正确的列表定义?- A. `list = [1, 2, 'a', 'b']`- B. `list = {1, 2, 'a', 'b'}`- C. `list = (1, 2, 'a', 'b')`- D. `list = [1, 2, 3]`答案:D二、简答题1. 简述Python中的列表推导式是什么,以及它的基本用法。

列表推导式是一种简洁的构建列表的方法,它允许你通过一个现有的列表(或其他可迭代对象)来创建一个新列表。

基本用法是通过一个表达式,后跟一个 `for` 循环,再跟一个可选的 `if` 语句。

```pythonsquares = [x2 for x in range(10)]```上述代码将创建一个新列表 `squares`,其中包含数字0到9的平方。

python编程试题及答案

python编程试题及答案

python编程试题及答案1. 单选题:以下哪个是Python的正确语法?A. if x = 5 then print("x is 5")B. if x == 5: print("x is 5")C. if x = 5: print("x is 5")D. if x == 5 then print("x is 5")答案:B2. 多选题:下列哪些是Python中的数据类型?A. 字符串B. 列表C. 元组D. 字典答案:A, B, C, D3. 判断题:Python中的函数可以返回多个值。

正确错误答案:正确4. 填空题:在Python中,使用____关键字可以定义一个函数。

答案:def5. 简答题:请简述Python中列表和元组的区别。

答案:列表是可变的,可以修改其内容;元组是不可变的,一旦创建就不能修改。

6. 编程题:编写一个Python函数,计算并返回一个列表中所有数字的和。

答案:```pythondef sum_of_list(numbers):return sum(numbers)```7. 计算题:给定一个列表[1, 2, 3, 4, 5],请使用Python代码计算其元素的乘积。

答案:```pythonnumbers = [1, 2, 3, 4, 5]product = 1for number in numbers:product *= numberprint(product)```8. 案例分析题:假设你有一个字典,键是字符串,值是整数,编写一个Python程序,打印出所有值大于10的键。

答案:```pythonmy_dict = {'a': 5, 'b': 15, 'c': 20, 'd': 8}for key, value in my_dict.items(): if value > 10:print(key)```。

python考试最新试题及答案

python考试最新试题及答案

python考试最新试题及答案Python考试最新试题及答案一、单项选择题(每题2分,共20分)1. Python中,以下哪个是正确的变量命名?A. 2variableB. variable-nameC. variable$nameD. variable name答案:B2. Python中,以下哪个是合法的列表?A. [1, 2, 3, 'a', 'b']B. (1, 2, 3, 'a', 'b')C. {1, 2, 3, 'a', 'b'}D. [1, 2, 3, 'a', 'b',]答案:A3. Python中,以下哪个是正确的函数定义?A. def my_function()B. def my_function(a, b)C. def my_function(a, b):D. def my_function(a=1, b=2)答案:C4. Python中,以下哪个是正确的字典定义?A. dict = {'key1': 'value1', 'key2': 'value2'}B. dict = [1, 2, 3]C. dict = {1, 2, 3}D. dict = ('key1': 'value1', 'key2': 'value2')答案:A5. Python中,以下哪个是正确的字符串格式化方法?A. "Hello, %s" % "world"B. "Hello, {0}".format("world")C. "Hello, {}".format("world")D. "Hello, %d" % 5答案:B6. Python中,以下哪个是正确的条件语句?A. if x > 0:B. if x > 0C. if x > 0 then:D. if x > 0 then答案:A7. Python中,以下哪个是正确的循环语句?A. for i in range(10)B. for i = 0 to 10C. for i in 0..10D. for i in range(0, 10, 2)答案:D8. Python中,以下哪个是正确的异常处理语句?A. try:passB. try:passexcept:passC. try:passD. try:passexcept Exception as e:pass答案:D9. Python中,以下哪个是正确的类定义?A. class MyClass:B. class MyClass():C. class MyClass() {}D. class MyClass: {}答案:B10. Python中,以下哪个是正确的模块导入方式?A. import moduleB. from module importC. import module as modD. from module import function答案:C二、多项选择题(每题3分,共15分)1. Python中,以下哪些是合法的标识符?A. _variableB. 2variableC. variable-nameD. variable$name答案:A, C2. Python中,以下哪些是合法的列表推导式?A. [x for x in range(10)]B. [x if x % 2 == 0 else x for x in range(10)]C. [x for x in range(10) if x % 2 == 0]D. [x for x in range(10) for y in range(10)]答案:A, C3. Python中,以下哪些是合法的字典推导式?A. {x: x2 for x in range(10)}B. {x: x2 if x % 2 == 0 else x for x in range(10)}C. {x: y for x, y in enumerate(range(10))}D. {x: y for x, y in zip(range(10), range(10))}答案:A, C, D4. Python中,以下哪些是合法的文件操作?A. with open('file.txt', 'r') as f:content = f.read()B. f = open('file.txt', 'w')f.write('Hello, world!')f.close()C. with open('file.txt', 'r') as f:for line in f:print(line)D. f = open('file.txt', 'r')content = f.readlines()f.close()答案:A, C, D5. Python中,以下哪些是合法的装饰器使用?A. @decoratordef function():passB. def decorator(func):def wrapper():print("Before function")func()print("After function")return wrapperC. @decoratordef function():pass@decoratordef another_function():passD. def decorator(func):def wrapper(args, kwargs):print("Before function")func(args, kwargs)print("After function")return wrapper答案:A, B, C, D三、填空题(每题4分,共20分)1. Python中,字符串的______方法可以用来连接字符串。

Python编程能力测试题(基础篇)

Python编程能力测试题(基础篇)

Python编程能力测试题一、单选题(共20题,每题2分)1.Python中input()函数的主要作用是()。

A.输入 B.输出 C.画图 D.运行2.在Python的代码编程环境下运行程序应点击()。

A.File菜单下的New File B.File菜单下的Save C.Run菜单下的Run Module D.Edit菜单下的Copy3.Python中的语句turtle.pensize(8),其功能是()。

A.设置画布的尺寸 B.设置画笔颜色C.设置画笔线条粗细 D.开始绘图4.Python中的语句turtle.circle(200),其功能是()。

A.设置画布的尺寸为200 B.海龟画笔前进200个像素C.画半径为200的圆 D.旋转200度5.运行以下Python程序,最后画出来的图形大致是()。

import turtleturtle.forward(200)turtle.left(90)turtle.forward(200)turtle.left(90)turtle.forward(200)turtle.left(90)turtle.forward(200)A. B. C. D.6.以下关于Python中变量的说法,错误的是()。

A.变量必须要命名。

B.变量第一次赋值为1,第二次赋值为4,那么最后变量的值为4。

C.变量只能用来存储数字,不能存储文字。

D.变量名区分大小写,同一个字母的大写和小写,被视为不同的变量。

7.Python中语句print(2*4)的执行结果是()。

A.0.5 B.6 C.8 D.1/28.下面哪个选项的变量中,装的内容是整数类型?()A.a = 2.5 B.b = int('90') C.c = '1 + 1' D.d = '-10' 9.Python中的“==”代表的是()。

A.把“==”左边的值赋值给右边。

B.把“==”右边的值赋值给左边。

(完整版)Python题库精品练习

(完整版)Python题库精品练习

(完整版)Python题库精品练习一、选择题1.在python中运行print(“3+6”)的结果是()。

A.9B.“3+6”C.3+6D.“9”2.在Python中,设a=2,b=3,表达式 not(b-a>0)值是()A.1 B.-1 C.True D.False3.如下Python程序段x = 0while x < 50:x = (x + 2) * (x + 3)运行后,x的值为()A.0 B.72 C.50 D.1684.下列选项中,可以作为 Python程序变量名的是()A.a/b B.ab C.a+b D.a-b5.下列Python语句中,会导致程序运行出错的语句是()A.x=(y=1) B.x,y=y,x C.x=1;y=1 D.x=y=16.在Python中print(8+7%2**2)的执行结果是()A.5 B.1 C.6 D.117.在Python中,算式5+6*4%(2+8)结果为()A.25B.15C.9D.7.48.下列选项都是属于高级语言的是( )A.汇编语言、机器语言B.汇编语言、Basic语言C.Basic语言、Python语言D.机器语言、Python语言9.变量K表示某天是星期几(k=1,表示星期一),下列python表达式中能表示K的下一天的是()A.K+1 B.K%7+1 C.(K+1)%7 D.(K+1)%7-110.下列Python表达式中,能正确表示不等式方程|x|>1解的是()A.x>1 or x<-1 B.x>-1 or x<1 C.x>1 and x<-1 D.x>-1 and x<1 11.要实现将实数型变量a的值保留三位小数,以下python可以实现的是()A.a%0.001 B.a//0.001 C.round(a,3) D.round(3,a)12.以下Python程序段执行后,输出结果为()。

Python模拟题(含答案)

Python模拟题(含答案)

Python模拟题(含答案)一、单选题(共57题,每题1分,共57分)1.若字符串s=‘ab\tc’,则len(s)的值是A、5B、7C、4D、6正确答案:A2.字符串是一个字符序列,例如,字符串s,从右侧向左第2个字符用()索引?A、s[0:-2]B、s[2]C、s[-2]D、s[:-2]正确答案:C3.foriinrange(0,2):print(i)上述程序的输出结果是()A、012B、12C、1D、01正确答案:D4.给出如下代码S='PythonisOpenSource!'print(s[0:].upper())上述代码的输出结果是()A、PYTHONB、PYTHONISOPENSOURCEC、PythonisOpenSource!D、PYTHONISOPENSOURCE!正确答案:D5.给出如下代码:DictColor={“seashell”:“海贝色”,“gold”:“金色”,“pink”:“粉红色”,“brown”:“棕色”,“purple”:“紫色”,“tomato”:“西红柿色”}以下选项中能输出“海贝色”的是()A、print(DictColor[“seashell”])B、print(DictColor.keys())C、print(DictColor.values())D、print(DictColor[“海贝色”])正确答案:A6.下列说法中哪项是错误的:A、选择语句可以嵌套B、while语句的循环体中可以包括if语句C、循环语句不可以嵌套D、if语句中可以包括循环语句正确答案:C7.以下关于函数参数传递的描述,错误的是:A、函数的实参位置可变,需要形参定义和实参调用时都要给出名称B、定义函数的时候,可选参数必须写在非可选参数的后面C、调用函数时,可变数量参数被当做元组类型传递到函数中D、Python支持可变数量的参数,实参用”*参数名”表示正确答案:D8.关于列表数据结构,下面描述正确的是()A、不支持in运算符B、必须按顺序插入元素C、可以不按顺序查找元素D、所有元素类型必须相同正确答案:C9.下列表达式的值为True的是()A、(3,2)<(‘a’,‘b’)B、‘abc’>‘xyz’C、3>2>2D、'5+4j'>‘2-3j’正确答案:D10.Python语句print(type([1,2,3,4]))的输出结果是()。

Python测试题与答案

Python测试题与答案

Python测试题与答案一、单选题(共50题,每题1分,共50分)1、下面代码的输出结果是( ) a = [] for i in range(2,10): count = 0 for x in range(2,i-1): if i % x == 0: count += 1 if count == 0: a。

append(i) print(a)A、[2 ,4 ,6 ,8]B、[4, 6 ,8 ,9 ,10]C、[3 ,5 ,7 ,9]D、[2, 3, 5, 7]正确答案:D2、关于Python的全局变量和局部变量,以下选项中描述错误的是( )A、使用global保留字声明简单数据类型变量后,该变量作为全局变量使用B、全局变量指在函数之外定义的变量,一般没有缩进,在程序执行全过程有效C、简单数据类型变量无论是否与全局变量重名,仅在函数内部创建和使用,函数退出后变量被释放D、局部变量指在函数内部使用的变量,当函数退出时,变量依然存在,下次函数调用可以继续使用正确答案:D3、以下关于字典描述错误的是A、字典是一种可变容容器,可存储任意类型对象B、键值对中,值必须唯一C、键值对中,键必须是不可变的D、每个键值对都用冒号(:)隔开,每个键值对之间用逗号(,)隔开正确答案:B4、以下关于Python自带数据结构的运算结果中错误的是( )项?A、l = [1, 2, 3, 4]; l.insert(2, -1); 则l为 [1, 2, -1, 4]。

B、l = [1, 2, 3, 4]; l.pop(1); 则l结果为[1, 3, 4]。

C、l = [1, 2, 3, 4]; l.pop(); 则l.index(3) 结果为2。

D、l = [1, 2, 3, 4]; l.rerverse(); 则l[1]为3。

正确答案:A5、下面代码的执行结果是( ) >>> def area(r, pi = 3.14159):Return pi * r *r>>> area(pi = 3.14, r = 4)B、出错C、无输出D、39.4384正确答案:A6、len(“abc”)的长度是3,len(“老师好”)的长度是( )A、3B、6C、1D、9正确答案:A7、下面的语句( )会无限循环下去A、for a in range(10): time.sleep(10)B、while 1<10:Time.sleep(10)C、while True: breakD、a = [3,-1,’,’]For I in a[:]: if not a :Break正确答案:B8、以下关于元组的描述正确的是A、元组中的元素允许被删除B、元组中的元素允许被修改C、创建元组tup:tup = ();D、创建元组tup:tup = (50);正确答案:C9、在Python中,以下关于函数的描述错误的是( )项?A、在Python中,函数的return语句可以以元组tuple的方式返回多个值。

python编程考试题库及答案

python编程考试题库及答案

python编程考试题库及答案Python编程考试题库及答案一、选择题1. 下列哪个是Python的关键字?A. classB. functionC. returnD. myclass答案:A2. 在Python中,以下哪个操作符用于逻辑与?A. &&B. ||C. andD. or答案:C3. Python中的列表推导式是用于创建列表的简洁方式,以下哪个是正确的列表推导式?A. [x for x in range(10)]B. x for x in range(10)C. list(x for x in range(10))D. for x in range(10): x答案:A二、填空题4. 在Python中,使用______可以定义一个函数。

答案:def5. Python中的字符串可以使用______来连接。

答案:+ 或者 format() 方法6. 在Python中,使用______可以创建一个字典。

答案:{} 或者 dict()三、简答题7. 解释Python中的列表和元组的区别。

答案:列表(list)和元组(tuple)在Python中都是用于存储有序的数据集合。

列表是可变的,意味着可以修改列表的内容,如添加、删除或更改元素。

而元组是不可变的,一旦创建就不能更改其内容。

列表用方括号[]定义,元组用圆括号()定义。

8. 请简述Python中的异常处理机制。

答案:Python中的异常处理机制允许程序在遇到错误时,通过try...except语句捕获并处理错误,而不是使程序崩溃。

try块包含可能会引发异常的代码,except块包含处理异常的代码。

此外,还可以使用else块来指定如果没有异常发生时执行的代码,以及finally 块来指定无论是否发生异常都会执行的清理代码。

四、编程题9. 编写一个Python函数,该函数接受一个字符串作为参数,并返回字符串中每个单词的首字母大写形式。

Python练习题库及答案

Python练习题库及答案

Python练习题库及答案一、单选题(共57题,每题1分,共57分)1.用()函数接收用输入的数据A、readline()B、input()C、accept()D、login()正确答案:B2.关于Python遍历循环,以下选项中描述错误的是( )A、遍历循环可以理解为从遍历结构中逐一提取元素,放在循环变量中,对于所提取的每个元素只执行一次语句块B、无限循环无法实现遍历循环的功能C、遍历循环中的遍历结构可以是字符串、文件、组合数据类型和range()函数等D、遍历循环通过for实现正确答案:B3.使用( )关键字来创建python自定义函数。

A、funcB、procedureC、defD、function正确答案:C4.字典对象的______________方法返回字典的“值”列表A、key()B、keys()C、values()D、items()正确答案:C5.下列哪项不是Python中对文件的读取操作( )A、readlinesB、readlineC、readallD、read正确答案:C6.以下选项中,不是Python对文件的读操作方法的是( )A、readB、readlinesC、readtextD、readline正确答案:C7."下面代码的输出结果是( ) for i in range(1,6): if i%3 == 0: break else: print(i,end =",")"A、1,2,3,4,5,B、1,2,C、1,2,3,D、1,2,3,4,5,6正确答案:B8.给出如下代码: MonthandFlower={“1月”:“梅花”,“2月”:“杏花”,“3月”:“桃花”, “4月”:“牡丹花”,\ 5月:“石榴花”,“6月”:“莲花”,“7月”:“玉簪花”,“8月”:“桂花”,“9月”:“菊花”,10月:“芙蓉花”,“11月”:“山茶花”,“12月”:“水仙花”} n = input(“请输入1-12的月份:”) print(n + “月份之代表花:” + MonthandFlower.get(str(n)+ “月”)) 以下选项中描述正确的是( )A、*MonthandFlower是一个列表B、*MonthandFlower是一个集合C、代码实现了从键盘上获取一个整数(1-12)来表示月份,输出该月份对应的代表花名D、*MonthandFlower是一个元组正确答案:C9.在print函数的输出字符串中可以将( )作为参数,代表后面指定要输出的字符串A、%cB、%dC、%sD、%t正确答案:C10.关于文件的打开方式,以下选项中描述正确的是( )A、文件只能选择二进制或文本方式打开B、所有文件都可能以文本方式打开C、文本文件只能以文本方式打开D、所有文件都可能以二进制方式打开正确答案:D11.关于Python语言的特点,以下选项中描述错误的是( )A、Python语言是多模型语言B、Python语言是非开源语言C、Python语言是脚本语言D、Python语言是跨平台语言正确答案:B12.面代码的输出结果是( )>>> oct(–255)A、‘–0o377’B、‘–0d377’C、‘0o–377’D、‘0d–377’正确答案:A13.下列表达式的值为True的是A、2!=5 or 0B、1 or TrueC、1 and 5==0D、3>2>2正确答案:A14.关于函数的参数,以下选项中描述错误的是( )A、可选参数可以定义在非可选参数的前面B、一个元组可以传递给带有星号的可变参数C、在定义函数时,可以设计可变数量参数,通过在参数前增加星号(*)实现D、在定义函数时,如果有些参数存在默认值,可以在定义函数时直接为这些参数指定默认值正确答案:A15.下面代码的输出结果是( ) str1=“k:1|k1:2|k2:3|k3:4” str_list=str1.split(’|’) d= {} for l in str_list: key,value=l.split(’:’) d[key]=value print(d)A、[‘k’:‘1’, ‘k1’:‘2’, ‘k2’:‘3’,‘k3’:‘4’]B、{k:1,k1:2,k2:3,k3:4}C、{‘k’: ‘1’, ‘k1’: ‘2’, ‘k2’: ‘3’, ‘k3’: ‘4’}D、[k:1,k1:2,k2:3,k3:4]正确答案:C16.下列运算符的使用错误的是( )A、1 + ‘a’B、3 * ‘abc’C、-10 % -3D、[1, 2, 3] + [4, 5, 6]正确答案:A17.写出下面代码的运行结果。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.what does the following code do?(B)def a(b, c, d): passA.defines a list and initializes itB.defines a function, which does nothingC.defines a function, which passes its parameters throughD.defines an empty class2.what gets printed? Assuming python version 2.x(A) print type(1/2)A.<type 'int'>B.<type 'number'>C.<type 'float'>D.<type 'double'>E.<type 'tuple'>3. what is the output of the following code?(E)print type([1,2])A.<type 'tuple'>B.<type 'int'>C.<type 'set'>D.<type 'complex'>E.<type 'list'>4. what gets printed?(C)def f(): passprint type(f())A.<type 'function'>B.<type 'tuple'>C.<type 'NoneType'>D.<type 'str'>E.<type 'type'>5. what should the below code print?(A)print type(1J)A.<type 'complex'>B.<type 'unicode'>C.<type 'int'>D.<type 'float'>E.<type 'dict'>6. what is the output of the following code?(D)print type(lambda:None)A.<type 'NoneType'>B.<type 'tuple'>C.<type 'type'>D.<type 'function'>E.<type 'bool'>7. what is the output of the below program?(D)a = [1,2,3,None,(),[],]print len(a)A.syntax errorB.4C.5D.6E.78.what gets printed? Assuming python version 3.x(C) print (type(1/2))A.<type 'int'>B.<type 'number'>C.<type 'float'>D.<type 'double'>E.<type 'tuple'>9. What gets printed?(C)d = lambda p: p * 2t = lambda p: p * 3x = 2x = d(x)x = t(x)x = d(x)print xA.7B.12C.24D.36E.4810. What gets printed?(A) x = 4.5y = 2print x//yA.2.0B.2.25C.9.0D.20.25E.2111. What gets printed?(C) nums = set([1,1,2,3,3,3,4]) print len(nums)A.1B.2C.4D.5E.712. What gets printed?(A) x = Truey = Falsez = Falseif x or y and z:print "yes"else:print "no"A.yesB.noC.fails to compile13. What gets printed?(C) x = Truey = Falsez = Falseif not x or y:print 1elif not x or not y and z:print 2elif not x or y or not y and x:print 3else:print 4A.1B.2C.3D.414. If PYTHONPATH is set in the environment, which directories are searched for modules?(D)A) PYTHONPATH directoryB) current directoryC) home directoryD) installation dependent default pathA.A onlyB.A and DC.A, B, and CD.A, B, and DE.A, B, C, and D15. In python 2.6 or earlier, the code will print error type 1 if accessSecureSystem raises an exception of either AccessError type or SecurityError type(B)try:accessSecureSystem()except AccessError, SecurityError:print "error type 1"continueWork()A.trueB.false16. The following code will successfully print the days and then the months(B) daysOfWeek = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']months = ['Jan', \'Feb', \'Mar', \'Apr', \'May', \'Jun', \'Jul', \'Aug', \'Sep', \'Oct', \'Nov', \'Dec']print "DAYS: %s, MONTHS %s" %(daysOfWeek, months)A.trueB.false17. Assuming python 2.6 what gets printed?(A)f = Nonefor i in range (5):with open("data.txt", "w") as f:if i > 2:breakprint f.closedA.TrueB.FalseC.None18. What gets printed?(C)counter = 1def doLotsOfStuff():global counterfor i in (1, 2, 3):counter += 1doLotsOfStuff()print counterA.1B.3C.4D.7E.none of the above19. What gets printed?(C)print r"\nwoow"A.new line then the string: woowB.the text exactly like this: r"\nwoow"C.the text like exactly like this: \nwoowD.the letter r and then newline then the text: woowE.the letter r then the text like this: nwoow20.What gets printed?(B)print "hello" 'world'A.on one line the text: hello worldB.on one line the text: helloworldC.hello on one line and world on the next lineD.syntax error, this python program will not run21.What gets printed?(E)print "\x48\x49!"A.\x48\x49!B.4849C.4849!D. 48 49!E.HI!22. What gets printed?(D)print 0xA + 0xaA.0xA + 0xaB.0xA 0xaC.14D.20E.0x2023. What gets printed?(E)class parent:def __init__(self, param):self.v1 = paramclass child(parent):def __init__(self, param):self.v2 = paramobj = child(11)print "%d %d" % (obj.v1, obj.v2)A.None NoneB.None 11C.11 NoneD.11 11E.Error is generated by program24. What gets printed?(E)kvps = {"user","bill", "password","hillary"} print kvps['password']erB.billC.passwordD.hillaryE.Nothing. Python syntax error25. What gets printed?(B)66% on 1871 times askedclass Account:def __init__(self, id):self.id = idid = 666acc = Account(123)print acc.idA.NoneB.123C.666D.SyntaxError, this program will not run26. What gets printed?(C)name = "snow storm"print "%s" % name[6:8]A.stB.stoC.toD.torE.Syntax Error27. What gets printed?(D)name = "snow storm"name[5] = 'X'print nameA.snow stormB.snowXstormC.snow XtormD.ERROR, this code will not run28. Which numbers are printed?(C)for i in range(2):print ifor i in range(4,6):print iA.2, 4, 6B.0, 1, 2, 4, 5, 6C.0, 1, 4, 5D.0, 1, 4, 5, 6, 7, 8, 9E.1, 2, 4, 5, 629. What sequence of numbers is printed?(B) values = [1, 2, 1, 3]nums = set(values)def checkit(num):if num in nums:return Trueelse:return Falsefor i in filter(checkit, values):print iA.1 2 3B.1 2 1 3C.1 2 1 3 1 2 1 3D.1 1 1 1 2 2 3 3E.Syntax Error30. What sequence of numbers is printed?(E) values = [2, 3, 2, 4]def my_transformation(num):return num ** 2for i in map(my_transformation, values): print iA.2 3 2 4B.4 6 4 8C.1 1.5 1 2D.1 1 1 2E.4 9 4 1631. What numbers get printed(C)import pickleclass account:def __init__(self, id, balance):self.id = idself.balance = balancedef deposit(self, amount):self.balance += amountdef withdraw(self, amount):self.balance -= amountmyac = account('123', 100)myac.deposit(800)myac.withdraw(500)fd = open( "archive", "w" )pickle.dump( myac, fd)fd.close()myac.deposit(200)print myac.balancefd = open( "archive", "r" )myac = pickle.load( fd )fd.close()print myac.balanceA.500 300B.500 500C.600 400D.600 600E.300 50032. What gets printed by the code snippet below?(B) import mathprint math.floor(5.5)A.5B.5.0C.5.5D.6E.6.033. What gets printed by the code below?(E) class Person:def __init__(self, id):self.id = idobama = Person(100)obama.__dict__['age'] = 49print obama.age + len(obama.__dict__)A.1B.2C.49D.50E.5134. What gets printed?(E)x = "foo "y = 2print x + yA.fooB.foo fooC.foo 2D.2E.An exception is thrown35. What gets printed?(E)def simpleFunction():"This is a cool simple function that returns 1"return 1print simpleFunction.__doc__[10:14]A.simpleFunctionB.simpleC.funcD.funtionE.cool36. What does the code below do?(C)sys.path.append('/root/mods')A.Changes the location that the python executable is run fromB.Changes the current working directoryC.Adds a new directory to seach for python modules that are importedD.Removes all directories for modsE.Changes the location where sub-processes are searched for after they are launched37. What gets printed?(C)import resum = 0pattern = 'back'if re.match(pattern, 'backup.txt'):sum += 1if re.match(pattern, 'text.back'):sum += 2if re.search(pattern, 'backup.txt'):sum += 4if re.search(pattern, 'text.back'):sum += 8print sumA.3B.7C.13D.14E.1538. Which of the following print statements will print all the names in the list on a seperate line(A)names = ['Ramesh', 'Rajesh', 'Roger', 'Ivan', 'Nico']A.print "\n".join(names)B.print names.join("\n")C.print names.concatenate("\n")D.print names.append("\n")E.print names.join("%s\n", names)39. True or false? Code indentation must be 4 spaces when creating a code block?(B) if error:# four spaces of indent are used to create the blockprint "%s" % msgA.TrueB.False40. Assuming the filename for the code below is /usr/lib/python/person.pyand the program is run as:python /usr/lib/python/person.pyWhat gets printed?(D)class Person:def __init__(self):passdef getAge(self):print __name__p = Person()p.getAge()A.PersonB.getAger.lib.python.personD.__main__E.An exception is thrown41. What gets printed(B)foo = {}print type(foo)A.setB.dictD.tupleE.object42. What gets printed?(C)foo = (3, 4, 5)print type(foo)A.intB.listC.tupleD.dictE.set43. What gets printed?(D)country_counter = {}def addone(country):if country in country_counter:country_counter[country] += 1 else:country_counter[country] = 1 addone('China')addone('Japan')addone('china')print len(country_counter)A.0B.1C.2D.3E.444. What gets printed?(D)confusion = {}confusion[1] = 1confusion['1'] = 2confusion[1] += 1for k in confusion:sum += confusion[k]print sumA.1B.2C.3D.4E.545. What gets printed?(C)confusion = {} confusion[1] = 1 confusion['1'] = 2 confusion[1.0] = 4sum = 0for k in confusion:sum += confusion[k]print sumA.2B.4C.6D.7E.An exception is thrown46.What gets printed?(E)boxes = {}jars = {}crates = {}boxes['cereal'] = 1 boxes['candy'] = 2jars['honey'] = 4crates['boxes'] = boxes crates['jars'] = jarsprint len(crates[boxes])B.2C.4D.7E.An exception is thrown47. What gets printed?(E)numberGames = {} numberGames[(1,2,4)] = 8 numberGames[(4,2,1)] = 10 numberGames[(1,2)] = 12sum = 0for k in numberGames:sum += numberGames[k]print len(numberGames) + sumA.8B.12C.24D.30E.3348. What gets printed?(A)foo = {1:'1', 2:'2', 3:'3'}foo = {}print len(foo)A.0B.1C.2D.3E.An exception is thrown49. What gets printed?(B)foo = {1:'1', 2:'2', 3:'3'}del foo[1]foo[1] = '10'del foo[2]print len(foo)B.2C.3D.4E.An exception is thrown50. What gets printed?(E)names = ['Amir', 'Barry', 'Chales', 'Dao'] print names[-1][-1]A.AB.rC.AmirD.DaoE.o51. What gets printed?(B)names1 = ['Amir', 'Barry', 'Chales', 'Dao'] names2 = names1names3 = names1[:]names2[0] = 'Alice'names3[1] = 'Bob'sum = 0for ls in (names1, names2, names3): if ls[0] == 'Alice':sum += 1if ls[1] == 'Bob':sum += 10print sumA.11B.12C.21D.22E.3352. What gets printed?(E)names1 = ['Amir', 'Barry', 'Chales', 'Dao']loc = names1.index("Edward")print locA.-1B.0C.4D.EdwardE.An exception is thrown53. What gets printed?(B)names1 = ['Amir', 'Barry', 'Chales', 'Dao']if 'amir' in names1:print 1else:print 2A.1B.2C.An exception is thrown54. What gets printed?(C)names1 = ['Amir', 'Barry', 'Chales', 'Dao'] names2 = [name.lower() for name in names1] print names2[2][0]A.iB.aC.cD.CE.An exception is thrown55. What gets printed?(B)numbers = [1, 2, 3, 4]numbers.append([5,6,7,8])print len(numbers)A.4B.5C.8D.12E.An exception is thrown56. Which of the following data structures can be used with the "in" operator to check if an item is in the data structure?(E)A.listB.setC.dictionaryD.None of the aboveE.All of the above57. Wat gets printed?(D)list1 = [1, 2, 3, 4]list2 = [5, 6, 7, 8]print len(list1 + list2)A.2B.4C.5D.8E.An exception is thrown58. What gets printed?(C)def addItem(listParam):listParam += [1]mylist = [1, 2, 3, 4]addItem(mylist)print len(mylist)A.1B.4C.5D.8E.An exception is thrown59. What gets printed?(E)my_tuple = (1, 2, 3, 4)my_tuple.append( (5, 6, 7) )print len(my_tuple)A.1B.2C.5D.7E.An exception is thrown60. What gets printed?(B)a = 1b = 2a,b = b,aprint "%d %d" % (a,b)A.1 2B.2 1C.An exception is thrownD.This program has undefined behavior61. What gets printed?(A)def print_header(str):print "+++%s+++" % strprint_header.category = 1print_header.text = "some info"print_header("%d %s" % \(print_header.category, print_header.text))A.+++1 some info+++B.+++%s+++C.1D.some info62. What gets printed?(C)def dostuff(param1, *param2):print type(param2)dostuff('apples', 'bananas', 'cherry', 'dates')A.strB.intC.tupleD.listE.dict63. What gets printed?(E)def dostuff(param1, **param2):print type(param2)dostuff('capitals', Arizona='Phoenix',California='Sacramento', Texas='Austin')A.inB.strC.tupleD.listE.dict64. What gets printed?(B)def myfunc(x, y, z, a):print x + ynums = [1, 2, 3, 4]myfunc(*nums)A.1B.3C.6D.10E.An exception is thrown65. How do you create a package so that the following reference will work?(C) p = mytools.myparser.MyParser()A.Declare the myparser package in mytools.pyB.Create an __init__.py in the home dirC.Inside the mytools dir create a __init__.pyD.Create a myparser.py directory inside the mytools directoryE.This can not be done66. What gets printed?(E)class A:def __init__(self, a, b, c):self.x = a + b + ca = A(1,2,3)b = getattr(a, 'x')setattr(a, 'x', b+1)print a.xA.1B.2C.3D.6E.767. What gets printed?(E)class NumFactory:def __init__(self, n):self.val = ndef timesTwo(self):self.val *= 2def plusTwo(self):self.val += 2f = NumFactory(2)for m in dir(f):mthd = getattr(f,m)if callable(mthd):mthd()print f.valA.2B.4C.6E.An exception is thrown68. What gets printed?(A)one = chr(104)two = chr(105)print "%s%s" % (one, two)A.hiB.hC.Inside the mytools dir create a __init__.py and myparser.pyD.104105E.10469. What gets printed?(A)x = 0y = 1a = cmp(x,y)if a < x:print "a"elif a == x:print "b"else:print "c"A.aB.bC.c70. What gets printed?(C)x = 1y = "2"z = 3sum = 0for i in (x,y,z):if isinstance(i, int):sum += iprint sumB.3C.4D.6E.An exception is thrown71. What gets printed (with python version 2.X) assuming the user enters the following at the prompt?(D)#: fooa = input("#: ")print aA.fB.fooC.#: fooD.An exception is thrown72. What gets printed?(C)x = sum(range(5))print xA.4B.5C.10D.15E.An exception is thrown73. If the user types '0' at the prompt what gets printed?(B)def getinput():print "0: start"print "1: stop"print "2: reset"x = raw_input("selection: ")try:num = int(x)if num > 2 or num < 0:return Nonereturn numexcept:return Nonenum = getinput()if not num:print "invalid"else:print "valid"A.validB.invalidC.An exception is thrown74. What gets printed?(D) kvps = { '1' : 1, '2' : 2 } theCopy = kvpskvps['1'] = 5sum = kvps['1'] + theCopy['1'] print sumA.1B.2C.7D.10E.An exception is thrown75. What gets printed?(C) kvps = { '1' : 1, '2' : 2 } theCopy = kvps.copy()kvps['1'] = 5sum = kvps['1'] + theCopy['1'] print sumA.1B.2C.6D.10E.An exception is thrown76. What gets printed(D)aList = [1,2]bList = [3,4]kvps = { '1' : aList, '2' : bList } theCopy = kvps.copy()kvps['1'][0] = 5sum = kvps['1'][0] + theCopy['1'][0] print sumA.1B.2C.6D.10E.An exception is thrown77. What gets printed?(C)import copyaList = [1,2]bList = [3,4]kvps = { '1' : aList, '2' : bList } theCopy = copy.deepcopy(kvps) kvps['1'][0] = 5sum = kvps['1'][0] + theCopy['1'][0] print sumA.1B.2C.6D.10E.An exception is thrown78. What gets printed?(C)kvps = { '1' : 1, '2' : 2 }theCopy = dict(kvps)kvps['1'] = 5sum = kvps['1'] + theCopy['1']print sumA.1B.2C.6D.10E.An exception is thrown79. What gets printed?(B)kvps = { '1' : 1, '2' : 2 , '3' : 3, '4' : 4, '5' : 5}newData = { '1' : 10, '3' : 30 }kvps.update(newData)x = sum(kvps.values())print xA.15B.51C.150D.An exception is thrown80. What gets printed (with python version 3.X) assuming the user enters the following at the prompt?(B)#: fooa = input("#: ")print aA.fB.fooC.Not a numberD.An exception is thrown。

相关文档
最新文档