Moment 4 Flashcards

Ch 6 Arrays and ArrayLists Kapitel 6 (6.1 - 6.12)

1
Q

Arrays (p. 246) are?

A

fixed-length data structures consisting of related data items of the same type.

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

Types in Java are divided into two categories, which?

A

—primitive types and reference types.

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

The primitive types are?

A

boolean, byte, char, short, int, long, float and double.

All other types are reference types, so classes, which specify the types of objects, are reference types.

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

A primitive-type variable can store?

A

exactly one value of its declared type at a time.

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

Reference-type variables (called references; p. 247) store the?

A

location of an object in the computer’s memory. Such variables refer to objects in the program.

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

A reference to an object (p. 247) is required to invoke an?

A

object’s methods. A primitive-type variable does not refer to an object and therefore cannot be used to invoke a method.

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

An array is a group of variables (called elements or components; p. 247) containing values that?

A

all have the same type. Arrays are objects, so they’re considered reference types.

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

A program refers to any one of an array’s elements with an array-access expression (p. 248) that includes?

A

the name of the array followed by the index of the particular element in square brackets ([]; p. 248).

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

The first element in every array has index?

A

zero (p. 248) and is sometimes called the zeroth element.

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

An index must be a nonnegative?

A

integer. A program can use an expression as an index.

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

An array object knows its own length and stores this information in a?

A

“length” instance variable

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

To create an array object, specify the array’s?

A

element type and the number of elements as part of an array-creation expression (p. 249) that uses keyword new.

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

When an array is created, each element receives a default value, which?

A

zero for numeric primitive-type elements, false for boolean elements and null for references.

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

In an array declaration, the type and the square brackets can be combined at the beginning of the declaration to indicate that?

A

all the identifiers in the declaration are array variables.

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

Every element of a primitive-type array contains a?

A

variable of the array’s declared type.

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

Every element of a reference-type array is a reference to an?

A

object of the array’s declared type.

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

A program can create an array and initialize its elements with an?

A

array initializer (p. 251).

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

Constant variables (p. 253) are declared with keyword?

A

final, must be initialized before they’re used and cannot be modified thereafter.

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

An exception indicates a?

A

problem that occurs while a program executes.

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

Exception handling (p. 259) enables you to?

A

create fault-tolerant programs.

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

When a Java program executes, the JVM checks array indices to ensure that?

A

they’re greater than or equal to 0 and less than the array’s length.

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

If a program uses an invalid array index, Java generates an?

A

exception (p. 259) to indicate that an error occurred in the program at execution time.

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

To handle an exception, place any code that might throw an exception (p. 259) in a?

A

“try” statement.

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

The “try” block (p. 259) contains the code that might throw an exception, and the “catch” block (p. 259) contains the

A

code that handles the exception if one occurs.

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

You can have many “catch” blocks to handle?

A

different types of exceptions that might be thrown in the corresponding “try” block.

26
Q

When a “try” block terminates, any variables declared in the “try” block go?

A

out of scope.

27
Q

A “catch” block declares a

A

type and an exception parameter. Inside the “catch” block, you can use the parameter’s identifier to interact with a caught exception object.

28
Q

When a program is executed, array element indices are?

A

checked for validity

29
Q

all array element indices must be greater than or equal?

A

to 0 and less than the length of the array.

30
Q

If an attempt is made to use an invalid index to access an element, an?

A

“ArrayIndexOutOfBoundsException” (p. 259) exception occurs.

31
Q

An exception object’s “toString” method returns the?

A

exception’s error message.

32
Q

The enhanced for statement (p. 260) allows you to?

A

iterate through the elements of an array or a collection without using a counter.

33
Q

The syntax of an enhanced “for” statement is:?

A

for (Type parameter : arrayName)
{
statement
}

34
Q

The enhanced for statement cannot be used to

A

modify elements in an array.

35
Q

If a program needs to modify elements, use the?

A

traditional counter-controlled for statement.

36
Q

When an argument is passed by value, a copy of the argument’s value is made and passed to?

A

the called method. The called method works exclusively with the copy.

37
Q

When an argument is passed by reference (p. 264), the called method can access the?

A

argument’s value in the caller directly and possibly modify it.

38
Q

All arguments in Java are passed by?

A

value.

39
Q

A method call can pass two types of values to a method, which?

A

copies of primitive values and copies of references to objects. Although an object’s reference is passed by value (p. 264), a method can still interact with the referenced object by calling its public methods using the copy of the object’s reference.

40
Q

When you pass an array or an individual array element of a reference type to a method, the called method receives a?

A

copy of the array or element’s reference.

When you pass an individual element of a primitive type, the called method receives a copy of the element’s value.

41
Q

To pass an individual array element to a method, use the?

A

indexed name of the array.

42
Q

Multidimensional arrays with two dimensions are often used to?

A

represent tables of values consisting of information arranged in rows and columns.

43
Q

two-dimensional array (p. 264) with m rows and n columns is called an?

A

m-by-n array.

44
Q

An m-by-n array can be initialized with an array initializer of the form?

A

arrayType[][] arrayName = {{row1 initializer}, {row2 initializer}, …};

45
Q

Multidimensional arrays are maintained as arrays of?

A

separate one-dimensional arrays. As a result, the lengths of the rows in a two-dimensional array are not required to be the same.

46
Q

A multidimensional array with the same number of columns in every row can be created with an?

A

array-creation expression of the form

arrayType[][] arrayName = new arrayType[numRows][numColumns];

47
Q

An argument type followed by an ellipsis (…; p. 268) in a method’s parameter list indicates that?

A

the method receives a variable number of arguments of that particular type.

48
Q

The ellipsis can occur?

A

only once in a method’s parameter list. It must be at the end of the parameter list.

49
Q

A variable-length argument list (p. 268) is treated as?

A

an array within the method body. The number of arguments in the array can be obtained using the array’s length field.

50
Q

Passing arguments to main (p. 269) from the command line is achieved by?

A

including a parameter of type String[] in the parameter list of main. By convention, main’s parameter is named args.

51
Q

Java passes command-line arguments that appear after the class name in the java command to the application’s “main” method as?

A

“Strings” in the array args.

52
Q

Lists and tables of values can be stored in ____________ and _______________ .

A

arrays, collections.

53
Q

An array is a group of _______________ (called elements or components) containing values that all have the same ______________.

A

variables, type.

54
Q

The _____________ allows you to iterate through an array’s elements without using a
counter.

A

enhanced “for” statement.

55
Q

The number used to refer to a particular array element is called the element’s _____________.

A

index (or subscript or position number).

56
Q

An array that uses two indices is referred to as a(n) ___________ array.

A

two-dimensional.

57
Q

Use the enhanced for statement _______________ to walk through “double” array numbers.

A

for (double d : numbers).

58
Q

Command-line arguments are stored in _____________.

A

an array of “Strings”, called “args” by convention.

59
Q

Use the expression _____________ to receive the total number of arguments in a command line. Assume that command-line arguments are stored in String[] args.

A

args.length.

60
Q

Given the command java MyClass test, the first command-line argument is _______________.

A

test.

61
Q

A(n) __________ in the parameter list of a method indicates that the method can receive a variable number of arguments.

A

ellipsis (…).