目录
2. (单选题)Which of the following is the best for generating random integer 0 or 1?
3. (单选题) is a simple but incomplete version of a method.
5. (单选题)Arguments to methods always appear within .
7. (单选题)What is k after the following block executes?{int k = 2;nPrint( );}System.out.println(k);
10. (单选题)The signature of a method consists of .
11. (单选题) is to implement one method in the structure chart at a time from the top to the bottom.
14. (单选题)All Java applications must have a method .
18. (单选题)A variable defined inside a method is referred to as .
19. (单选题)(char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character
21. (单选题)Which of the following should be defined as a void method?
22. (单选题)(int)('a' + Math.random() * ('z' - 'a' + 1)) returns a random number .
24. (单选题)(int)(Math.random() * (65535 + 1)) returns a random number .
一. 单选题
1. (单选题)You should fill in the blank in the following code with .
public class Test {
public static void main(String[] args) { System.out.print("The grade is " + getGrade(78.5)); System.out.print("\nThe grade is " + getGrade(59.5));
}
public static getGrade(double score) { if (score >= 90.0)
return 'A';
else if (score >= 80.0) return 'B';
else if (score >= 70.0) return 'C';
else if (score >= 60.0) return 'D';
else
return 'F';
}
}
- A. int
- B. double
- C. boolean
- D. char
- E. void
我的答案: D
2. (单选题)Which of the following is the best for generating random integer 0 or 1?
- A. (int)Math.random()
- B. (int)Math.random() + 1
- C. (int)(Math.random() + 0.5)
- D. (int)(Math.random() + 0.2)
- E. (int)(Math.random() + 0.8)
我的答案: C
3. (单选题) is a simple but incomplete version of a method.
- A. A stub
- B. A main method
- C. A non-main method
- D. A method developed using top-down approach
我的答案: A
4. (单选题)Analyze the following code.
public class Test {
public static void main(String[] args) { System.out.println(m(2));
}
public static int m(int num) { return num;
}
public static void m(int num) { System.out.println(num);
}
}
- A. The program has a compile error because the two methods m have the same signature.
- B. The program has a compile error because the second m method is defined, but not invoked in the main method.
- C. The program runs and prints 2 once.
- D. The program runs and prints 2 twice.
我的答案: A
5. (单选题)Arguments to methods always appear within .
- A. brackets
- B. parentheses
- C. curly braces
- D. quotation marks
我的答案: B
6. (单选题)Given the following method
static void nPrint(String message, int n) { while (n > 0) { System.out.print(message);
n--;
}
}
What is k after invoking nPrint("A message", k)?
int k = 2;
nPrint("A message", k);
- A. 0
- B. 1
- C. 2
- D. 3
我的答案: C
7. (单选题)What is k after the following block executes?
{
int k = 2;
nPrint( );
}
System.out.println(k);
- A. 0
- B. 1
- C. 2
- D. k is not defined outside the block. So, the program has a compile error 答案:d k is defined inside the block. Outside the block, k is not defined.
#
Section 6.10 Case Study: Generating Random Characters
我的答案: A
8. (单选题)Analyze the following code:
public class Test {
public static void main(String[] args) { System.out.println(xMethod(5, 500L));
}
public static int xMethod(int n, long l) { System.out.println("int, long");
return n;
}
public static long xMethod(long n, long l) { System.out.println("long, long");
return n;
}
}
- A. The program displays int, long followed by 5.
- B. The program displays long, long followed by 5.
- C. The program runs fine but displays things other than 5.
- D. The program does not compile because the compiler cannot distinguish which xmethod to invoke.
我的答案: A
9. (单选题)Suppose 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
我的答案: A
10. (单选题)The signature of a method consists of .
- A. method name
- B. method name and parameter list
- C. return type, method name, and parameter list
- D. parameter list
我的答案: B