arrow
arrow
arrow
Which of the following Function Headers in a Python code will result in error?
Question

Which of the following Function Headers in a Python code will result in error?

A.

def Compu(X=100, Y=10, Z):

B.

def Compu(X, Y, Z=1):

C.

def Compu(X=100, Y=10, Z=1):

D.

def Compu(X, Y=10, Z=1):

Correct option is A


In Python, when defining a function with parameters, default values must be given to the parameters that come after parameters without default values. This rule ensures that Python knows how to assign values to parameters when the function is called.
Let's examine the options:
· (a) def Compu(X=100, Y=10, Z): - This will result in an error because Z has no default value but comes after parameters that already have default values (X=100 and Y=10). In Python, all parameters with default values must come after those without default values.
· (b) def Compu(X, Y, Z=1): - This is valid. Z has a default value, but X and Y do not, so the order is correct.
· (c) def Compu(X=100, Y=10, Z=1): - This is valid because all parameters (X, Y, and Z) have default values, which is allowed.
· (d) def Compu(X, Y=10, Z=1): - This is valid because Y and Z have default values, but X does not. This is a correct way to define the function.
Important Key Points:
1. Default Parameters: All parameters with default values must be defined after parameters without default values.
2. Order of Parameters: You cannot specify a default value for a parameter if there are already parameters without default values following it.
Knowledge Booster:
· If you want to give a default value to a parameter, it must be placed at the end of the parameter list.
· Functions can have any number of parameters with default values, as long as they come after parameters that don’t have default values.

Free Tests

Free
Must Attempt

CDP Subject Test 01

languageIcon English
  • pdpQsnIcon20 Questions
  • pdpsheetsIcon20 Marks
  • timerIcon20 Mins
languageIcon English
Free
Must Attempt

EVS Subject Test 01

languageIcon English
  • pdpQsnIcon20 Questions
  • pdpsheetsIcon20 Marks
  • timerIcon20 Mins
languageIcon English
Free
Must Attempt

CTET Paper-I PYP (07 July 2024)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
test-prime-package

Access ‘EMRS PGT’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
289k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow