QMCS280, Spring 1999
Exam Two
Name: _________________________________________ Date:_______________
1. In the space provided, write in the value that the integer will have after execution of the code segment.
a. _____________
int x = 4;
x -= 6;
b. _____________
int y = 2, x = 3;
x *= y;
c. _____________
int y = 2, x = 3;
x /= y;
d. _____________
int y = 2, x = 3;
x = y++;
e. _____________
int y = 2, x = 3;
x = --y;
2. Fill in the blank. ___________________ occurs when a number grows too small to fit in its allocated memory space.
3. Fill in the blank. A floating point number can be represented by three components: 1) the sign of the number, 2) the mantissa (the actual digits that make up the number), and 3) the ____________________ of the base value.
4. Circle the correct answer. How many bits are used to represent characters using the Unicode coding system?
a. 4
b. 8
c. 12
d. 16
e. 20
5. In the space below, explain why the following code would be allowed:
double x;
int y = 45;
x = y;
Explanation:
6. In the space below, indicate the three ways in which primitive data types can be converted from one type to another – e.g. integer to double.
a. ____________________________________________________
b. ____________________________________________________
c. ____________________________________________________
7. Circle the correct answer. In Java, the symbol for the logical operator "or" is:
a. !
b. &
c. &&
d. ||
8. In the space below, write what the result of the following code segment would be:
int alpha = 30;
System.out.println(++alpha + " " + ++alpha + " " + alpha-- + " " + --alpha);
Output: _________________________________________________
9. In the space below, write the output of the following program:
class Exam2_9{
public static void main (String[] args){
char first = 'z';
int xcount = 0, ycount = 0, zcount = 0;
switch (first){
case 'x':
xcount++;
break;
case 'z':
zcount++;
case 'y':
ycount++;
}//switch
System.out.println("Result: " + xcount + " "
+ ycount + " " + zcount);
}//method main
Output: ______________________________________
10. Write the number of times each of the following loops will execute. Remember that zero and infinite are also possible answers.
a. _________________
int n = 4, count = 1;
do {
count++;
} while (count == n);
b. _________________
int n = 4, count = 1;
do {
count++;
} while (count < n);
c. _________________
int n = 4, count =1, sum = 0;
for (n = 0; count <= n; n++)
sum += n;
d. _________________
int n = 4, count = 1, sum = 0;
for (count = 0; count >= n; count++)
sum += n;
11. What will be printed from this Java code?
int a = 3, b = 4;
System.out.println((a > b) ? "hello a" : "hello b");
Output: _______________________________________________
12. Explain the difference between how primitive data types are stored in memory (by value) and how objects are stored in memory (by reference).
13. Briefly explain what a "Vector" is in Java and contrast it with arrays.
14. What will be printed as a result of the following program?
Output: ____________________________________
class Try_it{
public static void main(String[] args){
double x = 3.0, y = 2.0;
method_two(x,y);
}// method main
public static void method_two(double a, double b){
a = b;
System.out.println(a);
}// method method_two
}//class Try_it
15. Given the following Java code, answer the questions below.
public void paint (Graphics page) {
page.drawRect(10, 20, 30, 40);
a. What is the width of the rectangle? _________________
b. What is the "Y" coordinate of the upper left corner of the rectangle? ________
16. Briefly explain the difference between these two Java statements:
page.drawOval(10, 20, 30, 40);
and
page.fillOval(10, 20, 30, 40);
17. Circle the correct answer. Which of the following Graphics class methods will draw a closed shape with many sides?
a. drawRect()
b. drawPolyline()
c. drawPolygon()
d. drawPolyanna()
18. Circle the correct answer. Which of the following is not a valid constructor for the Color class?
a. Color (double rgb)
b. Color (int red, int blue, int green)
c. Color (float red, float blue, float green)
d. Color (int rgb)
19. Write two classes as follows:
- class Calculator -- this class will accept an array of integers (int) as a parameter in its constructor. It will store that away in its own integer array name. It will provide a method to calculate the total and average of the numbers in the array. It will also provide a method to print out the array values along with the total and average.
- class Exerciser -- this class will contain the "main" method. It will instantiate an integer array with a number of values, create an object of type "Calculator" and call the two methods in that class.
20. Write a complete Java Applet that will do the following: