Exam3(Ch5)

2020-06-07 16:19

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


Exam3(Ch5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:直流电机转速闭环控制 - 图文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: