Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.

Unlimited Access

Get Unlimited Contributor Access to the all ExamTopics Exams!
Take advantage of PDF Files for 1000+ Exams along with community discussions and pass IT Certification Exams Easily.

Exam PCAP topic 1 question 1 discussion

Actual exam question from Python Institute's PCAP
Question #: 1
Topic #: 1
[All PCAP Questions]

What will be the value of the i variable when the while e loop finishes its execution?

  • A. 1
  • B. 0
  • C. 2
  • D. the variable becomes unavailable
Show Suggested Answer Hide Answer
Suggested Answer: A 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
jarvisasim
Highly Voted 3 years, 7 months ago
initially i value is 0, while condition fails as not equal to 0 So, it enters into else block and execute the increment statement. Now i become 1
upvoted 17 times
...
Bere
Most Recent 6 months ago
Selected Answer: A
i = 0 print(f"initial-value: {i}") while i != 0: i = i - 1 print(f"while-value: {i}") else: i = i + 1 print(f"else-value: {i}") print(f"final-value: {i}") initial-value: 0 else-value: 1 final-value: 1
upvoted 1 times
...
Valcon_doo_NoviSad
6 months, 2 weeks ago
Selected Answer: A
A is correct, condition for the WHILE loop is never satisfied hence the ELSE part is executed.
upvoted 1 times
...
devadhar
7 months, 3 weeks ago
i=1 i=1 i=1 i=1 i=1 i=1 i=1 i=1 i=1 i=1 i=1 i=1 i=1 i=1 i=1 i=1 i=1
upvoted 1 times
...
Elsa_Python
8 months, 3 weeks ago
The answer should be A because i is 0 in the first line, which means While won't be execute, then direct goes in to else section. i = i +1 which means i = 0+1, the result is 1.
upvoted 2 times
...
CristianCruz
9 months, 2 weeks ago
Selected Answer: B
Answer is B
upvoted 1 times
...
CristianCruz
9 months, 2 weeks ago
Answer is B
upvoted 1 times
...
DrMKG
11 months, 1 week ago
answer is A
upvoted 1 times
...
Adeshina
1 year, 4 months ago
The value of the i variable when the while loop finishes its execution will be 1. This is because the else clause of the while loop will be executed only if the condition of the loop (i != 0) is not met, which means that the loop will not be executed at all. Since the loop is not executed, the value of i will not be changed, and it will remain 0. However, the else clause will be executed, which contains the statement i = i + 1. This will set the value of i to 1, which is the final value of i when the loop finishes its execution.
upvoted 1 times
...
Janpcap123
1 year, 10 months ago
To add to my previous comment the question is what is the variable after the while loop is run, it does not ask after the else?
upvoted 1 times
...
Janpcap123
1 year, 10 months ago
is it possible that the answer is D, the variable becomes unavailable? Technically the else statement is: 0 = 0 + 1, we are saying nothing is equals to something? I know when we run the code and add a print statement the returned value is 1, but the code snipped does not include a print statement, so when the code is run without a print statement, we have to assume the answer is D noting is returned?
upvoted 2 times
...
zaxxy
1 year, 11 months ago
Selected Answer: A
In [4]: i=0 In [5]: while i != 0: ...: i=i-1 ...: else: ...: i=i+1 ...: In [6]: i Out[6]: 1
upvoted 2 times
...
macxsz
1 year, 11 months ago
i=0 so i becomes i+1 A is correct
upvoted 1 times
...
hibana2077
1 year, 12 months ago
Selected Answer: A
A is right.
upvoted 2 times
...
Erik_Bloodaxe
2 years, 1 month ago
A is correct. First i is given the value of 0. The next line says "if i is not equal to 0, then decrease i's value by 1". The value of i is currently 0, so this condition does not apply and the programme proceeds to the "else" part, which is "add 1 to i". The value of i is 0, so adding 1 gets you 1.
upvoted 2 times
...
Developer2215
2 years, 3 months ago
Selected Answer: D
the answer should be D you can try it for your self as well if u want: i = 0 while i ! = 0 : i=i-1 else: i=i+1 print(i)
upvoted 1 times
...
Ashley1
2 years, 3 months ago
Selected Answer: A
Got it
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 ...