100% Latest & Updated Python Institute PCEP-30-02 Practice Test Questions, Exam Dumps & Verified Answers!
30 Days Free Updates, Instant Download!
Question 170
A developer tests iteration over string characters. The following code prints '3'. Which explanation best accounts for that observed behavior?n=0
for c in "banana":
if c=="a": n+=1
print(n)
- continue skips the remaining body of the current iteration but does not end the whole loop.
- A for loop else suite runs when the loop ends normally rather than because of break.
- pass does not alter variables or stop execution; it merely provides a syntactically valid empty statement.
- A for loop iterating over a string yields one-character strings in left-to-right order.
Explanation
Correct Answer
D:This is the operative language rule for the code. It directly explains why the example prints '3'; the displayed behavior was independently reproduced by a Python 3 execution check.
Incorrect Answers
A:The continue skips the rest of one loop pass topic does not explain why the code prints '3'. The relevant Python rule is: A for loop iterating over a string yields one-character strings in left-to-right order.
B:The for else after natural completion topic does not explain why the code prints '3'. The relevant Python rule is: A for loop iterating over a string yields one-character strings in left-to-right order.
C:The pass as a no-op placeholder topic does not explain why the code prints '3'. The relevant Python rule is: A for loop iterating over a string yields one-character strings in left-to-right order.
Question 171
A learner is checking iteration over string characters. After this modification, what does Python 3 actually do?out=[]
for c in "ab": out.append(c*2)
print(out)
- Prints "['aa']"
- Prints "['bb']"
- Prints "['aa', 'bb']"
- Prints "['bb', 'aa']"
Explanation
Correct Answer
C:The snippet prints "['aa', 'bb']". A for loop iterating over a string yields one-character strings in left-to-right order.
Incorrect Answers
A:This predicts that the snippet prints "['aa']", but executing the displayed Python 3 code prints "['aa', 'bb']". A for loop iterating over a string yields one-character strings in left-to-right order.
B:This predicts that the snippet prints "['bb']", but executing the displayed Python 3 code prints "['aa', 'bb']". A for loop iterating over a string yields one-character strings in left-to-right order.
D:This predicts that the snippet prints "['bb', 'aa']", but executing the displayed Python 3 code prints "['aa', 'bb']". A for loop iterating over a string yields one-character strings in left-to-right order.
Question 172
During a review of iteration over string characters, three independent Python 3 fragments are shown. Which THREE statements accurately describe their behavior? (Choose THREE.)Program A:
n=0
for c in "banana":
if c=="a": n+=1
print(n)
Program B:
out=[]
for c in "ab": out.append(c*2)
print(out)
Program C:
r=""
for c in "abc": r+=c.upper()
print(r)
- Program B prints "['aa', 'bb']".
- Program C prints 'god'.
- Program C prints 'abc'.
- Program B prints '[0, 1, 2, 3]'.
- Program A prints '3'.
- Program A prints '4'.
Explanation
Correct Answers
A:Running Program B prints "['aa', 'bb']". A for loop iterating over a string yields one-character strings in left-to-right order.
C:Running Program C prints 'abc'. A for loop iterating over a string yields one-character strings in left-to-right order.
E:Running Program A prints '3'. A for loop iterating over a string yields one-character strings in left-to-right order.
Incorrect Answers
B:Program C does not produce 'god'; its verified result is: Prints 'ABC'. A for loop iterating over a string yields one-character strings in left-to-right order.
D:Program B does not produce '[0, 1, 2, 3]'; its verified result is: Prints "['aa', 'bb']". A for loop iterating over a string yields one-character strings in left-to-right order.
F:Program A does not produce '4'; its verified result is: Prints '3'. A for loop iterating over a string yields one-character strings in left-to-right order.
Question 173
A learner is checking loop filtering with membership. What is the exact printed result or uncaught exception?out=[]
for x in [1,2,3,4]:
if x in (2,4): out.append(x)
print(out)
- Prints '[4]'
- Prints '[4, 2]'
- Prints '[2, 4]'
- Prints '[2]'
Explanation
Correct Answer
C:The snippet prints '[2, 4]'. A loop can use membership tests to include or exclude selected values from an accumulator.
Incorrect Answers
A:This predicts that the snippet prints '[4]', but executing the displayed Python 3 code prints '[2, 4]'. A loop can use membership tests to include or exclude selected values from an accumulator.
B:This predicts that the snippet prints '[4, 2]', but executing the displayed Python 3 code prints '[2, 4]'. A loop can use membership tests to include or exclude selected values from an accumulator.
D:This predicts that the snippet prints '[2]', but executing the displayed Python 3 code prints '[2, 4]'. A loop can use membership tests to include or exclude selected values from an accumulator.
Question 174
A developer tests loop filtering with membership. The following code prints '11'. Which explanation best accounts for that observed behavior?s=0
for n in range(6):
if n not in (1,3): s+=n
print(s)
- continue skips the remaining body of the current iteration but does not end the whole loop.
- A for loop iterating over a string yields one-character strings in left-to-right order.
- A loop can use membership tests to include or exclude selected values from an accumulator.
- Two separate if statements are tested independently; both can execute when both conditions are true.
Explanation
Correct Answer
C:This is the operative language rule for the code. It directly explains why the example prints '11'; the displayed behavior was independently reproduced by a Python 3 execution check.
Incorrect Answers
A:The continue skips the rest of one loop pass topic does not explain why the code prints '11'. The relevant Python rule is: A loop can use membership tests to include or exclude selected values from an accumulator.
B:The iteration over string characters topic does not explain why the code prints '11'. The relevant Python rule is: A loop can use membership tests to include or exclude selected values from an accumulator.
D:The independent if branches topic does not explain why the code prints '11'. The relevant Python rule is: A loop can use membership tests to include or exclude selected values from an accumulator.
Question 175
A learner is checking loop filtering with membership. After this modification, what does Python 3 actually do?t=""
for ch in "a1b2":
if ch in "12": t+=ch
print(t)
- Prints '11'
- Prints '14'
- Prints '12'
- Prints '13'
Explanation
Correct Answer
C:The snippet prints '12'. A loop can use membership tests to include or exclude selected values from an accumulator.
Incorrect Answers
A:This predicts that the snippet prints '11', but executing the displayed Python 3 code prints '12'. A loop can use membership tests to include or exclude selected values from an accumulator.
B:This predicts that the snippet prints '14', but executing the displayed Python 3 code prints '12'. A loop can use membership tests to include or exclude selected values from an accumulator.
D:This predicts that the snippet prints '13', but executing the displayed Python 3 code prints '12'. A loop can use membership tests to include or exclude selected values from an accumulator.
Question 176
During a review of loop filtering with membership, three independent Python 3 fragments are shown. Which THREE statements accurately describe their behavior? (Choose THREE.)Program A:
s=0
for n in range(6):
if n not in (1,3): s+=n
print(s)
Program B:
t=""
for ch in "a1b2":
if ch in "12": t+=ch
print(t)
Program C:
out=[]
for n in (4,5,6):
if n in range(5,7): out.append(n)
print(out)
- Program A prints '11'.
- Program A prints '12'.
- Program B prints '12'.
- Program B prints '[]'.
- Program C prints 'middle'.
- Program C prints '[5, 6]'.
Explanation
Correct Answers
A:Running Program A prints '11'. A loop can use membership tests to include or exclude selected values from an accumulator.
C:Running Program B prints '12'. A loop can use membership tests to include or exclude selected values from an accumulator.
F:Running Program C prints '[5, 6]'. A loop can use membership tests to include or exclude selected values from an accumulator.
Incorrect Answers
B:Program A does not produce '12'; its verified result is: Prints '11'. A loop can use membership tests to include or exclude selected values from an accumulator.
D:Program B does not produce '[]'; its verified result is: Prints '12'. A loop can use membership tests to include or exclude selected values from an accumulator.
E:Program C does not produce 'middle'; its verified result is: Prints '[5, 6]'. A loop can use membership tests to include or exclude selected values from an accumulator.
Question 177
A learner is checking sentinel-controlled while loop. What is the exact printed result or uncaught exception?data=[1,2,0,5]
i=0
s=0
while True:
x=data[i];i+=1
if x==0: break
s+=x
print(s)
- Prints '2'
- Prints '4'
- Prints '5'
- Prints '3'
Explanation
Correct Answer
D:The snippet prints '3'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
Incorrect Answers
A:This predicts that the snippet prints '2', but executing the displayed Python 3 code prints '3'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
B:This predicts that the snippet prints '4', but executing the displayed Python 3 code prints '3'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
C:This predicts that the snippet prints '5', but executing the displayed Python 3 code prints '3'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
Question 178
A developer tests sentinel-controlled while loop. The following code prints '2'. Which explanation best accounts for that observed behavior?numbers=(4,3,-1,8)
i=0
while True:
if numbers[i]<0: break
i+=1
print(i)
- A disjunction using or is true if either condition is true, including when the other is false.
- A for loop else suite runs when the loop ends normally rather than because of break.
- A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
- A negative range step decreases values and still excludes the stop bound.
Explanation
Correct Answer
C:This is the operative language rule for the code. It directly explains why the example prints '2'; the displayed behavior was independently reproduced by a Python 3 execution check.
Incorrect Answers
A:The or branch accepts either condition topic does not explain why the code prints '2'. The relevant Python rule is: A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
B:The for else after natural completion topic does not explain why the code prints '2'. The relevant Python rule is: A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
D:The range descending with negative step topic does not explain why the code prints '2'. The relevant Python rule is: A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
Question 179
A learner is checking sentinel-controlled while loop. After this modification, what does Python 3 actually do?values=["ok","ok","stop"]
k=0
while True:
if values[k]=="stop": break
k+=1
print(k)
- Prints '3'
- Prints '2'
- Prints '1'
- Prints '4'
Explanation
Correct Answer
B:The snippet prints '2'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
Incorrect Answers
A:This predicts that the snippet prints '3', but executing the displayed Python 3 code prints '2'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
C:This predicts that the snippet prints '1', but executing the displayed Python 3 code prints '2'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
D:This predicts that the snippet prints '4', but executing the displayed Python 3 code prints '2'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
Question 180
During a review of sentinel-controlled while loop, three independent Python 3 fragments are shown. Which THREE statements accurately describe their behavior? (Choose THREE.)Program A:
numbers=(4,3,-1,8)
i=0
while True:
if numbers[i]<0: break
i+=1
print(i)
Program B:
values=["ok","ok","stop"]
k=0
while True:
if values[k]=="stop": break
k+=1
print(k)
Program C:
items=(1,0,2)
i=0
while True:
if items[i]==0: break
i+=1
print(items[i])
- Program C prints '0'.
- Program A prints '2'.
- Program B prints '2'.
- Program B prints '0'.
- Program C prints '2'.
- Program A prints '0'.
Explanation
Correct Answers
A:Running Program C prints '0'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
B:Running Program A prints '2'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
C:Running Program B prints '2'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
Incorrect Answers
D:Program B does not produce '0'; its verified result is: Prints '2'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
E:Program C does not produce '2'; its verified result is: Prints '0'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
F:Program A does not produce '0'; its verified result is: Prints '2'. A sentinel test can terminate an otherwise indefinite while loop after the sentinel value is reached.
PCEP - Certified Entry-Level Python Programmer (PCEP-30-02) Practice Tests