All about Functions Flashcards

1
Q

What keyword is used in python to define a function?

A
  1. def
  2. Example = def my_introduction():
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What will be the output of the following function if it was invoked?

def my\_function():
print('Hello World')
A

It would result in a IndentationError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

True or False:

Custom function names in python can start with a digit.

A

False

  1. Functions can start with either an underscore of a letter.
  2. The letter can be uppercase or lowercase
  3. Digits can be included after letters or the underscore
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Explain what is happening with the line “hi_function = my_function”
def my\_function():
 print('Hello World')

hi_function = my_function

A
  1. Functions can be treated as objects
  2. hi_function is a variable which has a reference to the original function
  3. You can use either name to invoke the function.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What type of scope do the following variables have?

**name = 'Bryce'
city = 'Toronto'**

def introduction():
print(‘Hi my name is: ‘ , name)
print(‘I live in : ‘ , city)

A
  1. Both variables have global scope
  2. Variables declared with global scope can be accessed by functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How would you refer to the name variable on line 1.

  1. def introduction(name):
    print(“Hello, my name is “ , name)
A
  1. Input argument
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What will be the output when the following function is called

def my\_introduction():
 print("Hello, my name is John")

my_introduction(‘Ted’)

A
  1. This would result in a TypeError as you’re trying to pass an argument to a function which doesn’t except an input argument.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True of False:

Input arguments passed into functions are limited to certain data types.

A
  1. False
  2. Input arguments can be of any data type, such as floats, ints, lists etc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When using multiple return values in a function, what is returned?

def add\_state(x,y):
 add\_result = x + y
 sub\_result = x - y

return add_result, sub_result

A

The function will return a tuple with the values as fields of the tuple.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

maths = 100 is an example of what?

total_score(math=100, physics=56, chemistry=34, biology=99)

A
  1. A keyword argument
  2. Allows you to specify input arguments by name while invoking functions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

True or False

Python allows you to mix positional and keyword arguments to invoke a function.

A
  1. True
  2. However, the order does matter.

This will work:

total_score(100,56, chemistry=34, biology=99)

This will result in an SyntaxError

total_score(maths =100,56, chemistry=34, biology=99)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When invoking a function, do keyword arguments have to be in the correct order?

A
  1. No
  2. For example, this code is correct.
def total\_score(math, physics, chemistry, biology):
 print(math, physics, chemistry, biology)

total_score(biology=99, math=50, physics=89, chemistry=90)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does *args do when used in functions?

A

*args indicates that the function can be invoked with any number of arguments.

def print_fn(*args):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Using the example below, what is **kwargs

def student_details(**kwargs)

A
  1. It stands for keyword variable length arguments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When you use **kwargs, what datatype does python pack the arguments into?

A
  1. Python packs the variable length arguments into a dictionary, not a tuple.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which of the following are valid types of arguments in Python?

  1. Variable length arguments
  2. Ordered arguments
  3. Default arguments
  4. Keyword arguments
A

Correct answers are highlighted.

  1. Variable length arguments
  2. Ordered arguments
  3. Default arguments
  4. Keyword arguments
17
Q

Which of the following are valid function names in Python?

  1. functionName()
  2. -functionName()
  3. _function_name()
  4. 123_Function_Name()
  5. _123_Function_Name()
A
  1. functionName()
  2. -functionName()
  3. _function_name()
  4. 123_Function_Name()
  5. _123_Function_Name()
18
Q

What does this function indicate?

def some\_fn(a,b,c=True):
 print(a,b,c)
  1. a and b are optional arguments, c is required
  2. a and b are string arguments, c is boolean
  3. a and b are numeric arguments, c is boolean
  4. a and b are required arguments, c is optional
A

a and b are required arguments, c is optional

19
Q

Which of the following statements about the data types of return values is/are false?

  1. A return statement is mandatory in functions
  2. A function in Python can return a dictionary
  3. A function in Python can return nothing
  4. A function in Python may have no return statement
A

A return statement is mandatory in functions

20
Q

Which of the following function definitions allows the function to accept variable length arguments?

  1. some_fn(…args)
  2. some_fn(…)
  3. some_fn(*)
  4. some_fn(*args)
A

some_fn(*args)

21
Q

Which of the following statements about functions is false?

  1. Functions can have different names
  2. Functions are invoked using parenthesis
  3. Functions can contain any number of statements in the function body
  4. Function cannot access variables which are declared outside the function
A

Function cannot access variables which are declared outside the function

22
Q

What is the default return value from a function when no return statement is specified?

  1. ’’’
  2. 0
  3. -1
  4. None
A

None

23
Q

Which of the following statements about positional arguments to function is/are true?

  1. A function can accept any number of positional arguments
  2. They can be of any data type
A