exam questions

Exam PCEP-30-02 All Questions

View all questions & answers for the PCEP-30-02 exam

Exam PCEP-30-02 topic 2 question 22 discussion

Actual exam question from Python Institute's PCEP-30-02
Question #: 22
Topic #: 2
[All PCEP-30-02 Questions]

What is the expected output of the following code?
x = 1 + 1 // 2 + 1 / 2 + 2
print(x)

  • A. 3
  • B. 4.0
  • C. 4
  • D. 3.5
Show Suggested Answer Hide Answer
Suggested Answer: D 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
hovnival
1 month, 1 week ago
Selected Answer: D
Certainly! Let's break down the expression step by step, considering floor division in Python: x = 1 + 1 // 2 + 1 / 2 + 2 1. The `//` operator (floor division) and `/` operator (float division) have higher precedence than the `+` operator, so they will be evaluated first. 2. Evaluate `1 // 2`: - `1 // 2` results in `0` because floor division of 1 by 2 rounds down to 0. 3. Evaluate `1 / 2`: - `1 / 2` results in `0.5` because it is float division. Now substitute these results back into the expression: x = 1 + 0 + 0.5 + 2 4. Next, evaluate the additions from left to right: - `1 + 0` results in `1` - `1 + 0.5` results in `1.5` - `1.5 + 2` results in `3.5` Therefore, the expected output of the code is: D. 3.5
upvoted 1 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago