PCEP-30-02 · Question #266
What is the expected output of the following code? ``python print(type(1J)) ``
The correct answer is D. <type 'complex'>. In Python, the suffix J (or j) after a number creates a complex number literal - specifically the imaginary part - making 1J equivalent to the complex value 0+1j. Option D is correct because type(1J) evaluates to <class 'complex'> (shown as <type 'complex'> in Python 2 style)…
Question
print(type(1J))
Options
- A<type 'dict'>
- B<type 'unicode'>
- C<type 'oat'>
- D<type 'complex'>
How the community answered
(29 responses)- A3% (1)
- B7% (2)
- C10% (3)
- D79% (23)
Explanation
In Python, the suffix J (or j) after a number creates a complex number literal - specifically the imaginary part - making 1J equivalent to the complex value 0+1j. Option D is correct because type(1J) evaluates to <class 'complex'> (shown as <type 'complex'> in Python 2 style).
Why the distractors are wrong:
- A (
dict) - dictionaries are created with{}ordict(), not numeric literals. - B (
unicode) - unicode is a string type;1Jhas no quotes around it. - C (
oat) -floatis the floating-point type, and even then1.0would producefloat, notoat(this choice appears to be a typo/trick).
Memory tip: Think of j as the engineering notation for the imaginary unit (mathematicians use i, engineers use j). Whenever you see a bare number ending in j or J in Python, it's always a complex number - no exceptions.
Community Discussion
No community discussion yet for this question.