JMU CS159 - Advanced Programming
Help Policies Solutions Study-Aids Syllabus Tools
Sample Questions for Exam 1


  1. The following short answer questions are all related to debugging.
    1. Give a concise definition of the term debugging.
    2. List two different techniques for instrumenting code (for the purposes of debugging).
    3. Explain the purpose of a breakpoint.
  2. Given the following declarations:
      boolean       checked;
      double        price;
      double[]      weights;
      String[]      eids;
      

    indicate whether each of the following is a value type or a reference type.

    1. checked
    2. price
    3. weights
    4. weights[0]
    5. eids
    6. eids[0]
    7. eids[0].length()
    8. eids.length
    9. eids[0].charAt(1)
    10. "president@jmu.edu"
  3. Given the following Animation class:
        public class Animation
        {
            public void play()
            {
                System.out.println("I don't know how!");
            }
        }
        

    and the following application:

        public class Bad
        {
            public static void main(String[] arguments)
            {
                int  i;
    
                if ((arguments.length > 0) || (arguments != null)) 
                {	
                    System.out.println(arguments[0]);
                    System.out.println(arguments[arguments.length]);
                }
    
                i = 1;
                j = 2;
    
                ++i;
    
    	    Animation.play();
            }
        }
        
    1. Put an N next to all of the statements that will not compile (i.e., next to all of the statements that have syntactic defects) in the Bad class.
    2. Put an X next to all of the statements that will compile but will definitely generate an exception at run-time (i.e., faults that will definitely cause a failure at run-time).
    3. Put a ? next to all of that statements that will compile and may, depending on the parameters, generate an exception at run-time.
  4. Given the following paws() method in the Animal class:
        public static void paws(String[] a, String[] b)
        {
            String[] temp;
            temp = a;
            a    = b;
            b    = temp;
        }
    

    what would be printed by the following fragment?

        String[] u, v;
        u = new String[2];
        u[0] = "German";
        u[1] = "Shepherd";
        v = new String[2];
        v[0] = "Belgian";
        v[1] = "Malinois";
        Animal.paws(u, v);
        System.out.printf("%s %s and %s %s\n", u[0], u[1], v[0], v[1]);
    
  5. Given the following implementation of the SoundModifier class:
        public class SoundModifier
        {
            int b[];
    
            public SoundModifier(int[] a)
            {
                b = a;
            }
    
    
            public void modify()
            {
                int i;
    
    
                for (i=0; i<b.length; i++)
                {
                    b[i] = i*2;
                }
            }
    
        }
        

    What will be printed by the following code fragment?

        public class Question
        {
            public static void main(String[] args)
            {
                int             i;
                int[]           tune;
                SoundModifier   m;
    
    
    	    tune = new int[5];
    
    	    for (i=0; i<5; i++)
                {
    	        tune[i] = 100;
                }
    
    	    m = new SoundModifier(tune);
    
    	    m.modify();
    
    	    for (i=0; i<5; i++)
                {
    	        System.out.println(tune[i]);
                }
            }
        }
        
  6. Given the following RunningTime and Movie classes:
        public class RunningTime
        {
            public double hours, minutes;
      
            public RunningTime(double hrs, double mins)
            {
                hours = hrs;
                minutes = mins;
            }
        }
      
      
        public class Movie 
        {
            public RunningTime rt;
            public String      title;
      
            public Movie(RunningTime length, String title)
            {
                rt = length;
                this.title = title;
            }
        }
        

    What will be printed by the following code fragment? (Note: Be careful!)

        public class Theater
        {
            public static void main(String[] args)
            {
                Movie          frozen, matrix;
                RunningTime    length;
      
      
                length = new RunningTime(2.0, 10.0);
                matrix = new Movie(length, "The Matrix");
      
                System.out.println(matrix.title + ": " + 
                                   matrix.rt.hours+"hrs "+
                                   matrix.rt.minutes+"mins");
      
                length.hours = 5.0;
      
                System.out.println(matrix.title + ": " + 
                                   matrix.rt.hours+"hrs "+
                                   matrix.rt.minutes+"mins");
    
                frozen = matrix;
                frozen.title = "Frozen";
    
                System.out.println(matrix.title + ": " + 
                                   matrix.rt.hours+"hrs "+
                                   matrix.rt.minutes+"mins");
    
                System.out.println(frozen.title + ": " + 
                                   frozen.rt.hours+"hrs "+
                                   frozen.rt.minutes+"mins");
            }
        }
        
  7. 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 fragment? (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. Consider the following UML diagram of the Coin and Metal classes.
    Coin-Metal.png
    1. Write the declaration (only) of the attribute named weight.
    2. Write the declaration (only) of the explicit value constructor.
    3. Implement the getWeight() method.
    4. Assuming the UML diagram is a complete specification of the class, are Coin objects mutable or immutable?
    5. Write the declaration (only) of the totalWeight() method.
    6. Write a statement (in the totalWeight() method) that checks to see if memory has been allocated for the parameter name coins and throws the appropriate exception if it has not been.
    7. Assuming that the explicit value constructor in the Coin class includes the following statement:
          this.metal = new String(metal);
          

      what will the expression (target == coins[i].getMetal()) evaluate to in the totalWeight() method if the actual parameter corresponding to target is "Gold", and coins[i].getMetal() returns "Gold"?

  9. Given the Coin and Metal classes from the previous question:
    1. Write the declaration for a local variable named copper that can contain a double value.
    2. Write the declaration for a local variable named penny that is a (reference to) a Coin object.
    3. Write the declaration for a local variable named found that is a (reference to) an array of Coin objects.
    4. Write a statement that allocates enough memory for found to hold (references to) 100 Coin objects.
    5. Write a statement that assigns penny to element 3 of found.
    6. Write a statement that calls totalWeight(), passes it found and the String literal "Copper", and assigns the result to the variable named copper.
  10. Construct the following utility class.
    Geometry.png

    In addition to the specifications that are contained in the UML diagram, the categoryCount() method must comply with the following specifications.

    1. Parameter Validation
      1. If memory was not allocated for the actual parameter corresponding to the formal parameter named sides then it must throw an IllegalArgumentException.
    2. Operation
      1. For any element of sides that is less than 3 it must use 3 plus the absolute value of that element.
      2. It must calculate the number of elements in the parameter named sides that are odd and the number of elements in the parameter named sides that are even. For example, for the array {3,1,4,7,5,-2} (corresponding to a triangle, invalid value, quadrilateral, heptagon, pentagon, and another invalid value) there are two elements with an even number of sides (using the value 4 in place of the 1) and four with an odd number of sides (using the value 5 in place of the -2).
      3. It must construct a String containing the number of elements that are odd followed by the number of elements that are even.
      4. It must return the String containing the number of elements that are odd followed by the number of elements that are even.
      5. It must not change the elements of the sides array in any way (i.e., it must have no side effects).
    3. Format of the Returned String
      1. The number of odd sides and the number of even sides must each be in a field of width 4 (with no decimal point since they are integers).
      2. There must be a slash (i.e., a '/' character between the number of odd sides and the number of even sides.
  11. Professors often like to leave empty seats between students on exam days. One way to ensure that at least every-other seat is empty along both the row and column dimensions is to ensure that in even-numbered rows the even-numbered columns are empty and that in odd-numbered rows the odd-numbered columns are empty. Assuming the SeatingChart class contains the following attribute:
      private Student[][] seats
      

    and assuming a seat is empty if the corresponding element of this array is null, write a method (in the SeatingChart class) with the following signature:

      public boolean okForExams()
      

    that returns true if and only if this specification is satisfied.

  12. Lecture rooms are often deeper than they are wide, while lab rooms are often wider than they are deep. Serendipitously, for CS111 this semester, the lecture room has \(N\) rows and \(M\) columns and the lab room has \(M\) rows and \(N\) columns. To that end, write a method with the following signature:
      public static Student[][] transpose(Student[][] seats)
      

    that constructs and returns an array of arrays in which the student in element \((n,m)\) of the parameter is in element \((m,n)\) of the result. (Note: You should assume that the array of arrays is rectangular.)

  13. To support the development of unit tests for a new version of the SeatingChart class it will be helpful to have a method that verifies the dimensions of a rectangular array of arrays (a.k.a., a rectangular two-dimensional array). To that end, implement a checkShape() method with the following signature:
    public static boolean checkShape(Student[][] seats, int rows, int cols)
    

    Your implementation must satisfy the following requirements:

    It must return false if:

    1. Memory has not been allocated for `seats`.
    2. The number of rows in seats does not match the rows argument.
    3. Any row is null.
    4. Any row in has a number of columns that does not match cols.

    Otherwise, it must return true.

Copyright 2019