RU beehive logo ITEC dept promo banner
ITEC 120
2008fall
aaray,
ejderrick,
ibarland,
jmdymacek

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive

exam02-ibarland
exam 2

ibarland v1.01

#pts possible  pts  
1 10
2 10
3 10
4 10
5 10
6 20
7 20
8 10
Total

 

 

 

 

 

 

 

 

 

  1. (10pts) T/F questions
    1. true or false?: The Java keyword “final” is always used when declaring a constant.
    2. true or false?: A java.util.Scanner object can be constructed to read input from the keyboard, or a file, or a URL.
    3. true or false?: Every for-loop can be re-written as a while-loop, and vice versa.
    4. true or false?: The body of a for loop is always executed at least once.
    5. true or false?: (n == 3 || 4) returns true whenever n is equal to three or four.
    6. true or false?: The body of for (int i=0; i<10; ++i) executes exactly 10 times.
    7. true or false?: It is best to rely Java's the order-of-precedence between && and ||, rather than use parentheses which aren't strictly needed.
    8. true or false?: To make a field a named-constant, you must do more than write it in ALL_CAPS.
    9. true or false?: When you create a java.util.LinkedList, you can provide the constructor with the maximum number of items it will hold.
    10. true or false?: If a method expects an instance of the wrapper class Double, and you accidentally pass it a primitive double, java will auto-box the primitive into an object for you.
  2. (10pts) Short Answer
    1. To test if n is between 10 and 20 (inclusive), write                                                             .

    2. How many times will the following loop-body be executed2?                 
      for (int i=20; i<25; ++i) { }
    3. To check if two Strings s1 and s2 are equal, write s1                    s2    .
    4. Suppose k=1, m=3, n=5. What do each of the following boolean expressions evaluate to? (either true (“T”) or false (“F”))
      •   k+m >= n-1                 
      •   k<m || n<m                 
      •   k<m && n<m                 
      • !(k<m && n<m)                 
      • !(!(k<m) || !(n<m))                 

    5. What Java type would you use to represent a student's name?                                                             

    6. What Java type would you use to represent the names of all the students in a class?                                                             

    7. The wrapper class for int is                                                             
  3. (10pts) Interpreting code. Answer the questions below, about the following method. (Coloring is provided for readability only.)
    /* @param weight An organism's weight, in kg.
     * @param faveFood An organism's favorite food.
     * @param maxSpeed An organism's maximum speed, in kph.
     * @return Our guess as to the organism's species.
     */
    String mysteryMethod1( double weight, String faveFood, double maxSpeed ) {
      if (maxSpeed >= 80.0) {
        return "cheetah";
        }
      else if (weight >= 1000 && !(faveFood.equals("krill")) {
        if (maxSpeed < 0.00001) {
          return "giant fungus colony";
          }
        else {
          return "brontosaurus (a.k.a apatosaurus)";
          }
        }
      else if (maxSpeed >= 15.0) {
        if (faveFood.equals("bugs") {
          return "african swallow";
          }
        else if (weight < 0.01) {
          return "blue tail fly";
          }
        else {
          return "pony";
          }
      else {
        return "jackalope";
        
        } 
      }
    

    1. What does mysteryMethod1(1111.1,"krill",80.0) return?                               

    2. What does mysteryMethod1(1111.1,"krill",1.6) return?                               

    3. What does mysteryMethod1(99,"young children",22.2) return?                               
  4. Interpreting code.
      String mysteryMethod2( String s ) {
        String resultSoFar = "";
        int i=0;
        while (i < s.length()) {
          resultSoFar = resultSoFar + s.charAt(s.length()-1-i);
          // What is the current value of resultSoFar?  (Write into column 2)
          ++i;
          // What is the current value of i?  (Write into column 1)
          }
        return resultSoFar;
        }
    
    When we call mysteryMethod2( "!star" ), complete the following table (entering information as directed in the comments).
    (N.B. There might be more rows than needed; leave the extra rows empty.)
    i resultSoFar

    0
    ""

            

                  

            

                  

            

                  

            

                  

            

                  

            

                  

            

                  
  5. (10pts) Re-write the following two lines of code using a while-loop instead of a for-loop.
    int csf = 0;  // 'cubes-so-far'
    for ( int i=0;  i < 77;  ++i ) {
      csf = csf + i*i*i;
      }
    
    
    
    
    
    
    
    
    
    
  6. The following code is supposed to take a list of weights in pounds, and return a list of those weights in kilograms (by multiplying each weight by KG_PER_POUND), omitting any “bad” measurements (that is, weights less than zero). Although this method passes a few of its test cases, it fails most of them.
     1  public static final double KG_PER_POUND = 1.0/2.2;
     2
     3  java.util.LinkedList<Double> convertAllLbsToKg( java.util.LinkedList<Double> nums ) {
     4     java.util.LinkedList<Double> numsSoFar;
    
     5     numsSoFar =                                                   ;
     6
     7     for (int i=0;  i < nums.size();  ++i) {
     8       if ( nums.get(i) >= 0.0 ) {
     9         numsSoFar.add( i * KG_PER_POUND );
    10         }
    11       }
    12     return numsSoFar;
    13     }
    
    1. (5pts) First, fill in the blank in the code so that numsSoFar is initialized to contain an empty list.
    2. (5pts) If convertAllLbsToKg is passed the list [-2.0, 1.0, -40.0], it returns the expected result. Describe what happens each time through the loop. (Fill in “n/a” if there is no applicable answer.)
      i nums.get(i) added to numsSoFar:
      0
                  

                    
      1
                  

                    
      2
                  

                    
    3. (5pts) However, if convertAllLbsToKg is passed the list containing the two numbers [2.0, 2.0], the method does not return the expected result. Describe what happens each time through the loop. (Fill in “n/a” if there is no applicable answer.)
      i nums.get(i) added tonumsSoFar:
      0
                  
                    
      1
                  
                    
    4. (5pts) How would you fix the method? (You can write over the above code, or re-write selected line(s) here.)
  7. (20pts) Write a method countZs which takes in a String, and returns how many times the letter 'Z' occurs (either uppercase or lowercase). No comments required. For example,
    countZs("") = 0,
    countZs("Aa") = 0,
    countZs("Z") = 1,
    countZs("Zyzzyva") = 3.
    (Recall the String methods listed at the start of the exam. You won't need to use all of those methods. In particular, use either substring or charAt, but not both.)
    
    
    
    
    
    
    
    
    
    
    
    
  8. (10pts) Implement the following method. Do not use the String method contains, and do not include a loop. But you should call the method from the previous problem. This shouldn't require more than 4 lines of actual code3.
    /** Return whether or not a String contains any 'Z's.
     * @param str A string to check for 'Z's.
     * @return whether or not `str` contains either 'Z' or 'z'.
     */
    
    
    
    
    
    
    

1Technically, the return type is boolean, but since true is always returned, it doesn't actually provide any information.      

2 You may assume that the loop body does not modify the index variable i.      

3 In fact, depending on your comfort with booleans as values, there is a plausible, well-indented version requiring only two lines of actual code.      

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Nov.21 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme