Which of the following expressions evaluate to a non-zero result? (Select two answers.)
Correct Answer: A,B
Explanation In Python, the ** operator is used for exponentiation, the / operator is used for floating-point division, and the // operator is used for integer division. The order of operations is parentheses, exponentiation, multiplication/division, and addition/subtraction. Therefore, the expressions can be evaluated as follows: A). 2 ** 3 / A - 2 = 8 / A - 2 (assuming A is a variable that is not zero or undefined) B). 4 / 2 * * 3 - 2 = 4 / 8 - 2 = 0.5 - 2 = -1.5 C. 1 * * 3 / 4 - 1 = 1 / 4 - 1 = 0.25 - 1 = -0.75 D. 1 * 4 // 2 ** 3 = 4 // 8 = 0 Only expressions A and B evaluate to non-zero results.
Question 2
Insert the code boxes in the correct positions in order to build a line of code which asks the user for an integer value and assigns it to the depth variable. (Note: some code boxes will not be used.)
Correct Answer:
Explanation: One possible way to insert the code boxes in the correct positions in order to build a line of code which asks the user for an integer value and assigns it to the depth variable is: depth = int(input("Enter the immersion depth: ")) This line of code uses the input function to prompt the user for a string value, and then uses the int function to convert that string value into an integer number. The result is then assigned to the variable depth. You can find more information about the input and int functions in Python in the following references: * [Python input() Function] * [Python int() Function]
Question 3
What is the expected result of the following code?
Correct Answer: A
The code snippet that you have sent is trying to use the global keyword to access and modify a global variable inside a function. The code is as follows: speed = 10 def velocity(): global speed speed = speed + 10 return speed print(velocity()) The code starts with creating a global variable called "speed" and assigning it the value 10. A global variable is a variable that is defined outside any function and can be accessed by any part of the code. Then, the code defines a function called "velocity" that takes no parameters and returns the value of "speed" after adding 10 to it. Inside the function, the code uses the global keyword to declare that it wants to use the global variable "speed", not a local one. A local variable is a variable that is defined inside a function and can only be accessed by that function. The global keyword allows the function to modify the global variable, not just read it. Then, the code adds 10 to the value of "speed" and returns it. Finally, the code calls the function "velocity" and prints the result. However, the code has a problem. The problem is that the code uses the global keyword inside the function, but not outside. The global keyword is only needed when you want to modify a global variable inside a function, not when you want to create or access it outside a function. If you use the global keyword outside a function, you will get a SyntaxError exception, which is an error that occurs when the code does not follow the rules of the Python language. The code does not handle the exception, and therefore it will terminate with an error message. The expected result of the code is an unhandled exception, because the code uses the global keyword incorrectly. Therefore, the correct answer is A. The code is erroneous and cannot be run. Reference: Python Global Keyword - W3SchoolsPython Exceptions: An Introduction - Real Python The code is erroneous because it is trying to call the "velocity" function without passing any parameter, which will raise a TypeError exception. The "velocity" function requires one parameter "x", which is used to calculate the return value of "speed" multiplied by "x". If no parameter is passed, the function will not know what value to use for "x". The code is also erroneous because it is trying to use the "new_speed" variable before it is defined. The "new_speed" variable is assigned the value of 20 after the first function call, but it is used as a parameter for the second function call, which will raise a NameError exception. The variable should be defined before it is used in any expression or function call. Therefore, the code will not run and will not produce any output. The correct way to write the code would be: # Define the speed variable speed = 10 # Define the velocity function def velocity(x): return speed * x # Define the new_speed variable new_speed = 20 # Call the velocity function with new_speed as a parameter print(velocity(new_speed)) Copy This code will print 200, which is the result of 10 multiplied by 20. References: [Python Programmer Certification (PCPP) - Level 1] [Python Programmer Certification (PCPP) - Level 2] [Python Programmer Certification (PCPP) - Level 3] [Python: Built-in Exceptions] [Python: Defining Functions] [Python: More on Variables and Printing]
Question 4
Arrange the code boxes in the correct positions in order to obtain a loop which executes its body with the level variable going through values 5, 1, and 1 (in the same order).
Correct Answer:
Question 5
What is true about exceptions in Python? (Select two answers.)