USYD悉尼大学INFO1110 详细作业解析Week3 all quizzes

第三周所有的quiz,详细解释,答案供参考。详细

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

List

Week 3 all the quizzes

Preface

Lecture: Control flow

Expressions and control flow quiz

question 1

question 2

question 3

question 4

While loops quiz

question 1

question 2

question 3

question 4

Arrays and lists part one quiz

question 1(单选)

question 2(多选)

question 3(判断)

question4(单选)

总结

Preface

提示:这里可以添加本文要记录的大概内容:

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。

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"))))

1

answer : 0.74

讲解: 在代码块里运行的时候,输出结果是8。但是那是错的,正确理解是取浮点数小数点后面的数字相加,就是结果。(待更新更好的解释)

question 2

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

ticket=240ifspeed>0:ifspeed<40:ticket=0else:ifspeed>100:passelseifspeed<=100:ticket=120else:passelse:pass

1

2

3

4

5

6

7

8

9

10

11

12

13

ticket=0ifspeed<40:ticket=0elseifspeed<=100:ticket=120ifspeed>100:ticket=240

1

2

3

4

5

6

7

answer :

 True

 False

讲解: 显然一个大于另一个! 但请注意覆盖范。第一个嵌套的if是如何解决控制流问题的错误示例。 每个间隔都可以连续测试,而不必测试是否依赖于先前条件的结果。例如,如果速度大于100,则不应首先检查速度是否小于40。 这些是非依赖性标准!【官方解释】

question 3

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

ticket=0ifspeed<40:ticket=0elseifspeed<=100:ticket=120ifspeed>100:ticket=240

1

2

3

4

5

6

7

ticket=240ifspeed<40:ticket=0elseifspeed<=100:ticket=120

1

2

3

4

5

answer:

 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'ifattribute=='Green':attr_str='Adventurous'elifattribute=='Blue':attr_str='Tenacious'sibling=input('Do you have siblings [ Yes / No ] ')sibling_str='Lone wolf'ifsibling=='Yes':oldest=input('Are you the oldest? [ Yes / No ] ')ifoldest=='Yes':sibling_str='Leader'else:youngest=input('Are you the youngest? [ Yes / No ] ')ifyoungest=='Yes':sibling_str='Provocateur'else:sibling_str='Conqueror'print("You are: ")print(name+" the "+attr_str+" "+sibling_str)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

correct answer : 12

讲解:

3种可能的颜色选择之一

是否为同级2个选择中的1个

内有一个兄弟姐妹

最老,中,最年轻的3种选择之一

可以选择3种颜色且没有兄弟姐妹-> 3 * 1 = 3条代码路径

可以选择任意3种颜色和任意3种同级-> 3 * 3 = 9个代码路径

共有12条代码路径【官方解释】

While loops quiz

question 1

Will the following code ever end?

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

1

2

3

4

answer :

 True

 False

解释: 循环继续的条件必须为True,在这种情况下,始终为False!【官方解释】

question 2

Does this code print numbers from 0 to 10 inclusive?

i=10whilei>=0:x=10-iprint(x)i=i-1

1

2

3

4

5

answer:

 True

 False

解释: 是的,只是顺序相反!【官方解释】

question 3

How many iterations would be executed for the following loop?

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

1

2

3

answer : 10

解释:

您可以通过观察计数器变量的三项来以这种形式计算迭代次数:

1.起始条件,

2.它如何变化,

3.结束条件

有时它并不那么琐碎,可以在许多地方进行更改!【官方回答】

question 4

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

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

1

2

3

4

answer: 6

解释: 注意循环不变式和循环计数器,办公桌检查在这里很有帮助

Arrays and lists part one quiz

question 1(单选)

Which best defines an array?

 It’s like this:

list=['a','1','2',]

1

 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).

解释: 数组是一个通用概念,没有附加到Python或列表中。列表与数组不同。数组与列表不同。【官方解释】

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

解释: 数组始终从索引0开始。【官方解释】

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

解释: 会有错误。 不同的编程语言以不同的方式处理此问题。 通常,如果您请求了固定大小的内存,并且开始访问不在该区域内的区域,计算机将停止您的访问。

完整版答案看我头像联系

你可能感兴趣的:(USYD悉尼大学INFO1110 详细作业解析Week3 all quizzes)