RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

exam01-practice

This is a sample exam, based on a previous semester. Although it represents most of the major topics we've covered, it doesn't necessarily cover everything that will be on this semester's exam.

Instructions:
  1. (5pts) What Java type is most appropriate, to represent each of the following pieces of information — boolean, int, double, char, or String?
    1. The price of some medicine, in dollars.                         
    2. The price of some medicine, in cents.                         
    3. Instructions on how frequently to take some medicine (e.g. 3 times per day, once a month, before every meal, at age 5 only (like the Mumps vaccine), etc.).                         
    4. A person's weight, in kg.                         
    5. The weight of some medicine, in mg.                         
  2. (3pts) How many implicit type-conversions are happening in the following code?     
    System.out.println( "Hi-ho " + Math.sqrt(16) )
    Re-write the statement, making the conversions explicit (using functions in Object120): System.out.println(                                                                                                              )
  3. (1pt) In Java, + can mean one of two different functions, depending on context. What are they?                                                                                                                                                                                                                                                 
  4. (1pt) In Java, / can mean one of two different functions1, depending on context. What are they?                                                                                                                                                                                                                                                 
  5. (3pts) What is the result of the following expressions, in Java? (No calculator needed.) Include a decimal point in your answer only if the result is a double. Ignore any round-off error.
    1. 2+7/3*6+1       
    2. 2.0+7/3*6.0+1             
    3. 2.0+7.0/3.0*6.0+1             
  6. (6pts) What is the result of the following expressions, in Java? Include quotation-marks in your answer only if the result is a String.
    1. toUpperCase( "hee" + "haw" )                         
    2. toUpperCase( "hee" ) + "haw"                         
    3. toUpperCase( substring( "howdy", 1, 3) ) + "ch"                         
    4. length(toUpperCase( substring("howdy", 1, 3) )) + "ch"                         
    5. substring( "howdy", 1, length("folk")+1 )                         
    6. substring( "howdy", 1, length("folk") ) + 1                         
  7. (3pts) Browsing through the on-line Java documentation, you find a class TimeZone, you see the following2:
    static boolean inDaylightTime( String country, int year, int month, int day )
    Determine whether the given date is in daylight savings time, for the given country.
    Call this function, to find whether Peru is observing daylight savings time today.

                                                                                                                      
  8. Suppose there were a function isFrabjous which takes in an double and a String (in that order), and returns a boolean.
    1. (2pts) Declare a variable which could hold the result of calling isFrabjous.                                                             
    2. (2pts) Call isFrabjous, passing it 12.3 and aloha, and store the result in the variable you declared in (a):

                                                                                                                              
    3. (2pts) Write the signature for isFrabjous.                                                                                                                         
    1. (1pt) To see if two Strings are the same, use the function                         .
    2. (1pt) To see if two chars are the same, use                         .
    3. (1pt) When initializing a variable, use                          between the variable-name and the expression.
  9. (1pt) Write an expression determining whether a (already-initialized) variable s has the value "Wumpus".                                                             
  10. We will write a function hasPrefix which is given two Strings, and determines whether the first starts with the second. For example3, catheter has cat as a prefix, and also catheter has ca as a prefix, but catheter does not have cathy as a prefix.
    1. (4pts) Complete the following with three plausible test cases which are different from any of the ones suggested above:
        static void testHasPrefix() {
      
          System.out.println( "Actual : " +                                                              );
      
          System.out.println( "Desired: " +                                                              );
      
      
          System.out.println( "Actual : " +                                                              );
      
          System.out.println( "Desired: " +                                                              );
      
      
          System.out.println( "Actual : " +                                                              );
      
          System.out.println( "Desired: " +                                                              );
        }
          
    2. (3pts) Complete the signature for hasPrefix in the space provided below.
    3. (4pts) Complete the body for hasPrefix in the space provided below.
      hint: When calling substring, be sure that the indices you provide aren't bigger than the length of the string you're extracting the substring from!
      // (b) Signature here
    
      static                                                  (                                                  ,  
    

                                                      ) {

    // (c) Body here }
  11. You want to advertise your company, and have purchased a list of a million names and addresses. However, when you start to browse the list you see that some names include a title (either Mr. or Dr., nothing else), and some don't. You need to write a function which takes in the entire name as one String, and adds the title4 Mr. if the name didn't already include either Dr. or Mr.. For example, here are some test cases your code must pass:
        static void testAddTitleIfNeeded() {
          System.out.println( "Actual : " + addTitleIfNeeded( "Mr. Oscar Grouch" ) );
          System.out.println( "Desired: " + "Mr. Oscar Grouch" ); // no need to add title
    
          System.out.println( "Actual : " + addTitleIfNeeded( "Dr. B. Burner" ) );
          System.out.println( "Desired: " + "Dr. B. Burner" ); // no need to add title
    
          System.out.println( "Actual : " + addTitleIfNeeded( "Big Bird" ) );
          System.out.println( "Desired: " + "Mr. Big Bird" ); // NEED to add title
    
          System.out.println( "Actual : " + addTitleIfNeeded( "T" ) );
          System.out.println( "Desired: " + "Mr. T" ); // NEED to add title
        }
      

    Below, write the signature (2pts) and body (5pts) for addTitleIfNeeded. Even if you didn't finish the previous problem, you may assume that hasPrefix works correctly.

    
    
    
    
    
    
    
    
    
    
    
    
    

1 Comments aren't functions.      
2 In truth, whether you observe daylight savings can vary within a country; the actual Java classes tend to use a Locale instead of a country-name. Also, in real life inDaylightTime is actually a non-static method, but I made it static for the purposes of this exam.      
3 If you want a technical definition: String s1 has String s2 as a prefix if there is a solution to the equation equals(s1,s2+x) (where we are solving for x, which must be a String).      
4 It turns out the list of names only includes males. You might be having second thoughts about the sleaziness of the person you bought the list of names from! However, this does make your code a bit easier.      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.