This website is preserved for historical and scholarly reference and is no longer actively maintained.
Midterm Exam
October 17, 1997
#1. What value will be assigned to the variable m by each of the following ( 2 pts. each ).
a) int m = 20 / 3 ;
b) int m = 20 % 3 ;
c) int m = 15 / 5 ;
d) int m = 15 % 5 ;
e) int m = 3 / 20 ;
f) int m = 3 % 20 ;
#2. Draw the result (in the style of the text) of executing the following code. You should draw boxes for the variables p and q, draw hexagons for objects, and show pointers from p and q. ( 4 pts. )
Point p = new Point ( 0, 0 );
Point q = p;
#3. Draw the result (in the style of the text) of executing the following code. You should draw boxes for the variables p and q, draw hexagons for objects, and show pointers from p and q. ( 4 pts. )
Point p = new Point ( 0, 0 );
Point q = (Point) p.clone( );
#4. Draw a table giving all 4 possible truth assignments to boolean variables A and B. Fill in table values for A && B, A || B, and ! A. ( 10 pts. )
#5. Draw a flowchart (graphic illustration) for the following code segment (10 pts.)
while ( count > num )
{
if ( x >= 0 )
{
}
else
{
}
count = count - 1;
}
#6. Show the output of execution of the following code segment. (6 pts.)
int k;
int m = -1;
while ( m < 2 )
{
k = 4;
while ( k <= 7 )
{
System.out.println("m = " + m + " and k = " + k);
k ++;
}
m ++;
}
#7. Rewrite the code for problem #6 using two for-loops instead of while statements. (4 pts.)
#8. Draw a flowchart (graphic illustration) for the following code segment. (10 pts.)
if ( numGrade >= 0 )
{
if ( numGrade >= 90 )
{
}
else if ( numGrade >= 70 )
{
}
else
{
}
}
#9. 5! = __________ (Give the answer an a single integer -- 5 pts.)
#10. List the first 5 prime numbers ( 5 pts. )
#11. Select words from this list to identify with phrases below (10 pts.)
A. impartiality
B. the Market test
C. Egoism
D. the TV Test
E. Utilitarianism
F. Principle of consistency
G. Principle of respect
H. the Mom test
I. shushers
J. deontology
a. To maximize benefit to yourself or minimize harm to yourself.
b. Would everyone benefit (or would no one be harmed) if everyone were to take the action being considered.
c. These people recognize unethical action but mistakenly feel it is justified if kept secret.
d. Suggests that we treat people with dignity.
e. To consider primarily the good (or harm) to others affected by your decision.
#12 Complete the function below. The function finds and returns the smallest of four integers. Doi not write any extra code other than the code required to complete the assigned task. After you have written your code "walk through" your code to be sure it works. (10 pts.)
/**
* Function finds and returns the smallest of integers a, b, c, and d.
* Function allows for case that a = b, etc.
@param a type int
@param b type int
@param c type int
@param d type int
@return the smallest value from among a, b, c, and d.
*/
public static int smallVal ( int a, int b, int c, int d )
{
#13. Complete the function below. The function finds and returns the nth power of x. DO NOT USE THE BUILT-IN Math.Pow function. Compute this yourself (10 pts.).
Examples:
If x = 0 and n > 0, 0 is returned.
If X = 1 and n > 0, 1 is returned.
If x > 0 and n = 0, 1 is returned.
Also try such combinations as x = 2, n = 3. Result is 2 * 2 * 2 = 8.
Also try x = 5, n = 2, result is 5 * 5 = 25.
If x = 5, n = 3, result is 5 * 5 * 5 = 125.
If x = 5, n = 4, result is 5 * 5 * 5 * 5 = 625.
After writing your code step through at least the above examples to be sure your code works.
/**
* Function finds and returns the nth power of x
* for the restricted case that both x and n are >= zero.
* (Ignore the case x = 0 and n = 0.)
* @param x (type int) is >= zero.
* @param n (type int) is >= zero.
* @return x to the nth power.
*/
public static int myPower ( int x, int n )
{