PCAP Exam QuestionsBrowse all questions from this exam

PCAP Exam - Question 1


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

Show Answer
Correct Answer: A

A

Discussion

17 comments
Sign in to comment
jarvisasimOption: A
Sep 8, 2020

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

Erik_BloodaxeOption: A
Mar 26, 2022

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.

hibana2077Option: A
Apr 27, 2022

A is right.

zaxxyOption: A
May 23, 2022

In [4]: i=0 In [5]: while i != 0: ...: i=i-1 ...: else: ...: i=i+1 ...: In [6]: i Out[6]: 1

Janpcap123Option: D
May 31, 2022

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?

Elsa_PythonOption: A
Aug 2, 2023

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.

7cell
May 10, 2024

I just pass the e xam, be aware, 50% of same questions on exam, 25 are derivatives of existing ones, 25% new question

macxszOption: A
May 2, 2022

i=0 so i becomes i+1 A is correct

Janpcap123
May 31, 2022

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?

AdeshinaOption: A
Dec 11, 2022

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.

DrMKGOption: A
May 24, 2023

answer is A

CristianCruzOption: B
Jul 12, 2023

Answer is B

CristianCruzOption: B
Jul 12, 2023

Answer is B

devadharOption: A
Sep 4, 2023

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

Valcon_doo_NoviSadOption: A
Oct 9, 2023

A is correct, condition for the WHILE loop is never satisfied hence the ELSE part is executed.

BereOption: A
Oct 26, 2023

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

pablisluiz1
Apr 25, 2024

if there is an space between ! = it will fail