CSC 1350 Test 3
Consider the following code snippet. What does the array contain at the end of the program? public static fillWithRandomNumbers (double[ ] vales) { double[ ] numbers = new double[values.length]; for (int i = 0; i < numbers.length; i++) { numbers[i] = Math.random( ); } } public static void main(String[ ] args) { double[ ] num = new double[20]; fillWithRandomNumbers (num); }
20 zeros because array num is not changed by method
What is the output of the code snippet below? int[ ][ ] arr = { { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 0, 0, 0, 0 } }; int val = arr[1][2] + arr[1][3]; SYstem.out.println(val);
6
What is the content of the array below after the 3rd pass of an ascending Bubble Sort. 74. 7. 27. 76. 33. 21. 9. 30
7. 27 21. 9. 30. 33. 74. 76
Consider the following code snippet: int[ ][ ] arr = { { 1, 2, 3 }, { 4, 5, 6 } }; int val = arr[0][2] + arr[1][2]; System.out.println(val); What is the output of the given code snippet on execution?
9
Which of the following statements about constructors is NOT correct?
A call to a constructor must always have construction parameters.
Which of the following statements about classes is correct?
A class describes a set of objects with the same behavior
Which statement calls a constructor with no construction parameters?
Circle c = new Circle( );
What is the purpose of this algorithm? double total = 0; for (double element : data) { total = total + element; }
Computing the sum
Given the following class definition, which of the following are considered part of the class's public interface? public class CashRegister { public static final double DIME_VALUE = 0.1; private static int objectCounter; public void updateDimes (int dimes) {. . .} private boolean updateCounter (int counter) {. . .} }
DIME_VALUE and updateDimes
Which statements about array algorithms are true? I. The array algorithms are building blocks for many programs that process arrays. II. Java contains ready-made array algorithms for every problem situation. III. It is inefficient to make multiple passes through an array if you can do everything in one pass.
I and III only
Which statements are true about array reference? I. Assigning an array reference to another creates a second copy of the array. II. An array reference specifies the location of an array. III. Two array references can reference the same array.
II and III only
Why is the use of physical objects helpful in algorithm design?
Many people feel it is less intimidating than drawing diagrams
You have created a Motorcycle class which has a constructor with no parameters. Which of the following statements will construct an object of this class?
Motorcycle myBike = new Motorcycle( );
What is the purpose of this algorithm? for (int i = 0; i < names.length; i++) { if (i > 0) { System.out.print("; "); } System.out.print(names[i]); }
Prints out the names in an array separated by semicolons
Consider the telephone book as a physical object that can help understand algorithms. What kind of algorithm might be visualized using it?
Searching
Consider the following code snippet: int[ ][ ] arr = { { 13, 23, 33 }, { 14, 24, 34 } }; Identify the appropriate statement to display the value 24 from the given array.
System.out.println(arr[1][1]);
Which one of the following statements is correct for displaying the value in the third row and the fourth column of a two-dimensional 5 by 6 array?
System.out.println(arr[2][3]);
Consider the following code snippet: public class Coin { private String coinName; public String getCoinValue( ) {. . .} } Which of the following statements is correct?
The getCoinValue method can be accessed by any user of a Coin object
Consider the following code snippet: int val = arr[0][2]; Which value of arr is stored in the val variable?
The value in the first row and the third column
The partial linear search method below is designed to search an array of String objects. Select the expression that would be needed to complete the method. public static int search (String[ ] a, String item) { for (int i = 0; i < a.length; i++) { if ( __________________) { return i; } return -1; } }
a[I].equals(item)
What is the output of the following code snippet? public static void main (String[ ] args) { String[ ] arr = { "aaa", "bbb", "ccc" }; mystery(arr); System.out.println(arr[0] + " " + arr.length); } public static void mystery (String[ ] arr) { arr = new String[5]; arr[0] = "ddd";
aaa 3
A method in a class that returns information about an object but does not change the object is called a/an ____ method.
accessor
An instance variable declaration consists of ____.
an access specifier, the type of the instance variable, and the name of the instance variable
Another name for linear search is ____ search.
binary
Consider the following code snippet: public int getCoinValue(String coinName) { . . . } Which of the following statements is correct?
coinName is an explicit parameter
The process of hiding object data and providing methods for data access is called ____.
encapsulation
Each object of a class has a separate copy of each ___.
instance variable
Which code correctly swaps elements in a non-empty data array for valid positions j and k?
int t = data[j]; data[j] = data[k]; data[k] = t;
Which one of the following statements is a valid initialization of an array named some array of ten elements?
int[ ] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Which of the following lists the correct order of items in an instance method declaration?
modifiers, a return type, a method name, and a list of the parameters (if any)
Which of the following declares a sideLength instance variable for a Square class that stores an integer value?
private int sideLength;
Which one of the following is the correct header for a method named arrMeth that is called like this: arrMeth(intArray); //intArray is an integer array of size 3
public static void arrMeth(int[ ] ar)
Consider the following code snippet: public class Vehicle { . . . public void setVehicleAttributes (String attributes) {. . .} public String getVehicleAttributes( ) {. . .} public String getModelName( ) {. . .} } Assuming that the names of the methods reflect their action, which of the following statements about this class is correct?
setVehicleAttributes is a mutator method
A constructor is invoked when ___ to create an object.
the new keyword is used
Fill in the blank for this algorithm for computing the average of values in a data array, assuming the array is not empty. double total = 0; for (double element : data) { total = total + element; } double average = __________________;
total / data.length