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

##### Consider the code:
1. s='Python is easy'
2. s1=s[6:-4]
3. #Line-1
4. print(len(s2))

To print 2 as output, which code we have to insert at Line-1

Options are :

• s2 = s1.lrstrip()
• s2 = s1.lstrip()
• s2 = s1.rstrip()
• s2 = s1.strip()

Options are :

• 8
• 9
• 7
• 6

##### Consider the code
1. s='AB CD'
2. list=list(s)
3. list.append('EF')
4. print(list)

What is the result?

Options are :

• ['A','B','C','D','E','F']
• {'A','B',' ','C','D','EF'}
• ['A','B','C','D','EF']
• ['A','B',' ','C','D','EF']
• ('A','B',' ','C','D','EF')

##### You are writing a Python program to read two int values from the keyboard and print the sum.
1. x=input('Enter First Number:')
2. y=input('Enter Second Number:')
3. #Line-1

Which of the following code we have to write at Line-1 to print the sum of given numbers?

Options are :

• print('The Result:'+str(int(x+y)))
• print('The Result:'+(int(x)+int(y)))
• print('The Result:'+str(int(x)+int(y)))
• print('The Result:'+(int(x+y)))

##### Consider the code
1. a=2
2. a += 1
3. # Line-1

To make a value as 9,which expression required to place at Line-1

Options are :

• a**=2
• a*=2
• a+=2
• a-=2

##### Consider the following code segments:# Code Segment-1
1. a1='10'
2. b1=3
3. c1=a1*b1

# Code Segment-2

1. a2=10
2. b2=3
3. c2=a2/b2

# Code Segment-3

1. a3=2.6
2. b3=1
3. 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 int 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 float 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

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

##### Consider the following lists:
1. n1=[10,20,30,40,50]
2. n2=[10,20,30,40,50]
3. print(n1 is n2)
4. print(n1 == n2)
5. n1=n2
6. print(n1 is n2)
7. print(n1 == n2)

What is the result?

Options are :

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

##### Consider the following code
1. x= 'Larry'
2. y= 'Larry'
3. result=condition
4. print(result)

For which of the following condition True will be printed to the console?

Options are :

• x is not y
• x < y
• x != y
• x is y

Options are :

• list
• list
• list
• list[-1]

##### Consider the following lists:
1. n1=[10,20,30,40,50]
2. n2=[10,20,30,40,50]
3. print(n1 is n2)
4. print(n1 == n2)

What is the output?

Options are :

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

##### You have the following code:
1. a=bool([False])
2. b=bool(3)
3. c=bool("")
4. d=bool(' ')

Which of the variables will represent False:

Options are :

• d
• a
• c
• b

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

Options are :

• Parenthesis Exponents Unary Positive, Negative and Not Addition and Subtraction Multiplication and Division And
• Parenthesis Exponents 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
• Exponents Parenthesis 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 are developing a python application for your company. A list named employees contains 500 employee names. In which cases, we will get IndexError while accessing employee names?

Options are :

• None of the above
• employees[-1]
• employees
• employees

Options are :

• 6.0
• 7.0
• 5.0
• 5

##### Consider the expression:`result=a-b*c+d`Which 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
• The above expression is equivalent to a-(b*c)+d
• First, a-b will be evaluated followed by multiplication and addition

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

##### Consider the Python code:
1. a=5
2. b=10
3. c=2
4. d=True
5. x=a+b*c
6. y=a+b/d
7. if(condition):
8. print('Valid')
9. else:
10. print('invalid')

To print 'Valid' to the console, which condition we have to take for if statement?

Options are :

• x==y
• x
• x>y
• x<=y

##### Consider the python code
1. a=1
2. b=3
3. c=5
4. d=7

In Which of the following cases, the result value is 0?

Options are :

• result = a-b//d
• result = a+b*2
• result = a**d-1
• result = a%b-1

Answer :result = a**d-1 result = a%b-1

##### You are writing a Python program. You required to handle data types properly. Consider the code segment:
1. a=10+20
2. b='10'+'20'
3. c='10'*3

Identify the types of a,b and c?

Options are :

• a is of int type,b and c are invalid declarations
• a is of int type,b is of str type, and c is of int type
• a is of int type,b is of int type, and c is of int type
• a is of int type,b is of str type, and c is of str type

Answer :a is of int type,b is of str type, and c is of str type

##### 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[0:-3]
• employees[1:-2]
• employees[1:-3]
• employees[:-3]
• employees[0:-2]

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

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

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

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

What is the Result?

Options are :

• try else except finally
• try else finally
• try except finally
• SyntaxError: invalid syntax

##### Consider the code:
1. f=open('abc.txt')
3. f.close()

We required to add exception handling code to handle FileNotFoundError.Which of the following is an appropriate code for this requirement?

Options are :

• f=None try: f=open('abc.txt') except FileNotFoundError: print('Fild does not exist') else: print(f.read()) finally: if f != None: f.close()
• f=None try: f=open('abc.txt') except FileNotFoundException: print('Fild does not exist') else: print(f.read()) finally: if f != None: f.close()
• f=None try: f=open('abc.txt') else: print(f.read()) except FileNotFoundException: print('Fild does not exist') finally: if f != None: f.close()
• None of these

Answer :f=None try: f=open('abc.txt') except FileNotFoundError: print('Fild does not exist') else: print(f.read()) finally: if f != None: f.close()

##### Consider the code
1. a=10
2. b=20
3. c='30'
4. result=a+b+c

What is the result?

Options are :

• 102030
• 3030
• TypeError
• ArithmeticError

##### Consider the code:
1. prices=[30.5,'40.5',10.5]
2. total=0
3. for price in prices:
4. total += price
5. print(total)
6. While executing this code we are getting the following error
7. Traceback (most recent call last):
8. File "test.py", line 4, in <module>
9. total += price
10. TypeError: unsupported operand type(s) for +=: 'float' and 'str'

Which of the following code should be used to fix this error?

Options are :

• total += str(price)
• total += int(price)
• total += float(price)
• total = total+price

##### Consider the code:
1. prices=[10,'20',30,'40']
2. total=0
3. for price in prices:
4. total +=price
5. print(total)
6. While executing this code we are getting the following error
7. Traceback (most recent call last):
8. File "test.py", line 4, in <module>
9. total +=price
10. TypeError: unsupported operand type(s) for +=: 'int' and 'str'total += str(price)

By using which of the following code segments we can fix this problem(Choose Two)?

Options are :

• total += str(price)
• total += int(price)
• total += float(price)
• total = total+price

Answer :total += int(price) total += float(price)

##### Consider the code
1. courses={1:'Java',2:'Scala',3:'Python'}
2. for i in range(1,5):
3. print(courses[i])
4. While executing this code we are getting the following error
5. Traceback (most recent call last):
6. File "test.py", line 3, in <module>
7. print(courses[i])
8. KeyError: 4

By using which of the following code segments we can fix this problem ?

Options are :

• courses={1:'Java',2:'Scala',3:'Python'} for i in range(1,5): if i in courses: print(courses[i])
• courses={1:'Java',2:'Scala',3:'Python'} for i in courses: print(courses[i])
• courses={1:'Java',2:'Scala',3:'Python'} for i in range(1,4): print(courses[i])
• All of these

##### Consider the code
1. def area(b,w):
2. return B*w
3. print(area(10,20))

What is the result?

Options are :

• NameError will be raised at runtime
• AttributeError will be raised at runtime
• IdentationError will be raised at runtime
• 200

Answer :NameError will be raised at runtime

##### Consider the following code:
1. def get_score(total=0,valid=0):
2. result=int(valid)/int(total)
3. return result

For which of the function calls we will get Error?

Options are :

• score=get_score(40,4)
• score=get_score('40','4')
• score=get_score(40)
• score=get_score(0,10)

##### Consider the code:
1. data=[]
2. def get_data():
3. for i in range(1,5):
4. marks=input('Enter Marks:')
5. data.append(marks)
6. def get_avg():
7. sum=0
8. for mark in data:
9. sum += mark
10. return sum/len(data)
11. get_data()
12. print(get_avg())

For the input: 10,20,30,40 what is the result?

Options are :

• 25
• 25.0
• NameError is thrown at runtime
• TypeError is thrown at runtime

Answer :TypeError is thrown at runtime

##### Consider the code:
1. a=10
2. b=0
3. try:
4. print(a/b)

Which of the following except block print the name of exception which is raised,i.e exception class name?

Options are :

• except ZeroDivisionError as e: print('Exception Type:',e.__class__.__name__)
• except ZeroDivisionError as e: print('Exception Type:',type(e).__name__)
• except ZeroDivisionError as e: print('Exception Type:',e)
• All of these

Answer :except ZeroDivisionError as e: print('Exception Type:',e.__class__.__name__) except ZeroDivisionError as e: print('Exception Type:',type(e).__name__)

Which of the following is a valid way of creating our own custom exception?

Options are :

• class MyException: pass
• class MyException(): pass
• class MyException(Exception): pass
• It is not possible to define custom exceptions in python

##### Consider the code
1. x=int(input('Enter First Number:'))
2. y=int(input('Enter Second Number:'))
3. try:
4. print(x/y)

Which of the following is valid except block that handles both ZeroDivisionError and ValueError

Options are :

• except(ZeroDivisionError,ValueError) from e: print(e)
• except(ZeroDivisionError,ValueError) as e: print(e)
• except(ZeroDivisionError | ValueError) as e: print(e)
• except(ZeroDivisionError, ValueError as e): print(e)

##### 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:
6. else:
7. return None

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

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

##### We are creating a function that reads a data file and prints each line of that file. Consider the following code:
1. import os
3. line=None
4. if os.path.isfile(file):
5. data=open(file,'r')
6. while line != '':
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 indentation problems?

Options are :

• First 3 Lines inside the function
• Last 3 Lines inside the 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('in.txt','r')
4. file_out=open('out.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 generates a runtime error
• The code will generates a syntax error

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

##### Consider the file abc.txt has the following content:Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.We have to write python code to read total data and print to the console.
1. try:
2. f=open('abc.txt','r')
3. //Line-1
4. except:
5. print('Unable to open the file')
6. print(data)

Which code should be inserted at Line-1 to meet the given requirement ?

Options are :

##### To write 'Python Certificaton' to abc.txt file, which of the following is valid code?

Options are :

• f=open('abc.txt','b') f.write('Python Certificaton') f.close()
• f=open('abc.txt','r') f.write('Python Certificaton') f.close()
• f=open('abc.txt') f.write('Python Certificaton') f.close()
• f=open('abc.txt','w') f.write('Python Certificaton') f.close()

##### Consider the data present in the file: abc.txt
1. Cloudera,50,60,70,80,90
2. MICROSOFT,10,20,30,40,50

Which of the following is valid code to read total data from the file?

Options are :

• with open('abc.txt') as f: data=f.read()
• with open('abc.txt','w') as f: data=f.read()
• with open('abc.txt') as f: data=f.readline()

##### Assume that we are writing python code for some voting application.You need to open the file voters_list.txt and add new voters info and print total data to the console?
1. with open('voters_list.txt','a+') as f:
2. f.write('New voters info')
3. #Line-1
5. print(data)

Which Line should be inserted at Line-1 ?

Options are :

• f.seek(0)
• f.flush()
• f.begin()
• f.close()

##### You are creating a function that manipulates a number.The function has the following requirements:A float passed to the functionThe function must take absolute value of the floatAny 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)