IPL / Mr. Hansen
5/26/2011

Name: ___________KEY___________

“Big Quiz” (50 pts.) in preparation for Final Exam

 

Rules

  • Please print legibly. If you need more space, write OVER and continue on reverse.
  • If you have any questions regarding the test, do not stand up. Simply raise your hand and wait 30 seconds or so for your question to be addressed.
  • When you have finished, please start working on your assignment for Friday.

 

 

 

Part I: Terminology and Definitions (20 points; 5 points per numbered problem)

 

 

1.

Is the JVM (Java Virtual Machine) hardware, software, or neither? ___software_____ Explain briefly what the purpose of the JVM is. Use the term byte code in your response.

___A Java compiler translates source code into Java byte code, which can then___

___be executed by the JVM. Each operating system requires its own unique JVM._

 

 

2.

What is meant by truncation? ___chopping off the decimal portion (if any) of a floating-point value, leaving an integer___

You are given a Java method that contains these lines:
     double bubbly = −4.132;

     double crubbly = 7.58;

 

What would be the result of each of the following? Fill in the blanks provided. Warning: You should know that the Math.floor method, which is the mathematical “floor” function, always returns a double.

 

            System.out.println( (int) bubbly);   // __ −4 __

     System.out.println(Math.floor(bubbly)); // __ −5.0 __

     if ( (int)crubbly==(int)Math.floor(crubbly)) {

           System.out.println("same");

     } else {

           System.out.println("different");
     }                                      // __ same __

 

 

3.

In a __hierarchical__ object model, each class except for the root class has exactly one parent object class. Objects constructed (allocated) using a class definition are said to be __instances__ of that class. A subclass created from a higher-level class will start with all the features of the higher-order class, following an OOP paradigm known as __inheritance__, except for any changed methods or data that may be present in the subclass. Such changes are an example of __polymorphism__, which may be defined as the ability of different objects to respond in different ways to the same messages or environment.

 

 

4.

What Java calls “methods” and what Python calls “functions” are examples of functional abstraction, or more specifically, the __encapsulation__ of code details within structures that other programmers should not have to tinker with. It is a general OOP strategy to isolate the __implementation__ details of any object or method from the __interface__ that users and other programmers can see. OOP stands for __object-oriented programming__ . [Note: The hyphen in “object-oriented programming” is required.]


 

 

Part II. Longer response (points as marked)

 

 

5.
(3 pts.)







(a) Draw a state diagram to illustrate two pointers, A and B, that refer to two different objects that have been allocated from available RAM.



 

 

(8 pts.)

(b) Then explain, in clear English sentences, the standard pattern for swapping the references so that, when we are finished, A points to what B used to point to, and vice versa. Be very clear.

 

1. Make a copy of the A pointer, which we will call A_copy. In other words, there are now two identical pointers that refer to the object originally called A. There is no need to create a new object; simply create a new duplicate pointer. Optional state diagram:

 

 

 

2. Reassign the A pointer so that it points to the object referenced by B.

 

 

 

3. Reassign the B pointer so that it points to the object referenced by A_copy.

 

 

 

4. (Optional step.) Set A_copy to null, or give the pointer variable A_copy back to the heap for garbage collection.


 

6.
(12 pts.)

Write a Java method called swapIntegers that takes as its argument an array of ints. The requirements for your method are as follows:

1. Check to see if the number of entries in the array is positive and even. If not, exit immediately with an error message.

2. Assuming the check in #1 is passed, swap entry 0 with entry 1, entry 2 with entry 3, entry 4 with entry 5, and so on until reaching the end of the array.

 

 

 

One possible solution:

 

 

public static void swapIntegers(int x[]) {

   int i;                                     // loop counter

   int scratch;                               // swap space

   if ( (x.length>0) && (x.length%2 == 0) ) { // check for positive AND even size

      for (i=0; i<x.length; i=i+2) {

         scratch=x[i+1];                      // swap logic

         x[i+1]=x[i];                         // swap logic, continued

         x[i]=scratch;                        // swap logic, continued

      }

   } else {

      System.out.println("Error: Array must have positive, even number of entries.");

   }

}


 

7.
(7 pts.)

Write a Python program that contains a function called brangle. Requirements for your program are as follows:

1. The brangle function should accept a nonnegative integer less than 20 as an argument. If the argument violates any of those conditions, the function should return −999. If the argument is a positive integer, the function should return the square root of the integer, using whatever method you wish.

 

2. The rest of the program should call brangle for a range of input values from −4 to 22, inclusive, and print those results, one per line.

 

 

 

 

 

One possible solution:

 

 

import math

def brangle(x):

    if int(x)!=x:         #error if input is not an integer

        return -999

    if x>=20 or x<0:      #error unless x is nonnegative and <20

        return -999

    else:

        return math.sqrt(x)

    

n=-4

while n<=22:

    print brangle(n)

    n+=1