Moment 5 Flashcards

1
Q

What dose the technique “divide and conquer” entail?

A

Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces. This technique is called divide and conquer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Methods are declared within?

A

classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Classes are typically grouped into packages so they can be?

A

imported and reused.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Methods allow you to modularize a program by?

A

separating its tasks into self-contained units. The statements in a method are written only once and hidden from other methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Using existing methods as building blocks to create new programs is a form of?

A

software reusability (p. 206) that allows you to avoid repeating code within a program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

A method call specifies the?

A

name of the method to call and provides the arguments that the called method requires to perform its task. When the method call completes, the method returns either a result, or simply control, to its caller.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

A class may contain static methods to perform?

A

common tasks that do not require an object of the class. Any data a static method might require to perform its tasks can be sent to the method as arguments in a method call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

A static method is called by?

A

specifying the name of the class in which the method is declared followed by a dot (.) and the method name, as in:

ClassName.methodName(arguments)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Class Math provides static methods for performing?

A

common mathematical calculations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The constant Math.PI (3.141592653589793; p. 208) is the?

A

ratio of a circle’s circumference to its diameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The constant Math.E (2.718281828459045; p. 208) is the?

A

base value for natural logarithms (calculated with static Math method log).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Math.PI and Math.E are declared with the?

A

modifiers public, final and static. Making them public allows you to use these fields in your own classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

A field declared with keyword final (p. 209) is?

A

constant—its value cannot be changed after it’s initialized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Both PI and E are declared final because?

A

their values never change. Making these fields static allows them to be accessed via the class name Math and a dot (.) separator, just like class Math’s methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When you execute the Java Virtual Machine (JVM) with the java command, the JVM loads the class you specify and?

A

uses that class name to invoke method main. You can specify additional command-line arguments (p. 209) that the JVM will pass to your application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What method will be called by the java command?

A

You can place a main method in every class you declare—only the main method in the class you use to execute the application will be called by the java command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Class, method and variable names are?

A

identifiers.

By convention all use camel case names. Class names begin with an uppercase letter, and method and variable names begin with a lowercase letter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Parameters are declared in?

A

a comma-separated parameter list (p. 211), which is located inside the parentheses that follow the method name in the method declaration. Multiple parameters are separated by commas. Each parameter must specify a type followed by a variable name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Every method’s body is delimited by?

A

left and right braces ({ and }).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Each method’s body contains one or more statements that?

A

perform the method’s task(s).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

The method’s return type specifies the?

A

type of data returned to a method’s caller.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Keyword void indicates that?

A

a method will perform a task but will not return any information.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Empty parentheses following a method name indicate that?

A

the method does not have parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

When a method that specifies a return type (p. 211) other than void is called and completes its task, the method must?

A

return a result to its calling method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

The return statement (p. 212) passes a?

A

value from a called method back to its caller.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

When a method is called, the program makes a copy of?

A

the method’s argument values and assigns them to the method’s corresponding parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

When program control returns to the point in the program where the method was called, the method’s parameters are?

A

removed from memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Strings can be concatenated (p. 212) using?

A

operator +, which creates a new String containing the characters of the left operand followed by those of the right operand.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Every primitive value and object in Java can be represented as a?

A

String. When an object is concatenated with a String, the object is converted to a String, then the two Strings are concatenated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

The word “true” or the word “false” is used to?

A

represent a boolean value as a String.

31
Q

All objects in Java have a special method named toString that returns a?

A

String representation of the object’s contents.

When an object is concatenated with a String, the JVM implicitly calls the object’s toString method to obtain the string representation of the object.

32
Q

You can break large String literals into?

A

several smaller Strings and place them on multiple lines of code for readability, then reassemble the Strings using concatenation.

33
Q

There are three ways to call a method, which?

A
using a method name by itself to call another method of the same class 
using a variable name, followed by a dot (.)  and a method name to call a non-static method of an object 
using the class name and a dot (.) to call a static method of a class.
34
Q

A static method can call directly only?

A

other static methods of the same class and can manipulate directly only static variables in the same class.

35
Q

There are three ways to return control to the statement that calls a method, which?

A

when the methodending right brace is reached in a method with return type void

when the following statement executes in a method with return type void:
return;

when a method returns a result with a statement of the following form in which the expression is evaluated and its result (and control) are returned to the caller:
return expression;

36
Q

Stacks are known as?

A

last-in, first-out (LIFO) data structures ()—the last item pushed (inserted) onto the stack is the first item popped (removed) from the stack.

37
Q

A called method must know how to return to its caller, so the?

A

return address of the calling method is pushed onto the method-call stack when the method is called.
If a series of method calls occurs, the successive return addresses are pushed onto the stack in last-in, first-out order so that the last method to execute will be the first to return to its caller.

38
Q

The method-call stack contains the memory for?

A

the local variables used in each invocation of a method during a program’s execution. This data is known as the method call’s stack frame or activation record.

39
Q

When a method call is made, the stack frame for that method call is?

A

pushed onto the method-call stack.

40
Q

When the method returns to its caller, its stack frame is?

A

popped off the stack and the local variables are no longer known to the program.

41
Q

If there are more method calls than can have their stack frames stored on the method-call stack,
an error known as a?

A

stack overflow occurs. The application will compile correctly, but its execution causes a stack overflow.

42
Q

Argument promotion (p. 215) converts an argument’s value to?

A

the type that the method expects to receive in its corresponding parameter.

43
Q

Promotion rules (p. 215) apply to expressions containing values of?

A

two or more primitive types and to primitive-type values passed as arguments to methods. Each value is promoted to the
“highest” type in the expression.

In cases where information may be lost due to conversion, the Java compiler requires you to use a cast operator to explicitly force the conversion to occur.

44
Q

Objects of class SecureRandom (package java.security; p. 218) can?

A

produce nondeterministic random values.

45
Q

SecureRandom method nextInt (p. 219) generates a?

A

random int value.

46
Q

Class SecureRandom provides another version of?

A

method nextInt that receives an int argument and returns a value from 0 up to, but not including, the argument’s value.

47
Q

Random numbers in a range (p. 219) can be generated with?

A

int number = shift + randomNumbers.nextInt(scale);

where shift specifies the first number in the desired range of consecutive integers, and scale specifies how many numbers are in the range.

48
Q

Random numbers can be chosen from nonconsecutive integer ranges, as in?

A

int number = shift + difference * randomNumbers.nextInt(scale);

where shift specifies the first number in the sequence, difference represents the difference between consecutive numbers in the sequence and scale specifies how many numbers are in the sequence.

49
Q

An enum type (p. 226) is introduced by?

A

the keyword enum and a type name. As with any class, braces ({ and }) delimit the body of an enum declaration. Inside the braces is a comma-separated list of enum constants, each representing a unique value. The identifiers in an enum must be unique. Variables of an enum type can be assigned only constants of that enum type.

50
Q

Constants can also be declared as?

A

private static final variables.

Such constants are declared by convention with all capital letters to make them stand out in the program.

51
Q

Scope (p. 227) is the portion of the program in which an?

A

entity, such as a variable or a method, can be referred to by its name. Such an entity is said to be “in scope” for that portion of the program.

52
Q

The scope of a parameter declaration is the?

A

body of the method in which the declaration appears.

53
Q

The scope of a local-variable declaration is from?

A

the point at which the declaration appears to the end of that block.

54
Q

The scope of a local-variable declaration that appears in the initialization section of a for statement’s header is the?

A

body of the for statement and the other expressions in the header.

55
Q

The scope of a method or field of a class is the?

A

entire body of the class.

56
Q

Any block may contain variable declarations. If a local variable or parameter in a method has the same name as a field, the field is?

A

shadowed (p. 228) until the block terminates execution

57
Q

Java allows overloaded methods (p. 230) in a class, as long as the methods have?

A

different sets of parameters (determined by the number, order and types of the parameters).

58
Q

Overloaded methods are distinguished by their?

A

signatures (p. 231)—combinations of the methods’ names and the number, types and order of their parameters, but not their return types.

59
Q

A method is invoked with a(n)?

A

method call.

60
Q

A variable known only within the method in which it’s declared is called a(n)?

A

local variable.

61
Q

The ___________________ statement in a called method can be used to pass the value of an expression
back to the calling method.

A

return.

62
Q

The keyword _______________ indicates that a method does not return a value.

A

void.

63
Q

Data can be added or removed only from the _______________ of a stack.

A

top.

64
Q

Stacks are known as _______________ data structures; the last item pushed (inserted) onto the stack is the first item popped (removed) from the stack.

A

last-in, first-out (LIFO).

65
Q

The three ways to return control from a called method to a caller are ___________ , ____________ and ___________ .

A

return; or return expression; or encountering the closing right brace of a method.

66
Q

An object of class ________________ produces truly random numbers.

A

Secure- Random.

67
Q

The method-call stack contains the memory for local variables on each invocation of a method during a program’s execution. This data, stored as a portion of the method-call stack, is known as the _____________ or ___________ of the method call.

A

stack frame, activation record.

68
Q

If there are more method calls than can be stored on the method-call stack, an error known as a(n) ______________ occurs.

A

stack overflow.

69
Q

The _______________ of a declaration is the portion of a program that can refer to the entity in the declaration by name.

A

scope.

70
Q

It’s possible to have several methods with the same name, each operating on different types or numbers of arguments. This feature is called method _______________ .

A

overloading.

71
Q

Give the method header for each of the following methods:

a) Method hypotenuse, which takes two double-precision, floating-point arguments side1 and side2 and returns a double-precision, floating-point result.

A

double hypotenuse(double side1, double side2)

72
Q

Give the method header for each of the following methods:

b) Method smallest, which takes three integers x, y and z and returns an integer.

A

int smallest(int x, int y, int z)

73
Q

Give the method header for each of the following methods:

c) Method instructions, which does not take any arguments and does not return a value.
[Note: Such methods are commonly used to display instructions to a user.]

A

void instructions()

74
Q

Method intToFloat, which takes integer argument number and returns a float.

A

float intToFloat(int number)