Download Java Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Programming: Midterm Examination
姓名:______________
學號:_______________
04/28 2017
分數:______________
I (30 pts) 是非題
1.
The type of the literal 0x12ff is short.
2. A formal parameter of a method and a local variable within the same method can be declared with the same name.
3.
Java bytecode is hardware dependent.
4.
It is permitted for two methods in the same class to have the same name.
5.
A field of a class and a local variable of a method in the same class can be declared using the same name.
6.
We cannot use the key word 'this' inside a static Java method to refer to any instance field or method.
7.
In order to create instances every Java class must provide explicitly at least one constructor.
8.
A java source file may contain more than one class definitions.
9.
The expression:
1 ? true : false will have value true.
10. The default value of a field int variable inside a class is 0.
11. The statement: byte x = 128; will cause a compilation error.
12. A non-public Java class cannot be accessed by code within other classes of the same package.
13. The execution of the expression 1 <= 2 || x++ > 0 will change the value of x.
14. The expression (int) 0xffffffff + 1 will cause runtime error.
15. Private constructors of a Java class are useless since they cannot be used to create instances by client code.
II (40 pts) 選擇題
16. What is the output of the following statement:
(a) 12345
(b)
339
System.out.print(1 + 2 + "3" + 4 + 5) ;
(c) 3345
(d) 1239
17. Which of the following are legal Java identifiers?
(a) my-name
(b) 3rdDay (c) @reserved
(e) 學生_3
(d) class
18. Which of the following expression has the same value as the expression x / 16, where x is int type.
(a) x >> 4
(b) x >>> 4
(c) x << 4
(d)
x <<< 4.
19. Which of the following expression has the same effect as x % 8, suppose x >= 0 is int type.
(a) x | 8
(b) x || 8
(c)
x & 0x08
(d)
x && 0x08
20. What is the hexadecimal representation of the largest int number?
(a) 0xffffffff
(b) 0x7fffffff
21. Given the statement:
(a) x[0]++ ;
(c) 0x80000000
(d) 0x00000000
double[] x = {1,2,3,4}. Which of the following statements will cause error?
(b)
x[1] += 4.1;
(c)
x[1] = --x[x.length-1];
22. What is the value of the expression: (int) 4.5 * 6.0 / 3 + 2
(a) 11
(b) 11.0
(c) 10
(d)
10.0
23. Which of the following is a not primitive data type?
(a) boolean
(b) char
(c) byte
(d) String (e) double
1
(d) x[2] = x[--x.length];
24. Given the following class:
pubic class Main { public static void main (String [] args) { System.out.println( args.length)
}
}
What is the output if we execute the following java command from OS:
>java Main Welcome to JP2017
(a) 5
(b) 4
(c) 3
(d) 15
(e) 19
25. Which of the following is not a correct way of declaring an array?
(a) int[][] a = new int[5][];
(b) int[] a[] = new int[5][0];
(c) int a[][];
(d) int[5][5] a;
26. Given the following declaration :
int[][][] data = {{{1}, {2, 3}}, {{4},{5,6},{5, 6,7}, {7, 8,9,9}} };
What is the value of data[1][2].length ?
(a) 0
27. 43 % -5 =
(b) 1
(c) 2
(d) 3
(e) none of the above
?
(a) 3
(b) -3
(c) 2
(d) -2
28. Suppose y has type int. Then which of the following is incorrect?
(a) short x = 11 + y; (b) long x = 11+y;
(c) float x = 1.1f + y;
(d) double x = 1.1 + y;
29. 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
30. Which of the following expression can be used to generate a random char among 'a' … 'z' with equal probability?
(a) (char) ('a' + ('z' - 'a') * math.random())
(b) (char) ('a' + ('z' - 'a' +1) * math.random())
(c) (char)('a' + ('z' - 'a' -1) * math.random())
(d) none of the above
31. Which of the following Java types can be used to get input from its methods?
(a) String
(b)
Scanner
(c) System
(d) File
III (50 pts) 程式設計題
1.
[15pts] Given a two dimensional array of type int[][], Implement the following method to count the number of
integer data stored in the array. Remember to deal with the case that some elements of the outer array may be null.
public static int nInts( int[][] arr ) {
2
}
2.
[10 pts] Let prices and volums be two arrays recording the prices (per share) and volumes of a stock traded in a
market in a period of days. Complete the following method to find the average stock prices of recent 5 days(五日每
股平均價) for each day in the period. Start your computation of the average value from day 4 since there is no
sufficient data to compute the average for day 0 to day 3. The first 4 elements of the returned array are not cared and
can be arbitrary values.
public static double[] avgIn5days(double[] prices, int[] amounts ) {
}
3.
[15pts] Given an integer t  0 , return an array consisting of all 2-element int array {p, q} with p > 0 such that p*q =
t. The result should be ordered according to p in increasing order.
public static int[][]
findAllProducts(int t) { // assume t  0
}
3
4.
[10 pts] Suppose p(x) = a x3 + b x2 + c x + d (a > 0) be a polynomial of integer coefficients, and   are two integers
with  > 0. If p() = 0 then x -  is a factor of p, i.e., p(x) = (x - ( a'x2+b'x+c') for some integers a',b' and c'.
According to this fact write a boolean function to determine if a polynomial (x - divides p.
public static boolean divide(int[] dv, int[] p ) { // dv = {,}; p = {a,b,c,d}
int = dv[0],  = dv[1], a = p[0],b=p[1],c=p[2],d=p[3];
}
4