Python Functions Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q11
What is a docstring in Python functions?
  • A A string that contains documentation
  • B A special type of comment
  • C A function that returns strings
  • D A string variable inside function
Answer: Option A
Explanation: A docstring is a string literal that occurs as the first statement in a function and is used for documentation.
Q12
How can you access a function"s docstring?
  • A function.doc
  • B function.__doc__
  • C function.document
  • D function.help
Answer: Option B
Explanation: Docstrings can be accessed using the __doc__ attribute or the help() function.
Q13
What is the scope of variables defined inside a function?
  • A Global scope
  • B Local scope
  • C Module scope
  • D Universal scope
Answer: Option B
Explanation: Variables defined inside a function have local scope and are not accessible outside the function.
Q14
Which keyword is used to modify a global variable inside a function?
  • A global
  • B nonlocal
  • C extern
  • D var
Answer: Option A
Explanation: The "global" keyword is used to declare that a variable inside a function refers to a global variable.
Q15
What is a recursive function?
  • A A function that calls another function
  • B A function that calls itself
  • C A function with multiple returns
  • D A function with no parameters
Answer: Option B
Explanation: A recursive function is a function that calls itself during its execution.
Q16
What is the purpose of the "nonlocal" keyword?
  • A To declare global variables
  • B To work with variables in nested functions
  • C To create new local variables
  • D To import non-local modules
Answer: Option B
Explanation: The "nonlocal" keyword is used to work with variables in nested functions, where the variable should belong to the outer function.
Q17
What is function overloading in Python?
  • A Defining multiple functions with same name but different parameters
  • B Python doesn"t support traditional function overloading
  • C Creating functions that are too long
  • D Using functions in multiple modules
Answer: Option B
Explanation: Python doesn"t support traditional function overloading like other languages. The last defined function with the same name will override previous ones.
Q18
What is a generator function?
  • A A function that generates random numbers
  • B A function that uses yield instead of return
  • C A function that creates other functions
  • D A function that generates code
Answer: Option B
Explanation: Generator functions use yield instead of return and return an iterator that can be iterated over.
Q19
What is the difference between parameters and arguments?
  • A Parameters are in definition, arguments are values passed
  • B Arguments are in definition, parameters are values passed
  • C They are the same thing
  • D Parameters are for classes, arguments for functions
Answer: Option A
Explanation: Parameters are variables in the function definition, while arguments are the actual values passed to the function.
Q20
What are default parameters in Python functions?
  • A Parameters that must be provided
  • B Parameters with predefined values
  • C Parameters that cannot be changed
  • D Parameters that are always None
Answer: Option B
Explanation: Default parameters are parameters that have default values if no argument is provided for them.
Questions and Answers for Competitive Exams Various Entrance Test