Solution: Part I.
a. (3 pts) Complete the code:
public class Test {
public static void main(String[] args) { int i = 4; int j = 5;
// Fill in the code to invoke the sum method and display the sum of i and j.
System.out.println(sum(i, j)); }
public static int sum(int i, int j) {
// Fill in the code here to return the sum of i and j. return i + j; } }
b. (3 pts) Complete the code:
public class Test {
public static void main(String[] args) { int i = 4; int j = 5;
// Fill in the code to invoke the printSum method to display the sum of i and j.
printSum(i, j); }
public static void printSum(int i, int j) {
// Fill in the code here to display the sum of i and j. System.out.println(i + j); } }
Part II. (9 pts) Show the printout of the following code:
a.
2 2 4 2 4 8 2 4 8 16 2 4 8 16 32 2 4 8 16 32 64
b.
1 2 1 2 1 4 2 1 i is 5
Part III: (8 pts):
a.
public class Test {
public static void main(String[] args) {
6
System.out.printf(\, \, \); for (int i = 1; i <= 20; i++)
System.out.printf(\, i, m(i)); }
public static double m(int i) { double sum = 0;
for (int j = 1; j <= i; j++) sum += j / (2 * j + 1.0);
return sum; } }
b.
public static void displayNumber(int n) { for (int i = 1; i <= n; i++) System.out.print(i + \ }
Part IV:
1 What is Math.rint(3.5)? A. 3 B. 5.0 C. 4 D. 3.0 E. 4.0 Key:e #
2 Suppose
static void nPrint(String message, int n) { while (n > 0) {
System.out.print(message); n--; } }
What is the printout of the call nPrint('a', 4)? A. aaaaa B. aaaa
C. invalid call, because 'a' is a character, not a string. D. aaa Key:c #
3 (char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character __________.
A. between 'a' and 'y' B. between 'b' and 'z'
7
C. between 'b' and 'y' D. between 'a' and 'z' Key:d #
4 The signature of a method consists of ____________. A. method name and parameter list
B. return type, method name, and parameter list C. parameter list D. method name Key:a #
5 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 syntax error because the second m method is defined, but not invoked in the main method.
B. The program runs and prints 2 once. C. The program runs and prints 2 twice.
D. The program has a syntax error because the two methods m have the same signature. Key:d #
6 A variable defined inside a method is referred to as __________. A. a local variable B. a block variable C. a global variable D. a method variable Key:a #
7 When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as _________. A. pass by name B. pass by value
C. pass by reference D. method invocation Key:b #
8
8 Which of the following should be declared as a void method? A. Write a method that returns a random integer from 1 to 100. B. Write a method that prints integers from 1 to 100.
C. Write a method that checks whether current second is an integer from 1 to 60. D. Write a method that converts an uppercase letter to lowercase. Key:b #
9 Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion. A. storage area B. a heap C. a stack D. an array Key:c
9
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Exam3(Ch5)(2)在线全文阅读。
相关推荐: