mt1 - Fog.ccsf.edu

advertisement
CIS 254
August 10, 2006
Summer 2006
Final Exam
Do NOT turn the page until you are told to do so.
Your name:
Problem 1
/12
Problem 2
/10
Problem 3
/12
Problem 4
/6
Problem 5
/10
Problem 6
/10
Total:
/60
You must use a pencil to write your exam. Please use a pencil to write
tour exam.
To avoid confusion, read the problems carefully. If you find it hard to
understand a problem, ask me to explain it. If you have any questions
during the exam, please ask me. Don't ask anyone else!
Partial credit may be given for wrong answers. Your exam should
contain six problems (numbered 1 through 6) on six pages. Please write your
answers only in the spaces provided on the test.
Take a deep breath, relax and Good Luck!!!
1
Problem 1 [12 points]
The following questions are worth one point each, except part b., which
is worth 4 points.
a. Evaluate the following boolean expression. Assume x=10, y=20
and z=30. Write whether the expression is true, false, always true or always
false.
!(z >= x || x > z)
Answer: Always false
b. Rewrite the following expression using DeMorgan's Law. (f is a
boolean variable). [4 points]
!(!(c != d) && (f) || (g + h <= 10))
Answer: ((c!=d) || (!f) && (g + h > 10))
c. - d. Write the value of the following expressions:
(4 + 5 / 2 % 10 - 9)
Answer: -3
((int)‘@’ + 2)
Answer: 66
e. - h. Given that String str = "Hippo". Write the value of the
following Java statements:
str.IndexOf("p")
Answer: 2
str.IndexOf("a")
Answer: -1
str.IndexOf("o")
Answer: 4
str.CharAt(1)
Answer: i
i. Write the data type of the following expression:
(7.0d - 32.7f + 9)
Answer: double
2
Problem 2 [10 points]
The following three True/False questions are worth two points each.
Circle the answer you think is the best.
a. (1 == 1.0 == "1" == '1'). False.
b. If x and y are String objects and x == y is true, then x.equals(y) is
true. True.
c. If x and y are String objects and x.equals(y) is true, then x == y is
true. False.
The next two questions that are worth two points each. Write your
answers in the spaces provided.
d. Write a regular expression for a California License plate whose format is a
single digit (not including zero), followed by three capital letters and three
digits.
Answer: “[1-9][A-Z]{3}[0-9]{3}” or “[1-9][A-Z][A-Z][A-Z][09][0-9][0-9]. (Also [1-9] = [0-9&&^0])
e. Write two different strings that match the expression "[8](foo|a)[PQ]{2}"
Answers: 8fooPP, 8fooQQ, 8fooPQ, 8fooQP, 8aPP, 8aQQ,
8aPQ or 8aQP.
3
Problem 3 [12 points]
Consider the following program. Write the values of the array when
the program is finished.
Source code:
class TestFQ3 {
public static void main( String args[] ) {
int a = 100;
Square b = new Square();
miscMethod (b, a);
System.out.println("a = " + a);
System.out.println("b = " + b.getSide());
}
public static void miscMethod(Square a, int b) {
System.out.println("c = " + b);
System.out.println("d = " + a.getSide());
b = 3000;
a.setSide(4000);
a.setside(5000);
System.out.println("e = " + b);
System.out.println("f = " + a.getSide());
}
}
class Square {
private int side;
public Square () {
side = 1; }
public int getSide() {
return side; }
public void setSide(int a) {
side = a; }
}
Output: (Write answer below this line)
c = 100
d = 1
e = 3000
f = 5000
a = 100
b = 5000
4
Problem 4 [6 points]
Consider the following program. Write the values of the array when
the program is finished.
Source code:
class TestFQ4 {
public static void main(String[] args) {
int counter, temp;
int[] array = {12, 15, 0, 22, -2, 14, 3};
for (counter = 1; counter <= 3; counter++) {
if (array[counter] > array[counter + 1]) {
temp = array[counter + 1];
array[counter + 1] = array[counter];
array[counter] = temp;
}
else
array[counter + 2] = array[counter];
} //for
} //main
} //class
Write answers in the table below.
Initial Array:
0
12
1
15
2
0
3
22
4
-2
5
14
6
3
Write the contents of the array after the program has finished:
0
12
1
0
2
15
3
15
4
22
5
5
14
6
3
Problem 5 [10 points]
Complete a method called REVERSE which, when given a positive
integer argument K of one or two digits (0-99), returns the reverse of K. For
example, REVERSE(42) should return 24, REVERSE(18) should return 81,
REVERSE(99) should return 99,and REVERSE(6) should return 60. Do not
change, move or delete any statement already written. Do not write an entire
Java program. Just write the body of the method. If you use a System.in or
System.out statement in the answer for this problem you will get zero
points on this problem.
public int REVERSE(int K){
int reverse;
if (K < 0 || K > 99)
System.out.println(“Illegal! The parameter must be between 0 and 99”);
else
K = ((K % 10) * 10) + (K / 10);
return reverse;
}
Problem 6 [10 points]
Complete a method called BACKWARDS which, when given a string
argument S, it prints the string S backwards. For example,
BACKWARDS("Hi!") should print !iH, BACKWARDS("How are you?") should
print ?uoy era woH, and BACKWARDS("6") should print 6. Do not change,
move or delete any statement already written. Do not write an entire Java
program. Just write the body of the method. If you use a System.in
statement in the answer for this problem you will get zero points on
this problem.
public void BACKWARDS(String S){
for(int i = S.length() – 1; i >= 0; i--)
System.out.println(S.charAt(i));
6
}
7
Download