Practice 3 (Year 2 Sem 1)

Practice 3 is the third practice in semester 1 of year 2, and the third practice in the total of year 2. It is a practice for functions, and is considered to be easy.

Description
No description was provided for this practice.

Unlocked after completion

 * Practice makes perfect! (Score at least 60% for Practice 3 [Chapter 5])
 * Intern of the Year (Score at least 60% for Practice 3 [Chapter 5])

Question
Which of the following is a valid function header (first line of a function definition)?

Select all correct answers.

Options

 * def drawCircle(t):
 * def drawCircle:
 * drawCircle(t, sz):
 * def drawCircle(t, sz)
 * def drawCircle(t, u, v):

Question
Which of the following is the correct output for the code snippet below?

x = 2 def square(x): x = 3 return x * x

print(square(x), x, square(5))

Options

 * 2 2 2
 * 9 3 9
 * 4 9 25
 * 9 2 9
 * 4 2 25

Question
What is the printout of the following code:

def function(x): print(x) x = 4.5 y = 3.4 print(y) x = 2 y = 4 function(x) print(x)

Options

 * 2 3.4 4.5
 * 4.5 3.4 4.5
 * 2 4 4.5
 * 2 3.4 2
 * 2 4 2

Question
The function  returns the area of a rectangle of length x and breadth y.

Define a function  that:


 * 1) Returns the area of a square of length x. The   function should make use of the   function. Thus, you do not need to know how the area of a rectangular is calculated.
 * 2) Returns   if the input parameter is <= 0.

Note: You do not have to define the  ''' function yourself. You simply have to call the function.'''

Solution
Make sure that you check if x <= 0 before using area_rect first.

Question
The two roots of a quadratic equation, for example, ax2 + bx + c = 0, can be obtained using the following formula:



Write a function  which takes in the values a, b and c, and return the two roots of the quadratic equation. You may assume that the quadratic equation given will always return two real roots.

Solution
To return multiple values, use return x, y where x and y are the values to be returned.

To raise x to the power of 2, do x**2.

To get the positive square root of x, do either x**0.5 or import math, then use math.sqrt(x).