NEW
Font size
WorksheetsReview on Python Functions, Scope, and Libraries Lecture
Total questions: 10
Worksheet time: 5mins
What is the fundamental purpose of a function in programming?
To create global variables
To serve as a block of code that can only be called once
A group of statements existing within a program for the purpose of performing a specific task.
To define an object-oriented method.
What is the required syntax keyword used to declare a function in Python?
function
define
def
function_name
When a function is called, the specific values passed to the function (e.g., addAndPrint(12, 34)) are referred to as:
Formal parameters
Local parameters
Actual parameters (or arguments)
Return values
Consider the declaration: def function2(x, y): .... If a programmer attempts to call this function using function2(1), what error will occur, based on the rules of function calling?
A documentation string error
A TypeError indicating a missing required positional argument/parameter
The function will run, setting the second parameter y to None
A scope error.
Which statement is required inside the body of a user-defined function if the function is intended to send a value back to the calling program?
print()
global
return expression
def
What is the "scope" of a variable declared inside a function?
It is globally visible throughout the entire program
It is visible from the point it is declared to the end of the function
It is visible only if the function uses the global modifier
It is visible only to other local variables
What is the key difference in syntax between calling a built-in function (like len()) and calling a method (like s.upper())?
Functions use parentheses; methods do not.
Methods are always defined by the user.
A method call specifies an object that the method is applied to, using dot notation
Functions always require parameters, while methods do not.
In Python, what is the term for the triple-quote syntax ("""...""") used immediately after a function header, and what is its primary purpose?
Block comment; used for debugging only.
Documentation string (Docstring); used to explain what the function does to other programmers
Global variable declaration; used to share code between files
Method definition; used to define an object reference
Why are programmers generally advised to avoid using global variables inside functions?
Global variables are always immutable and cannot be modified
Using parameters instead enhances understandability and prevents confusion with local variables of the same name
Only methods can access global variables
Functions pass parameters by reference, making global modification unavoidable
If you create a file named myLibrary.py containing a function doubleUp(), and you import the library using the statement import myLibrary, how must you call the doubleUp function in your main program?
doubleUp(value)
myLibrary_doubleUp(value)
myLibrary.doubleUp(value)
import myLibrary.doubleUp(value)
