77范文网 - 专业文章范例文档资料分享平台

Exam3(Ch5)(2)

来源:网络收集 时间:2020-06-07 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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)在线全文阅读。

Exam3(Ch5)(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/1099350.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: