QMCS280 01, Spring 1998
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 large 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 ____________________ (the actual digits that make up the number), and 3) the exponent 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 not be allowed:
byte x;
double y = 45.0;
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 "not" 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. Explain the difference between the two Java statements below:
private String name;
// versus
public String name;
12. Explain why we should almost always declare class variables "private".
13. Write the Java code needed to fully define a class called "Grades" that meets the following specifications:
a. It maintains the cumulative grade points and the cumulative credits for an object.
b. It has a constructor method that accepts two parameters – cumulative grade points and cumulative credits.
c. It has a method that updates the cumulative numbers by accepting two parameters (grade points and credits) and adds these values to the current cumulative values.
d. It has a method to calculate and return the grade point average, which is the cumulative grade points divided by the cumulative credits.
14. Write a complete Java program called "Registrar" that exercises (through its "main" method) the class defined in 13 above. The program should create two objects with different grade points and credits, update the grade points and credits, and calculate a grade point average. The program should print out appropriate messages at each step.
15. Explain the difference between how primitive data types are stored in memory (by value) and how objects are stored in memory (by reference).
16. 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);
System.out.println(x);
}// method main
public static void method_two(double a, double b){
a = b;
}// method method_two
}//class Try_it
17. Briefly explain the nature of a "static" method within a class.
18, Write a complete Java program to initialize an array of integers with the values 3, 5, 3, 2, 12, 16, and 23. The program should print out each of the items in the array, compute and print the sum of the items in the array, and compute and print the average for the values.
19. List and briefly explain at least three characteristics of objects as we discussed in class.
20. Briefly explain what is meant by method "overloading".
21. Briefly explain what a "Vector" is in Java and contrast it with arrays.