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

Exam3(Ch5)

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

Name:_______________________ Chapter 5 on Methods CSCI 1301 Introduction to Programming Armstrong Atlantic State University Instructor: Y. Daniel Liang

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class. Signed by ___________________.

(50 minutes)

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. }

public static int sum(int i, int j) {

// Fill in the code here to return the sum of i and 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. }

public static void printSum(int i, int j) {

// Fill in the code here to display the sum of i and j. } }

Part II. Show the printout of the following code:

a. (3 pts)

public class Test {

public static void main(String[] args) { int i = 1;

while (i <= 6) { method1(i, 2); i++; } }

public static void method1(int i, int num) { for (int j = 1; j <= i; j++) { System.out.print(num + \); num *= 2; }

System.out.println();

1

} }

b. (3 pts)

public class Test {

public static void main(String[] args) { int i = 0;

while (i <= 4) { method1(i); i++; }

System.out.println(\ + i); }

public static void method1(int i) { do {

if (i % 3 != 0)

System.out.print(i + \); i--; }

while (i >= 1);

System.out.println(); } }

Part III:

(8 pts) Write a method to compute the following series:

m(i)?13?25?...?i2i?1 Write a test program that displays the following table:

i m(i) 1 0.3333 2 0.7333 . . .

19 8.7602

20 9.2480

Here is the outline of the program:

public class Test {

public static void main(String[] args) { // Fill in the code here }

2

public static double m(int i) { // Fill in the code here } }

b. (2 pts) Write a method that will display numbers from 1 to n. The

numbers are separated by one space. The method header is as follows:

public static void displayNumber(int n)

3

Part IV: Multiple Choice Questions: (1 pts each)

(Take the multiple-choice questions online from LiveLab before midnight today. Log in and click Take Instructor Assigned Quiz for Quiz3. You have to take it before it is due. You have to submit within 20 minutes.)

1 What is Math.rint(3.5)? A. 3 B. 5.0 C. 4 D. 3.0 E. 4.0 #

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 #

3 (char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character __________.

A. between 'a' and 'y' B. between 'b' and 'z' C. between 'b' and 'y' D. between 'a' and 'z' #

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 #

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) {

4

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. #

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 #

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 #

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. #

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

5

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Exam3(Ch5)在线全文阅读。

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