Module 6

¡Supera tus tareas y exámenes ahora con Quizwiz!

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

A. between 'a' and 'z'

Analyze the following code: class Test {public static void main(String[] args) {System.out.println(xmethod(5));}public static int xmethod(int n, long t) {System.out.println("int");return n;}public static long xmethod(long n) {System.out.println("long");return n;}}

B. The program displays long followed by 5.

Multiple-Choice Question 6.10.1 (int)(Math.random() * (65535 + 1)) returns a random number __________.

C. between 0 and 65535

is achieved by separating the use of a method from its implementation.

Method Abstraction

divide and conquer

The concept of method abstraction can be applied to the process of developing programs. When writing a large program, you can use the "divide and conquer" strategy to decompose it into subproblems. The subproblems can be further decomposed into smaller, more manageable problems.

A variable defined inside a method is referred to as __________.

a local variable

Trace the gcd method to find the return value for gcd(4, 6).

gcd(4, 6) returns 2.

The signature of a method consists of ____________.

method name and parameter list

What is the output of the following code?public class Test { public static void main(String[] args) { int n = 1; m(n); System.out.print(n); } public static void m(int n) { n++; } }

1

What is the return value from invoking m("1234")? public static int m(String s) { int result = 0; for (int i = 0; i < s.length(); i++) { result = result * 10 + (s.charAt(i) - '0'); } return result; }

1234

What is the output of following code? public class Test { public static void main(String[] args) { m(1); } public static void m(int n) { n++; System.out.print(n); } }

2

What is the output from invoking m("1234")? public static void m(String s) { for (int i = s.length() - 1; i >= 0; i--) { System.out.print(s.charAt(i)); } }

4321

What is the output from invoking m(1234)? public static void m(int n) { while (n != 0) { System.out.print(n % 10); n = n / 10; } }

4321

modifier

A Java keyword that specifies the properties of data, methods, and classes and how they can be used. Examples of modifiers are public, private, and static.

method

A collection of statements grouped together to perform an operation. See class method, instance method.

stub

A simple, but not a complete version of the method. The use of stubs enables you to test invoking the method from a caller.

information hiding

A software engineering concept for hiding the detail implementation of a method for the client.

method abstraction

A technique in software development that hides detailed implementation. Method abstraction is defined as separating the use of a method from its implementation. The client can use a method without knowing how it is implemented. If you decide to change the implementation, the client program will not be affected.

pass by value

A term used when a copy of the value of the argument is passed to the method. For a parameter of a primitive type, the actual value is passed; for a parameter of a reference type, the reference for the object is passed.

parameter

A variable that is defined in the method header.

__________ is a simple but incomplete version of a method

A. A stub

The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is known as __________.

A. information hiding B. encapsulation

Suppose your method does not return any value, which of the following keywords can be used as a return type?

A. void

refers to the value passed to the method when invoking the method.

Actual Parameter

happens when more than one method can match a method class, but none is more specific than the others.

Ambiguous Invocation

public class Test { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { System.out.println("max(int, double) is invoked"); if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { System.out.println("max(double, int) is invoked"); if (num1 > num2) return num1; else return num2; }}

B. The program cannot compile because the compiler cannot determine which max method should be invoked.

__________ is to implement one method in the structure chart at a time from the top to the bottom.

B. Top-down approach

When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.

B. pass by value

Which of the following is the best for generating random integer 0 or 1?

C. (int)(Math.random() + 0.5)

Given the following method static void nPrint(String message, int n) {while (n > 0) {System.out.print(message);n--;}}What is k after invoking nPrint("A message", k)?int k = 2;nPrint("A message", k);

C. 2

All Java applications must have a method __________

C. public static void main(String[] args)

You should fill in the blank in the following code with ______________. public class Test {public static void main(String[] args) {System.out.print("The grade is " + getGrade(78.5));System.out.print("\nThe grade is " + getGrade(59.5));}public static _________ getGrade(double score) {if (score >= 90.0)return 'A';else if (score >= 80.0)return 'B';else if (score >= 70.0)return 'C';else if (score >= 60.0)return 'D';elsereturn 'F';}}

D. char

Given the following method static void nPrint(String message, int n) {while (n > 0) {System.out.print(message);n--;}}What is the output of the call nPrint('a', 4)?

D. invalid call

refers to the variables defined in the method header.

Formal Parameter

refers to that the details of the implementation are encapsulated in the method and hidden from the client who invokes the method.

Information Hiding

specifies the modifiers, return value type, method name, and parameters of the method

Method Header

is to define multiple methods with the same name, but different signatures.

Method Overloading

refers to the method name and the parameter list together.

Method Signature

method overloading

Method overloading means that you can define methods with the same name in a class as long as there is enough difference in their parameter profiles.

Does the method call in the following method cause compile errors? public static void main(String[] args) {Math.pow(2, 4);}

No

is to pass the parameters by value when invoking a method.

Pass-By-Value

argument

Same as actual parameter.

method signature

The combination of the name of a method and the list of its parameters.

scope of a variable

The portion of the program where the variable can be accessed.

public class Test {public static void main(String[] args) {System.out.println(xMethod(5, 500L));}public static int xMethod(int n, long l) {System.out.println("int, long");return n;}public static long xMethod(long n, long l) {System.out.println("long, long");return n;}}

The program displays int, long followed by 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);}}

The program has a compile error because the two methods m have the same signature. Explanation: You cannot override the methods based on the type returned.

formal parameter (i.e., parameter)

The variables defined in the method signature.

actual parameter

The variables or data to substitute formal parameters when invoking a method.

ambiguous invocation

There are two or more possible methods to match an invocation of a method, and neither is more specific than the other(s). Therefore, the invocation is ambiguous.

stepwise refinement

When writing a large program, you can use the "divide and conquer" strategy, also known as stepwise refinement, to decompose it into subproblems. The subproblems can be further decomposed into smaller, more manageable problems.

Which of the following should be defined as a void method?

Write a method that prints integers from 1 to 100.

Does the return statement in the following method cause compile errors? public static void main(String[] args) {int max = 0;if (max != 0)System.out.println(max);elsereturn;}

Yes

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 stack

(int)('a' + Math.random() * ('z' - 'a' + 1)) returns a random number __________.

between (int)'a' and (int)'z'

toThePowerOf is a method that accepts two int arguments and returns the value of the first parameter raised to the power of the second.An int variable cubeSide has already been declared and initialized. Another int variable, cubeVolume, has already been declared.Write a statement that calls toThePowerOf to compute the value of cubeSide raised to the power of 3 and that stores this value in cubeVolume.Assume that toThePowerOf is defined in the same class that calls it.

cubeVolume = toThePowerOf(cubeSide, 3);

add is a method that accepts two int arguments and returns their sum.Two int variables, euroSales and asiaSales, have already been declared and initialized. Another int variable, eurasiaSales, has already been declared.Write a statement that calls add to compute the sum of euroSales and asiaSales and that stores this value in eurasiaSales.Assume that add is defined in the same class that calls it.

eurasiaSales = add(euroSales, asiaSales);

What is the return value from invoking p(27)? public static boolean p(int n) { for (int i = 2; i < n; i++) { if (n % i == 0) { return false; } } return true; }

false

Given that a method receives three parameters a, b, c, of type double,write some code, to be included as part of the method, that checks to see if the value of a is 0;if it is, the code prints the message "no solution for a=0" and returns from the method.

if (-0.000001 < a && a < 0.0000001) { System.out.println("no solution for a = 0"); return; }

Trace the isPrime method to find the return value for isPrime(25)

isPrime(25) returns false.

What is k after the following block executes? {int k = 2;nPrint("A message", k);}System.out.println(k);

k is not defined outside the block. So, the program has a compile error

max is a method that accepts two int arguments and returns the value of the larger one. Four int variables, population1, population2, population3 and population4 have already been declared and initialized. Write an expression (not a statement!) whose value is the largest of population1, population2, population3 and population4 by calling max. Assume that max is defined in the same class that calls it.

max(max(population1, population2), max(population3, population4))

Given the int variables x, y, and z,write a fragment of code that assigns the smallest of x, y, and z to another int variable min.Assume that all the variables have already been declared and that x, y, and z have been assigned values.

min = x; if (y < min) min = y; if (z < min) min = z;

Arguments to methods always appear within __________.

parentheses

Write the definition of a method add, which receives two int parameters and returns their sum.

public static int add(int a, int b) { return a + b; }

Write the definition of a method twice, which receives an int parameter and returns an int that is twice the value of the parameter.

public static int twice(int x) { return 2 * x; }

Write the definition of a method dashedLine, with one parameter, an int. If the parameter is negative or zero, the method does nothing. Otherwise it prints a complete line terminated by a newline to standard output consisting of dashes (hyphens) with the parameter's value determining the number of dashes. The method returns nothing.

public static void dashedLine(int n) { for (int i = 0; i < n; i++) System.out.print('-'); System.out.println(); }

Write the definition of a method printDottedLine, which has no parameters and doesn't return anything. The method prints to standard output a single line (terminated by a new line) consisting of five periods.

public static void printDottedLine() { System.out.println("....."); }

Write the definition of a method printGrade, which has a char parameter and returns nothing. The method prints on a line by itself the message string Grade: followed by the char parameter (printed as a character) to standard output. Don't forget to put a new line character at the end of your line.

public static void printGrade(char ch) { System.out.println("Grade: " + ch); }

Write the code for invoking a method named sendObject. There is one argument for this method which is of type Customer.Assume that there is a reference to an object of type Customer, in a variable called John_Doe. Use this reference as your argument.Assume that sendObject is defined in the same class that calls it.

sendObject(John_Doe);

Write the code for invoking a method named sendSignal. There are no arguments for this method.Assume that sendSignal is defined in the same class that calls it.

sendSignal();

What is the return value from invoking p(7)? public static boolean p(int n) { for (int i = 2; i < n; i++) { if (n % i == 0) { return false; } } return true; }

true

You should fill in the blank in the following code with ______________.public class Test {public static void main(String[] args) {System.out.print("The grade is ");printGrade(78.5);System.out.print("The grade is ");printGrade(59.5);}public static __________ printGrade(double score) {if (score >= 90.0) {System.out.println('A');}else if (score >= 80.0) {System.out.println('B');}else if (score >= 70.0) {System.out.println('C');}else if (score >= 60.0) {System.out.println('D');}else {System.out.println('F');}}}

void


Conjuntos de estudio relacionados

Chapter 22: Nursing Care of the Child With an Alteration in Mobility/Neuromuscular or Musculoskeletal Disorder - ML8

View Set

Section 7: Promulgated Addenda, Notices, and Other Forms in Texas

View Set

Ch 19 - Food Labels and Portion Sizes

View Set

Chapter 3- Subject Matter Jurisdiction and Diversity

View Set

Formulas and Names of Common Cations and Anions

View Set