CSE142 Sample Assertions Problems        handout #15

 

1. Assertions.  You will identify various assertions as being either always

   true, never true or sometimes true/sometimes false at various points in

   program execution.  The comments in the method below indicate the points

   of interest.

 

        public static void mystery(int x, int y) {

            int z = 0;

            // Point A

            while (x >= y) {

                // Point B

                x -= y;

                // Point C

                z++;

                // Point D

            }

            // Point E

            System.out.println(z + " " + x);

        }

 

   Fill in the table below with the words ALWAYS, NEVER or SOMETIMES.

 

                     x < y                x == y                z == 0

            +---------------------+---------------------+---------------------+

    Point A |                     |                     |                     |

            +---------------------+---------------------+---------------------+

    Point B |                     |                     |                     |

            +---------------------+---------------------+---------------------+

    Point C |                     |                     |                     |

            +---------------------+---------------------+---------------------+

    Point D |                     |                     |                     |

            +---------------------+---------------------+---------------------+

    Point E |                     |                     |                     |

            +---------------------+---------------------+---------------------+

 

2. Assertions.  You will identify various assertions as being either always

   true, never true or sometimes true/sometimes false at various points in

   program execution.  The comments in the method below indicate the points

   of interest.

 

        // pre : y >= 0

        // post: returns x^y

        public static int pow(int x, int y) {

            int prod = 1;

            // Point A

            while (y > 0) {

                // Point B

                if (y % 2 == 0) {

                    // Point C

                    x *= x;

                    y /= 2;

                    // Point D

                } else {

                    // Point E

                    prod *= x;

                    y--;

                    // Point F

                }

                // Point G

            }

            // Point H

            return prod;

        }

 

   Assume the method is called only if the precondition is true.  Fill in the

   table below with the words ALWAYS, NEVER or SOMETIMES.

 

                    y == 0              y % 2 == 0      

            +---------------------+---------------------+

    Point A |                     |                     |

            +---------------------+---------------------+

    Point B |                     |                     |

            +---------------------+---------------------+

    Point C |                     |                     |

            +---------------------+---------------------+

    Point D |                     |                     |

            +---------------------+---------------------+

    Point E |                     |                     |

            +---------------------+---------------------+

    Point F |                     |                     |

            +---------------------+---------------------+

    Point G |                     |                     |

            +---------------------+---------------------+

    Point H |                     |                     |

            +---------------------+---------------------+

 

 

                 Solution to CSE142 Sample Assertions Problems

 

1.                   x < y                x == y                z == 0

            +---------------------+---------------------+---------------------+

    Point A | sometimes           | sometimes           | always              |

            +---------------------+---------------------+---------------------+

    Point B | never               | sometimes           | sometimes           |

            +---------------------+---------------------+---------------------+

    Point C | sometimes           | sometimes           | sometimes           |

            +---------------------+---------------------+---------------------+

    Point D | sometimes           | sometimes           | never               |

            +---------------------+---------------------+---------------------+

    Point E | always              | never               | sometimes           |

            +---------------------+---------------------+---------------------+

 

2.                  y == 0              y % 2 == 0      

            +---------------------+---------------------+

    Point A | sometimes           | sometimes           |

            +---------------------+---------------------+

    Point B | never               | sometimes           |

            +---------------------+---------------------+

    Point C | never               | always              |

            +---------------------+---------------------+

    Point D | never               | sometimes           |

            +---------------------+---------------------+

    Point E | never               | never               |

            +---------------------+---------------------+

    Point F | sometimes           | always              |

            +---------------------+---------------------+

    Point G | sometimes           | sometimes           |

            +---------------------+---------------------+

    Point H | always              | always              |

            +---------------------+---------------------+

Stuart Reges Last modified: Fri Oct 28 16:48:55 PDT 2005