JavaMidTerm-2015!Solution

advertisement
Java Programming: Midterm Examination
姓名:______________
05/01 2015
學號:__Solution________
分數:______________
I (70 pts) 選擇題
1.
Assume int[] t = {1, 2, 3, 4}. What is t.length?
(a) 0
2.
(b) 3
(b) 10000.2
(b) ^
(d) 'A'
(c) >>=
(d) ||
The JVM stores the array in an area of memory called _______.
(a) stack
5.
(c) 555f
Which of the following operators are right-associative?
(a) +
4.
(d) 5
Which of the following java values requires least number of bits?
(a) 2L
3.
(c) 4
(b) heap
(c) memory block
(d) dynamic memory
Which of the followings is the correct head of a method declaration?
(a) public int… f(long d1, int d2)
(b) public int f(long d1, int… d2)
(c) public void f(boolean … string, int … numbers)
(d) public void f(double … numbers, int name)
6.
7.
In the statement : public static final double PI = 3.14159; PI cannot be changed because...?
(a) it is in capital letters
(b) of the word public
(c) of the use of the = sign
(d) of the word final
What is the value of y after the following switch statement is executed?
int x = 3; int y = 4;
switch (x + 3) {
(a) 4
8.
(b) 3
case 6: y = 0;
(c) 2
(d) 1
default: y += 1; }
(e) 0
What is the output after invoking a method with the following code :
{ float f;
int x;
char c = 'A' ;
(a) compile-time error
9.
case 7: y = 1;
boolean b;
( b) runtime error
System.out.println(""+f + x + c + b ); }
(c) 0.00Afalse
(d) 000true
Given the following declaration :
int[][][] data = {{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}};
What is the value of data[1][1][0] ?
(a) 1
(b) 8
(c) 7
(d) 5
(e) 6
10. To check whether a char variable ch is an lowercase letter, you write ___________.
(a) ch >= 'a' && ch >= 'z'
(b)
ch >= 'a' && ch <= 'z'
(c) ch >= 'a' || ch <= 'z'
(d)
'a' <= ch <= 'z'
11. Which of the following statements is valid?
(a) int i = new int(30);
(b) double d[2] = new double[2];
(d) char[] c = new char();
(e) char[] c = new char[4]{'a', 'b', 'c', 'd'};
12. When you pass an array to a method, the method receives __________.
1
(c) int[]
i = {3, 4, 3, 2};
(a) a copy of the array
(b) a copy of the first element
(c) the reference of the array
(d) the length of the array
13. The main method header in a java class is written as:
(a)
public static void main(String[] args)
(b) public static void Main(String[] args)
(c)
public static void main(string[] args)
(d) public static main(String[] args)
14. Let sc be an instance of Scanner. Which of the following method can be used to get an int value from sc?
(a) sc.nextInteger()
(b) sc.nextInt()
(c) sc.int()
(d) sc.integer()
15. Which of the following is a correct java statement suppose x,y and z are declared int variables?
(a) x = y = z = 1; (b) x=1; y = 1; z = 1;
(c) x == y == z == 1 ; (d) x ==1; y == 1; z==1;
16. -23 % -5 = ?
(a) 3
(b) -3
(c) 2
(d) -2
(e)
0.6
17. How many times is the println method executed in the following code:
for (int i = 0; i < 10; i++){
(a)100
(b) 20
for (int j = 0; j <= i; j++) { System.out.println(i * j) ; }}
(c) 45
(d) 55
18. Which of the following is incorrect?
(a) int x = 9;
(b) long x = 9;
(c) float x = 1.0;
(d) double x = 1.0;
19. The expression 4 + 20 / (3 - 1) * 2 is evaluated to
(a) 4
(b) 20
(c) 24
(d) 9
20. To obtain the current minute, we use:
(a) % 3600
(b) % 60
(e) 25
long cminute = System.currentTimeMills() ________.
(c) / 1000 % 60
(d) / 1000 / 60 % 60
21. Given the declaration: double[] lst = {3.5, 2.0, 4.4, 5.5}; lst[1] is ________.
(a) 3.5
(b) 2.0
(c) 4.4
(d) 5.5
22. Suppose d is a double variable and x is a float variable. How many of the following statements will cause
compile-time error?
(a) 4
(b) 3
(1) x = (long) d;
(c) 2
(2) x = (int)d;
(d) 1
(3) x = d;
(4)
x = (float) d;
(e) 0
23. Suppose a method p has the following heading: void p(int[][] xx). Then which of the following is a correct
invocation of p.
(a)
p(new int[][] )
(b) p({{1,2,},{3,4,5}})
(c) p(new int[3][4])
(d) p(new int[][]{3,4,5})
24. (int) 5.678 * 100 /100.0 = _____
(a) 5.0
(b) 5.67
(c)
5
(d)
6
25. If your method does not return any value, which of the following keywords can be used as a return type?
(a) void
(b) int
(c) double
(d) public (e) None of the above
26. What is the output of the java code?
int y = 0;
(a) 9
for (int i = 0; i<10; ++i) { y += i; }
(b) 10
(c) 11
System.out.println(i) ;
(d) compile-time error
27. What is the value of k after execution of the following code?
int k= 0, count = 0;
(a) 8
(b) 9
do { k ++; } while (++count < 10);
(c) 10
(d) 11
2
28. Assume double[][] x = new double[4][5], what are x.length and x[0].length?
(a) 4 and 4
(b) 4 and 5
(c) 5 and 4
(d) 5 and 5
29. Suppose int[][] x = {{1,2,3}, null, {4,5}, {6}}. Then which of the following reference to array element will cause
error?
(a) x[0][2]
(b) x[1]
(c) x[2][1]
30. Suppose x is a long variable. Then
(a) x << 3
(b) x <<< 3
x/8
(d) x[3][1]
== __________?
(c) x >> 3
(d) x >>> 3
31. Which method in the class java.lang.System will cause the program to terminate immediately?
(a) System.terminate(0);
(b) System.halt(0);
(c) System.exit(0);
(d) System.quit(0);
32. The order of the precedence (from high to low) of the operators binary +, *, &&, ||, ^ is:
(a) &&, ||, ^, *, +
(b) *, +, ||, ^, &&
(c) *, +, ^, &&, ||
(d) *, +, ^, ||, &&
33. What is the output from System.out.println((int) Math.random() * 4)?
(a) 0
(b) 1
(c) 2
(d) 3
(e) 4
34. Given the method: boolean pg(int x) { return (x > 0 ? x + x > x : true) ;}. What about the return value of pg(n) ?
(a) true for all int n
(b) false for all int n
(c) false for some int n.
35. Which of the following are correct names for variables according to Java naming conventions?
(a) radius
(b) Radius (c) RADIUS
(d) FindArea
III (50 pts) 程式設計題
1.
[10pts] Given three doubles a,b,c, find the number (-b + square-root( b2 - 4ac) ] / 2a, which is a root of the equation
ax2+bx +c = 0. Note: the function square-root was implemented in the method Math.sqrt(n).
public static double root(double a, double b, double c) {
return (-b + Math.sqrt(b*b - 4 * a * c)) / (2 * a) ;
}
2.
[10pts] Given an array xs of int numbers, find the summation of all elements in the array which are even. For
instance, if xs is {7, 2,9,4,8}, then the result is 2 + 4 + 8 = 14.
public static int sumOfEvenElements(int[] xs) { // assume xs is not null;
int sum = 0;
for(int k : xs){
if(k % 2 == 0) sum += k ;
}
return sum;
}
3
[10 pts] Given a double x and a positive int n, compute the sum: 1 + x/1! + x2/2! + x3/3! + … + xn/n!. Note you must
3.
implement the whole computation inside the following method and cannot call any method of other or your own.
public static double sumOfExponentialSeries(double x, int n) {
double rlt = 1, term_k = 1 ;
for(int k = 1; k <= n; k++) {
term_k *= x/k ;
rlt += term_k ;
}
return rlt;
4.
}
[10pts] Given an int array xs, determine whether all its element are distinct. I.e., for all indices i,j, i != j implies
xs[j] != xs[j].
public static boolean allDistinct(int[] xs) {
for(int j = 0; j < xs.length-1; j++) {
for(int k = j+1; j < xs.length; k++){
if(xs[j] == xs[k]) return false ;
}}
return true;
}
5. [10 pts] Implement the following method to determine if the internal bit content of a given 'int' number contains the bit
pattern '1001'. Hint: Use bit mask (0x0000000F), unsigned shift right( >>>) and bit-and(&) operations.
public static boolean contains_1001(int x) {
int mask = 0x0000000F;
for(int k = 0; k< 29; k++) {
if(x & mask == 9) return true ;
x >>>= 1 ;
}
return false;
}
4
Download