nerdexam
Python_Institute

PCEP-30-02 · Question #314

You are coding a math utility by using Python. You are writing a function to compute roots. The function must meet the following requirements: If a is non-negative, return a (1 / b) If a is negative…

The correct answer is B. ```python def safe_root(a, b): if a >= 0: answer = a ** (1 / b) elif a % 2 == 0: answer = 'Result is an imaginary number' else: answer = -(a) ** (1 / b) return answer ```. See the full explanation below for the reasoning.

Question

You are coding a math utility by using Python. You are writing a function to compute roots. The function must meet the following requirements:
  • If a is non-negative, return a ** (1 / b)
  • If a is negative and even, return 'Result is an imaginary number'
  • If a is negative and odd, return -(a) ** (1 / b) Which of the following functions meets the requirements?

Options

  • A
    def safe_root(a, b):
     if a >= 0:
     answer = a ** (1 / b)
     elif a % 2 == 0:
     answer = 'Result is an imaginary number'
     else:
     answer = -(-a) ** (1 / b)
     return answer
    
  • B
    def safe_root(a, b):
     if a >= 0:
     answer = a ** (1 / b)
     elif a % 2 == 0:
     answer = 'Result is an imaginary number'
     else:
     answer = -(a) ** (1 / b)
     return answer
    
  • C
    def safe_root(a, b):
     if a % 2 == 0:
     answer = a ** (1 / b)
     elif a >= 0:
     answer = 'Result is an imaginary number'
     else:
     answer = -(-a) ** (1 / b)
     return answer
    
  • D
    def safe_root(a, b):
     if a >= 0:
     answer = -(a) ** (1 / b)
     elif a % 2 == 0:
     answer = 'Result is an imaginary number'
     else:
     answer = a ** (1 / b)
     return answer
    

How the community answered

(22 responses)
  • A
    5% (1)
  • B
    86% (19)
  • C
    9% (2)

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice