nerdexam
Python_Institute

PCEP-30-02 · Question #297

Analyze the following code fragments that assign a boolean value to the variable even? Code-1: ``python 1 num = 42 2 3 # Code-1 4 if num % 2 == 0: 5 even = True 6 else: 7 even = False ` Code-2…

The correct answer is B. All three are correct, but Code-3 is preferred. See the full explanation below for the reasoning.

Question

Analyze the following code fragments that assign a boolean value to the variable even? Code-1:
1 num = 42
2
3 # Code-1
4 if num % 2 == 0:
5 even = True
6 else:
7 even = False
Code-2:
9 # Code-2
10 even = True if num % 2 == 0 else False
Code-3:
12 # Code-3
13 even = num % 2 == 0

Options

  • AAll three are correct, but Code-2 is preferred.
  • BAll three are correct, but Code-3 is preferred.
  • CCode-3 has a syntax error because you attempt to assign a number to even.
  • DAll three are correct, but Code-1 is preferred.
  • ECode-2 has a syntax error because you cannot have True and False literals in the conditional expression.

How the community answered

(21 responses)
  • A
    5% (1)
  • B
    81% (17)
  • C
    10% (2)
  • E
    5% (1)

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice