Send a link to your students to track their progress
214 Terms
1
New cards
In an array declaration, this indicates the number of elements that the array will have. a. subscript b. size declarator c. element sum d. reference variable
size declarator
2
New cards
Each element of an array is accessed by a number known as a(n) __________. a. subscript b. size declarator c. address d. specifier
a. subscript
3
New cards
The first subscript in an array is always ___________. a. 1 b. 0 c. 21 d. 1 less than the number of elements
b. 0
4
New cards
The last subscript in an array is always __________. a. 100 b. 0 c. 21 d. 1 less than the number of elements
d. 1 less than the number of elements
5
New cards
Array bounds checking happens __________. a. when the program is compiled b. when the program is saved c. when the program runs d. when the program is loaded into memory
c. when the program runs
6
New cards
This array field holds the number of elements that the array has. a. size b. elements c. length d. width
c. length
7
New cards
This search algorithm steps through an array, comparing each item with the search value. a. binary search b. sequential search c. selection search d. iterative search
b. sequential search
8
New cards
This search algorithm repeatedly divides the portion of an array being searched in half. a. binary search b. sequential search c. selection search d. iterative search
a. binary search
9
New cards
This is the typical number of comparisons performed by the sequential search on anarray of N elements (assuming the search values are consistently found). a. 2N b. N c. N2 d. N/2
d. N/2
10
New cards
When initializing a two-dimensional array, you enclose each row's initialization list in___________. a. braces b. parentheses c. brackets d. quotation marks
a. braces
11
New cards
To insert an item at a specific location in an ArrayList object, you use this method. a. store b. insert c. add d. get
c. add
12
New cards
To delete an item from an ArrayList object, you use this method. a. remove b. delete c. erase d. get
a. remove
13
New cards
To determine the number of items stored in an ArrayList object, you use this method. a. size b. capacity c. items d. length
a. size
14
New cards
True or False: Java does not allow a statement to use a subscript that is outside therange of valid subscripts for an array
True
15
New cards
True or False: An array's sitze declarator can be a negative integer expression.
False
16
New cards
True or False: Both of the following declarations are legal and equivalent:int[] numbers;int numbers[];
True
17
New cards
True or False: The subscript of the last element in a single-dimensional array is oneless than the total number of elements in the array.
True
18
New cards
True or False: The values in an initialization list are stored in the array in the orderthat they appear in the list.
True
19
New cards
True or False: The Java compiler does not display an error message when it processesa statement that uses an invalid subscript.
True
20
New cards
True or False: When an array is passed to a method, the method has access to the original array
True
21
New cards
True or False: The first size declarator in the declaration of a two-dimensional arrayrepresents the number of columns. The second size declarator represents the numberof rows.
False
22
New cards
True or False: A two-dimensional array has multiple length fields.
True
23
New cards
True or False: An ArrayList automatically expands in size to accommodate the itemsstored in it.
True
24
New cards
int[] collection = new int[-20];
The size declarator cannot be negative.
25
New cards
int[] hours = 8, 12, 16;
The initialization list must be enclosed in braces.
26
New cards
int[] table = new int[10];for (int x = 1; x
The loop uses the values 1 through 10 as subscripts. It should use 0 through 9.
With an array, length is a field. With a String, length is a method. This code uses length as if it were a method in the array, and a field in the String.
28
New cards
String[] words = { "Hello", "Goodbye" };System.out.println(words.toUpperCase());
A subscript should be used with words, such as words[0].toUpperCase().
29
New cards
This is a collection of programming statements that specify the fields and methods that a particular type of object may have. a. class b. method c. parameter d. instance
a. class
30
New cards
A class is analogous to a(n) __________. a. house b. blueprint c. drafting table d. architect
b. blueprint
31
New cards
An object is a(n) __________. a. blueprint b. primitive data type c. variable d. instance of a class
d. instance of a class
32
New cards
This is a class member that holds data. a. method b. instance c. field d. constructor
c. field
33
New cards
This key word causes an object to be created in memory. a. create b. new c. object d. construct
b. new
34
New cards
This is a method that gets a value from a class's field, but does not change it. a. accessor b. constructor c. void d. mutator
a. accessor
35
New cards
This is a method that stores a value in a field or in some other way changes the value of a field. a. accessor b. constructor c. void d. mutator
d. mutator
36
New cards
When the value of an item is dependent on other data, and that item is not updated when the other data is changed, what has the value become? a. bitter b. stale c. asynchronous d. moldy
b. stale
37
New cards
This is a method that is automatically called when an instance of a class is created. a. accessor b. constructor c. void d. mutator
b. constructor
38
New cards
When a local variable has the same name as a field, the local variable's name does this to the field's name. a. shadows b. complements c. deletes d. merges with
a. shadows
39
New cards
This is automatically provided for a class if you do not write one yourself. a. accessor method b. default instance c. default constructor d. variable declaration
c. default constructor
40
New cards
Two or more methods in a class may have the same name, as long as this is different. a. their return values b. their access specifier c. their parameter lists d. their memory address
c. their parameter lists
41
New cards
The process of matching a method call with the correct method is known as__________. a. matching b. binding c. linking d. connecting
b. binding
42
New cards
A class's responsibilities are __________. a. the objects created from the class b. things the class knows c. actions the class performs d. both b and c
d. both b and c
43
New cards
True or False: The new operator creates an instance of a class.
True
44
New cards
True or False: Each instance of a class has its own set of instance fields
True
45
New cards
True or False: When you write a constructor for a class, it still has the default constructor that Java automatically provides.
False
46
New cards
True or False: A class may not have more than one constructor.
False
47
New cards
True or False: To find the classes needed for an object-oriented application, you identify all of the verbs in a description of the problem domain.
False
48
New cards
Find the error in the following class:public class MyClass{ private int x; private double y; public void MyClass(int a, double b) { x = a; y = b; }}
The constructor cannot have a return type, not even void.}
49
New cards
Assume that the following method is a member of a class. Find the error.public void total(int value1, value2, value3){ return value1 + value2 + value3;}
The method should have int as its return type
50
New cards
The following statement attempts to create a Rectangle object. Find the error.Rectangle box = new Rectangle;
The parentheses are missing. The statement should read:Rectanglebox = new Rectangle();
51
New cards
Find the error in the following class:public class TwoValues{ private int x, y; public TwoValues() { x = 0; } public TwoValues() { x = 0; y = 0; }}
The constructors must have different parameter lists. Both are empty.
52
New cards
Find the error in the following class:public class FindTheError{ public int square(int number) { return number * number; } public double square(int number) { return number * number; }}
The square methods must have different parameter lists. Bothaccept an int.
53
New cards
Design a class named Pet, which should have the following fields:• name. The name field holds the name of a pet.• animal. The animal field holds the type of animal that a pet is. Example values are"Dog", "Cat", and "Bird".• age. The age field holds the pet's age. The Pet class should also have the following methods:• setName. The setName method stores a value in the name field.• setAnimal. The setAnimal method stores a value in the animal field.• setAge. The setAge method stores a value in the age field.• getName. The getName method returns the value of the name field.• getAnimal. The getAnimal method returns the value of the animal field.• getAge. The getAge method returns the value of the age field.a. Draw a UML diagram of the class. Be sure to include notation showing each field and method's access specification and data type. Also include notation showing any method parameters and their data types.b. Write the Java code for the Pet class.
A. UML diagram B. Class code
54
New cards
Look at the following partial class definition, and then respond to the questions that follow it:public class Book{ private String title; private String author; private String publisher; private int copiesSold;}a. Write a constructor for this class. The constructor should accept an argument foreach of the fields.b. Write accessor and mutator methods for each field.c. Draw a UML diagram for the class, including the methods you have written.
a. Constructor: b. Accessor and mutator methods c. UML diagram:
55
New cards
Consider the following class declaration:public class Square{ private double sideLength; public double getArea() { return sideLength * sideLength; } public double getSideLength() { return sideLength; }}a. Write a no-arg constructor for this class. It should assign the sideLength field the value 0.0.b. Write an overloaded constructor for this class. It should accept an argument that is copied into the sideLength field.
a) public Square() { sideLength = 0.0; } b) public Square(double s) { sideLength = s; }
56
New cards
Look at the following description of a problem domain: The bank offers the following types of accounts to its customers: savings accounts, checking accounts, and money market accounts. Customers are allowed to deposit money into an account (thereby increasing its balance), withdraw money from an account (thereby decreasing its balance), and earn interest on the account. Each account has an interest rate. Assume that you are writing an application that will calculate the amount of interest earned for a bank account.a. Identify the potential classes in this problem domain.b. Refine the list to include only the necessary class or classes for this problem.c. Identify the responsibilities of the class or classes.
a) After eliminating duplicates, objects, and primitive values, the potential classes are: bank, account, and customer b) The only class needed for this particular problem is account. c) The account class knows its balance and interest rate. The account can calculate interest earned.
57
New cards
What is the difference between a class and an instance of a class?
A class is a collection of programming statements that specify the attributes and methods that a particular type of object may have. You should think of a class as a"blueprint" that describes an object. An instance of a class is an actual object that exists in memory.
58
New cards
A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses?
Classes are analogous to the blueprint.
59
New cards
What is an accessor method? What is a mutator method?
An accessor method is a method that gets a value from a class's field but does not change it. A mutator method is a method that stores a value in a field or in some other way changes the value of a field.
60
New cards
Is it a good idea to make fields private? Why or why not?
When an object's fields are hidden from outside code, the fields are protected from accidental corruption. It is good idea to make all of a class's fields private and to provide access to those fields through methods.
61
New cards
If a class has a private field, what has access to the field?
Methods that are members of the same class.
62
New cards
What is the purpose of the new key word?
It creates an object (an instance of a class) in memory.
63
New cards
Assume a program named MailList.java is stored in the DataBase folder on your hard drive. The program creates objects of the Customer and Account classes. Describe the steps that the compiler goes through in locating and compiling the Customer andAccount classes.
It looks in the current folder or directory for the file Customer.class.If that file does not exist, the compiler searches for the file Customer.java and compiles it. This creates the file Customer. class,which makes the Customer class available. The same procedure is followed when the compiler searches for the Account class.
64
New cards
Why are constructors useful for performing "start-up" operations?
Because they execute when an object is created.
65
New cards
Under what circumstances does Java automatically provide a default constructor fora class?
If you do not write a constructor for a class,Java automatically provides one.
66
New cards
What do you call a constructor that accepts no arguments?
A no-arg constructor
67
New cards
When the same name is used for two or more methods in the same class, how doesJava tell them apart?
By their signatures, which includes the method name and the data types of the method parameters, in the order that they appear.
68
New cards
How does method overloading improve the usefulness of a class?
Several different versions of the same method can be created, each performing an operation in a different manner. This makes the class more flexible.
69
New cards
Array
can hold multiple values of the same dat type simultaniously
70
New cards
Size Declarator
the number inside the brackets ex: numbers = new int[6]
71
New cards
Once an array is created, it cannot be changed
True
72
New cards
Subscript
used as an index to pinpoint a specific element within an array i.e. 6 elements, subscripts 0-5 ex: numbers[0] pronounced "numbers sub zero"
73
New cards
1. numbers[0] = 20 2. numbers[3] = 30
1. numbers sub zero is assigned 20 2. numbers sub 3 is assigned 30
74
New cards
By default, Java initializes array elements as..
Zero
75
New cards
Bounds Checking
-Occurs at runtime -if invalid subscripts, program throws exception and terminates
76
New cards
Individual elements are processed like any other type of variable
True
77
New cards
You cannot change the value of an array's length field
True
78
New cards
enhanced for loop
iterates once for every element in an array
79
New cards
int[ ] numbers = {3,6,9}
for (int val : numbers) System.out.println(val);
3 6 9
80
New cards
You cannot use the enhanced for loop when..
-you need to change the contents of array element -you need to work through elements in reverse -if you're not accessing all of the elements -if you need to work with two or more arrays -if you need to refer to the subscript number of a certain element
81
New cards
Reference Copy
int [ ] array1 = { 2,4,6]; int [] array2 = array2
-only the address of the array is copied, not the contents of the array object -after execution, both array variables will reference the same array
82
New cards
Passing an array as an argument to a method
-pass the value in the variable that references an array -when a single elements is passed, it is handled like any other argument
83
New cards
Partially filled arrays passed as an argument to a method
variable that holds the count of items must also be passed. If not, method would not be able to determine number of items stored
84
New cards
Methods may return arrays and accept as arguments
True
85
New cards
String Arrays
each string must be created individually if the array is uninitialized
86
New cards
1. In an array declaration, this indicates the number of elements that the array will have a) subscript b) size declarator c) element sum d) reference variable
b
87
New cards
each element of an array is accessed by a number known as a a) subscript b) size declarator c) address d) specifier
a
88
New cards
the first subscript in an array is always a) 1 b) 0 c) -1 d) 1 less than the number of elements
b
89
New cards
the last subscript in an array is always a) 100 b) 0 c) -1 d) 1 less than the number of elements
d
90
New cards
array bounds checking happens when the program is a) compiled b) saved c) run d) loaded into memory
c
91
New cards
this array field holds the number of elements that the array has a) size b) elements c) length d) width
c
92
New cards
this search algorithm steps through an array comparing each item with the search value a) binary search b) sequential search c) selection search d) iterative search
b
93
New cards
this search algorithm repeatedly divides the portion of an array being searched in half a) binary search b) sequential search c) selection search d) iterative search
a
94
New cards
this is the typical number of comparisons performed by the sequential search on an array of N elements a) 2N b) N c) Nsquared d) N/2
d
95
New cards
when initializing a two-dimensional array, you enclose each row's initialization list in a) braces b) parenthesis c) brackets d) quotation marks
a
96
New cards
to insert an item at a specific location in an ArrayList object, you use this method a) store b) insert c) add d) get
c
97
New cards
to delete an item from an ArrayList object, you use this method a)remove b)delete c)erase d)get
a
98
New cards
to determine the number of items stored in an ArrayList object, you use this method a)size b) capacity c) items d) length
a
99
New cards
T/F Java does no allow a statement to use a subscript that is outside the range of valid subscripts for an array
true
100
New cards
T/F an array's size declarator can be a negative integer expression