Drag and drop the code boxes in order to build a program which prints Unavailable to the screen. (Note: one code box will not be used.)
Correct Answer:
Question 7
Which of the following are the names of Python passing argument styles? (Select two answers.)
Correct Answer: A,D
Explanation Keyword arguments are arguments that are specified by using the name of the parameter, followed by an equal sign and the value of the argument. For example, print (sep='-', end='!') is a function call with keyword arguments. Keyword arguments can be used to pass arguments in any order, and to provide default values for some arguments1. Positional arguments are arguments that are passed in the same order as the parameters of the function definition. For example, print ('Hello', 'World') is a function call with positional arguments. Positional arguments must be passed before any keyword arguments, and they must match the number and type of the parameters of the function2. References: 1: 5 Types of Arguments in Python Function Definitions | Built In 2: python - What's the pythonic way to pass arguments between functions ...
Question 8
What is the expected output of the following code?
Correct Answer: D
Explanation The code snippet that you have sent is defining and calling a function in Python. The code is as follows: def runner(brand, model, year): return (brand, model, year) print(runner("Fermi")) The code starts with defining a function called "runner" with three parameters: "brand", "model", and "year". The function returns a tuple with the values of the parameters. A tuple is a data type in Python that can store multiple values in an ordered and immutable way. A tuple is created by using parentheses and separating the values with commas. For example, (1, 2, 3) is a tuple with three values. Then, the code calls the function "runner" with the value "Fermi" for the "brand" parameter and prints the result. However, the function expects three arguments, but only one is given. This will cause a TypeError exception, which is an error that occurs when a function or operation receives an argument that has the wrong type or number. The code does not handle the exception, and therefore it will terminate with an error message. However, if the code had handled the exception, or if the function had used default values for the missing parameters, the expected output of the code would be ('Fermi ', '2021', 'False'). This is because the function returns a tuple with the values of the parameters, and the print function displays the tuple to the screen. Therefore, the correct answer is D. ('Fermi ', '2021', 'False').
Question 9
Drag and drop the conditional expressions to obtain a code which outputs * to the screen. (Note: some code boxes will not be used.)
Correct Answer:
Explanation One possible way to drag and drop the conditional expressions to obtain a code which outputs * to the screen is: if pool > 0: print("*") elif pool < 0: print("**") else: print("***") This code uses the if, elif, and else keywords to create a conditional statement that checks the value of the variable pool. Depending on whether the value is greater than, less than, or equal to zero, the code will print a different pattern of asterisks to the screen. The print function is used to display the output. The code is indented to show the blocks of code that belong to each condition. The code will output * if the value of pool is positive, ** if the value of pool is negative, and *** if the value of pool is zero. You can find more information about the conditional statements and the print function in Python in the following references: [Python If ... Else] [Python Print Function] [Python Basic Syntax]
Question 10
Assuming that the phonc_dir dictionary contains namemumber pairs, arrange the code boxes to create a valid line of code which retrieves Martin Eden's phone number, and assigns it to the number variable.
Correct Answer:
Explanation number = phone_dir["Martin Eden"] This code uses the square brackets notation to access the value associated with the key "Martin Eden" in the phone_dir dictionary. The value is then assigned to the variable number. A dictionary is a data structure that stores key-value pairs, where each key is unique and can be used to retrieve its corresponding value. You can find more information about dictionaries in Python in the following references: [Python Dictionaries - W3Schools] [Python Dictionary (With Examples) - Programiz] [5.5. Dictionaries - How to Think Like a Computer Scientist ...]