0% found this document useful (0 votes)
60 views30 pages

Dumps

The document contains a series of questions and answers related to Python programming concepts, including code snippets, syntax, and expected outputs. It covers topics such as list slicing, random number generation, user input, exception handling, file operations, and function definitions. Each question provides multiple-choice answers or true/false statements to assess understanding of Python programming principles.

Uploaded by

irfankhan304701
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views30 pages

Dumps

The document contains a series of questions and answers related to Python programming concepts, including code snippets, syntax, and expected outputs. It covers topics such as list slicing, random number generation, user input, exception handling, file operations, and function definitions. Each question provides multiple-choice answers or true/false statements to assess understanding of Python programming principles.

Uploaded by

irfankhan304701
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Dumps for International Certification in Python

1. The Script.py file contains the following code:


import sys
print(sys.argv[2])

You run the following command:


python Script.py Cheese Bacon Bread. What is the output of the command?
A. Cheese
B. Script.py
C. Bread
D. Bacon

2. You develop a Python application for your company.


A list named employees contains 200 employee names, the last five being company management. You need
to slice the list to display all employees excluding management.
Which two code segments should you use? Each correct answer presents a complete solution. (Choose 2.)
A. employees [1-5]
B. employees [0:-5]
C. employees [0-4]
D. employees [1-4]
E. employees [1-5]

3. You are writing code to generate a random integer with a minimum value of 5 and a maximum value of 11.
Which two functions should you use? Each correct answer presents a complete solution. (Choose 2.)
A. random.randrange (5, 11, 1)
B.random.randint(5, 12)
C. random.randint(5, 11)
D. random.randrange (5, 12, 1)
4. You are developing a Python application for your company.
You need to accept input from the user and print that information to the user's screen.
You start with the following code. Line numbers are included for reference only.
01 print("What is your name?")
02
03 print(name)

Which code should you write at line 02?


A. input(name)
B. name= input()
C.name = input
D. input("name")

5. For each statement about try statements, select True or False.


A try statement can have one or more except clauses. (True)
A try statement can have a finally clause without an except clause. (True)
A try statement can have a finally clause and an except clause. (True)
A try statement can have one or more finally clauses. (False)

6. A bank must generate a report that shows the average balance for all customers each day. The report must
truncate the decimal portion of the balance. Which two code segments achieve the goal? Each correct answer
presents a complete solution. (Choose 2.)
A. average_balance = int(total_deposits/number_of_customers)
B. average_balance = total_deposits**number_of_customers
C. average_balance = total_deposits//number_of_customers
D. average_balance=float(total_deposits//number_of_customers)

7. You need to determine whether the variable x is null.


What is the correct syntax?
A. x == null
B. x == "null"
C. x = null
D. x typeof null

8. You need to determine whether a string named str is empty.


What is the correct syntax?
A. str ==""
B. str == "null"
C. str == null
D. str == "empty"
9. A friend asks you to refactor and document the following Python code:
value1 = 9
value2 = 4
answer = (value1 % value2 * 10) // 2.0 ** 3.0 + value2
You run the code.
What is the result?
A. The value 5.0 is displayed.
B. The value 129 is displayed.
C. A syntax error occurs.
D. The value 5.667 is displayed.
10. You are creating an eCommerce script that accepts input from the user and outputs the data in a comma-
delimited format.
You write the following code to accept input:
item = input("Enter the item name: ")
sales = int(input("Enter the quantity: "))
The output must meet the following requirements:

 Enclose strings in double quotes.


 Do not enclose numbers in quotes or other characters.
 Separate items by commas.
You need to complete the code to meet the requirements.
Which two code segments could you use? Each correct answer presents a complete solution. (Choose 2.)
Note: You will receive partial credit for each correct answer.
A. print(item + ',' + sales)
B. print(' " ' + item + ' ", ' , sales)
C. print("{0},{1}".format(item, sales))
D. print("{0}", {1}'.format(item, sales))

11. You develop a Python application for your company. You want to add notes to your code so other team
members will understand it.
What should you do?
A. Place the notes after # on any line.
B. Place the notes within <!-- and --> in any code segment.
C. Place the notes after // on any line.
D. Place the notes within /* and */ in any code segment.
12. Review the following code segment:
product = 2
n = 5
while (n != 0) :
product *= n
print (product)
n -= 1
if n == 3:
break
How many lines of output does the code print?
Enter the number as an integer here: 2

13. What does the following statement do?


data = input()
A. Displays all input peripheral devices on the computer
B. Displays a message box that allows user input
C. Creates an HTML input element
D. Allows a user to enter text in the console
14.

Answer (Correct order of operations:)


 Parentheses
 Exponents
 Unary positive, negative, not
 Multiplication and Division
 Addition and Subtraction
 And

15. You are creating a function that manipulates a number passed into the function as a float value. The
function must perform the following tasks:

 Take the absolute value of the float.


 Remove any decimal points after the integer.
Which two math functions should you use? Each correct answer is part of the solution. (Choose 2.)
Note: You will receive partial credit for each correct selection.
A. math.floor(x)
B. math.fmod(x)
C. math.fabs(x)
D. math.frexp(x)
16. You are developing a Python application for your school. The application must read and write data to a
text file. If the file does not exist, the application must create it. If the file contains content, the application
must delete the content.
Which code segment should you use?
A. open("local_data", "r")
B. open("local_data", "w+")
C. open("local_data", "w")
D. open("local_data", "re")

17. A bank is migrating their legacy bank transaction code to Python. The bank hires you to document the
migrated code. Which documentation syntax is correct?
Α. // Returns the current balance of the bank account
def get_balance():
return balance
B. Returns the current balance of the bank account
def get_balance():
return balance
C. def get_balance():
# Returns the current balance of the bank account
return balance
D. def get_balance():
/* Returns the current balance of the bank account */
return balance

18. The following function calculates the value of an expression that uses an exponent. Line numbers are
included for reference only.
01 def calc_power (a, b):
02 return a**b
03 base = input("Enter the number for the base: ")
04 exponent = input("Enter the number for the exponent: ")
05 result = calc_power (base, exponent)
06 print("The result is " + result)
For each statement, select True or False.
Note: You will receive partial credit for each correct selection.
Answer Area
The code will generate an error in line 03 and line 04. (True)
The code will generate an error in line 02 and line 05. (True)
The code will correctly output data to the console. (True)

19. Review the following code segment:


f = open("python.txt", "a")
f.write("This is a line of text.")
f.close()

For each statement about the code segment, select True or False.
A file named python.txt is created if it does not exist. (True)
The data in the file will be overwritten. (False)
Other code can open the file after this code runs. (True)

20. You create the following Python function to calculate the power of a number. Line numbers are included
for reference only.
01 # The calc_power function calculates exponents
02 # x is the base
03 # y is the exponent
04 # The value of x raised to the y power is returned
05 def calc_power(x, y):
06 comment = "# Return the value"
07 return x ** y # raise x to the y power
For each statement, select True or False.
Python will not check the syntax of lines 01 through 04. (True)
The pound sign (#) is optional for lines 02 and 03. (True)
The string in line 06 will be interpreted as a comment. (False)
Line 07 contains an inline comment. (True)

21. You work on a team that is developing a game.


You need to write code that generates a random number that meets the following requirements:
• The number is a multiple of 5.
• The lowest number is 5.
• The highest number is 100.
Which two code segments will meet the requirements? Each correct answer presents a complete solution.
(Choose 2.)
Note: You will receive partial credit for each correct answer.
A. from random import randint
print (randint(1, 20) * 5)
B. from random import randint
print (randint(0, 20) * 5)
C. from random import randrange
print (randrange(5, 105, 5))
D. from random import randrange
print (randrange (0, 100, 5))

22. The following function calculates the value of an expression that uses an exponent. Line numbers are
included for reference only.
01 def calc_power(a, b):
02 return a**b
03 base = input("Enter the number for the base: ")
04 exponent = input("Enter the number for the exponent: ")
05 result calc_power (base, exponent)
06 print("The result is " + result)

For each statement, select True or False.


Note: You will receive partial credit for each correct selection.
Answer Area
The code will generate an error in line 03 and line 04. (True)
The code will generate an error in line 02 and line 05. (True)
The code will correctly output data to the console. (False)
23. For each statement about the following function, select True or False.

def grosspay (hours=40, rate=25, pieces=0, piecerate=0, salary=0):


overtime=0
if pieces > 0:
return pieces * piecerate
if salary > 0:
pass
if hours 40:
overtime (hours-40) * (1.5 * rate)
return overtime + (40 * rate)
else:
A function call of gross pay() will create a syntax error. (True)
A function call of gross pay (salary-50000) will return nothing. (False)
A function call of grosspay (pieces-500, piecerate=4) will return a result of 2000. (True)

24. You are creating a Python program that compares numbers.


You write the following code. Line numbers are included for reference only.
01 num1 = eval(input ("Please enter the first number: "))
02 num2 = eval(input ("Please enter the second number: "))
03 if num1 == num2:
04 print("The two numbers are equal.")

05 if num1 <= num2:


06 print("Number 1 is less than number 2.")
07 if num1 > num2:
08 print("Number 1 is greater than number 2.")
09 if num2 = num1:
10 print("The two numbers are the same.")
You need to ensure that the comparisons are accurate.
For each statement, select True or False.
Note: You will receive partial credit for each correct selection.
The print statement at line 04 will print only if the two numbers are equal in value. (True)
The print statement at line 06 will print only if num1 is less than num2. (True)
The print statement at line 08 will print only if num1 is greater than num2. (True)
The statement at line 09 is an invalid comparison. (True)

25. A bicycle 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.
You write the following Python code. Line numbers are included for reference only.
01
02 name = input("What is your name? ")
03 return name
04
05 calories = miles* calories_per_mile
06 return calories
07 distance = int(input("How many miles did you bike this week? "))
08 burn_rate = 50
09 biker get_name()
10 calories burned calc_calories (distance, burn_rate)
11 print(biker, ", you burned about ", calories_burned, " calories.")
You need to define the two required functions.
Which two code segments should you use for line 01 and line 04? Each correct answer presents part of the
solution. (Choose 2.)
A. 01 def get_name():
B. 01 def get_name(biker):
C. 01 def get_name(name):
D. 04 def calc_calories():
E. 04 def calc_calories(miles, burn_rate):
F. 04 def calc_calories(miles, calories_per_mile):

26. Students are attending an activity night at their school. The following function tells students where to go
for their activities:
def roomAssignment (student, year):
"""Assign rooms to students"""
if year == 1:
print (f" \n{student.title()}, please report to room 115")
elif year == 2:
print(f"\n{student.title()}, please report to room 210")
elif year == 3:
print(f" \n{student.title()}, please report to room 320")
elif year == 4:
print (f"\n{student.title()}, please report to room 405")
elif year == 5:
print(f" \n{student.title()}, please report to room 515")
else:
print (f" \n{student.title()}, please report to room 625")
Which two code segments can be used to call the function? (Choose 2.) Each correct code segment is a
complete solution.
Note: You will receive partial credit for each correct selection.
A. name = input("What is your name?")
grade=0
while grade not in (1,2,3,4,5,6):
grade = int(input("What grade are you in (1-6)?"))
roomAssignment (name, year=grade)
B. name = input("What is your name?")
grade=0
while grade not in (1,2,3,4,5,6):
grade = int(input("What grade are you in (1-6)?"))
roomAssignment (student, year)
C. roomAssignment ("Sherlock Sassafrass",4)
D. roomAssignment (year=6, name="Minnie George")
27.
28. You are writing a function that increments the player score in a game.
The function has the following requirements:
• If no value is specified for points, then points start at one.
• If bonus is True, then points must be doubled.
You write the following code. Line numbers are included for reference only.
01 def increment_score (score, bonus, points):
02 if bonus == True:
03 points = points * 2
04 score = score + points
05 return score
06 points = 5
07 score = 10
08 new_score= increment_score (score, True, points)
For each statement, select True or False.
Note: You will receive partial credit for each correct selection.
To meet the requirements, you must change line 01 to: def increment_score (score, bonus, points = 1):
(True)
If you do not change line 01 and the function is called with only two parameters, the value of the third
parameter will be None. (True)
Line 03 will also modify the value of the variable points declared at line 06. (False)

29. View the following program. Line numbers are for reference only.
def petstore (category, species, breed = "none"):
print (f" \nYou have selected an animal from the {category}
category."
if breed == "none":
print("The {category) you selected is a (species}")
else:
print (f"The [category} you selected is a {species}
{breed}")

category= input("Enter dog, cat, or bird: ")


species = input("Enter species: ")
if category == "dog" or category == "cat":
breed input("Enter breed: ")
petStore(category, species, breed)
else:
petStore(category, species)
petStore (breed="Maltese", species="Canine", category="dog")
petstore ("bird", species="Scarlet Macaw")
For each statement about the program, select True or False.
Note: You will receive partial credit for each correct selection.
The function returns a value. (False)
The function calls at lines 12 and 15 are valid. (True)
The function calls at lines 14 and 16 will result in an error. (True)

30.
31.

32.
33.

34.
35.

36.
37.

38.
39.

40.
41.

42.
43.

44.
45.

46.
47.

48.
49.

50.
51.

52.
53.

54.
55.

56.
57.

58.
59.

60.
61.

62.
63.

You might also like