reset password
Author Message
rabbott
Posts: 1649
Posted 20:26 Jan 27, 2014 |

The scores on the quiz were as follows.

Week 4 quiz score summary
Score Number of quizzes with that score
10 1
9 4
8 5
7 5
6 1
5 2
4 2
3 1

 

Even though many of the scores were not very good, only three of the questions were missed by more than 4 people. Of those 3, two were almost straight from the videos. There is no excuse for getting them wrong!

  1. What is wrong with the following code?

     

    int count = 2000 * 3000 * 4000;

    • Wrong data type 3
    • Variable is undefined 1
    • Integer overflow 15 This is the right answer. This question was almost straight from the videos.
    • Illegal expression 2
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8. Assume you have a String, bookText, representing the text contents of the book Alice In the WonderlandWhich of the following will find the number of occurrences of "Mad Hat" in bookText?

     

    a)

    public int occurrencesOfMadHat() {

    int occurrences = bookText.length();

    return occurrences;

    }

    b)

    public int occurrencesOfMadHat() {

    String word = "Mad Hat";
    int bookLength = bookText.length( );
    int lengthAfterReplace = bookText.replace(word , "").length( );
    int occurrences = (bookLength - lengthAfterReplace) / word.length( );        
    return occurrences;
    }

    c)

    public int occurrencesOfMadHat() {

    int bookLength = bookText.length();
    int afterReplace = bookText.indexOf("Mad Hat");
    int occurrences = (int) ( (bookLength - afterReplace) / word.length() );        
    return occurrences;
    }

    d)

    public int occurrencesOfMadHat() {

    return bookText.indexOf("Mad Hat");
    }
    • 1
    • 11 This is the right answer. This question was almost straight from the videos.
    • 4
    • 5
  9.  
  10. Does casting distribute over arithmetic operations? If it did, the following would be mathematical identities, i.e., they would be true for all values of and y

    In these examples, imagine that x and y are both double variables. Do these equalities hold?

    • (int) (x + y) = ((int) x) + ((int) y)
    • (int) (x - y) = ((int) x) - ((int) y)
    • (int) (x * y) = ((int) x) * ((int) y)
    • (int) (x / y) = ((int) x) / ((int) y)


    In these examples, imagine that x and y are both int variables. Do these equalities hold?

    • (double) (x + y) = ((double) x) + ((double) y)
    • (double) (x - y) = ((double) x) - ((double) y)
    • (double) (x * y) = ((double) x) * ((double) y)
    • (double) (x / y) = ((double) x) / ((double) y)
    • Casting to int distributes over arithmetic operations; casting to double distributes over arithmetic operations 4
    • Casting to int distributes over arithmetic operations; casting to double does not distribute over arithmetic operations 7
    • Casting to int does not distribute over arithmetic operations; casting to double distributes over arithmetic operations 8
    • Casting to int does not distribute over arithmetic operations; casting to double does not distribute over arithmetic operations 2  This is the right answer.  

                               Only 2 people got this right. Congratulations to the two of you!                              


                  (int) (x / y) != ((int) x) / ((int) y)
      If double x = 3.0 and double y = 1.5  then (int) (x/y) = 2 but ((int) x) / ((int) y) = 3.

                 (double) (x / y) != ((double) x) / ((double) y)
      If int x = 3 and int y = 2 then (double) (x/y) = 1.0 but ((double) x) / ((double) y) = 1.5.
Last edited by rabbott at 22:17 Jan 27, 2014.