homework2:根据已知代码,回答问题

本次作业要求如下:

答案:

1.Identify the fault.

在第一个程序中,for循环的判断条件是i>0,这就导致循环的时候不会读到x数组中的第一位,可能造成错误。

在第二个程序中,函数想做的是找到最后一个0的位置,但是这个循环是从前往后数的,i依次加1,这就导致:若数组中有多个0,那么只会返回第一个0对应的位置,而不是最后一个0的位子。所以如果改成从后往前数,i从x.length-1开始依次减1就可以了。

 

2. If possible, identify a test case that does not execute the fault. (Reachability)

第一个程序:可以令x=[1,4,5],y=4,  Excepted=1

第二个程序:可以令x=[2,3,0,4],  Excepted=2

 

3.If possible, identify a test case that executes the fault, but does not result in an error state.

第一个程序:可以令x=[1,4,5,6],y=3,  Excepted=-1

第二个程序:可以令x=[2,3,4,5],  Excepted=-1

 

4.If possible identify a test case that results in an error, but not a failure.

第一个程序:可以令x=[1,2,3,4],y=1,  Excepted=0

第二个程序:可以令x=[2,0,4,0],  Excepted=3

 

posted @ 2017-02-28 23:55  蟋_蟀  阅读(109)  评论(0编辑  收藏  举报