1. Written Portion of the Final Exam - Sample

     

    Answer all of the following questions. This is a "closed book" examination and you must work entirely on your own. You may use the following reference card (written in UML).
    Final reference
    1. (5 points) Choose the best answer to each of the following:
      (1) _____ In the Java statement branch = account.charAt(0);, account is:
      1. An attribute
      2. A class
      3. A method
      4. An object
      5. A parameter
      (2) _____ In the Java statement branch = account.charAt(0);, charAt is:
      1. An attribute
      2. A class
      3. A method
      4. An object
      5. A parameter
      (3) _____ In the Java statement branch = account.charAt(i);, i is:
      1. A class
      2. A method
      3. A parameter
      4. All of the above
      5. None of the above
      (4) _____ In the Java statement owes = (balance > 0);, = is:
      1. The assignment operator
      2. A concatenation operator
      3. A logical operator
      4. A relational operator
      5. None of the above
      (5) _____ In the Java statement owes = (balance > 0);, > is:
      1. The assignment operator
      2. A concatenation operator
      3. A logical operator
      4. A relational operator
      5. None of the above
      (6) _____ In the Java statement owes = (balance > 0);, owes must be declared to be a:
      1. boolean
      2. char
      3. double
      4. int
      5. None of the above
      (7) _____ In the Java statement currency = new String("Euros");, "Euros" is:
      1. An attribute
      2. An arithmetic operator
      3. A class
      4. A parameter
      5. None of the above
      (8) _____ In the Java statement currency = new String("Euros");, currency is a variable that holds a:
      1. Fundamental/Primitive/Atomic type
      2. Reference type
      3. Value type
      4. All of the above
      5. None of the above
      (9) _____ In the Java statement january = month[0];, month is:
      1. An array
      2. An element of an array
      3. A method
      4. An operator
      5. A parameter
      (10) _____ In the Java statement january = month[0];, month[0] is:
      1. An array
      2. An element of an array
      3. A method
      4. An operator
      5. A parameter
    2. (10 points) Indicate whether each of the following will generate a compile-time error (C), a run-time error (R), or neither (N).
      Using an explicit value constructor of a class that only has a default constructor.

       

      Referring to element 10 of an array that has 10 elements.

       

      Referring to element 0 of an array that has 2 elements.

       

      Using a non-static method as if it is static (i.e., using the class name).

       

      Using a static method as if it is non-static (i.e., using a particular object).

       

      Passing an array of String objects (i.e., as the actual parameter) to a method that has a formal parameter that is declared to be a String object.

       

      Passing an array of String objects (i.e., as the actual parameter) to a method that has a formal parameter that is declared to be an array of Color objects.

       

    3. (5 points) Using the operators and rules defined in Java, evaluate the following expressions or indicate the errors they contain:
      "$" + 1.00

       

      (5 == ((5 / 3) * 3))

       

      "A" == "a"

       

      "A".equals("a")

       

      21 >= 18 && <= 65

       

    4. (5 points) Answer each of the following questions.
      What word is used to describe two methods that have the same name but different parameters (like the valueOf() methods in the String class)?

       

      What does it mean when one says an object is immutable (like objects in the String class)?

       

      In addition to the fact that it doesn't include the immutable stereotype in the UML class diagram, how can you tell that objects in the Rectangle class are mutable?

       

      In addition to their names and parameters, what else is different about the intValue() and parseInt() methods in the Integer class?

       

      Why is it important that the String, Color, Point and Rectangle classes have equals() methods?

       

    5. (10 points) Write a statement that:
      Declares m and n to be int variables.

       

      Assigns the value -7 to m.

       

      Uses the static abs method in the Math class to assign the absolute value of m to n.

       

      Declares individuals to be an array of Person objects.

       

      Initializes individuals so that it can contain n different Person objects.

       

      Declares hasCredit to be an array of boolean values.

       

      Initializes hasCredit so that it can contain as many values as there are objects in individuals. Note: You may not use m or n.

       

      Loops over all of the elements in hasCredit, assigning true to each.

       

      Declares current to be a Billable object.

       

      Instantiates current using the explicit value constructor in the Billable class that is passed an array of Person objects and an array of boolean values (passing it individuals and hasCredit).

       

    6. (5 points) Show what will be printed by the following application (assuming it is compiled and executed properly).
      public class Loopy
      {
          public static void main(String[] args)
          {
             int        sign, sum;
             
             sign = 1;
             sum  = 0;
             
             System.out.printf("%5s  %5s  %5s\n", "i", "sign", "sum");          
             for (int i=0; i<5; i++)
             {
                sum  = sum + sign * (i * i);
                System.out.printf("%5d  %5d  %5d\n", i, sign, sum);          
      
                sign = sign * -1;          
             }
          }    
      }
      
    7. (5 points) Given the following Contact class:
      public class Contact
      {
          private int            extension;    
          private String[]       info;
          
      
          public Contact(String[] info, int extension)
          {
             this.info      = info;
             this.extension = extension;       
          }
          
      
          public String toString()
          {
             String     result;
             
             result = "Contact:\n";
             for (int i=0; i<info.length; i++)
             {
                result += info[i] + "\n";
             }
             result += "x" + extension + "\n";      
      
             return result;       
          }
          
      }
        

      what will be printed by the following code snippet? (Note: Be careful!)

       
      public class Driver
      {
          public static void main(String[] args)
          {
             Contact[]      contacts;       
             String[]       text;
             
             contacts = new Contact[3];       
      
             text     = new String[2];
             
             text[0]     = new String("Prof. Bernstein");
             text[1]     = new String("Room 257");
             contacts[0] = new Contact(text, 1671);
      
             System.out.println(contacts[0].toString());
      
      
             text[0]     = new String("Prof. Mayfield");
             text[1]     = new String("Room 208");
             contacts[1] = new Contact(text, 3314);
      
             System.out.println(contacts[1].toString());
      
      
             text[0]     = new String("Prof. Sprague");
             text[1]     = new String("Room 226");
             contacts[2] = new Contact(text, 3312);
      
             System.out.println(contacts[2].toString());
             
             System.out.println("Everybody");
             
             for (int i=0; i<contacts.length; i++)
             {
                System.out.println(contacts[i].toString());          
             }
          }
      }
        
    8. (5 points) Answer one of the following questions. (Note: If you answer both, you will receive the lower of the two grades.)
      1. Complete the following method.
            /**
             * This method calculates the probability of tossing a 
             * coin n times and and getting n heads.
             *
             * The probability of getting a single head is 0.5.  The
             * probability of getting two heads is 0.5*0.5.
             * The probability of n heads is 0.5*0.5*...*0.5 (where there are
             * n terms in the product).
             *
             * @param n      The number of tosses
             * @return       The probability of n heads in n tosses
             */
            public static double probabilityOfHeads(int n)
            {
        
        
        
        
        
            }
            
      2. Complete the following method.
            /**
             * This method finds the number of open parentheses in
             * a String.
             *
             * @param  text      The String
             * @return           The number of parentheses
             */
            public static int numberOfParentheses(String text)
            {
        
        
        
        
        
            }
            

Back to Top