Download JavaQuiz1-2012!Solution

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

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

Document related concepts
no text concepts found
Transcript
Java Programming: First Quiz
姓名:______SOLUTION_
學號:_______________
I
(84 pts) 選擇與填充: ddcad aceba dbbcd bde
1.
Which of the following is NOT a correct literal for floating-point numbers?
(a) 12.3
2.
5/4 2012
分數:______________
(b) 23.4e-2 (c) 39F (d) 40L (e) -20.0
Which of the following types could not be casted to any other type?
(a) char (b) short (c) double (d) Boolean (e) long
3.
Which of the following is not a Java keyword?
(a) package
4.
(b) import
(b) byte
x = 127;
(c) boolean
(d) double
x = ‘c’;
(b) byte
(d) float x = 12.3;
(e) float
(c)
byte x = 127;
(e) double x = 12.3;
Suppose x and y are long variables, then what is the type of the expression: (byte) x + (short) y?
(d) int
7.
(e) this
Which of the following statement is NOT correct ?
(a) int
6.
(d) int
Which of the following is not a primitive type?
(a) String
5.
(c) Private
(b) double (c) byte
(d) short
(e) float
Suppose we have the following declarations:
int i,j;
float x,y ;
double u,v;
Which of the following assignments is valid?
(a) x = u * v ;
(b) x = 4.2 - j * y;
(c) y = j / i * y;
(d) i = x;
8.
Given the class definition:
class C { int m() { int y ;
for (int x =0; x < 10; x++) { y = y + x;}
return y;
}
What is the value of the expression new C().m()?
(a) 45
9.
(b) 55
(c) no value
(d) run-time error (e) compile-time error
Which of the followings are valid array declarations?
(d) int i[] = (3,4,5,6);
(e) double[] d = new double[30];
(f) char[] r = new char(1..30);
(g) float[] f = {2.3, 4.5, 6.6};
10. Which of the following is not a correct way of declaring and initialize an array variable ?
(d) String[][]
s = new String [][4];
(e) String[][]
s = new String[10][]; ;
(f) String[]
s[] = new String[10][4];
(g) String
s[][] = null ;
11. Which of the followings is the correct header of a method declaration?
(d) public static void f(String … string, long … numbers)
1
}
(e) public static void f(long … numbers, String name)
(f) private double… f(long x, long y)
(g)
double f(double x, long… y)
12. Given the code :
int[][] scores = {{2,4,7}, {5, 9,2,1}, {1}, {}};
What is the value of the
expression: scores[2].length ?
(d) 0
(b) 1
(c) 3
(d) 4
(e) run-time error
13. In the statement : private static final double P = 3; P cannot be read because...?
(a) it is in capital letters
(b) of the word private
(c) of the use of the = sign
(d) of the word final
14. How many times is the following loop body repeated?
int[] as = new int[20];
int i = 0;
while(i < as.length) { as[i++] = 1+i++; }
(d) 6
(b)7 (c)10
(d) 19 (e) 20
15. How many ‘X’ would be printed out after executing the following for statement?
for(int k =0; k <20; k++) { System.out.print(“X”) ; }
(d) 10
(b)11 (c)19 (d) 20 (e) 21
16. Which of the following expressions have a true value?
(d) 2>3 | 3>2 && 2==3
(e) 3 > 2 || 2>1 && 3 ==2
(f) 2>3 | 2==2 & 3 != 3
(g) !2==2 | 2 != 2 & 3==3
17. What is the result of x after executing the following switch statement?
int x = 3 ;
switch(x) { case 3 : x = x + 6 ;
case 3: x++ ;
case 9:
default: x++
(d) 3
(b)
9 (c)
x++; break ;
}
10 (d) 11 (e) 12
18. Given the following class definition:
(1) public class Test {
(2)
publc static void main(String[] args) {
(3)
java.util.Date[] date = new java.util.Date[10];
(4)
System.out.println(date);
(5)
System.out.println(date[0]);
(6)
System.out.println(date[0].toString );
}
Which line would cause error?
(a)2 (b) 3 (c) 4 (d) 5 (e) 6
19. Given the class definition:
class A { int x = 10;
A(){ this(3) ; x = x /2 ; }
A(int y) { this(‘a’) ; x = x * y + 2 ; }
A(char c) { x = x * 5 ; } }
What is the value of the expression: new A().x ?
2
ANS: ________76____
20. Write the header for the static method named sumIsEven: given two int numbers and return true
iff their sum is even. Note: you need not give the body.
ANS: _static Boolean sunIsEven(int n1, int n2)_____
21. Use Math.random() to generate a random integer i such that it has the same probability for all 15 ≤
i < 30 ?
Ans: int i = __((int) (Math.random()* 15)) + 15;___________
II (26 pts) 程式設計題
1.
(8 pts) Complete the following method to return the maximum value in the input array.
static int max(int[] data) {
// assume data.length > 0
int rlt = data[0];
for(int k : data)
if( k > rlt) rlt = k ;
return rlt;
}
2.
(10 pts) A positive integer c is called a composite number if there are positive integers a and b distinct from
c such that c = a * b. Complete the following method isComposite(int c) to determine if an input number of
int type is a composite number. Note the input c may be negative or zero.
static boolean isComposite(int c) {
if(c < 4) return false ;
if( c % 2 == 0) return true ;
// now c is an odd number > 4
for(int k = 3; k <= Math.sqrt(c); k = k+2 ) {
if(c % k == 0) return true ;
}
return false;
}
3.
(8 pts) It can be proven that for all positive integers n, there exists an integer m such that all numbers in the
sequence m,m+1,…,m+n-1 are composite numbers. Using the above isComposite method as a subroutine,
3
implement the following method contiguousComposites(int n) to find a number m such that all integers in
[m, m+n-1] are composite.
static int
contiguousComposites(int n) {
// assume n > 0
int rlt = 4 ;
for(int k = 0; k < n; ){
if(isComposite(k+rlt)) k++;
else { // reset rlt and k
rlt = k + rlt + 1;
k = 0;
}
}
return rlt;
4
Related documents