A. (3 Pts) Complete the Code

Total Page:16

File Type:pdf, Size:1020Kb

A. (3 Pts) Complete the Code

Name:______CSCI 1301 Introduction to Programming Chapter 5 on Methods 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 is " + 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: 1 2 i m(i)    ...  3 5 2i 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 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("%10s%10s\n", "i", "m(i)"); for (int i = 1; i <= 20; i++) System.out.printf("%10d%10.4f\n", 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

Recommended publications