Sample Questions for Exam 1


  1. In Java:
    1. How would you instantiate an object of the class SoundStage?

    2. In a constructor of a derived class, how do you call the parent's constructor?

    3. What exception(s) are thrown by the parseDouble method in the Double class? Can you call the parseDouble method outside of a try-catch block? Why or why not?

    4. What are the differences between static methods and other methods.

  2. Evaluate each expression (written in Java) below or indicate why it will not compile.

    (5 % 3) + (8 / 16)

    (1)


    (0 < 5 & & < 100)

    (2)


    7 == (5 > 2)

    (3)


    !(5 == ((5 / 2) * 2))

    (4)


  3. Choose the best answer to each of the following:

    (1) _____ A class is:
    1. An instance of an object.
    2. A set of elements defined using the characteristics of the elements.
    3. Both of the above.
    4. Neither of the above.
    (2) _____ Information hiding:
    1. Prevents damage from errant external code.
    2. Makes components easier to understand/use.
    3. Both of the above.
    4. Neither of the above.
    (3) _____ Specialization is often described as the:
    1. "Is-a" relationship.
    2. "Has-a" relationship.
    3. "Uses-a" relationahip.
    4. None of the above.
    (4) _____ A private variable in the Java class Story is visible/accessible to:
    1. All objects.
    2. All objects in the Story class.
    3. All objects in the Story class and its descendants.
    4. All classes in the Java API.
    (5) _____ Abstraction involves:
    1. Focusing on what is important
    2. Being unclear
    3. Compilation into byte code
    4. All of the above

  4. Suppose the file LetterGrade contains the following:
    public enum LetterGrade
    {
           F, D, DPLUS, CMINUS, C, CPLUS, BMINUS, B, BPLUS, AMINUS, A;       
    }
        
      
    1. How would you declare a variable of type LetterGrade named eng299?

    2. How would you assign the LetterGrade corresponding to an "A-" to eng299?

    3. How would you compare two LetterGrade objects names eng299 and cs239?

  5. Given the following Animation class:
        public class Animation
        {
    
            public void play()
            {
                System.out.println("I don't know how!");
            }
        }
        

    Identify the problem(s) with the following code snippet:

        public class Bad
        {
            public static void main(String[] args)
            {
    
                int  i;
    
    
                i = 7;
                if (args[0] != null) System.out.println(args[0]);
    
    	    Animation.play();
            }
        }
        

  6. What error would be generated if you compiled and executed the following application? Would the error be generated at compile-time or run-time?
    public class StudyAid
    {
        public static void main(String[] args)
        {
    	int     denominator, numerator, ratio;
    
    
    	numerator   = 5;
    	denominator = 0;
    
    	ratio = numerator / denominator;
    	System.out.println("The answer is: "+ratio);
        }
    }
      

  7. Given the following Security, Stock, and Question classes:
        public class Security 
        {
            protected double price;
            private   int    volume;
    
            public Security()
            {
                price  = 0.0;
                volume = 0;
            }
    
    
            public String toString()
    	{
    	    String  value;
    
    	    value = "Price: "+price+"  Volume: "+volume;
    	    return value;
    	}
        }
        
        public class Stock extends Security
        {
            private String exchange;
    
    
            public Stock()
            {
                super();
                price  = 200.0;
    	    volume = 1000; 
                exchange = new String("NASDAQ");
            }
    
    
            public void toString()
            {
    	    String    value;
    
    	    value = super.toString() + " Exchange: " + exchange;
                return value;
            }
        }
        
        public class Question
        {
            public static void main(String[] args)
            {
                Stock hal;
    
    
                hal = new Stock();
                System.out.println(hal.toString());
            }
        }
        

    What errors will be generated when these classes are compiled?

  8. 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 snippet?

        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]);
                }
            }
        }
        











  9. 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 Movie(RunningTime length)
            {
                rt = length;
            }
        }
        

    What will be printed by the following code snippet?

        public class Theater
        {
            public static void main(String[] args)
            {
                Movie          matrix;
                RunningTime    rtMatrix;
      
      
                rtMatrix = new RunningTime(2.0, 10.0);
                matrix = new Movie(rtMatrix);
      
                rtMatrix.hours = 5.0;
      
                System.out.println(matrix.rt.hours+"hrs "+
                                   matrix.rt.minutes+"mins");
      
            }
        }
        

  10. Given the following Modem and SlowModem classes:
      /**
       * A partial implementation of a modem
       */
      public class Modem
      {
          protected   int   duplex, speed;
      
          /**
           * Default Constructor
           */
          public Modem()
          {
      	speed  = 38400;
      	duplex = 1;
          }
      
      
      
          /**
           * Gets the duplex
           *
           * @return  The duplex
           */
          public int getDuplex()
          {
      	return duplex;
          }
      
      
      
          /**
           * Gets the speed of the modem (in bits per second)
           *
           * @return  The speed
           */
          public int getSpeed()
          {
      	return speed;
          }
    
      }
      
      /**
       * A partial implementation of a modem
       *
       * This version is slow
       */
      public class SlowModem extends Modem
      {
      
          /**
           * Default Constructor
           */
          public SlowModem()
          {
      	super();
      	duplex = duplex / 2;
          }
      
      
          /**
           * Gets this speed
           *
           * @return  The speed
           */
          public int getSpeed()
          {
      	speed = super.getSpeed() / 2;
      	return speed;
          }
      }
      

    either identify all of the errors in the following driver (if there are any) or show what the driver will print when it is executed.

      public class ModemDriver
      {
      
          public static void main(String[] args)
          {
      	Modem         fast;
      	SlowModem     slow;
      	
      
      	fast = new Modem();
      	slow = new SlowModem();
      
      	System.out.println("Before:");
      	System.out.println("  Normal: Speed  = "+fast.getSpeed());
      	System.out.println("  Normal: Duplex = "+fast.getDuplex());
      	System.out.println("  Slow:   Speed  = "+slow.getSpeed());
      	System.out.println("  Slow:   Duplex = "+slow.getDuplex());
    	System.out.println("After:");
    	System.out.println("  Normal: Speed  = "+fast.getSpeed());
    	System.out.println("  Normal: Duplex = "+fast.getDuplex());
    	System.out.println("  Slow:   Speed  = "+slow.getSpeed());
    	System.out.println("  Slow:   Duplex = "+slow.getDuplex());
        }
      }
      

  11. 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("D. Bernstein");
           text[1]     = new String("Room 257");
           contacts[0] = new Contact(text, 1671);
    
           System.out.println(contacts[0].toString());
    
    
           text[0]     = new String("N. Harris");
           text[1]     = new String("Room 217");
           contacts[1] = new Contact(text, 8771);
    
           System.out.println(contacts[1].toString());
    
    
           text[0]     = new String("M. Norton");
           text[1]     = new String("Room 209");
           contacts[2] = new Contact(text, 2777);
    
           System.out.println(contacts[2].toString());
           
           System.out.println("Everybody");
           
           for (int i=0; i<contacts.length; i++)
           {
              System.out.println(contacts[i].toString());          
           }
        }
    }
      

  12. "Stub-out" a class that encapsulates the following:

      A combination lock is either "locked" or "unlocked". A combination lock has an associated combination (i.e., a sequence of 1 or more characters) that cannot be changed. A user can enter a combination; if the combination is correct then the combination lock becomes "unlocked". A user can also "lock" a combination lock.

  13. Complete the following class:
        /**
         * Adds the numbers entered at the command line.
         *
         * If one of the "numbers" is, in fact, not a number
         * it should be ignored.
         *
         */
        public class Adder
        {
    
    
            public static void main(String[] args)
    	{
    
    
    
    
    
    
    
    
    
    
    
            }
        }
        

    Note: If this application is executed from the command line as follows:

      java Adder 10 50 bob 40
      

    it should print out:

      100
      

  14. Given the following Score class:
        public class Score
        {
            public int      score;
            public String   name;
    
    
            /**
             * Construct a new Score
    	 *
    	 * @param score  The number of points in this Score
    	 * @param name   The person that got this score
             */
    	public Score(int score, String name)
            {
                this.score = score;
                this.name = name;
            }
        }
        

    Complete the insert method in the HighScoreList class below:

        public class HighScoreList
        {
            private Score[]   scores;
    
    
    	/**
             * Construct a new HighScoreList
             */
            public HighScoreList()
            {
                int   i;
    
                scores = new Score[10];
    	    for (i=0; i<10; i++)
                {
                    scores[i] = new Score(0," ");
                }
            }
    
    
    	/**
             * Insert the given score into the list if 
             * it is appropriate to do so
             * 
             * @param newscore   The Score to consider
    	 * @return           true if the score was inserted
             */
             public boolean insert(Score newscore)
             {
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
             }
        }
        

  15. (20 points) Given that the Review class contains the following methods (properly implemented):
    /**
     * A Review object is an encapsulation of a movie review
     */
    public class Review
    {
        /**
         * Explicit Value Constructor
         *
         * @param words     The words in this Review object
         */
        public Review(String[] words)
        
    
        /**
         * Determine if this word is a registered trademark
         *
         * @param word         The word to check
         * @return             true if the word is a trademark
         */
        protected boolean isTrademark(String word)
    
    
        /**
         * Get a particular word
         *
         * @param index    The word number
         */
        protected String getWord(int index)
    
    
        /**
         * Get the total number of words in this Review object
         *
         * @returns   The number of words
         */
        public int getNumberOfWords()
    
    
        /**
         * Set the words contained in this Review
         *
         * @param words   The words
         */
        private void setText(String[] words)
        
    
    }
      

    complete the following PrintReadyReview class. [Notes: (1) Your implementation must be consistent with the comments. (2) Make sure you understand the capabilities of the Review class before you start writing this class.]


    /**
     * A specialized Review that is ready to be printed.
     * Specifically, it knows how to create a String containing 
     * all of the words formatted into lines.
     */
    public class PrintReadyReview extends Review
    {
        private int      width;
    
    
        
        /**
         * Explicit Value Constructor - Constructs a PrintReadyReview
         * object with a maximum line width of 80 characters.
         *
         * @param words   The words in this PrintReadyReview object
         */
        public PrintReadyReview(String[] words)
        {
    
    
    
    
    
    
    
    
    
    
        }
    
    
    
    
    
        /**
         * Set the maximum line width (in characters)
         *
         * @param width     The maximum line width
         */
        public void setMaximumWidth(int width)
        {
    
    
    
    
    
    
           
        }
    
    
    
    
        /**
         * Returns a String representation of this PrintReadyReview.
         * This String contains a ' ' character between words
         * and a '\n' character at the end of each line. The
         * length() method in the String class is used to determine
         * the number of characters in each word. Each line contains
         * as many characters as possible, but not more than width.  
         * Also, lines do not break in the middle of a word.
         *
         * The String "(R)" is inserted after each word that is a 
         * registered trademark.
         */
        public String toString()
        {
    
    
        }
    }
      

Copyright 2010