Microsoft Python Certification Exam (98-381) Practice Tests Set 5

You created the following program to locate a conference room and display room name.
  1. rooms={1:'Left Conference Room',2:'Right Conference Room'}
  2. room=input('Enter the room number:')
  3. if not room in rooms:#Line-3
  4. print('Room does not exist')
  5. else:
  6. print('The room name is:'+rooms[room])

The team reported that the program sometimes produces incorrect results. You need to troubleshoot the program. Why does Line-3 Fail to find the rooms?

Options are :

  • Invalid Syntax
  • Mismatched data type(s)
  • Misnamed variable(s)
  • None of these

Answer :Invalid Syntax

The XYZ Book Company needs a way to determine the cost that a student will pay for renting a Book. The Cost is dependent on the time of the Book is returned. However, there are also special rates on Saturday and Sundays. The Fee Structure is shown in the following list: The cost is $3.00 per night.

If the book is returned after 9 PM, the student will be charged an extra day. If the Book is rented on a Sunday, the student will get 50% off for as long as they keep the book. If the Book is rented on a Saturday, the student will get 30% off for as long as they keep the book.
We need to write the code to meet these requirements.
# XYZ Book Rented Amount Calculator
  1. ontime=input('Was Book returned before 9 pm? y or n:').lower()
  2. days_rented=int(input('How many days was book rented?'))
  3. day_rented=input('What day the Book rented?').capitalize()
  4. cost_per_day=3.00
  5. if ontime == 'n':
  6. days_rented=days_rented+1
  7. if day_rented=='Sunday':
  8. total=(days_rented*cost_per_day)*0.5
  9. elif day_rented=='Saturday':
  10. total=(days_rented*cost_per_day)*0.7
  11. else:
  12. total=(days_rented*cost_per_day)
  13. print('The Cost of Book Rental is:$',total)

If the Book rented on 'Sunday', the number of days Book rented is 5 and Book returned after 9 PM, then what is the result?

Options are :

  • The Cost of Book Rental is:$ 7.0
  • The Cost of Book Rental is:$ 8.0
  • The Cost of Book Rental is:$ 9.0
  • The Cost of Book Rental is:$ 10.0

Answer :The Cost of Book Rental is:$ 9.0

You are developing a Python application for an online game. You need to create a function that meets the following criteria: The function is named update_scoreThe function receives the current score and value. The function adds value to the current score. The function returns the new score. Which of the following is a valid function to fulfill this requirement?


Options are :

  • update_score(score,value): new_score=score+value return new_score
  • def update_score(score,value): new_score=score+value return new_score
  • def update_score(score,value): new_score=score+value pass new_score
  • def update_score(): new_score=score+value return new_score

Answer :def update_score(score,value): new_score=score+value return new_score

The XYZ company is creating a program that allows customers to log the number of miles biked.The program will send messages based on how many miles the customer logs. Consider the following python code:
  1. Line-1:
  2. name=input('Enter Your Name:')
  3. return name
  4. Line-2:
  5. calories=miles*calories_per_mile
  6. return calories
  7. distance=int(input('How many miles did you bike this week:'))
  8. burn_rate= 44
  9. biker=get_name()
  10. calories_burned=calc_calories(distance,burn_rate)
  11. print(biker,", You burned about",calories_burned," calories")

The lines Line-1 and Line-2 should be replaced with:

Options are :

  • Line-1 should be replaced with def get_name():
  • Line-1 should be replaced with def get_name(name):
  • Line-1 should be replaced with def get_name(biker):
  • Line-2 should be replaced with def calc_calories(miles,calories_per_mile):
  • Line-2 should be replaced with def calc_calories(miles,burn_rate):
  • Line-2 should be replaced with def calc_calories():

Answer :Line-1 should be replaced with def get_name(): Line-2 should be replaced with def calc_calories(miles,calories_per_mile):

You work for a company that distributes media for all ages. You are writing a function that assigns a rating based on a user's age. The function must meet the following requirements. Anyone 18 years old or older receives a rating of "A" Anyone 13 or older,but younger than 18, receives a rating of "T" Anyone 12 years old or younger receives a rating of "C" If the age is unknown ,the rating is set to "C" Which of the following code meets above requirements:


Options are :

  • def get_rating(age): if age>=18: rating="A" elif age>=13: rating="T" else: rating="C" return rating
  • def get_rating(age): if age>=18: rating="A" if age>=13: rating="T" else: rating="C" return rating
  • def get_rating(age): if age>18: rating="A" elif age>13: rating="T" else: rating="C" return rating
  • def get_rating(age): if age>=18: rating="A" elif age>=13: rating="T" else: rating="C" pass rating

Answer :def get_rating(age): if age>=18: rating="A" elif age>=13: rating="T" else: rating="C" return rating

Which of the following is False?


Options are :

  • A try statement can have one or more except clauses
  • A try statement can have a finally clause without an except clause
  • A try statement can have a finally clause and an except clause
  • A try statement can have one or more finally clauses

Answer :A try statement can have one or more finally clauses

Consider the following code.
  1. import os
  2. def get_data(filename,mode):
  3. if os.path.isfile(filename):
  4. with open(filename,'r') as file:
  5. return file.readline()
  6. else:
  7. return None

Which of the following are valid about this code?

Options are :

  • This function returns the first line of the file if it is available
  • This function returns None if the file does not exist
  • This function returns total data present in the file
  • This function returns the last line of the file

Answer :This function returns the first line of the file if it is available This function returns None if the file does not exist

We are writing a Python program for the following requirements: Each line of the file must be read and printed if the blank line encountered, it must be ignored when all lines have been read, the file must be closed. Consider the code:
  1. inventory=open('inventory.txt','r')
  2. eof=False
  3. while eof == False:
  4. line=inventory.readline()
  5. if XXX:
  6. if YYY:
  7. print(line,end='')
  8. else:
  9. print('End of file')
  10. eof=True
  11. inventory.close()

Which of the following changes are required to perform to meet the requirements

Options are :

  • XXX should be replaced with line != '' YYY should be replaced with line!= '\n'
  • XXX should be replaced with the line!= '\n' YYY should be replaced with the line != ''
  • XXX should be replaced with the line != '' YYY should be replaced with the line != ''
  • XXX should be replaced with the line!= '\n' YYY should be replaced with the line!= '\n'

Answer :XXX should be replaced with the line != '' YYY should be replaced with the line!= '\n'

You develop a python application for your school. You need to read and write data to a text file. If the file does not exist, it must be created. If the file has the content, the content must be removed. Which code we have to use?


Options are :

  • open('abc.txt','r')
  • open('abc.txt','r+')
  • open('abc.txt','w+')
  • open('abc.txt','w')

Answer :open('abc.txt','w+')

We are creating a function that reads a data file and prints each line of thatfile. Consider the following code:
  1. import os
  2. def read_file(filename):
  3. line=None
  4. if os.path.isfile(file_name):
  5. data=open(filename,'r')
  6. while line != '':
  7. line=data.readline()
  8. print(line)

The code attempts to read the file even if the file does not exist.You need to correct the code. which lines having identation problems?

Options are :

  • First 3 Lines inside the function
  • Last 3 Lines inside a function
  • Last 2 Lines inside the function
  • There is no indentation problem

Answer :Last 3 Lines inside the function

Consider the code:
  1. import sys
  2. try:
  3. file_in=open('file1.txt','r')
  4. file_out=open('file2.txt','w+')
  5. except IOError:
  6. print('cannot open',file_name)
  7. else:
  8. i=1
  9. for line in file_in:
  10. print(line.rstrip())
  11. file_out.write(str(i)+":"+line)
  12. i=i+1
  13. file_in.close()
  14. file_out.close()

Assume that in.txt file is available, but out.txt file does not exist. Which of the following is true about this code?

Options are :

  • This program will copy data from in.txt to out.txt
  • The code runs but generates a logical error
  • The code will generate a runtime error
  • The code will generate a syntax error

Answer :This program will copy data from in.txt to out.txt

You are creating a function that manipulates a number. The function has the following requirements: A float passed to the function must take the absolute value of the float. Any decimal points after the integer must be removed. Which two math functions should be used?


Options are :

  • math.frexp(x)
  • math.floor(x)
  • math.fabs(x)
  • math.fmod(x)
  • math.ceil(x)

Answer :math.floor(x) math.fabs(x)

You are writing an application that uses the pow function. The program must referencethe function using the name power.Which of the following import statement required to use?


Options are :

  • import math.pow as power
  • import pow from math as power
  • from math import pow as power
  • from math.pow as power

Answer :from math import pow as power

You are writing code that generates a random integer with a minimum value of 18 and a maximum value of 32. Which of the following 2 functions did we require to use?


Options are :

  • random.randint(18,33)
  • random.randint(18,32)
  • random.randrange(18,33,1)
  • random.randrange(5,11,1)

Answer :random.randint(18,32) random.randrange(18,33,1)

Consider the lists:
  1. n=[10,20,30,40,50]
  2. a=['a','b','c','d','e']
  3. print( n is a)
  4. print( n == a)
  5. n = a
  6. print( n is a)
  7. print( n == a)

What is the result?

Options are :

  • False False True True
  • False True False True
  • True False True False
  • False True True True

Answer :False False True True

Consider the code
  1. a=15
  2. b=5
  3. print(a/b)

What is the result ?

Options are :

  • 3
  • 3.0
  • 0
  • 0.0

Answer :3.0

You are writing a python program that evaluates an arithmetic expression. The expression is described as y is equals x multiplied by negative one, then raised to the second power, where x is the value which will be input and y is the result.x = eval(input('Enter a number for the expression:'))Which of the following is a valid expression for the given requirement?


Options are :

  • y = (x-)**2
  • y = -(x)**2
  • y = (-x)**2
  • y = (x)**-2

Answer :y = (-x)**2

Consider the following expressionresult=(2*(3+4)**2-(3**3)*3)What is result value?


Options are :

  • 17
  • 16
  • 18
  • 19

Answer :17

Consider the following code segments
  1. #Code Segment-1
  2. a1='10'
  3. b1=3
  4. c1=a1*b1
  5. #Code Segment-2
  6. a2=10
  7. b2=3
  8. c2=a2/b2
  9. #Code Segment-3
  10. a3=2.6
  11. b3=1
  12. c3=a3/b3

After executing Code Segments 1,2 and 3 the result types of c1,c2 and c3 are:

Options are :

  • c1 is of str type,c2 is of int type,c3 is of float type
  • c1 is of str type,c2 is of float type,c3 is of float type
  • c1 is of str type,c2 is of int type,c3 is of int type
  • c1 is of str type,c2 is of str type,c3 is of str type

Answer :c1 is of str type,c2 is of float type,c3 is of float type

Which of the following is a valid python operator precedence order?


Options are :

  • Parenthesis Exponents Unary Positive, Negative and Not Addition and Subtraction Multiplication and Division And
  • Exponents Parenthesis Unary Positive, Negative and Not Multiplication and Division Addition and Subtraction And
  • Exponents Unary Positive, Negative and Not Multiplication and Division Addition and Subtraction And Parenthesis
  • Parenthesis Exponents Unary Positive, Negative and Not Multiplication and Division Addition and Subtraction And

Answer :Parenthesis Exponents Unary Positive, Negative and Not Multiplication and Division Addition and Subtraction And

You want to add comments to your code so that other team members can understand it. What should you do?


Options are :

  • Place the comments after the #sign on any line
  • Place the comments after the last line of the code separated by a blank line
  • Place the comments before the first line of code separated by a blank line
  • Place the comments inside parentheses anywhere

Answer :Place the comments after the #sign on any line

We are creating a function to calculate the addition of two numbers by using python. We have to ensure that the function is documented with comments. Consider the code(Line numbers included for reference):
  1. 01 # The calc_square function calculates exponents
  2. 02 # x is a first number
  3. 03 # y is a second number
  4. 04 # The value of x and y is added and returned
  5. 05 def calc_power(x, y):
  6. 06 comment="#Return the value"
  7. 07 return x + y # Adding x and y

Which of the following statements are true?

Options are :

  • Lines 01 through 04 will be ignored for syntax checking
  • The hash sign(#) is optional for lines 01 and 03.
  • The String in line 06 will be interpreted as a comment

Answer :Lines 01 through 04 will be ignored for syntax checking

Consider the expression:result=a-b*c+dWhich of the following are valid?


Options are :

  • First, b*c will be evaluated followed by subtraction and addition
  • First, b*c will be evaluated followed by addition and subtraction
  • First, a-b will be evaluated followed by multiplication and addition
  • The above expression is equivalent to a-(b*c)+d

Answer :First b*c will be evaluated followed by subtraction and addition The above expression is equivalent to a-(b*c)+d

Which of the following is True about else block?


Options are :

  • else block will be executed if there is no exception in try block
  • Without writing except block we cannot write else block
  • For the same try, we can write at most one else block
  • All the above

Answer :All the above

Consider the code
  1. try:
  2. print('try')
  3. print(10/0)
  4. except:
  5. print('except')
  6. else:
  7. print('else')
  8. finally:
  9. print('finally')

What is the result?

Options are :

  • try except else finally
  • try else finally
  • try except finally
  • try finally

Answer :try except finally

x='TEXT'which line of the code will assign 'TT' to the output?


Options are :

  • output=x[0]+x[-1]
  • output=x[1]+x[1]
  • output=x[0]+x[2]
  • output=x[1]+x[4]

Answer :output=x[0]+x[-1]

Consider the code :
  1. from sys import argv
  2. sum=0
  3. for i in range(2,len(argv)):
  4. sum += float(argv[i])
  5. print("The Average for {0} is {1:.2f}".format(argv[1],sum/(len(argv)-2)))

Which of the following command invocations will generate the output:The Average for Test is 20.00

Options are :

  • py test.py Test 10
  • py test.py Test 10 20 30
  • py test.py Test 10 20
  • py test.py 20

Answer :py test.py Test 10 20 30

Consider the Python code:
  1. a=['a','b','c','d']
  2. for i in a:
  3. a.append(i.upper())
  4. print(a)

What is the result?

Options are :

  • ['a','b','c','d']
  • ['A','B','C','D']
  • MemoryError thrown at runtime
  • SyntaxError

Answer :MemoryError thrown at runtime

Consider the Python Code
  1. l1=['sunny','bunny','chinny','vinny']
  2. l2=['sunny','bunny','chinny','vinny']
  3. print(l1 is l2)
  4. print(l1 == l2)
  5. l1=l2
  6. print(l1 is l2)
  7. print(l1 == l2)

What is the result?

Options are :

  • False True False True
  • False False True True
  • False True True False
  • False True True True

Answer :False True True True

Consider the python code:
  1. result=str(bool(1) + float(10)/float(2))
  2. print(result)

What is the output?

Options are :

  • 6.0
  • 6
  • SyntaxError
  • TypeError

Answer :6.0

If the user enters 12345 as input, Which of the following code will print 12346 to the console?


Options are :

  • count=input('Enter count value:') print(count+1)
  • count=int(input('Enter count value:')) print(count+1)
  • count=eval(input('Enter count value:')) print(count+1)
  • count=input('Enter count value:') print(int(count)+1)

Answer :count=int(input('Enter count value:')) print(count+1) count=eval(input('Enter count value:')) print(count+1) count=input('Enter count value:') print(int(count)+1)

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions