# Peer Instruction: Iterations Multiple Choice QuestionsΒΆ

- Try again! The string s changes after every iterations. First 'pirate ship', 'pirate ship', then 'irate ship'...
- Correct! In every iteration, i adds one up. Therefore, s[i:] begins with next letter in the string. However, the string s also changes after every iterations. So the final printing obmits several letters.
- Try again! The string s changes after every iterations. First 'pirate ship', 'pirate ship', then 'irate ship'... It does not follow the rule that printing the first letter in 'pirate ship', then the second letter in 'pirate ship'... Some letters are obmitted.
- None of the above
- Try again! In every iteration, i adds one up. Therefore, s[i:] begins with next letter in the string. However, the string s also changes after every iterations. It does not follow the rule that printing the first letter in 'pirate ship', then the second letter in 'pirate ship'...
- I don't know
- Try again! In every iteration, i adds one up. Therefore, s[i:] begins with next letter in the string. However, the string s also changes after every iterations. It does not follow the rule that printing the first letter in 'pirate ship', then the second letter in 'pirate ship'...

Q-1: What does the following code print?

```
s = 'pirate ship'
i = 0
while(len(s) > i ):
s = s[i:]
i = i + 1
print (s , i )
```

```
A.
pirate ship 1
irate ship 2
rate ship 3
ate ship 4
te ship 5
e ship 6
```

```
B.
pirate ship 1
irate ship 2
ate ship 3
ship 4
p 5
```

```
C.
pirate ship 1
irate ship 2
rate ship 3
ate ship 4
te ship 5
e ship 6
ship 7
ship 8
hip 9
ip 10
p 11
```

- y == 11
- Try again! This will evaluate to False if y is not equal to 11.
- y != 11
- Correct! This will evaluate to False if and only if y is equal to 11.
- y > 11
- Try again! This will evaluate to False if y is equal to or smaller than 11.
- y and 11
- Try again! This is not a boolean expression.
- I don't know
- Try again! An expression evaluates to False means its opposite expression is True. Therefore, try to find an expression that will evaluate to True if y it not equal to 11.

Q-2: Which of these will evaluate to `False`

if and only if `y`

is equal to 11.

- 6 5
- Correct! When x = 4, the loop stops.
- 6 5 4
- Try again. When x = 4, the loop stops. Therefore, 4 will not be printed.
- 5 4
- Try again. When x = 6, the loop runs. Therefore, 6 will be printed. And when x = 4, the loop stops. Therefore, 4 will not be printed.
- 5 4 3
- Try again. When x = 6, the loop runs. Therefore, 6 will be printed. And when x = 4, the loop stops. Therefore, 4 and 3 will not be printed.
- 6 5 4 3
- Try again. When x = 4, the loop stops. Therefore, 4 and 3 will not be printed.

Q-3:

```
What is printed by the following code? (Output is on one line to save space.)
x = 6
while x > 4:
print(x)
x = x - 1
```

- 6 5
- Try again. x = x - 1 comes before print(x). Therefore, 6 will never be printed. And when x = 5, the loop will continue and print(x - 1). So, 4 will be printed.
- 6 5 4
- Try again. x = x - 1 comes before print(x). Therefore, 6 will never be printed.
- 5 4
- Correct! When x = 4, the loop will stop. Because x = x - 1 comes before print(x), the code prints 5 and 4.
- 5 4 3
- Try again. x = x - 1 comes before print(x). When x = 5, the loop will continue and print(x - 1). So, 4 will be printed while 3 will not.
- 6 5 4 3
- Try again. x = x - 1 comes before print(x). Therefore, 6 will never be printed. And when x = 5, the loop will continue and print(x - 1). So, 4 will be printed and 3 will not.

Q-4:

```
What is printed by the following code? (Output is on one line to save space.)
x = 6
while x > 4:
x = x - 1
print(x)
```

- xyz
- Try again. while not valid means the loop will not stop until valid equals to True. And 'xyz' does not satisfy the requirement: len(5) == 5.
- abcxyz
- Try again. while not valid means the loop will not stop until valid equals to True. And 'abcxyz' does not satisfy the requirement: s[:2] == 'xy'.
- xyabc
- Correct! 'xyzabc' satisfies two requirements: len(5) == 5 and s[:2] == 'xy'.
- More than one of the above passwords get us out of the loop.
- Try again. There is only one password that satisfy the two requirements of getting out of the loop.
- None; the loop never executes and no passwords are obtained.
- Try again. There is one password that satisfy the two requirements of getting out of the loop.

Q-5:

```
Which of the following passwords stops the loop?
valid = False
while not valid:
s = input ("Enter a password: ")
valid = len(s) == 5 and s[:2] == 'xy'
```

- Correct! When the length is 5 and it starts with xy, break helps the string get out of the loop.
- Try again. When a string meets both of the requirements, being the length 5 and starting with xy, the while loop continues.
- Both are correct
- Try again. There is only one correct answer.
- None is correct
- Try again. There is a correct answer.

Q-6: A valid password is one that is length 5 and starts with xy. Such passwords should get us out of the loop. Which of these does this?

```
A.
while True:
s = input ("Enter a password: ")
if len(s) == 5 and s[:2] == 'xy':
break
```

```
B.
s = input ("Enter a password: ")
while len(s) == 5 and s[:2] == 'xy':
s = input ("Enter a password: ")
```

- 3 4
- Try again. When n = 5, n satisfies the if condition, and n turns to -99. And print(n) outputs -99.
- 3 4 5
- Try again. When n = 5, n satisfies the if condition, and n turns to -99. And print(n) outputs -99 rather than 5.
- 3 4 -99
- Correct! When n = 5, n satisfies the if condition, and n turns to -99. So the code outputs 3, 4 and -99.
- 3 4 5 -99
- Try again. When n = 5, n satisfies the if condition, and n turns to -99. And print(n) outputs -99 rather than 5.

Q-7:

```
What is the output of this code? (Output is on one line here to save space.)
n = 3
while n > 0:
if n == 5:
n = -99
print(n)
n = n + 1
```

You have attempted of activities on this page