4 篇文章 2 订阅

# Week 3 all the quizzes

You should take it easy. 详细的解说等我闲一点了再添加上去，【官方解释】的是老师给的解释，大部分都是能看明白的。

# Lecture: Control flow

## Expressions and control flow quiz

### question 1

The following code uses int casting. What is the difference between the value calculated with and without the int casting?

print(str(int(float("3.14"))+int(float("5.6"))))


### question 2

Do the following two code segments have equivalent outcome (control flow)?

ticket = 240
if speed > 0:
if speed < 40:
ticket = 0
else:
if speed > 100:
pass
else if speed <= 100:
ticket = 120
else:
pass
else:
pass

ticket = 0
if speed < 40:
ticket = 0
else if speed <= 100:
ticket = 120
if speed > 100:
ticket = 240


• True
• False

### question 3

Do the following two code segments have equivalent outcome (control flow)?

ticket = 0
if speed < 40:
ticket = 0
else if speed <= 100:
ticket = 120
if speed > 100:
ticket = 240

ticket = 240
if speed < 40:
ticket = 0
else if speed <= 100:
ticket = 120


• True
• False

### question 4

What is the number of unique code paths in this code?

In other words, if we were to run this program many times, how many possible execution configurations could we have? (Hint: draw the control flow diagram and count the paths)

name = input('What is your name ')
attribute = input('Which is your favourite colour among these [Red / Green / Blue] ')

attr_str = 'Courageous'
if attribute == 'Green':
elif attribute == 'Blue':
attr_str = 'Tenacious'

sibling = input('Do you have siblings [ Yes / No ] ')
sibling_str = 'Lone wolf'
if sibling == 'Yes':
oldest = input('Are you the oldest? [ Yes / No ] ')
if oldest == 'Yes':
else:
youngest = input('Are you the youngest? [ Yes / No ] ')
if youngest == 'Yes':
sibling_str = 'Provocateur'
else:
sibling_str = 'Conqueror'

print("You are: ")
print(name + " the " + attr_str + " " + sibling_str)


• 3种可能的颜色选择之一
• 是否为同级2个选择中的1个
• 内有一个兄弟姐妹
• 最老，中，最年轻的3种选择之一

## While loops quiz

### question 1

Will the following code ever end?

i = 0
while i > 0:
print(i)
i = i + 1


• True
• False

### question 2

Does this code print numbers from 0 to 10 inclusive?

i = 10
while i >= 0:
x = 10 - i
print(x)
i = i - 1


• True
• False

### question 3

How many iterations would be executed for the following loop?

x = 10
while x < 20:
x = x + 1


1.起始条件，
2.它如何变化，
3.结束条件

### question 4

How many iterations would be executed for the following while loop?

x = 5
while x <= 15:
print(x)
x = x + 2


## Arrays and lists part one quiz

### question 1(单选）

Which best defines an array?

• It’s like this:
list = [ 'a','1','2', ]

• A range of memory,divide into equal sized parts
• A collection of objects
• A collection of objects which are internally by an independent define manner(python3).

### question 2（多选）

What is the motivation to use arrays?

• Avoid repetitive code by way of defining variables
var1,var2,var3.etc.
• When you do not know the datatype,use a list.
• Group related data together
• Cool to use
• It is the only way to express arithmetic addition
• Allow reading and writing a memory based on critical determined at runtiome.
解释： 数组为程序员提供了多功能性。大型结构化数据可以利用数组，数组是一种存储相关数据的简单方法（无需求助于对象）假定索引是数字，则可以在运行时选择它。 例如 第一个元素索引= 0，例如 如果我们有一个升序排序的数组，并且用户想要第k个最小值，则可以使用index = k + 1。【官方解释】

### question 3（判断）

Is the first element of the array located at index 1?

• True
• False

### question4（单选）

Suppose there is an array called names, and there are 5 elements. What would happen if we accessed names[12] ?
[ For argument sake, suppose this was done in Python3 ]

• Nothing
• There is an attempt to access,but zero is return.
• None is return
• An error occurs
• The original programer is send an email

# 总结

• 2
点赞
• 1
收藏
• 打赏
• 5
评论
09-24 973
05-01 1105
01-16 514
08-09 639
03-18 572

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。