Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 75

You have the following block of code:

print("Determining the data types")


11 -- Add m i ssing code here--

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.

W hich line should you add?

Choose the correct answ er

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.

You run the following command:

python -m pydoc myM odule

W hich python file will pydoc att empt to create docume ntat ion for7

Choose the correct answe r

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

The variable named test is declared as follows:

test= "TEST"

Which line of code assi gns 'TT' to result?

Choose the correct answer

0 result = test[O] + test[2]

0 result = test[1] + test[4]

I . result = test[O] + test[-1]

0 result = test[1] + test[1]

( Previous Q Next)
L!!J
Vc:::,, u h ial"Y e-f" lao-.r 1ns c e1 :

f = e n C- n ue .=r s..._"'l::Jct"II p .- 1 rr=ta'_ re ad O)


f . c loo :s .<E-( 11

V c:::,, u n e e d t, o add c c:::ad ,e, t,o h a n d le .a fi le .-.cat #c,, u n d ,e.::,c c ,e,p.-ui.

W h • c:t-. c o de :s..hou ld V...-s:e ?

-.- 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

VVhich strin g d e cl a ra t i o n spans m o re than one line and resp e c t s w h it e sp a c e when th e s tr i n g i s p r i n t e d o n


the s c r e e n ?

C h o,o s e the correctanswe r

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_"

str 4 = "' "' L o r e m l p s u m i s s im p l y d u m m y text of the printing


• and typesetting
industry_ L o r e m l p s u m h a s b e e n t h e i n d u s t ry ' s
s ta n d a r d d u m m y t e x t e v e r s i n c e
t h e 1 5 0 0 s , when an unknown printer took a galley
of type and scrambled it
to m a k e a ty p e s p e c im e n b o o k ."' "'

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 _'

s t r 3 = ' It is a lo n g e s ta b l is h e d fact that a readie r

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 :

1 f =o p e n ( in p u t (' En t e r i n the fi l e nam,e to read: ')


2 w hi le Tru e:
3 I i n e =L r e a d l i n e O
4 i f lli n e == " :
5 b re ak
6 else:
7 i f i n t ( l i n e ) % 2 == 0:
8 p r in t ('N u m b e r i s e ve n ')
9 e l se :
10 p r int(' N u m b e r i s o d d '}

Which line of code has a syntax err o r?

C h o,o , thecorrectanswer
se

0 2

0 5

0 3

I• 1
I
( P re v io us O Nlext)

Reset

You have the fo llowing code

M yStr = "Pyt hon is the bes t programm ing language!"


myStr = "Py tho n is fun to learn !"

text = mySt r • 2

W ha t is the value of text 7

Choose the co rrect answer

10 ' Python is fun to learn!Python is fun to learn!'


I

0 ' Python is the best language!Python is the best language!'

0 'Python is the best language!2'

0 'Python is fun to learn!22'

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

What will be the value of x?

Choose the co rrect answe r

0 5

0 6

I• 4
I
0 7

r;;i ( Previous Q Next ) [ 11 I of 120 I-

Reset

n the an swer area.


n to implem ent the formula to calculate the area of a circle (p i t i mes r squa red}. W hic h code shou ld you use? To answ er, select the ap p ropriate option fr

Choose the correct options

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 have the co de sho wn in the an swer area .

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 .

Choose the correc t options

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

W hic h code segment shou ld you use?

Choose the correct answer

f = ope n (' fi le _name','r ')

0 f.write('He llo World!')


f.close()

f = ope n (' fi le _name','b')

0 f.w rite('He llo World!')


f.close()

f = ope n (' fi le _name')

0 •
f.write('He llo World!')
f.close()
f = open(' file_name','w')

f.w rite('Hel lo World!')


f.close()

r;;-i
( Previous <CJ N ex t )

Yo u s ave d ·m e f o llo , , , n -g,. c od e In a ifl l ,e n.a m e d Elo:cTniPl e2 p. y :

C a l e .l a t e:s. h e d ;t3nce fr om .-;s.te- a n d t i m e .


Fi n ds 11',, d: lst c::n c e b et-wee n t>.'"'D p aoln = .

_ a Lillho r:_ =, ""Pyttto n 'testter""


_ c op y.-1113 ht_ ,= " C o ls h : 20 19 . 3 1-n; l n, g P ro j e c t'"

I Dl!jp<H°t an, arth

d e c a l c:_d -i:a n :::,e( rc. te . m e_):


retum r ate " t ime

d e "' pa l nts._d l:stan c e{>:1.32":.v 1y. 2 )::

The d: lff e re n c e oF th e I q u a re d .and me


d :lfFe:r en c e of the v sq uared

retum math .s,qrt(f,c:2 - x1J," • 2 (y ::: - y 1 ) ·• :::)

Yo u e oc e ::: ut, e t h e ro ll'°""ln g: c orrvn.3 nd :

p .h o n - rn ,v d o c · E x .a m p, l e2

C T I O N 'S -s;e -:::1:lon wl-.en h e c o m m an 1-s e!JOE<::: -.rte· ?

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)

calc._d is't3n ce,{.-.a1E. tilne)

0 p ainl:caa_d i s t:a n ce{ x 1 . x:2 , y 1 . y2)


·-n d i e r e n ,ee, o f° th - x sq;uaue
d·i e re n ,ee, o·f ·th ey e:::il
and th e

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' '}

if g i-ad e :,, = 90 a n d g r ad e <= 10 0 :


ind e x = 1
e li f gi-ade >- = 80 a nd g i-ad e 90:
ind e x = 2
e li f gi-ade >= 70 and g .-ad e BO:
ind e x = 3
e li f gi-ade >- = 60 and g rrad e < 70 : i
nd e x = 4
else:
i nd e x = 5

p r i n tJ( system(ind e x])

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 ?

Choose the correct answer

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:

m y _s<ri n g A = ' I l i k e P i e . '


m y _ stri n g 8 = 'K id s.li ke t o y s.'
m y _silr l n gC = 'I t is h o t in th e su n _'

Yo u need to writ e code that wil l p r in t the f o l l ow i n g message on the screen:

Python

W hich code se gm e nt sh o u ld yo u u se ?

Choose the correct answer

-
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 )

tex t = m y_ s trin g1A [8]


m y _ st ri ng C[ -3 ]
p ri nt (t e x t )
+ m y _ s t r i ng B [ -2 1 + my _ s tn n gB [ -4] + my _ slri n gC [ 7! -1- m y _ s t ri ng B [1 2 J +

tex t = m y_ s trin g1A [7 J + m y _ s tri ngB [ -2 1 + my _ st rin gB [ -4] + m y _ st ri n gC[ 6 j m y _ s t ri ng B [11 J +

0
-1-
m y_s t ri ng C[ - 1 ]
p ri nt (t e x t )

tex t = m y_ s trin g1A[7] + m y _ s t r i ng B [-3 ) + my _ st rin gB [ -5 ] + my _ sl rin gc[6] + my _ slri n gB [11 ] +

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 )

W ha t w i l l the fina l statem ent p r int af te r the code is execu ted ?

Choose the co rrect answer

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:: .

stu d ent s = [("Ma r k", "A"I , [" B r ya n", "B"J,


! " Li sa· , " B"I, ["J ess ic a·, "C" ],
l "T im ","A"II

W hich c o d e seg m ent sh o u ld you use?

Choose the correct answer

• 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

0 with open ("'s tu de n ts .csv" . "w" , ne wline= "") as f:


f.w rite ro ws(stu den ts)

impo rt csv

0 with open ("'s lu de n ts . cs v" , "W'', ne wl ine = "'')


as f: w ri ter = cs v.writer(f)
writer.wril e(stude nts)

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)

( Prev ious 'O Next) 20 I o f 1 20 1 i:fJn:I


Reset

You have the follow ing declarat ions:

s = ' Str ings in Pytho n!' s2 = 'Inte r estin g'

W hich code ass i gns 'in ' to v ar?

Choose the correct answer

0 var= s[:3]

0 var= s2(9:11]

0 var = s2(1:3]

J•
var= s[3] + s[4]

( Previous O Next ) I 21 I of 120 I 14111


Reset

You execute the follow ing code:

word s = "The cow has spots and walks slowly ."

m y_sentence = words[-7:]
m y_sentence = words[-7:] + words [:8]
my _sentence = m y_sentence + words[4:7]

W ha t is the value of the variable my _sentence7

Choose the correct answe r

0 slowly The cow cow

100
slowly.
slowly.The cow cow

0 slowly.slowly.The cow

( Previous "Q Next ) [ 22 I of12 0 1 141+1


Reset

You have the co de shown in the ans wer area.

You need to com p lete the co de to p ro duce the following outpu t:

['eggs', 'm il k', 'steak ']

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.

Choose the co rrect options

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 )

W h at w ll l 1hef □ l l c ...,.s,-,i;: ,c, □ GEsn i pp et dlsplay In th e o utp..,...l nd ow ?

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

You have the followi ng decla rati on:

my _ite ms= (l8,'7',1+2j, Fa lse])

W h ich l i ne of[ code


3] : massigns
y_i t me <cl:sass( [8,
'li st'> to my _type? lse))
'7',1+2j,Fa
pr i nt ( t ype (my_i t me s ) )
pr i nt (the
Choose t ype
co (my_i t me s [0] ) ) pr i nt ( t ype (my_i t me s[ l ] ) ) pr i nt ( t ype (my_i t me s [ 2.] ) ) pr i nt ( t yp
rrect answer
<cl as s ' l i st ' >
<cl as s i nt I >
I

0
<cl as s ' st r ' >
my_type
<cl =astype (my_items
s ' compl ex ' > )
< cl as s ' boo l ' >

0 my_type = type (my_items [2])

0 my_type = type (my_items [3])

/
my_type = type(my_ite ms[1))
', A
-
r-;;i • -
( Previous <(}
'
Next )
I

[ 2s I o f 1 20 ] -

You have the followi ng c ode segment:

def cal c(a mou nt=5, rate=2):


if amoun t > 5:
retu rn a mou nt *
rate else:
retu rn a mou nt * 2 * rate

W h ich fu nction ca l l re su lts i n 4?

Choose the co rrect answer

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

The fo llow ing co d e cont ains an erro r:

d ef calc _area _tr i ang le(I, b, h):


return b • H / 2

ar ea= ca lc_area _t rian g le(O, 3 , 4)


p rint(a rea)

You need to fix the err or.

W hich code shou ld you use to fix th e erro r?

Choo se the correct an sw er

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

You need to pr int a list of files in the c :\ t m p d irecto ry.

W h i c h code should you use?

Choose the co rrect answ er

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):

de f find lt(item, item s)


if item in items:
return True
else:
return False
6
7 p r int(find lt(3,[2,4,5,6,7,8,9]))

W hic h line c ontain s an error?

Choose the co rrect answer

[ill ( Previous Q Next ) I 29 I of 120 I=■Fi.fl


[ill ( Previous Q Next )
You have the fo llowing co de:
1 30 1 0F1 20 1 111

w_l i st= ['d og','bear']


X= 0

fo r word in w_li st:


for i in range( len(word)):
X =X +1
if X > 6:
break

W hat will be the value of xl

Choose the co rrect answer

0 10
Reset

You need to cr eate a custom exce ption for your application.

Which code should you use?

Choose the correc t answer

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

You have the following code:

import sys

You need to add co de to print the location on the c omputer where the Python interpreter is installed.

Which line of co de should you use?

Choose the correct answer

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

You need to add a line of c ode so that x wil l be 16.

W hich l ine of co de sh ould you add?

Choose the correct answer

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 .

W hich l ine of co de sh ould you use?

Choose the correct answer

0 raise ValueError(e)

I• raise
I
0 raise ValueError('Error Division by zero.')

0 raise as e

r-;;i ( Previous r} Next ) 34 0 I m


Reset

You have the follow ing fun ction definition:

def print_d ir(* elements, sep= "''):


return "C:\\%s" % (sep .join(elements))

W hich function call will cause an err or?

Choose the correc t answer


• print(print_dir('Program files', None))

0 print(print_dir())

0 print(print_dir('Users','PythonCoder','Practice','Exercises')}

0 print(print_dir('Program files','Test', sep='\t'))

r-;;i
( Previous Q Next )
1 35 of 120 I -
1

Reset

You are workin g on the code sh o wn in the an swer ar ea.

You need to select an opera t o r so that the printed value of z is 5.0 .

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.

Choose the correct options

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

x = 10 + 1.1 + float (4.1E-2)

W hat is the valu e of x?

Choose the correct answer

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.

How will it evaluate?

Choose the correct answer

0 -1

0 25

( Previous Q Next ) 38 1 of12 0


L!!J 1 1-
You have the code sh own in the answer area.

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.

Choose the correct options

with open('PurchaseOrders.txt', 'w+') as f:


f.write('some data\n')

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):

1 m y_l etters= l'.A:, 'G','T','E']


2
3 def letter_ac cu m u lato r(nam e, bou nd= 2, letters):
4 resu lt = "
5 fo r letter in letters[O:bou n d ]:
6 resu lt += letter + '_'
7 r eturn "(O} accumulated {1}".forma t(na me, result)
B
9 print(lette r_ac cumulator('James', m y_letters))

W hic h line will cause an erro r when this p rog ram is ru n 7

Choose the co rrect answer

0 7

I• 3
I
0 5

0 9

r;;i ( Previous Q Next ) [ 4o I 0F1 20 ] Ill


You have the code segment sh own in the ans wer ar ea.

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.

Choose the correct opt ions

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

[ill ( Previous <CJ Next ) 1 41 1 of 120 l 1!11


You have the code show in the answe r area.

You need to lim it the file si ze when writing to it.

Which line of code sh ould you use? To ans wer, se lect the appropriate option from the drop-down
menu.

Choose the correct options

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()

[ill ( Previous <:J Next ) 1 42 1 of 120 1 11111


You have the following code:

import random

my_l i st= ['Ru by','Pyt hon','..Java','PHP']

You need to print a rand om value from my_list.

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.

Choose the correct options

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.

Choose the correct options

a= 'Test'

b = 'Test'

result= a V b

=
<

[■i■ll ( Previous <:i Next ) 44 ot 12 o P'fltl:I


Y u h v e 11h e f o l l o w i r,g d e cl a r a ti o n s :
o
= 1
X = O
y = 2
z = 3
5 l = r-e 51u l t. _2 = r e s u l t_ 3 = r e s u it:_4 = 0
r-e S1u l
t_
s,e1t of c o d e w i IIp r-i n t 10 o n th e s,c r e e n ?
W
hi
c
C o o e tlhe c or-r e c t. a n s w e r

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 )

w'h il e z < 2 .41:


r e s u llt_ 3 ·+= z
= 7:
0 if res l.llt _3 > = 4 and
r es u llt_ 3 -= 3
z+=2
r es u lt _3

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 have the code shown in the answer area.

'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.

Choose the correct options

import sys,

fh = openf'outpuUxt",''w')

p11i11t('save info lo outpuUxt")


th.close()
Yo u have the following co de:

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

if state in ['Orego n','.M o nt a n a ' ' N e v a d a ']:


if o rd e r _to t a l < = 50 :
s h i p p i n g_ c o s t = -4
e l if o r d e r _t o t a l > 50 an d o rd er _t ot al 15 0 :
shi p p i n g_ c o st = 9
e lse :
s h i p p i n g_ c o s t = 10
else:
sh i p p i n g_ c o s t = 5

if sit.at e i n [' N e w Yor k ',' ld a ho ","N e b r a sk a",'T e xa s']:


if order_t o t al > 10 0 :
shi p p i n g_c o st = 11
if o ,r d e r_ to t al = 200 a n d s.t a te i n [ ' N e w Yo r k' ' V i r gi n ia']:
sh ip p in g_c o st = 12'
e l if o rrd e r _t o t al > 10 0 :
s h ip p in g_ c o st = 13

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 ?

Cnoose tne correct answer

0 13

0 5

1• 12

You have the code se gment sho wn in the answer area .

You need to com plete the code so that c has a value of 4.

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.

Choose the correct options

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 :

Welcome to the great city of Dallas in TexasI

I n t h e e x a m p l e ' D a ll a s ' i s t h e c i t y a n d T e x a s " i s t h e st a t e _

W h i c h code segment sholl ld yoll use?

C lho c se the c c r r e c 't answer

city = s tr ( "l n p u t your city:")

0 state = s tr (' "l n p u t your state:")

print("Weloome to great cfrty o f O in O !" _J;o r m a t (c ity , s t a t e ) )

ci t y = e v a l (" IIn p u t yo m cfrty:")

0 s ta te = e v a l(" I np u t your state:"}

print("We loome to great cfrty o f O in O !" _fo r m a t (c i ty , s t a t 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 :" )

p ri n t ( " W e lc o m e t o g r e a t c it y o f O in O ! " _f o r m a t ( c ity , s t a t e ) )

0 ci t y = re a d ("l n p u t your city:")


state = re a d ( ' "lln p ut ya m s ta te :")

p rin t( " W e loo m e to g re at c frty o f O in O !" _fo r m a t (c i ty , s t a t e ) )


I

l
Reset

You have the following code:

import random

my_words = ["Python","two","ca r","house","PHP "]

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

Choose the correct answe r


print("".join(randorn.SysternRandom().choice(rny _words) for i in range(4}))

print("".join(randorn.SysternRandom ().randint(1,3) for i in rny_words))

0 print("".join(randorn.SysternRandom().choice(rny_words) for i in range(3)))


•0 ( Previous O Next ) I Mii·I
0
[ so I of 120
Yo u h a v e, h. e f o 11 o w i n,,g c o d e ::

n =4
X = 7

Yo u n eed t o p rii nlt o ut a m u ltip l i c at i o n t.ab l e f o .- 4- x 1 th .-o u g h 4- x 70 , w i1th. e a c h. o •u tp u t. on a s e p a r cli1:e l i n e .

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

W h i c h code segment shouldy o·u u s e ?

Choose 1:lhe c o rn ·e c t answer

wi'h[le X < 11:

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 :

0 p ni11t { s1Ir{ n ) -+-• x • + s t r( x ) + • = • + { n "x ))


X+ =1

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 :

am o u nt = fl o at(in pu t("Ent er the m o rt gage am ou nt : "))

The user enters 32515 4.56 87.

W hich sta tem en t w i ll p rin t 325,154 . 5 7 on the screen?

Choose the correct answer

0 print("{:.2e}".format(amount))

I
0 #print("{:,d}".form at(amount))

0 print("{:.2f}".format(amount))

( Previous O Next ) I s2 Io f 12 0 I Mii=i


Y o u ru n t h e f o l I o w i n ,g co de f o u r- t i n -ie s :

p r i n E nte r- a " a l u e : " J


v a I = i np um()
p r i nt:{9Th e yt p e of va l i s : • . yt p e( ..,.a l }}

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

W h i c h output will b e d i sp l ay e d -fo r- 1th e f o u r r u n.s ?

C h o os e t in e c o n re c t a ns w e r

T h e typ e of v a l is : < c la s s "i rnt' >

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"">

T h e t yp e of v a l is : <c a s s "i rnt ' >

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">

T h e ty p e of v a l is: < cl a s s ' i n t' >

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.

Choose the correct options

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

You run the fo ll ow ing co d e:

de f c alc 1{ra te, ite m): ite


m *= (1 + rat e)

rate = 0.25
item = 12000

ca lc l (rate, item )
p ri nt ('Rate :', ra te, '; Value: ', ite m)

W ha t w i ll be d isp lay ed on the screen7

Choose the correct answer

0 Rate: 0.25; Value: 15000

I• Rate: 0.25 ; Value: 12000


I
0 Rate: 1.25 ; Value: 12000

0 Rate: 1.25 ; Value: 15000

( Previous Q Next ) [ s6 I o f 1 §7 Ml·ti


Reset

You have the varia ble amount declared as shown below:

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.

Choose the co rrect options

print("{: v }" formal(amounl))


1- - - -l

<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:

c:\Py thon\practice \myFolder

W hic h code shou ld you use?

Choose the correct answer


• os.makedirs(r'c:\Python\practice\myFolder')

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

f c, i- i t:.e nr, ii n p rr r c e s : t o t .a I ·-+- = rt e nn

p rr i n t ( t o L a ,I )

T h ecode p .-o d u c e -s t.h e f o l l o w i"n ge r i- o .-:

T .-a c e b a c k ( m o s t. i- e c e n t:. c a l l l a s t. ),:


F i l e " c : / P yt lh o n L e ar n i ng /p .-o g .-a rn m fn g _p y, l i n e 5 , fn < m o d u l e >
t. o t a I - = i t .e m
Ty p e E r i- o .-: u n -s up p o i- t e do p er a n dty p e ( s.) f o i- -+= : "fl o a t. " a n d · s.t .-· Y o u n e e d ·1:0.fi x ith. e e r i-o L
W h i c hcode sh ouldy ou use?

Ch oose -cine c o rr e c 't. a n s .w e 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}

0 Y o u hl a v e a n e x is ting c <>d e rn o d 1u l e th .a -.:. fs ,e xp e er ed t <> p rr fn t ·1:h. e -r-o 1 1 o w fn g o u -.:.p u t

Java
P y ith. o n
Ruby

Yo u h a v e t:h. e f o llo w ing c o d e :

p r og r a m m i ng = {' ::.Jl a v a · · : 11, " 'P y-.:.h o n ··: 2 , "' R u b y "': 3 }


p rr og r a m m i ng 2 = { 1 : ":J a v a ,'" 2 ': "P yt. h o n ," 3 : "' R u lby "' }

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 ])

Y o u n e ed t. o i- e s o I v e 1th e -fa 11o w i ng e i-.- o rr w i t:lh -.:lh e c o d e :

T r-a c e b a c k 1( ,n o s 1t. r-e c e n -.:. c a l l l a s 1,t.) :


F i l e " ' c : / P y -clh o n L e a r n i ng /p .-c,,g .-a rn m fn g _p y , l ii n e 5 , rn < m o d u l e > p i-i n 1tp(
.-o g rr a nr, nr, i n g 2 [ r ] ),
K ey E rrr o .-: -4!-

v.Jhich lii ne-s o -f" c o d e s.lh o u ld y· o u u s e ·1:0. .- e p l a c e ·1:h. e e x rs-H ng f o .- l o o p ?

Ch oos e 'Cine c or r e c 't. a n s w e r

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 build_n am es():


names = [''Ariel","M att ",''..J o h n ","Sus ie","Dav id ","Steve","Douglas"]
retu rn names[1:5]

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

p rint{change_n am es(bu i Id_n ames()))

W h at wi ll b e disp laye d in the output window?


Choose the correct answe r

I•0 [' MA', 'JO', 'SU', 'DA')

['DA', 'JO', 'MA', 'SU']


I
0 ['DA', 'JO', 'MA', 'ST', 'SU']

( Previous Q Next )

You have the follow ing declarat i ons:

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.

W hich l ine of co de sh ould you use7

Choose the c orre ct answer

result = t.find{'\MII') if z > n else 7

0 result = 14 if len(t) > 4 else 7

0 result = 3 if None else z/n

{:7) result = t.rfind('will') if t else None

r-;;-i
( Previous O Next )
Yo u have t he follo win g code segment.:

if ac co u nL t y pe _Io w ,e r() = = ' d ' : i f i n v o i c e _a m o lJn t ,. = 10 0 :


p r i n t( ' n e e d s m an a g e m e nt a p p r o v a I') e ls e .:
p r i n t(' a m o u nt a p p r o v e d ') e l i f a c c o u nL t y p e _ l o w e r () = = ' a ' :
i f i n v o i c e _a m o lJn t ;,, = 10 0 an d in v o ic e _a m o u nt <= 10 0 0 : p r i n t(' n e e d s d i re c t o r a p p r o v a l')
e ls e .:
p r i nt (' a d d to w e e k l y re p o rt ')
else:
p r i m {' p l e a se stop by HR')

Youneed to add code to the beginning so that the amolJnt will need director approva I..

W h i c h code should you use?

Choe se the correct answer

account_type = ' B '


0 in vo ic e am ou n t = 1000

0 account_type = 'A '


in vo ic e am ou n t = 99

acco unt _ ty pe = 'A'


in v o ic e am ou n t = 100

acco unt _ ty pe = 'D '


in vo ic e am ou n t = 1 0 0

Reset

You have the code show in the answer area.

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.

Choose the correct options

import random

*
You have the following code:

def tesu co res(totaLquestion=s 0, total_co rrec=t 0):


return int(total_correct) / int(totaL questions)

For each of the following statements, selec t Yes if the statement would cause a runtime error.
Otherwise, select No.

Choose the correct options

Statement Yes No

test_score= test_scores(20, 19) D 0

test_score = test_scores("20","19") D

test_score= test_scores(1) D 0

test_score = test_scores(0, 20) 0 D

L!!J
( Previous (;i Next ) l 65 1 of 120 J-

You have the code shown in the answer area .

You need to comp lete the code so x has a va lue of 6.

Which line of code should you use? To answe r, se lect the appropriate option from the drop-down
menu.

Choose the correct options

numbers= ('0' '1' '2' '3' '4')


I I J 1

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

What is the value of x?

Choose the correct answer

0 32

0 0.1

0 64.0

IO 28.0
I
( Previous tJ Next ) 1 67 1 of 120 I-

You are developing a shopping website.

You need to offer custo mers th e Bronze level if they accumulate between 500 an d 600 points in sh opping.

W hich code block sh ould you use?

Choose the correct answer

0
if points > 500:
level = 'Bronze'

0if points >= 500 and points >= 600: level = 'Bronze'

0if points >= 500 or points <= 600: level = 'Bronze'


if 500 <= points <= 600:
level = 'Bronze'

L!!J
( Previous Q Next ) [ 6s I of120 I Mil
paycheck = 2500 bonus = O

if paycheck <= 1500: bonus = 750


elif paycheck > 2000 an d paychec k < 2500 : b onus = 900
elif paycheck > 2000: bonus = 800
if paycheck >= 2500: bonus = 950

What wilI be the value of bonus?

Choose the correct answer

0 0

0 800

0 900

r;;-i
1• 950
I

( Previous Q Next ) I I of 1 2?] 1¥4-#1


69

y·o u run t n e r o 11 o w 1n g coo:e:

1e1 11r

p r int th e e[l em e nt s se p a r at e d by sep


1 e1 11r

d ef p rinL th em ('"e le m ent s, s,e p =" ,'}:


""" '"ele m ent s: l ist of e[l e m e n t s
sep: sep ar at o r
H ll n

return sep j o i n(ele m ent s)

p r int1(p r i nL t h e m, _ d o c _ )

W hi"c h d o c u m em a tii o :n w illl be d isp lay e d i n the o u t p ut ?

Choose tile correct answ er·

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 e n t s separated by sep

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

W hich l ine of co de ass i gns 9 to resu lt?

Choose the correct answer

• result = a + d• 2

0 result = c .. d - 1

0 result = a - c II d

0 result = a % c - 1

[ill ( Prev ious O Next ) I 11 I of 120 I iiiۥ

Yoll have the fo llowi ng co de:

def division(a, b):


return a / b divisi on(2 ,0 }
Yoll need to add code lo han d le a d i v is io n by z.e r o exc ep t io n .

W h ic h code should yoll use?

Choose t Ile correct. answ er

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)

0 e>:icept O ve lfflow Err or as e:


raise ValueE rro r("C a nno t divid e by ze ro _ " } f ro m e


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 have the code shown in the an swe r area.

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.

Choose the correct opt ions

def division(a, b):


return a I b
by:
try: divi1sion(4 0) 1
division(4, 0)
11 11
division("3", "4")
divis,i on("3', 4 )

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

[ill ( Previous f:1 Next ) 1 73 of 120


1 I Ill
You have the code shown in the answer area.

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 .

Choose the correct options

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"

( Previous Q Next ) 1 74 1 of 120 1 E·fh


You have the following code:

X= 3
text= 'I have ' + X +
I

cars in my garage .'

What is the val ue of text?

Choose the correct answer

0 I have 3 cars in my garage.

I• An error will be generated.


I

0 I have 3
cars in my garage.

0 I have 3\ncars in my garage.

r-;;i
( Previous <} Next ) [ 7s I of 120 ] E·l·ii
You have the following code seg ment:

count= input('Enter the number of students in the


class: ') ii add missing line here
print(new_co unt)

You need to add a line of code so that when the use r enters 15, new_count will be 20.

Which line of co de should you use?

Choose the correct answer

0 new_count= str(count)+ 5

0 new_count= float(count) + 5

0 new count= 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.

Choose the correct options

Statement Yes No
When using format(), strings are right-aligned by default. D 0

The type code s converts a number to a D 0


scientific notation
If the user inputs 32767, the printout will be 32768:
D fl)
count :a input("entercount: ")
print(count+ 1)
When using format(), numbers are right-alignedby 0 D
default.
If the user inputs 25314.32568, the printout will be
25314.33:
0 D
amount = input("enteramount: ")
prinl("{:0.2f}".format(float(amount)))

r;;i
L!!J ( Previous Q Next) [ 11 I of 120 -I
I

You have the following code: ' 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?

Choose the correct answer

sum: x+y+z
0 Exception

0 ArithmeticError File <ipython-inpu t-9-fc4e6b7d49cd> li


11 11

X
•0 TypeError
IndentationError
sum=x+y+z
A

IndentationError: unexpecte d inden t

( Previous <:J Next ) I 78 of1 2 0 I


I ii=ifi
You have the following code:

import datetime

startTime = date time.date(20


18,1,15) endTime = datet
ime.date(20 18,1,20)

You need to add code to the end to print the number of seconds between the two dates.

W hich code should you use?

Choose the correct answer

• timeDiff = endTime - startTime


print(int(timeDiff.total_seconds())
)

0
timeDiff = endTime - startTime
print(int(datetime.timedelta(itmeDiff
)})

0 print((endTime - startTime)/ 60)

{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

for i in range(S, x): while i < 6:


n = n + 1 if n == 2: pass
else:
n += 1
i += 1

What wi ll be the va lu e of n7

Cho,ose the correct answer

I• 4
I
0 5

0 6

I.\ ..,

( Previous Q Next ) [ so [ 0 F1 20 1 iflJi


You have the following line of co de:

z= float('32.9')

Which expression evaluates to 2?

Choose the correct answer

I bool(z) + True

O
0 bool(z)

0 int(z) + False

0 str(z)

[i•l ( Previous t/ Next ) [ s1 I of 120 I iM•i


l
You have the following variables:

a= '6'
b= '5'

What is the data type of a + b?

Choose the correct answer


0 complex

1e str
I

0 int

0 fioat

( Previous O Next ) [ s2 [ of12 0


L!!J J -
d e f fi b ( x):
if X == 0:
return 0
e lif x == 1: return 1
else:
return fib(x -1) + fib (x - 2)

fo r i in ran ge(8):
p r int(fib (i), end =",")

W ha t w ill be d isplaye d on the scree n w hen you execute the code7

Choose the correct answer

0 1,0 ,1,0 ,1,0 ,1,0 ,

0 A n error message

10 0 .1.1 .2,3 ,5 ,8 ,13 ,

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.

Choose the corre·c t o p t i o n s

im p o rt sys

su m = 0

for i in lra ng e(2 , len (s ys .a rg v))

su m + = flo at(sy s.argv [i l)

pr int ("li" he a ve ra ge fo r {O} is {1 :2 f}". o rm a t( s y s . a rg v [ 1 ]. s u m /( le n (sy s .a rg v )- 2 )) J)

( 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 ..

d ef" p r i nLo n e {it e m l:

T h i s f u n c ti o n f o .-m a ts th e i te·m a n d p .-i n ts it o u t p rii nt (' Th

e i te m p .-o v i d ed i s (:-2f} '_ o .- m a t( i t.e m }}

d ef" p .-i nL t w o ( it!e m }::

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

p rii n ('Th e i te m s a..-e :, ' s t.r{ it e m ))

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 )

n v n n r. •-- n F :ira m n 'l 1


I
Reset
I

You have the following code:

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
-

You have the following code:

def do _something(ba lance ):


balance += 2
return balance • 2

x=4
y= do_so mething(x)

What is th e val ue of y?

Choose the correct answer

0 6

I• 12
I

0 4

0 8

r-;;i ( Previous <(} Next ) ss i of 120 ] Ell


day= input{'Please enter the day of the
week: ') percent_discount= 10

if day == 'M onday':


percenL disco unt= S
elif day == 'Wednesday':
percenL disco unt= 7
elif day == 'Friday':
perce nL discount= 8
elif day == 'Saturd ay':
perce nL discount= 6

What should the user enter as the day value for the percent_discount to be 10?

Choose the correct answe r

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

y = d + b 88 c :::i...-f :x: < - y:


p .,...:i... ..---. --t::: ( • • 'l,J ea m ad ,ei-t::: "" )

" -" "="IIT1 c3I d e,::i...it:


v j y:
print ('we made it') ] -:I-I- - - - - - - - - - - - - - - - - - - - - - -
r;;-i ( Previous Q Nex t ) I90I of 1 20 ] •fM
You have the code shown in the answer area.

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.

Choose the correct options

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 -

Which expression would eva luate to 3?

Choose the correct answer

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.

Choose the correct options

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:

new_val = 4647564755 + 857685795468745.456 0


print(type( new_val))
my_bool evaluatesto 12:

my_bool = False + 5 - True + 35//4


The following will print 'Here is what I have: 4.5':

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.

Choose the correct options

elements = (12,2,8,5,14,6,22,35,18,4)

items= {1:"C", 2:"C++", 3:"Java", 4:"Python", 5:"JavaScript",

6:"Go'1 count = O

for e in elements:

if e in items:

f:ontinue vj
count+= e
. ,, i\

r-;;-i ( Previous <:J Next ) [ 94 1 of 120 ] Il


You have the following code:

a = 1
b=O
try:
x= a / b

You need to identify the spe cific object class name of the exception that will occur_

W h i ch lines of code shou ld you use ?

Choose the cor rect answe r

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))

( Previous (:i Next ) I 95 I of 120 I ., .;; .;

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 .

Choose the correct option s

loan_amount = j55_1 vI
rate= 0

if loan_amount > O and loan amount <= 550

rate = 0 1

elif loan_amount > 550 and loan_amount < 750

rate = 0.2

elif loan_amount >= 750 and loan_amount <1000:

rate = 0.3

elif 1000 <= loan amount <= 2000:

rate = 0.4
( P revi ous 'Cl Next ) [ 96 I of 1 20 1 ifi J
You have the fol lowing code:

measu rements = [23.0 ,.005 6,.023]

You need to pri nt the numbers in the variab le measu rements to be 2 decimal places , resulting in the
foll owi ng:

23.00 , 0.0 1, 0.02

Which statement sh ould you use?

Choose the co rrect answe r

0 print(", ".join(".2f' % fforf in measurements))

0 print(", ".join("%.1f' % f for f in measurements))

•0 print(", ".join("%.2d" % f for f in measurements))


print(", ".join("%.2f' % f for fin measurements))

( Previous Q Next ) [ 97 1 of 120 I i€1Fi


i mport datetime as dt

my_d ates = ["12/24/ 2017 13:22",


"10/11/17 01:49:59",
"12/5 /17",
"11/29/2017"]

d1 = d t.datetime.strptime(my_d ates[0], "%m/%d/%Y $H:$M ")


d2 = dt.datetime.strptime(my_datesj1], "%m/%d/%y %H:%M :%S")
d3 = dt.dateti me.strptime(m y_d ates[2], "%m/%d /%y ")
d4 = d t.datetime.strptime(my _d ates[3], "%m/%d /%Y")

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.

Choose the co rr ect options

Statement Yes No

my_dates[O] D

my_dates[1] D

my_dates[2] D

D
-

( Previous Q Next ) [ 9s I of 120 ] lf:if•


Reset

You have the following variables.

a= 3
b= 2

W hich operat or should you place betw een a and b to get the valu e 9?

Choose the correct answer

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.

Choose the correct options

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.

W h ic h c o d e seg m e nt sh o uld you use ?

C h oo se the correct ans,w e r

i m p ort d a te ti m e
today = datetim e.da te. toda y()

0 p rin t( "{:% A , % B % Y} w as a g reat dla y !".f o r m a t ( t o d la y l)

i m p ort d a te li m e
today = datetim e.date. toda y()

0 p rin t("{:% A , %b % Y} w a s a great day!". fo rm at( to d ay))

im po rt da te li m e
today = datetim e.da te. toda y()

0 p rin t("{:% d , %m %Y} w a s a grea t da y!".f o rm at( toda y))

• im p ort da te li m e
tod ay = datetim e.da te. toda y()

p rin t("{:% d, % b %Y} w a s a g1r e a t dla y! ". fo rm a t(t o d a 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.

W hich comma nd shou ld you use?

Choose the correct answer

le0 python -m pydoc -w Example > Example.html

0
i mport pydoc
import sys
pydoc.help(sys)

0 python -m pydoc sys

r-;;-i ( Previous Q Next ) 2 I of 120 I 4,ju: ■


g ra d e s = (10 0 , 9 8 ,75 , 8 8 , 9 3 , 6 7,78 , 8 5 ,8 0 ,8 21
u Ad d m i s s i ng code here
p r i nt (' L o w es t a nd h i g h e s t grades are %d a nd %d ' % ( g ra de l , g ra de 2})

Yo u n e e d to c o m p l e te t h e ms i s i n g set o f code to di s p la y 67 and 10 0 as t h e l o w e s t a n d h i g h e s t g ra

d e s . H o w s ho u l d you c o m ple t e the code?

Choose tile correct answer

dle f fln d _g ra de s () :

0 return grades[ □],


g ra d e s[ 2 ]
gra d e 1 , g r ad e 2 = find_grades()

• 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()

0 return grades[ □],


g ra d e s[- 1 ]
grade1 = find _ g rade s (}
grade2 = find _ g rade s ()

You have a file named studentfile with the fo llowing co ntent:

Dave,100 ,90,77
Sam ,55,90,80

You need to write code to read al l of th e contents of the file.

W hich code segment shou ld you use?

Choose the co rrect answer

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()

r;;-i ( Previous Q Next )


[ 1 04 I o f 1 2?7 i Ji•
For each of the fol lowing statements, select Yes if the statement is true. Otherwise,
select No.

Choose the correct options

Stat ement Yes No


3 + False evaluates to False □
True + 1 evaluates to 2 □
True and False evaluates to False □
True and true evaluates to True □
Type{False) is <class 'int1> □

-( Previous <:i Next ) [1os i of 120 ] -


You have the code sh own in the answer area.

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.

Choose the correct options

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

languages = l i st(my _li st) languag es.append('Python') print(language s)

[ 11 ] :m y_l i st : C J a va •
1

l a ngu ages : l i st (my_l i st )


What will be the value of lang uag es?
1 a ngu age s . app end ( Pyt hon 1 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
, , , ]

•0 ('C' ' ' 'J' 'a' 'v' 'a' 'Python')


J I!IlI

('C 'J\ ·a·, 'v', ·a·, 'P', 'y', T, 'h',


1
1

, 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.

Which code segment should you use ?

Choose the correct answer

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(' ')

Which of the variables will equal Fal se?

Choose the correct answer

0 X

0 z

IO y
I
M V

( Previous O Next ) [ 109 1 of120 J -


L!!J
. ,_..,_

For each of the following statements , select Yes if the statement is true. Otherwise, select No.

Choose the correct options

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

You need to add a line of code so that 46 wi ll be ass igned to val.

W hic h lin e of code shou l d you use?

Choose the correct answer

0 val = len(myStr)

I• val = myStr.rfind('on')
I
0 val = myStr.find('question')

(v) val = myStr.find('on')

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)

W ha t willl b e p rinted on the sc reen ?

Choose the correct answe r

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')

You have the following code:

import datetime as d t

days = ["Mo n","Tue","Wed","Thu","Fri","Sat","Sun"]

You need to complete the code to print the current weekday from the list days.

Which code should you use?

Choose the co rrect answe r

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 have the code sn ip p et shown in the answer area.

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.

Choo se the correct options

try

l orem = open("lorem _tx t", "r")

Jc ontent = lorem .read() vj


l orem close() except Excep tion:
priin t ("Could not open lorem_tx t!"')

........; ......+ ,1 ...........................+\

( Prev ious * Next )

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']

for w in words: if len(w) -for (,} in words :


print (w
V
if len( w) <=4 :
pri nt( w)

!c 4
rat bat

-
< 4: barn

4:

( Pn []: I 116 I of 1 2 0 I Hill


Yo LI h a v e th e f o l l o w i n g code (line numt:>e r s added for cl ar•i t y ) :

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)

Yo LI run t h e code and inp ut th e v al Lie s 78 , 9 5 , 9 5 , and 96.

W h ic h l i n e c o nt a in s a n e rTo r ?

C trio o s e t hie c o r r e ct. a ns w e r

0 5

0 13

0 7

_LO 1 I
*
2

You have the code sh own in the answe r area:

You need to add code so that the value of total is 10.

W hic h code shou ld you use? To answer, select the appropriate option from the drop-do wn menu.

Choose the correct options

importmath

list_a = 15.6, 6.7, 9.2]


list_b = 16.7, 5.6, 9.4]

set_c = V

to ta l = O set(list_a) & set(list_b)


set(list a) ' sel(list b)
for item i - -

print{total)

( Previous Q Next ) 1 11sI of 1 2 0 l


ii=ii•I
I U U C:JI.C:\.UlC: LI IC: IU II U VV II I \. U U C:.

content= 'Learning Pyt hon is interesting especially when using

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?

Choose the correct answer


- Interesting-

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]

You need to calculate the total of the items in the

list. Which code should you use?

Choose the correct answer

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

You might also like