In: Computer Science
1. When creating your own functions, which of the following are good characteristics that make them robust, sharable, and reusable (check all that apply)
the function's name is suggestive of its purpose and/or actions and includes a summary docstring |
||
the function freely uses variables in the global namespace to perform its activities |
||
the function only uses variables it is passed by calling code or it otherwise internally creates |
||
the function only performs a limited number of actions |
2. Following list is given.
L L = [20, ‘Hello’, ‘Hi’, 6, 9] |
What is the type of L[-3] = ?
int |
||
float |
||
string |
||
list |
3. Functions are required to have the following number of parameters and return statements (check all that apply):
at least one parameter and one return statement |
||
zero or more parameters and zero or more return statements |
||
one parameter but zero or more return statements |
||
one return statement but zero or more parameters |
4. To repeat a code fixed number of times what should you use?
while loop |
||
repeat loop |
||
for loop |
||
conditional structure |
5. What is the value of 'total' at the end of the code?
total = 0 numbers = [11, 1, -1, -20, 15] for number in numbers:total += number |
Raise an error |
||
10 |
||
6 |
||
16 |
Ans 1) Some good characterstics of user defined functions are:
Ans 2) L = [20, ‘Hello’, ‘Hi’, 6, 9]
L[-3] will give the item from the list which is at 3rd position from last in the list due to '-'(negative) sign. So, the third item from last in the given list is 'Hi' which is a string.
Hence, option (c) string is correct.
Ans 3) Functions are required to have the following number of parameters and return statements.
Option (b) zero or more parameters and zero or more return statement is correct.
The arguments are not necessary in the functions and hence there can be 0 or more arguments in the function.
Also, the return statements depends on the type of the function (void, int, string, boolean etc). In void function, there should be 0 return statement, in other types, there should be 1 or more return statements.
Ans 4) To repeat a code fixed number of times what should you use?
We can use any loop type of code for repeating the statements in the code. For eg, do while, for loop, while loop, repeat loop etc.
So, option (a), (b), and (c) are correct.
Option (d) is wrong because the conditional structure consists of if-else statments and hence not used for repeatation of code.
Ans 5) What is the value of 'total' at the end of the code?
total = 0
numbers = [11, 1, -1, -20, 15]
for number in numbers:
total += number
Option (c) 6 is correct.
It will add all the elements in the 'numbers' list given in the code. So the value of total will be 6.