Tuesday, October 20, 2015

while loops test corrections

5)What is the value of g after the following code is executed?
    boolean p = false;
    int[] a = { 5, 4, 9, 2, 5, 3, 0, -1 };
    int g = 0;

    while ( !p && g < a.length ) 
    {
      p = ( p || (a[ g ] % 3 == 0) );
      g++;
    } 
  1. 0
  2. 2
  3. 3
  4. 5
  5. 8
5) 5 is incorrect because I did not notice that the g++ was after p would have been set to true so the answer would be one more than the index of the first number divisible by x.
9)What is the value of m after the following code is executed?
    int n = 314159;
    int m = 0;
    while ( n > 0 )
    {
      m = 10 * m + n % 10;
      n /= 10;
    }
  1. 31415.9
  2. 314159
  3. 317322713
  4. 951413
  5. The program enters an infinite loop and never completes.
9) I got 9 wrong because I did not notice n was an intiger so I thought that the idea that "the more you divide the closer it gets to zero but never truly hits zero" forgetting that because it is an int and because ints truncate and thus x/10 will become zero if x is a single digit number


10)Recall that, if s and t are Strings, then s.indexOf( t ) returns -1 if t is not a substring of s. However, if t is a substring of s, then s.indexOf( t ) returns the index in s of the first character of the first appearance within s of t. For example,
"abcbcdcde".indexOf( "cd" ) returns 4
"abcbcdcde".indexOf( "dcb" ) returns -1.
Assuming that s and t are Strings, which of the following code fragments are such that the value returned by s.indexOf( t ) before the code is executed is equal to the value of the int n after the code is executed?
    int n = -1;
    int tSize = t.length();
    boolean found = false;
    while ( !found && tSize <= s.length() )
    {
      n++;
      if ( t.equals( s.substring( 0, tSize ) ) )
        found = true;
      else
        s = s.substring( 1 );
    }
    if ( !found )
      n = -1;     
    int n = -1;
    int sSize = s.length();
    int tSize = t.length();
    boolean found = false;
    while ( !found && n + tSize + 1 <= sSize )
    {
      n++;
      if ( t.equals( s.substring( n, n + tSize ) ) )
        found = true;
    }
    if ( !found )
      n = -1;     
    int n = 0;
    int sSize = s.length();
    int tSize = t.length();
    boolean found = false;
    while ( !found && n + tSize <= sSize )
    {
      if ( t.equals( s.substring( n, n + tSize ) ) )
        found = true;

      n++;
    }
    if ( found )
      n--;
    else
      n = -1;
  1. I only
  2. II only
  3. III only
  4. I and II only
  5. I, II, and III
10)I got ten wrong because I just couldn't understand how the answers were trying to a accomplish their goals so I wrote my step by step write-up incorrectly and subsequently got the wrong answer.  

2 comments:

  1. How is your Python? It would be well worth taking the time to explore this last question further, since it is essentially asking which of these algorithms implement the indexOf method's logic. Here is a little test of all three of them:

    def index_of_1(t, s):
    n = -1
    tsize = len(t)
    found = False
    while not found and tsize <= len(s):
    n += 1
    if t == s[:tsize]:
    found = True
    else:
    s = s[1:]
    if not found:
    n = -1
    return n


    def index_of_2(t, s):
    n = -1
    ssize = len(s)
    tsize = len(t)
    found = False
    while not found and tsize + n + 1 <= ssize:
    n += 1
    if t == s[n:n + tsize]:
    found = True
    if not found:
    n = -1
    return n


    def index_of_3(t, s):
    n = 0
    ssize = len(s)
    tsize = len(t)
    found = False
    while not found and n + tsize <= ssize:
    if t == s[n:n + tsize]:
    found = True
    n += 1
    if found:
    n -= 1
    else:
    n = -1
    return n

    A good set of tests could be set up to determine if they work as expected. Are you familiar with doctest? If not, let me show it to you this afternoon and ask you to write tests for these.

    ReplyDelete
  2. Ooops, comments don't preserve white space, so the indentation above has been lost. I'll share the source with you in class.

    ReplyDelete