IPL / Mr. Hansen |
Name:
___________KEY___________ |
“Big Quiz” (50 pts.) in preparation for Final Exam
Rules |
|
|
|
|
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. |
|
|
2. |
What is meant by truncation? ___chopping off the decimal portion (if any) of a floating-point
value, leaving an integer___ 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"); |
|
|
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. |
|
|
|
(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. |
Write a Java method called swapIntegers that takes as its argument an array of ints. The requirements for your method are as follows: |
|
|
|
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. |
Write a Python program that
contains a function called brangle. Requirements for
your program are as follows: 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 |