CS302 Sample Questions for Exam #1



    Part I : True or False

  1. T A java compiler changes Java source code into Java bytecodes.
  2. T Compilation errors include mistakes in syntax.
  3. T The data type MainWindow is a reference data type.
  4. T During the analysis phase of software development, the program's requirements are specified.
  5. T Math.PI accesses the class constant PI in the Math class.
  6. T Instance methods are declared without the static modifier.
  7. T Visibility modifiers control access to the members of a class.
  8. T Arguments are passed to a method using the pass-by-value scheme.
  9. T Nesting is when a statement is placed inside the body of another statement.
  10. T Execution always begins in the main method for all Java applications.
  11. T Comments are intended for programmers.
  12. T 5.5 is an example of a literal constant.
  13. T x = y; means x is assigned the value in y.
  14. T Local variables only exists while their method executes.
  15. T The data type double is a primitive data type.
  16. F i = o.setMax(22) public int setmax(int m)
  17. F o.setData(d, 11.22) public void setData(double f,int n)
  18. F new TestObject(d,i) public TestObject(double di)
  19. F o.showValues() private void showValues()
  20. F o.find(j,i,d) public void find(double d,int i,int j)

    Part II: Multiple Choice

  21. A an if statement with a while loop nested inside
  22. E how the methods are implemented
  23. E
    if(i == 10 || i == 15) d = 2.2;
    else if(i == 25) d = 7.7;
    else d = 1.1;
    
  24. E
    int n = 5, factorial = 1;
    do {
      factorial = factorial * n;
      n = n + 1;
    } while (n > 0);
    
  25. C !coke && !pepsi
  26. C fragment 1 and fragment 3 only
  27. A i only
  28. D 7
  29. B All instances have the same values for instance variables age and weight.
  30. B 0.5

Part III: Written Answers

  1. Output of the code fragment:
    --
    ***
    ----
    *****
    ------
    
  2. The code for instantial class Bus.
    class Bus {
    
       private int maxCapacity; //maximum number of people on bus (pos. int)
       private int currentNum;  //number of people currently on bus (pos. int)
       private int totalPassengers; //total number of people ever on bus (pos. int)
       private double ticketPrice;  //amount people pay to get on bus (pos. double)
    
       /**
        * Constructor: sets currentNum and totalPassengers to 0. 
        * @param m sets the maxCapacity 
        * @param price sets the ticketPrice
        * Bugs: Does no error checking on parameters
        **/
        public Bus(int m, double price){
          totalPassengers = 0;
          currentNum = 0;
          ticketPrice = price;
          maxCapacity = m;
        }
    
    
        /**
         * calcIncome returns the income generated by all people who ever got
         * on this bus
         * @param none
         * @return totalPassengers * ticketPrice
         **/
         public double calcIncome(){
            return totalPassengers * ticketPrice;
         }
    
    
         /**
          * addPassengers returns error code if parameter is negative, otherwise
          * adds upto the capacity number of people wanting on this bus. If this 
          * would have exceeded the capacity, then return how many extra, else
          *  return 0
          * @param people number of people wanting on this bus
          * @return -1 is error, 0 is all fit, pos int is overflow
          **/
         public int addPassengers(int people){
           if(people < 0)
              return -1;
           //If we add people to bus, how much does that exceed capacity?
           int overflow = currentNum + people - maxCapacity; 
    
           if(overflow > 0) { //people is too much!
              totalPassengers += people - overflow;  //add only those who fit
              currentNum = maxCapacity;              //set to max
              return overflow;
           } else { //just add people
              currentNum += people;
              totalPassengers += people;
              return 0;
           }
        }
     
        /**
         * removePassengers returns -1 if parameter is negative, otherwise
         * removes at most all the current passengers. If people is more than 
         * the current number, then remove all and return 1. Otherwise, remove
         * requested number and return 0.
         * @param people number of people who wish to get off bus
         * @return -1 is error, 1 is underflow, 0 is OK
         **/
         public int removePassengers(int people){
           if(people < 0) 
              return -1;
           if(people > currentNum) { //people is too much!
              currentNum = 0;
              return 1;        //underflow value
           } else {            //just remove people
              currentNum -= people;
              return 0;
           }
         }
    
    }
    
  3. Write a code fragment:
    Bus busOne = new Bus(40, .5);
    Bus busTwo = new Bus(15, .75);
    
    busOne.addPassengers(22);
    busTwo.addPassengers(22);
    
    //Assume we know that busOne has more passengers
    out.printLine("The income is $" + busOne.calcIncome());