Professional Documents
Culture Documents
PIAIC Python Sample Test Questions
PIAIC Python Sample Test Questions
print(type(my_var))
p rint("Wr apping up the program!")
You need to add a line so that <class 'i nt'> will be in the output window.
0 my_var = 2+5j
0 my_var = 47.0
0 my_var = '45'
I• my_var = 2.. 2
I
r;;-i
Q Next) [ 1 I of120 I1 ¥1;■
You have the following python files:
c:\temp\myM odule\add .p y
c:\temp\myM odule2\add .py
c:\tmp\myM odule\add .py
c:\temp\add .p y
You have a terminal open and are in the "c: \temp" d irectory.
W hich python file will pydoc att empt to create docume ntat ion for7
0 c:\temp\myModule2\add.py
0 c:\tmp\myModule\add.py
0 c:\temp\add.py
JA c:\temp\myModule\add.py I
r;;i
( Previous O Next ) 1 2 1 0F1201 111
Reset
I
I Which expression evaluates to 3?
I
Choose the correct answer
IO 7%2*3
I
0 7-2*3
0 712 + 3
0 7 /12 * 3
( Previous Q Next)
L!!J
test= "TEST"
( Previous Q Next)
L!!J
Vc:::,, u h ial"Y e-f" lao-.r 1ns c e1 :
-.- w-
C h a <:as .ae•t h e ca -
F = o p e n ( "ll'VI..IUrlb e rs . t::x:t")
<.rye
p a s.:s
e.xc.ep ·t E.xw::::.ep ·tiic:::i,;n :
p a s.:s
p ri n t(f _)
F . c:Sc:::a:s e O
myc
F = o p e n ( "ll'VI..IUrlb e rs . t::x:t")
e .xc.ep ·t E rro r.
p rin·t>C'lr il e d ,c,,,e-s nC:X e-.x iS-c":. --:1
e l s-=::
p ri n t(f _)
t ry=
"'= op
F . c:5ca:s eO
e n ( " r,-1.a .,... be rs . t:x:t"'"}
e xp t F iloe N a tF a u n d E rra r
p ri n tr i= il e cl TI s, --:_)
e l s,.e:
p ri n t(f .-.e..ai,c10 )
-. n.a l ly"=
"'.c: k:rs-E!O
myc
F = o p e n ( "il"IIII..IIIrb e rs . t::x:t")
e .xc.ep ·t on:
p rin-t>CE= i Ie d ,c,,,e-s n C:X e-.x is.a. J
·
e ls-e :
p ri n t(f _)
F . c5cas: e O
Reset
0
s t r 1 = "'C o n t r a ry to p o p u la r b e lie f, Lorem lpsum is not\
simplly r a n d o m text_"
0
str2 = "T h e r e a r e m ,a n y v a r 1i a 1:Jion s o f passages of '\
''L o re m lp s u m a v a ila b le _'
0 w i ll be d is tra c te d by tih e r e a d a b l e c o n t
e n t of a page when looking at its la y o u t . '
j
( P re v io us O Next >
l I
You h a v e the f o l lo w in g c o d e :
C h o,o , thecorrectanswer
se
0 2
0 5
0 3
I• 1
I
( P re v io us O Nlext)
Reset
text = mySt r • 2
r;;-i
( Previous 'ti Next ) [ 1 0 1 0F1 20 1 Af.J:i
You have the following code:
y= 2
X=0
while x * y < 8:
if X == 3:
pass
while x < 2:
X=X+ 1
X += 1
0 5
0 6
I• 4
I
0 7
Reset
importmath
def area_circle(r):
return
L!!J
( Previous Q Next ) [ 12 I of 120 I MM
r;;i ( Previous O Next ) [ 13 I of 120 1-
Reset
You need to complete the function f(x) so it implements the following form ula:
y = x" 2 + 3.
W hich co de should you use? To answer, select the appropriate option from the drop-down menu. Note: x" 2 represents x to the 2nd power .
import math
def f(x):
y=
retur
math.factorial(x) + 3
math.floor(x) + 3
math.gcd(x,2) + 3
math.pow(x,2) + 3
r;;-i
( Previous Q Next ) [ 1 4 1 o f 12 0 1 iiti
Yo u n eed to write some contents to a fi le name d fi le_n ame th at w ill b e in the fo llow ing format:
Hel lo W o rl dl
0 •
f.write('He llo World!')
f.close()
f = open(' file_name','w')
r;;-i
( Previous <CJ N ex t )
p .h o n - rn ,v d o c · E x .a m p, l e2
0
c:.5.lc._dis'ta n ce{ .-a.1E. mme, :
p a i n tcaa_ d i s't3 n c:e ( x 1• .><2,y 1 . y2)
pr bleminExample:2 - lnd en tati anE rrar e.x pected an i....clented b l a o,c { E..xarnp e2. . py . lir..e 17)
-=
0
la te :aa h e , frD rn rate, an d •i rne .
F u.-,::i<a; h e ,:fis·t.aa.r..c.e 11::E:'b,,-'='=<n t--o.ra p,cu; 'Is .
Yo u e:xecUI.e t he f o l lo w in g cod e:
st ud enL in f o -= [6 7 ,8 9 ,9 5 ,8 0 ,8 3]
g i-ad e = s u mi(s t ud e nL in f o )ll l e n ( st ud e nL i n fo )
i nd e x = -1
system = {1:·:A:·, 2 : "B '", 3 : ..C .., 4 :'"D "', 5 F' '}
W h at g r a d e w i ll be p i-i n t ed o n th e s c r ee n ?
01 B
I
0 ,c
0 A
A D
*
Y o u h a v e t h e f o l lo w i n g d ec la r a t io n s:
Python
W hich code se gm e nt sh o u ld yo u u se ?
-
0
text = m y_ strin g A[7] + m y_ stringB [ -3 ) + m y_ stri n gB [- 5) + my _ sl rin gC[ 6 ] -1- m y _st rin g B [
11 J + m y _s t r i ng C[ - 2 ]
p rin t ( t e x t )
0
-1-
m y_s t ri ng C[ - 1 ]
p ri nt (t e x t )
0 my _st ri ng C[ -2
] p ri nt (t e xt)
( Pre v iou s <Ci N e xt ) I is I o f 1 2 0 I 14:MM
Reset
l
Yo u h ave the fo l low in g c od e:
X =2
y =6
X += 2 u 3
x -= y // 2 // 3 p r i nt (x )
I• 9
I
0
7
0
10
0
0
I i+@+i
r;;-i
( P revious 'O N ex t ) [ 19I of 120
I U U l l 't' 'C U t. U YV l l \. t' Ll l it::: I U IIU VY ll l ;i:j 1 1 '.:> l LU a .._,JV 111 1:: .
• import csv
with open("students.cs v" . "w", ne wline = "") as f:
writer = cs v.writer(f)
writer _wrtt ero w s(s tuden ts)
impo rt csv
impo rt csv
impo rt csv
0 with open ("'s l u den ts.cs v", " wb" , ne wl ine= "") as f:
w riter = cs v.writer(f)
writer.wrilero ws(stu dents)
0 var= s[:3]
0 var= s2(9:11]
0 var = s2(1:3]
J•
var= s[3] + s[4]
m y_sentence = words[-7:]
m y_sentence = words[-7:] + words [:8]
my _sentence = m y_sentence + words[4:7]
100
slowly.
slowly.The cow cow
0 slowly.slowly.The cow
W hic h line of co de should yo u use? To answer , selec t the appro p r ia te optio n from the drop-down m enu.
def my_list( V ):
L append(
return L a, L=0
a, L=['1
my_list("egg, L= None
my_list("milk·...,..._
pirt(m y_list("steak"))
I2 3 I o f 1 2?] 1+111
r;;-i ( Previous Q Next )
re su lt = -
f or I I n 1'3- n _ . s i,:
J=0
..,...h l l e J ..: 10 :
If" 1= ,3: = = 0 :
u t! - = - @) -
se:
.-e-s.u .: - = .#. .
j +<= -,
re s ul: t - =, ·
.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'-
0 ....................
; I; I; I; I; I; I; I; I; I; !:
@ @@@@@
0 @
XNNNNNNNNX
@@@@@
0
@@
@
@@
@
@-@,@@@@@@@-@
I! II J! I!}!M }! I! I!M
:: }}:: :: !!# }! :: /.'#
Reset
0
<cl as s ' st r ' >
my_type
<cl =astype (my_items
s ' compl ex ' > )
< cl as s ' boo l ' >
/
my_type = type(my_ite ms[1))
', A
-
r-;;i • -
( Previous <(}
'
Next )
I
[ 2s I o f 1 20 ] -
0 calc(1O,3)
I• calc (1)
I
0 calc(7)
0 calc()
r-;;i
( Previous <:J Next ) 1 26 [ of 1 20 1 ■:ilfi
r
0
def calc_area_triang1l e(I, b, h)
return I • b • H / 2
def calc_area_triangle(I, b, h)
0
area= b • H / 2
return area
• def calc_area_triangle(I, b, h)
return b • h / 2
0 *
def calc_area_triangle(b, h):
return b • H / 2
s
Reset
0 os.getcwd()
print(os.listdir())
• os.chdir(r'c:\lmp')
print(os.listdir())
0 os.mkdir(r'c:\tmp')
print( os.listdir())
0 os.chdir('c:\lmp')
print(os.listdir())
You have the fo llowing code (line numbers adde d fo r clarity):
0 10
Reset
I• class
MyException(Exception):
pass I
0 class MyException(SystemError:)
pass
0 class MyException():
pass
0 class MyException():Exception
pass
r-;;i
( Previous t1 Next ) : 31 of 120 1
1 111
import sys
You need to add co de to print the location on the c omputer where the Python interpreter is installed.
0 print(sys.platform)
0 print(sys.version)
0 print(sys.path)
I• print(sys.executable)
I
r-;;i ( Previous <(} Next ) I 32 I of 120 I 1¥1·€1
You have the following code:
X= 3
X +: 1
;;a d d code here
le X **= 2
I
0 X *= 2
0 X -= 2
0 X += 2
r-;;-i
-
( Previous O Next ) I 33 I of 120 I ■:fifi
You have the following code:
try:
X= 1/0
except Exception as e:
pr int{e)
You need to ad d code to raise the error while prese rv ing th e stac k trace .
0 raise ValueError(e)
I• raise
I
0 raise ValueError('Error Division by zero.')
0 raise as e
0 print(print_dir())
0 print(print_dir('Users','PythonCoder','Practice','Exercises')}
r-;;i
( Previous Q Next )
1 35 of 120 I -
1
Reset
W hic h ope rato r shou ld you use? To ans wer, select th e ap propriate option from the drop -do wn menu.
X= 10
y=6
print(z)
%
L!!J
( Previous Q Next ) I 36 I of 120 I i=f.it•I
You have the following code
0 11.51
IO 11.141
I
0 15
0 15.2
L!!J
( Previous Q Next ) I 37 I of 120 I i=i-141
You have the following expression:
6 // 4 % 5 + 2 ** 3 - 2.
0 -1
0 25
You need to co mplete the code segment so that it will open a file named Purc h as eOrders.txt and write new
records and read all of the file co ntents into the variable name d data .
W hic h line of code sho uld you use? To ans wer, select the appropriate option from the drop-do wn menu.
lt.seek(O) vj
data= fread()
L!!J
( Previous Q Next ) I 39 I of 120 I 14111
You have the fo llowing code (line numbe rs adde d fo r clarity):
0 7
I• 3
I
0 5
0 9
You need to add co de so th at when you run the program and enter the value 1, the printed value will be 1 :1.
W hich l ine of co de sh ould you use? To ans wer, se lect the ap pr opr iate option from the drop- down menu.
item= O
count = 1 [8] : i t me :
0
co unt : 1
while True:
w hi l e True :
em :: int(inpu t("Enter a value between 1 and 1O: "))
i t me : i nt ( i npu t ("En t er a va l ue beh een 1 and
lif 1 <= item <= 10: vj 10: " ) ) i f ( l <: i t me <: 1 0) :
break
break
el se:
else:
count +:
count += 1
1 val ue: i tme
pri nt ( coun tJ " , "+st r(va l ue ))
value :: item
print(count ,":" + str(value)) E nt er a val ue betwee n 1 a nd 1 0: 1
1 .1
Which line of code sh ould you use? To ans wer, se lect the appropriate option from the drop-down
menu.
max_size= 2000
f=open('test.bet','w')
f.write(1O'" Python')
!if f.tell() > max_size: vj
else:
f.write(1Ojj'Python')
f.close()
import random
For each of the following state ments, select Yes if the statement would print a r an dom value from my_l i st.
Otherwise, select No.
Statement Yes No
prinl(random sample(my_lisl,1)) D
print(random.choice(m_ylist)) D
print(random.choices{my_list)[0]) D
print(random.sample(my_ljst,2)[0]) D
r-;;i
( Previous O Next ) [ 43 I of 120 l 111
Reset
In the code shown in the answer area, an operator is needed so that the value of resu lt i s True.
W hich operato r should you use? To ans wer, se lect the appropriate opt ion from the drop-down menu.
a= 'Test'
b = 'Test'
result= a V b
=
<
w' h il e t > = O :
0 r e s u lt _ 4! = t 0
t -= 5
pr i nt {r e.s ullt_ 4 )
wi h i l e x = 6:
0 res u lt _ 1 ·+= x + 2.
X+ =1
p nin t (r es u l t_·1 )
p nin t (r es u l t_3 )
• w h i l e y -=== 1 O:
if y % 3 == O:
re.su lt _ 2 - = 1
e ls e :
r e s l.llt _ 2 + = 3
y-=2
p ri n t {r e s u lt_ 2 )
'fou need to add code to save the output of the print state ment s to the output.txt file.
W hich code should you insert? To ans wer, s, elect the appropriate option fr,om the drop-downi menu.
import sys,
fh = openf'outpuUxt",''w')
o rd er _t o t.al = 150
st at e = '"N e w Yo r k "
sh ip p in g_c o sil. = 0
W h at w il l b e t h e v a lu e o f s.h i p p i n g_ c o s t ?
0 13
0 5
1• 12
W hic h lin e of code shou l d you use? To ans wer, selec t the app ro priate optio n from the drop-do wn menu.
my_list = [4 ,7,[2,4,6,8]]
c=O
for i in range(3):
if type(my_list[ij) == list
or J in my list[i] vj
if j% 2 == O:
c+=1
else:
C-=1
L!!J
( Previous '(:i Next ) 1 4s I of 120 l
1441
Yo u a r e w o r k i n g o n a n a pp i n w h i c h u se r s w i ll b e asked to inp ut t h e i r c
a n d s t at e _ The c o d e w i ll u se t h a t
i-yt Lis e , i n p ut to p r i n t a g rre e tJi n g s r m i l a r to t h e fo ll o w i n g e x a m p l e :
•
c i ty = in p u t (" ln p ut y o u r c ity : ")
s ta te = in p u t( " ln p u t y o u r s ta te :" )
l
Reset
import random
You nee d to generate a ran d om password using three item s from a list of word s named my_words.
print("".join(randorn.SysternRandom ().choice(rny_words) for i in range(2}))
Which code should you use7
n =4
X = 7
T h e n .- s t o ut p ut s h o u l d b e :
4x1=4
T h e I a s t. o u.p ut s h o u l d b e:
4 X 10 = 4-0
0
pni11t { s1Ir { n ) -+-• x • + s t r( x ) + • = • + s t r( 11"'x ))
X+=1
• w h i l e x < 111:
p ri n t { s t r ( n ) + ' x ' + .str( x ) + ' = ' + s t r( n * x ))
X -= 1
w' ih [ l e X = 11:
0 p ni11t { s1Ir{ n ) -+-• x • + s t r( x ) + • = • + s t r( 11"'x ))
X+ = 1
wi h i l e x < ·11 :
I
A user is pr o m p te d to ent er the m o rt ga ge am ou nt u sin g the fo llo wing st at em ent :
0 print("{:.2e}".format(amount))
I
0 #print("{:,d}".form at(amount))
0 print("{:.2f}".format(amount))
F o r- e a c h ru n , y o u e rn e r- a s i n g l e " a l u e as -fo ll o w s :
54 6 54 756 54 9 3
False
689 59995 _54 6 6
T h i s i s o n. I v a 11e s t
C h o os e t in e c o n re c t a ns w e r
0
T h e typ e of v a l is : < c la s s "sli r"">
T h e typ e of v a l is : < c la s s "1il o a t' >
T h e t yp e of v a l is : < c la s s "s lir"">
0
T h e t yp e of v a l is : < c la s s "i rnt ' >
T h e t yp e of v a l is : < c la s s "iilo a t' >
T h e t yp e of v a l is : < c la s s " slir">
0
T h e ty p e of v a l is: < cl a s s 'b o or >
T h e typ e of v a l is: < cl a s s ' iflo a t'>
T h e typ e of v a l is: < cl a s s 's t r"►
• T
Thhe
e ttyp
ypee of
of vv aa ll is
is :: << cc la
la ss ss "sli
"s lir"">
r"">
T h e typ e of v a l is : < c la s s "sli r"">
T h e typ e of v a l is : < c la s s "sli r"">
Reset
In the code sh own in the ans wer area, an operator will allow the exp ressio n to be evaluated co rrectly from
left to right.
W hic h op erato r shou ld you use? To ans wer, select th e ap propriate option from the drop -do wn menu.
x=3
y=4
z=3
t=x y/z V 2
: i +
L!!J
( Previous '(:i Next ) I 54 I of 120 I 14411
Choose t he correct answer
rate = 0.25
item = 12000
ca lc l (rate, item )
p ri nt ('Rate :', ra te, '; Value: ', ite m)
amount= 1250
You need to write a print statement so that the amount will be printed as follows and aligned to the right
in the space provided in the print stateme nt:
1250.00
How should you complete the print statement? To answer, select the appropriate option from the drop
down menu.
<10,.2f
r-;;i 10.10f
( Previous Q Next ) [ s7 I of 120 I 1111
-
10,.2f
You need to create the follow ing directo ry tree on a W in dows co mp uter:
0 os.chdir(r'c :\Python\practice\myFolder')
0 os.makedir(r'c:\Python\practice\myFolder')
0 os.listdir(r'c:\Python\practice\myFolder')
L!!J
( Previous '{;] Next ) I ss I o f 120 I l1•iii
Y o u h a v e t.h e f o l l o w i n gc o d e s.lh o w n ·1:h. a t c a l c u l a t. e -s t h e t. o t. a ll o ·r a lll i t e m s i n t h e ll i s t :
p rr i c e s = [1 _0 0 , _ 3 _5 0, 5 .05 ] t o t a l = 0
p rr i n t ( t o L a ,I )
0 t o t a l + = s t r ( it e m )
01
t a l t o+ =fil o a t ( i t e m)
to t a l1i:o t a ll + it e m
0 total+=int(item}
Java
P y ith. o n
Ruby
f o i- ii i"n .- a n g e- ( 1 ,5 ) :
p r i n t:(p l" O g I"a rn 1"'171 i"n g 2 [ i ])
f or i in p r og ra lll'l1117 i n g 2 :
p ri n t (p .-og ira mm i 11191 2 [ii]I)
for i in p r og ra lll'l1717 i ng :
p ri n t (p .-og ira mm i 111g1 [ i] )
f or i in i- a n g e ( O . 4- ):
p ri n t (p .-og ira mm i 11191 2 [ i] I )
You have the fo llow ing cod e segment:
def change_n
ames(elements):
new_elements = []
for e in element s:
new _elements.append(e[0:2].upper())
1tn ew _e leme nts.sort()
retu rn new _elements
( Previous Q Next )
z = 92
n=4
t= 'That he will not when he may, when he will, he shal l have nay!'
result= o
You need to add a line of code to the end so that r esult will have the value 8.
r-;;-i
( Previous O Next )
Yo u have t he follo win g code segment.:
Youneed to add code to the beginning so that the amolJnt will need director approva I..
Reset
You need to ad d code to create a li st of 10 rando m integers between 1 and 10 inc lu si ve.
W hich code sh ould you use? To answer, select the appropriate opt ion from the drop- d ow n menu.
import random
*
You have the following code:
For each of the following statements, selec t Yes if the statement would cause a runtime error.
Otherwise, select No.
Statement Yes No
test_score = test_scores("20","19") D
test_score= test_scores(1) D 0
L!!J
( Previous (;i Next ) l 65 1 of 120 J-
Which line of code should you use? To answe r, se lect the appropriate option from the drop-down
menu.
x=0
for i in ('3','1','2'):
f t i in numbers: vj
X += int(i)
* Nill
r;;-i
( Previous Next ) 66 of 120
You have the following code:
X= 3 / 3 + 3** 3
0 32
0 0.1
0 64.0
IO 28.0
I
( Previous tJ Next ) 1 67 1 of 120 I-
You need to offer custo mers th e Bronze level if they accumulate between 500 an d 600 points in sh opping.
0
if points > 500:
level = 'Bronze'
0if points >= 500 and points >= 600: level = 'Bronze'
•
if 500 <= points <= 600:
level = 'Bronze'
L!!J
( Previous Q Next ) [ 6s I of120 I Mil
paycheck = 2500 bonus = O
0 0
0 800
0 900
r;;-i
1• 950
I
1e1 11r
p r int1(p r i nL t h e m, _ d o c _ )
NA!lv1E
Exa m p le 3 - print ttl.e e le m ents se pa rarl:e d b y sep
0 F U N G Tl l O N S
prin t_ the m {" e le m e n ts, s e p
= '.') l e m e nt s.: li st of e !e m e
n ts
s e p : se pa r a o :r
0
print tfl.e e le m ents sepa ra te d by
sep lem ents.: li st o f er:ements
sep: sepa rm o:r
I•
I
"lel em ents.: li st of el:ements
sep: sepa rato:r
I
a, b , c, and d are defined as fo llo w s:
a=7
b= 3
C=5
d=1
• result = a + d• 2
0 result = c .. d - 1
0 result = a - c II d
0 result = a % c - 1
try :
d iv iision (2 ,0 )
0 e>:icept Ar irthm et id E rr m as e :
raise ValuelE r ro r ( "C a n no t divi:de by zero."} from e
try:
diviision (2,0)
•
try :
divisro n ( 2 , 0 )
e x ce p t Ze ro Dl Vi s io nE rro r as e:
ra rse V a lu e E rr o r ( "C a nno t d i v i d e by z e ro . ") f ro m e
try :
0 d i v iisio n (2 ,0 )
e);lcept Fl oatin gP ointError as e:
raise ValueE rro r("C anno t divid e by ze ro _ " } f ro m e
Res et
You need to add code to catc h mu ltip le exceptions in one except ion block.
W hich l ine of code sh ould you use? To ans wer, select the appr opriate option from the drop-dow n menu.
V
except (Zero DivisicmError, Typ eError) as e:
except (ZeroDivisionError I TypeError) as e:
pri ·tr e•xception caught %s" %e)
except (ZeroDivisionError, TypeError) from e:
except (ZeroDivisionError, TypeError) as e: exceptZeroDivis1onError, TypeErrnr as e
You need to add each item from my_dates to a new list and sort it.
W hich format code shou ld you use? To answ er, select the appro priate opt ion from the drop -dow n menu .
import datetime as di
my_dates = r· 2017-12-28T05
3809", "2017-10-14T01:08:32",
"2017-03-04T21:18:22"]
dates_ordered=O
for i in my_dates:
dates_ordered.append(dt.datetime.strptime( i, " %Y-%m-%dT %H %M % S" V ))
'%y-%m-%dT%H:%M:%S"
dates_ordered.sort()
"%Y-%m-%dT %H %M %S"
'%Y-%m-%d %H:%M:%S"
'%Y-%M-%DT%H:%M:%S"
X= 3
text= 'I have ' + X +
I
0 I have 3
cars in my garage.
r-;;i
( Previous <} Next ) [ 7s I of 120 ] E·l·ii
You have the following code seg ment:
You need to add a line of code so that when the use r enters 15, new_count will be 20.
0 new_count= str(count)+ 5
0 new_count= float(count) + 5
I• new_count = int(count) + 5
I
r-;;i ( Previous <} Next ) I 76 1 of 120 1 ml
For eac h of the following statements, select Yes if the state ment is true. Otherwise, select No.
Statement Yes No
When using format(), strings are right-aligned by default. D 0
r;;i
L!!J ( Previous Q Next) [ 11 I of 120 -I
I
X =0
y= 1
2=11 211
[9]: x:0
sum= x ... y + z
y: 1
z:"ZU
W hich except ion will the code raise?
sum: x+y+z
0 Exception
X
•0 TypeError
IndentationError
sum=x+y+z
A
import datetime
You need to add code to the end to print the number of seconds between the two dates.
0
timeDiff = endTime - startTime
print(int(datetime.timedelta(itmeDiff
)})
{v)
r;;-i print(endTime - startTime)
( Previous <CJ Next ) I 79 I of 120 I ii=&it
rou nave me ro11 owing cooe :
n=2
X =7
What wi ll be the va lu e of n7
I• 4
I
0 5
0 6
I.\ ..,
z= float('32.9')
I bool(z) + True
O
0 bool(z)
0 int(z) + False
0 str(z)
a= '6'
b= '5'
1e str
I
0 int
0 fioat
fo r i in ran ge(8):
p r int(fib (i), end =",")
0 A n error message
1e 1,1.2,3,5 ,8 ,13. X I
( Pr e vi o u s O Ne xt ) Is 3 I of 1 §1 iiiFI
0 FU r-.ecT IO NIS
0 AUTH R
, .DATA
0□ ESCRIP TIO
Reset
You are w riting a Pytho n pro gram that calc ulates a student 's average grade. When The program is run from
the command line, the user will provide the student name fo llowed by multip le grades.
You need to complete the code so that the program prints the student's name and the average grade.
Which code segments should you use7 To a n s w e r , se le c t th e a p p ro p r iate options from the drop-down
menus.
im p o rt sys
su m = 0
( P r e v io u s O Ne x t ) I ss I of 12,c
T h e m o d u l e E x a m p l e l i s dlefi n e d a.s f o ll o w s :
T h i s m o d u l e c o rn: a i n s f u n c t i o n s f o r t e s n g p u rp o se s ..
T h i s f u n c ti o n p r i nt s a li st as a st r i n g
d ef" m a i n ():
p rii nL o n e( 1 J _ 5 4 B }
p rii nL tw o ([1, 2 ,.3 ]l
i f _n a m e _ =,=, · _m a i n _ " :
m a i n ()
Y o u n e e d t o v i e w th e d o c u m e nt at i o n o f t h i s m od u l e _
W h i c h c o m m a n d o .- c o m m a n d s s h o u l d y o u u se ?
Ch o o s e t h e c o 1n e c t. answ er
0 h e l p E x a mp l e 1
0 i m p o rt { Ex a m p 'le 1 )
import Examp l
e 1 h el p (E x a m p
le 1 )
def add(a= 0 , b= 0
): return a + b
W hich of the follow ing is NOT a val id way to call the add functio n?
le
Choose the correct answer
add a=3, b=5
I
0 add()
0 add(10)
0 add("30","2"}
r-;;i
( Previous <(} Next ) I 87 I of 120 I iiifi
-
x=4
y= do_so mething(x)
What is th e val ue of y?
0 6
I• 12
I
0 4
0 8
What should the user enter as the day value for the percent_discount to be 10?
0 Saturday
0 Friday
I Sunday
I
I
(v) Monday
r-;;i
( Previous <.:J Next ) [ s9 I of 120 I 111
the code sho wn in the an swer ar ea. You need to evalua te the exp ressio ns to dec ide on a su itab le operato r p laced between x and y so 'we made it' is prim
hou ld you use? To answe r, select th e ap propriate option from the drop -do wn menu.
-
Choose the correct options
a = 10 -+- ► Go d
b=5 [ 1 0] : a = :1. 0
b= 5
c=2 c: = 2·
d= T r- llUIE! ·
d = True
:x: = a -+ b% d
y= d-+ b*c::
x = a + b o/o d
You need to comp lete the code so True is output to the console.
Which line of code sho uld you use? To answer, se lect the appropriate option from the drop-down
menu.
colors = ('red','green','blue')
more_colors = ('red','green\'blue')
extra- colors = more- colors
print(extra_colors is more_colors)
print(colors is more_colors)
print(colors is extra_colors)
print(colors is in extra
[ill colors)
( Previous <:i Next ) 1 91 1 1201
of
111
I\ l;;;; l;;;;l -
IO 13 / /4
I
0 3**2
0 11/2
A
r;;-i
22%5
* 1 ►
For eac h of the follow ing statements, select Yes if the state ment is true. Otherwise, select No.
Statement Ye No
s
Using a mix of single and double quotes in my_str will
cause an error.
D
my_str = "he says'Python is an interestinglanguage'"
The following code will print 41:
0
str_test = """Astring that can span multiple
lines""" print( len( str_test))
The type of new_val is int:
l ■il
L!!J
( Previous Next ) * 1 93 of 120 I - 1
mu neea to comp lete me coae snown 1 n me answer area so mat me print statement prints 1U '1 1n me
output window.
W hich sta tement sho uld you a dd? To answer, select the appr opriate opt ion from the drop-down
menu.
elements = (12,2,8,5,14,6,22,35,18,4)
6:"Go'1 count = O
for e in elements:
if e in items:
f:ontinue vj
count+= e
. ,, i\
a = 1
b=O
try:
x= a / b
You need to identify the spe cific object class name of the exception that will occur_
0 except Exception as e:
print('"Exception type ",type(e)
_name)
0 except Ze roDivisionError as e:
print('"Exception type ",type(e)__ name _ )
• except Exception as e:
print("Excep tion type:",type(e)__ name_ )
IL-
0 except Exception as e:
print('"Exception type:", str(Exception))
You are wo rking with the code shown in the answe r area.
You need to select the loan amount so that the rate will be 0.2.
Which loan amount sh ould you use? To ans wer, select the appropriate option from the dro p- down menu .
loan_amount = j55_1 vI
rate= 0
rate = 0 1
rate = 0.2
rate = 0.3
rate = 0.4
( P revi ous 'Cl Next ) [ 96 I of 1 20 1 ifi J
You have the fol lowing code:
You need to pri nt the numbers in the variab le measu rements to be 2 decimal places , resulting in the
foll owi ng:
For each of the following stateme nts, select Yes if the my_d ates item wi ll raise an excep tion. Otherw ise,
select No.
Statement Yes No
my_dates[O] D
my_dates[1] D
my_dates[2] D
D
-
a= 3
b= 2
W hich operat or should you place betw een a and b to get the valu e 9?
I• **
I
0 II
0 *
0 I
r-;;i
( Previous Q Next ) I 9 9 I of 1 20 1 mm
Reset
You are working on the code sh o wn in the an swer ar ea. You n eed to evalua te each operat o r an d select the one the resu lts with the sm all est value.
W hich operato r shou ld you use? To answer, select th e ap propriate option from the drop -down menu.
a= 17
b = 5 and True
a V b
r;;-i
L!!J ( Previous Q Next ) [ 1 00 1 of 1 20 I 14{1
- Yo u n ee d t o w r it e c o d e t o u se th e c u rr ent d at e t o p ri nt ou tp u1 sim il a r 10 t h e fo ll o w i n g e xa m p l e :
24 , Dec 20 17 w as a gr ea t d ay !
24 , Dec 20 17 rep resen ts 1he curr en t m o nt h and year. Th e m o nt h m u st be f o rm att e d w it h t h e t h r e,e - c h a r ac t er ab b re v iat i o n.
i m p ort d a te ti m e
today = datetim e.da te. toda y()
i m p ort d a te li m e
today = datetim e.date. toda y()
im po rt da te li m e
today = datetim e.da te. toda y()
• im p ort da te li m e
tod ay = datetim e.da te. toda y()
( P re vi o u s * N e xt )
Reset
You need to st art a web serve r to acc ess th e documentation of the flies you are workin g on in the current
directo ry, in add iti on to the Pytho n documentation.
0
i mport pydoc
import sys
pydoc.help(sys)
dle f fln d _g ra de s () :
• def find_grades():
grades.sort{)
return grades[ □],
g ra de s[- 1 ]
grade1, grade2 = fiin d _ g ra d e s ()
dle f fin d _g ra de s () :
0
gra d e s . so rt ()
return, gra d e s [ □],
g ra d e s[l len liJ r a d e s ) ]
gra d e 1 , g r a d e 2 = find_grades()
dle f flnd_grades():
grades.sort()
Dave,100 ,90,77
Sam ,55,90,80
0
with open('studenlfi le','w') as
f: file_data = f.read()
0
with open('studenlfile') as f:
file_data = f.readline()
• with open('studenlfile') as f:
file_data = f.read()
0
with open('studenlfile','r') f:
file_data = f.read()
You need to complete the code so the number 1 is output to the c onsole.
W hic h line of code sho uld you use? To answer , select the appropriate option from the drop-down menu.
V= 1
x=4
y=6
2=2
result = V
print(resu
L!!J
( Previous Q Next) [ 106 I of 120 1 111.J•i
You execute the follo win g code
I
my_li s=t'C Ja va' i-
X ro□► I C Code V
[ 11 ] :m y_l i st : C J a va •
1
)I )
Choose the correct answer
pr i nt (l anguage s
[ ·c , J'
0 1Pyt hon
II Ir IIII
['C' ' ' 'J' 'a' 'v' 'a' 'Python'] I
J a' V'a
J
l J'I'I
' 11
]
0
11 11
['C', ' ·, 'J 'a', 'v 'a', 'P\ 'y', T, 'h', '0 'n
, , , ]
, I ,,
X
0
1 1
'n')
r-;;i
( Previous <J Next ) [ 1071 of120 1 111
I
You have a file named VotingData.txt. You need to open the file for writing in binary format only.
0 f = open('VotingDatat.xt', 'w+')
0 f = open('VotingDatat.xt', 'wb+')
0 f = open('VotingDatat.xt', 'w')
L!!J
( Previous Q Next ) [ 1osI of 120 J Mill•■
You have the following code:
v = bool{[False])
x= bool{3}
y= bool(""}
z= bool(' ')
0 X
0 z
IO y
I
M V
For each of the following statements , select Yes if the statement is true. Otherwise, select No.
Statement Yes No
The pydoc module can generate documentation served
in It) 0
a Web browser.
The pydoc module can generate documentation saved
to 0 0
HTML files.
The pydoc module can generate documentation
saved to 0 0
PDF files.
The pydoc module can generate documentation in a
It) 0
console.
r;;-i
( Previous * Next )
You have the following code:
myStr = 'To Pyt hon or not to Python , that i s the quest io n 1'
:i+ Add m issing li ne here
0 val = len(myStr)
I• val = myStr.rfind('on')
I
0 val = myStr.find('question')
L!!J
( Previous '{;J Next ) 1 11 1 I of 1 20 ] 1 ►1
11
Yo u run t he fo llo w in g code segm ent:
i te m s = [1,3,'13')
values = 0
fo r i in items:
i f t y pe (i) == l i s t :
fo r j in i:
if J % 2 != 0 :
b reak
valu e s + = j
elif ty pe (i) == str :
value s -= i n t {i) p rint (va lues)
0 -4
0 -17
-13
I• 17
Fo r each of the foll l o w in g s t a t e m e n t s , s e l e c t Yesi f t h e statement is tr ue. O t h e rw is e , s e l e c t No.
C h o,o s e the c o, r re c t o p t i o n s
S tat e m e nt Y es No
The p ri nt s ta te m en t w i lll d i sp l a y T r e .:
u
a = 45 D
lb = 45
p rint (a i s not b)
The p ri n t s t a te m e n t wi lll d i sp l a y T ru e .:
t es t1 = 'T h is is a te s t " D
tes t 2 = 'T h is i s a tes t' .up p er()
print (tes t1 is tes t2 )
Th e p rint sta tem en t willl d i sp l a y Tr u e :
C = [ 2 ,4 ,7]
d = 1[2 , 4 ,7 ]1
print( cisd)
T h e p ri n t s t a te m e n t wi l ll d i sp l a y T ru e .:
D
printf h ' i n " P y t h on " )
T h e p ri n t s t a te m e n t wi lll d i sp l a y Tr u e .:
D
print ('is' in 'T his IS a test')
import datetime as d t
You need to complete the code to print the current weekday from the list days.
0
wd = dt.datetime.day
print(days[wd])
• today=
wd = dt.datelime.now()
dt.date.weekday(today)
print(days[w]d}
0
wd = dl.dalet
ime.dale
print(days[wd])
0
wd =
dt.date.weekday(dt.now())
print(days[wd])
r;;-i ( Previous O Next )
I lie Ille /O l f:1111. l X l lld::. ur 1,e 1 u 11u w 11 1g ,c,_;,u 1 1l e 1111 l :
Lo rem l p sum is sim p ly d u mm y te xt o f the print in g and typese tt ing indu stry.
Lo rem I psum has been the indu str y 's stan d ar d dummy text ever since th, e 1 50 0 s, when an unknown print er to o k a gallley of t y p e andsc ram b led it to
type specimen book. It has su r vived not o n ly fi ve ce nt u r i es , but also t he
,le api nt o e ,le c t ro nict y p e sett i ng , rem ain in g esse nt ial l y u n ch an ged . It was p o p u l ar ized i n the 1960s with the release o f Le tra set shee ts c o n
Lo rem l p sum p assages , and more recently with desktop publish ing so f tw are like Alld u s P ageMak er incl u d in g versions of Lo rem lp sum .
You n eed t h, e code to read the ent ire lo rem .txt file as a str i ng.
How should you complete the code? To an sw er , se lec t the ap p ro p riate o p tio n from the drop -down menu.
try
Reset
You have the c o d e sho w n in the answ er area. You n eed to p ro d u c e the fo ll o w in g output:
rat b at
barn
W hich code shou ld y o u in sert ? To answ er, select the app ro p ri ate o pt ion fr om the d rop -dow n m enu. Choose the correct options
XI[] □ ► ■ C C o de V
words = r rat','bat','house','bam'] : 12]:word s=['rat,' 'bat ', 'h ouse,' 'bar n']
!c 4
rat bat
-
< 4: barn
4:
1 i t e rn s = 0;
2 it e m = 0
3
4 d eif g eL d at a O:
5 f o r i i n r a n g e (1 ,5 ) :
6 it.e m = inp utJ('"E n t er a g r ad e : ")
7 i t e r-ns .a pp e nd (i t e m )
8
9 def av g () :
10 sum=0
11 f o r i in it.e m s :
12 s u n, + = i
13 r e t u r n r o u n d (s u rn / l e n ( it e n, s) , 2)
14
15 g eL d a t a ()
16 p r in tJ( a v g O)
W h ic h l i n e c o nt a in s a n e rTo r ?
0 5
0 13
0 7
_LO 1 I
*
2
W hic h code shou ld you use? To answer, select the appropriate option from the drop-do wn menu.
importmath
set_c = V
print{total)
strings' x= content.find('in',6)
result= content[x:30 ].capitali ze()
print(result.ce nter(20 ,' ')
IO
What will be displayed on the scree n?
0 -Especial!
0 -Learni11y##//#// ,
'l##t#pvthOn" 11
n, "9("1""'I' "" 'IrI'
1,1 n 1, n 1 I
( Previous O Next )
L!!J
import math
my_l ist =[.3,.3,.3, .1, .1]
0 math.ceil(my_list)
0 math.floor(my_list)
0 math.factorial(my_list)
I• math.fsum(my_list)
I
( Previous Q Fin ish Test 1 120 1 of 120 1 Mil