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

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

##### Consider the following Python Code:
1. def count_letter(letter,word_list):
2. count=0
3. for word in word_list:
4. if letter in word:
5. count +=1
6. return count
7. word_list=['apple','pears','orange','mango']
8. letter=input('Enter some alphabet symbol:')
9. letter_count=count_letter(letter,word_list)
10. print(letter_count)

If the user provides input 'a' then what is the result?

Options are :

• 1
• 2
• 3
• 4

##### Consider the code:
1. startmsg = "hello"
2. endmsg = ""
3. for i in range(0,len(startmsg)):
4. endmsg = startmsg[i] + endmsg
5. print(endmsg)

What is the result?

Options are :

• olleh
• hello
• IndexError
• hlelo

##### Consider the code:
1. x = [13,4,17,10]
2. w = x[1:]
3. u = x[1:]
4. y = x
5. u = 50
6. y = 40
7. print(x)

What is the result?

Options are :

• [13, 40, 17, 10]
• [50, 40, 10]
• [13,4,17,10]
• [50,40,17,10]

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

##### We are creating a function to calculate the power of a number 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_power function calculates exponents
2. 02 # x is the base
3. 03 # y is the exponent
4. 04 # The value of x raided to the y power is returned
5. 05 def calc_power(x, y):
6. 06 comment="#Return the value"
7. 07 return x**y #raise x to the power 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

##### You are writing a python script to convert student marks into a grade. The grades are defined as follows:
1. 90 through 100 --> A grade
2. 80 through 89 --> B grade
3. 70 through 79 --> C grade
4. 65 through 69 --> D grade
5. 0 through 64 --> E grade

And the developed application is :

2. marks=int(input('Enter Student Marks:'))
3. if marks >=90: #Line-1
5. elif marks>=80: #Line-2
7. elif marks>=70: #Line-3
9. elif marks>=65:
11. else:

Which of the following changes should be performed to fulfill the requirement?

Options are :

• Line-1 should be replaced with if marks <= 90:
• Line-2 should be replaced with if marks>=80 and marks <= 90 :
• Line-3 should be replaced with if marks>=70 and marks <= 80 :
• No Changes are required.

##### You are developing a Python application for an online product distribution company. You need the program to iterate through a list of products and escape when a target product ID is found. Which of the following code can fulfill our requirement

Options are :

• productIdList=[0,1,2,3,4,5,6,7,8,9] index=0 while index
• productIdList=[0,1,2,3,4,5,6,7,8,9] index=1 while index
• productIdList=[0,1,2,3,4,5,6,7,8,9] index=0 while index
• productIdList=[0,1,2,3,4,5,6,7,8,9] index=0 while index

##### You are writing a python program that displays all prime numbers from 2 to 200. Which of the following is the proper code to fulfill our requirement?

Options are :

• p=2 while p<=200: is_prime=True for i in range(2,p): if p % i == 0: is_prime=False break if is_prime==True: print(p) p=p+1
• p=2 is_prime=True while p<=200: for i in range(2,p): if p % i == 0: is_prime=False break if is_prime==True: print(p) p=p+1
• p=2 while p<=200: is_prime=True for i in range(2,p): if p % i == 0: is_prime=False break if is_prime==False: print(p) p=p+1
• p=2 while p<=200: is_prime=True for i in range(2,p): if p % i == 0: is_prime=False break if is_prime==True: print(p)

Answer :p=2 while p<=200: is_prime=True for i in range(2,p): if p % i == 0: is_prime=False break if is_prime==True: print(p) p=p+1

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

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

Options are :

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

##### 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.
1. # XYZ Book Rented Amount Calculator
2. ontime=input('Was Book returned before 9 pm? y or n:').lower()
3. days_rented=int(input('How many days was book rented?'))
4. day_rented=input('What day the Book rented?').capitalize()
5. cost_per_day=3.00
6. if ontime == 'n':
7. days_rented=days_rented+1
8. if day_rented=='Sunday':
9. total=(days_rented*cost_per_day)*0.5
10. elif day_rented=='Saturday':
11. total=(days_rented*cost_per_day)*0.7
12. else:
13. total=(days_rented*cost_per_day)
14. 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 the result is?

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

##### We are developing one school automation application. If the student marks between 80 and 100, then we have to offer 'A' grade. Which code block we have to use?

Options are :

• if marks>=80 and marks>=100: grade='A'
• if marks>=80 or marks<=100: grade='A'

##### Consider the code
1. import random
2. print(random.sample(range(10), 7))

Which of the following is valid?

Options are :

• It will print a list of 10 unique random numbers from 0 to 6
• It will print a list of 7 unique random numbers from 0 to 9
• It will print a list of 7 unique random numbers from 0 to 10
• It will print a list of 7 unique random numbers from 1 to 10

Answer :It will print a list of 7 unique random numbers from 0 to 9

##### Consider the following python code:
1. weight=62.4
2. zip='80098'
3. value=+23E4

The types of weight, zip, and value variables, respectively:

Options are :

• float, str, str
• int, str, float
• double, str, float
• float, str, float

##### Consider the code:
1. start=input('How old were you at the time of joining?')
2. end=input('How old are you today?')

Which of the following code is valid to print Congratulations message?

Options are :

• print('Congratulations on '+ (int(end)-int(start))+' Years of Service!')
• print('Congratulations on '+ str(int(end)-int(start))+' Years of Service!')
• print('Congratulations on '+ int(end-start)+' Years of Service!')
• print('Congratulations on '+ str(end-start)+' Years of Service!')

Answer :print('Congratulations on '+ str(int(end)-int(start))+' Years of Service!')

##### You are developing a python application for your company.A list named employees contains 600 employee names,the last 3 being company management.You need to slice employees to display all employees excluding management.Which two code segments we should use?

Options are :

• employees[1:-2]
• employees[:-3]
• employees[1:-3]
• employees[0:-2]
• employees[0:-3]

##### You are interning for XYZ Cars Company. You have to create a function that calculates the average velocity of the vehicle on a 2640 foot(1/2 mile)track.
1. Consider the python code
2. distance=xxx(input('Enter the distance travelled in feet:')) #Line-1
3. distance_miles=distance/5280
4. time=yyy(input('Enter the time elapsed in seconds:')) #Line-2
5. time_hours=time/3600
6. velocity=distance_miles/time_hours
7. print('The average Velocity:',velocity,'miles/hour')

To generate the most precise output, which modifications should be done at Line-1 and at Line-2.

Options are :

• xxx should be replaced with float and yyy should be replaced with float
• xxx should be replaced with float and yyy should be replaced with int
• xxx should be replaced with int and yyy should be replaced with float
• xxx should be replaced with int and yyy should be replaced with int

Answer :xxx should be replaced with float and yyy should be replaced with float

##### You develop a Python application for your company. You required to accept input from the user and print that information to the user screen. Consider the code:
2. #Line-1
3. print(fullname)

At Line-1, which code we have to write?

Options are :

• fullname=input
• input('fullname')
• input(fullname)
• fullname=input()

##### The XYZ Company has hired you as an intern on the coding team that creates an e-commerce application. You must write a script that asks the user for a value. The value must be used as a whole number in a calculation, even if the user enters a decimal value. Which of the following meets this requirement?

Options are :

• items=input('How many items you required?')
• items=float(input('How many items you required?'))
• items=str(input('How many items you required?'))
• items=int(float(input('How many items you required?')))

Answer :items=int(float(input('How many items you required?')))

##### The XYZ organics company needs a simple program that their call center will use to enter survey data for a new coffee variety. The program must accept input and return the average rating based on a five-star scale. The output must be rounded to two decimal places. Consider the code:
1. sum=count=done=0
2. average=0.0
3. while(done != -1):
4. rating=float(input('Enter Next Rating(1-5),-1 for done'))
5. if rating == -1:
6. break
7. sum+=rating
8. count+=1
9. average=float(sum/count)
10. #Line-1

Which of the following print() statement should be placed at Line-1 to meet the requirement?

Options are :

• print('The average star rating for the new coffee is:{:.2f}'.format(average))
• print('The average star rating for the new coffee is:{:.2d}'.format(average))
• print('The average star rating for the new coffee is:{:2f}'.format(average))
• print('The average star rating for the new coffee is:{:2.2d}'.format(average))

Answer :print('The average star rating for the new coffee is:{:.2f}'.format(average))

##### We are developing a sports application. Our program should allow players to enter their name and score. The program will print player name and his average score. Output must meet the following requirements:The user name must be left aligned. If the user name is fewer than 20 characters ,additional space must be added to the right. The average score must be 3 places to the left of the decimal point and one place to the right of the decimal point ( like YYY.Y). Consider the code:
2. score=0
3. count=0
4. sum=0
5. while(score != -1):
6. score=int(input('Enter your scores: (-1 to end)'))
7. if score==-1:
8. break
9. sum+=score
10. count+=1
11. average_score=sum/count
12. #Line-1

Which print statement we have to take at Line-1 to meet requirements.

Options are :

• print('%-20s,Your average score is: %4.1f' %(name,average_score))
• print('%-20f,Your average score is: %4.1f' %(name,average_score))
• print('%-20s,Your average score is: %1.4f' %(name,average_score))
• print('%-20s,Your average score is: %4.1s' %(name,average_score))

##### Consider the following code:
1. n=[0,1,2,3,4,5,6,7,8,9]
2. i=0
3. while (i<10)#Line-1
4. print(n[i])
5. if n(i) = 6#Line-2
6. break
7. else:
8. i += 1

To print 0 to 6,which changes we have to perform in the above code?

Options are :

• Line-1 should be replaced with while(i<10):
• Line-2 should be replaced with if n[i]==6:
• Line-2 should be replaced with if n[i]=6:
• Line-1 should be replaced with while(i>0):

Answer :Line-1 should be replaced with while(i<10): Line-2 should be replaced with if n[i]==6:

##### You are writing a Python program to validate employee numbers. The employee number must have the format dd-ddd-dddd and consists of only numbers and dashes. The program must print True if the format is correct, otherwise print False.
2. parts=empno.split('-')
3. valid=False
4. if len(parts) == 3:
5. if len(parts)==2 and len(parts)==3 and len(parts)==4:
6. if parts.isdigit() and parts.isdigit() and parts.isdigit():
7. valid=True
8. print(valid)

Options are :

• It will throw an error because of the misuse of split() method
• It will throw an error because of the misuse of isDigit() method
• There is no error, but it won't fulfill our requirements.
• No changes are required for this code, and it can fulfill the requirement.

Answer :No changes are required for this code, and it can fulfill the requirement.

##### You are coding a math utility by using python. You are writing a function to compute rootsThe function must meet the following requirements If r is non-negative, return r**(1/s) If r is negative and even, return "Result is an imaginary number" if r is negative and odd, return -(-r)**(1/s)Which of the following root function should be used?

Options are :

• def root(r,s): if r>=0: result=r**(1/s) elif r%2 == 0: result="Result is an imaginary number" else: result=-(-r)**(1/s) return result
• def root(r,s): if r>=0: result=r**(1/s) elif r%2 != 0: result="Result is an imaginary number" else: result=-(-r)**(1/s) return result
• def root(r,s): if r>=0: result=r**(1/s) if r%2 == 0: result="Result is an imaginary number" else: result=-(-r)**(1/s) return result
• def root(r,s): if r>=0: result=r**(1/s) elif r%2 == 0: result=-(-r)**(1/s) else: result="Result is an imaginary number" return result

Answer :def root(r,s): if r>=0: result=r**(1/s) elif r%2 == 0: result="Result is an imaginary number" else: result=-(-r)**(1/s) return result

##### You are writing a python script to convert student marks into grade. The grades are defined as follows:
1. 90 through 100 --> A grade
2. 80 through 89 --> B grade
3. 70 through 79 --> C grade
4. 65 through 69 --> D grade
5. 0 through 64 --> E grade

And developed application is :

2. marks=int(input('Enter Student Marks:'))
3. if marks >=90: #Line-1
5. elif marks>=80: #Line-2
7. elif marks>=70: #Line-3
9. elif marks>=65:
11. else:

Which of the following changes should be performed to fulfill the requirement?

Options are :

• Line-1 should be replaced with if marks <= 90:
• Line-2 should be replaced with if marks>=80 and marks <= 90 :
• Line-3 should be replaced with if marks>=70 and marks <= 80 :
• No Changes are required.

##### You are developing a Python application for an online product distribution company. You need the program to iterate through a list of products and escape when a target product ID is found. Which of the following code can fulfill our requirement

Options are :

• products=[0,1,2,3,4,5,6,7,8,9] index=0 while index
• products=[0,1,2,3,4,5,6,7,8,9] index=1 while index
• products=[0,1,2,3,4,5,6,7,8,9] index=0 while index
• products=[0,1,2,3,4,5,6,7,8,9] index=0 while index

##### You are writing a python program that displays all prime numbers from 2 to 200. Which of the following is the proper code to fulfill our requirement?

Options are :

• p=2 while p<=200: is_prime=True for i in range(2,p): if p % i == 0: is_prime=False break if is_prime==True: print(p) p=p+1
• p=2 is_prime=True while p<=200: for i in range(2,p): if p % i == 0: is_prime=False break if is_prime==True: print(p) p=p+1
• p=2 while p<=200: is_prime=True for i in range(2,p): if p % i == 0: is_prime=False break if is_prime==False: print(p) p=p+1
• p=2 while p<=200: is_prime=True for i in range(2,p): if p % i == 0: is_prime=False break if is_prime==True: print(p)

Answer :p=2 while p<=200: is_prime=True for i in range(2,p): if p % i == 0: is_prime=False break if is_prime==True: print(p) p=p+1

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions