Computer Program Reading Comprehension Project
U of T Computer Program Reading Comprehension Project
Simple Function
A simple function utilizing class String

Download files: SimpleFunc.java
Areas: Introduction to classes and objects.


SimpleFunc.java
1/*
2 *Program found on a midterm given out by Paul Gries during Summer 2000
3 */
4
5public class SimpleFunc{
6
7    public boolean f(String s, String t, int k) {
8
9        return (s.indexOf(t) != k) || (s.charAt(k) != 'w');
10
Displaying 10 of 12 lines
View full source


Question 1
What class do indexOf() and charAt() belong to?
Solution

Question 2
If one were to make the method call f(s, t, k) with s="hello", t="he", and k=0, what would be the order of operations executed for the line (s.indexOf(t) != k) || (s.charAt(k) !='w')?
Solution

Question 3
What possible values are returned by this function?
Hints Solution

Question 4
If s.indexOf(t) != k, but t is not a substring of s, what can one say about k?
Hints Solution

Question 5
If one were to execute charAt(index) with index equal to -10, a string index out of bounds exception (i.e. java.lang.StringIndexOutOfBoundsException) would be thrown. However, the method f still works properly if parameter k is passed the value -10. Why is this so?
Hints Solution

Question 6
If the parameter k is passed the value -1, does the method f work? If yes, then under what conditions?
Hints Solution

Question 7
What would be a good precondition for f()?
Hints Solution

Question 8
What kind of exception is thrown if the first two conditions of the precondition in question 7 are not fulfilled?
Solution