MIS 207 Exam 3

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

When an integer literal is added to an array list declared as ArrayList<Integer>, Java performs ___________.

auto-boxing

Consider an array list values declared as ArrayList<Double> that has been constructed and populated. What concept does the given code represent? double d = values.get(0);

auto-unboxing

Consider the following code snippet: String[] data = { "123", "ghi", "jkl", "def", "%&*" }; Which statement sorts the data array in ascending order?

data = Arrays.sort(data);

Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; String [] data2; In Java 6 and later, which statement copies the data array to the data2 array?

data2 = Arrays.copyOf(data, data.length);

Fill in the blank to find the total for the specific row of a two-dimensional integer array data2D with dimensions ROWS and COLUMNS? int total = 0; for (int i = 0; i < COLUMNS; i++) { total = total + ______________; }

data2D[row][i]

Java 7 introduced enhanced syntax for declaring array lists, which is termed _________________.

diamond syntax

Select the statement that reveals the logic error in the following method. public static double minimum(double[] data) { double smallest = 0.0; for (int i = 0; i < data.length; i++) { if (data[i] < smallest) { smallest = data[i]; } } return smallest; }

double m = minimum(new double[] { 12.2, 31.5, 6.6, 2.4 });

You need to write a method that calculates the shipping cost for an appliance, which depends on the item's 3D dimensions (width, height, depth) and weight. What should be the inputs and their data types for this method?

double width, double height, double depth, double weight

You need to write a method that calculates the volume for a shape, which depends on the shape's length, width, and height. What should be the parameter variables and their data types for this method?

double width, double length, double height

Consider the following 2-dimensional array. Select the statement that gives the number of columns in the third row. int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } };

int cols = counts[2].length;

Which code snippet calculates the sum of all the even elements in an array values?

int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum += values[i]; } }

Which one of the following is the correct definition for initializing data in a two-dimensional array of three rows and two columns?

int[][] arr = { { 1, 1 }, { 2, 2 }, { 3, 3 } };

Which of the following is the correct header for a greaterThan method definition that takes two arguments of type double and returns true if the first value is greater than the second value?

public static boolean greaterThan(double a, double b)

Suppose you need to write a method that calculates the volume of a 3D rectangular solid. Which of the following is the best choice for the declaration of this method?

public static double volume(double w, double h, double l)

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)

Which of the following code snippets can be used for defining a method that does not return a value? The method should accept a string and then display the string followed by "And that's all folks!" on a separate line.

public static void displayMessage(String str) { System.out.println(str); System.out.println("And that's all folks!"); }

Which one of the following is a valid signature of a method with an integer two-dimensional array parameter of size 10 x 10?

public static void func(int[][] arr)

In a partially filled array, the number of slots in the array that are not currently used is _______________.

the length of the array minus the number of elements currently in the array

The purpose of a method that returns void is __________________.

to package a repeated task as a method even though the task does not yield a value

What is the valid range of index values for an array of size 10?

0 to 9

What is the output from the following Java program? public class Test { public static void main(String[] args) { for (int i = 0; i < 4; i++) { System.out.print(myFun(i) + " "); } System.out.println(); } public static int myFun(int perfect) { perfect = 0; return ((perfect - 1) * (perfect - 1)); } }

1 1 1 1

What is the output of the following code snippet? public class test04 { public static int pow(int base, int power) { int result = 1; for (int i = 0; i < power; i++) { result = result * base; } return result; } public static void main(String[] args) { System.out.println(pow(pow(2, 2), 2)); } }

16

How many elements can be stored in a two-dimensional 5 by 6 array?

30

What is the output of the following code snippet? int[] myarray = { 10, 20, 30, 40, 50 }; System.out.print(myarray[2]); System.out.print(myarray[3]);

3040

What is the output of the following code snippet? public static int recurrAverage(int num) { int sum = 0; for (int x = 1; x <= num; x++) { sum = sum + x; } return sum / num; } public static void main(String[] args) { System.out.println(recurrAverage(recurrAverage(16))); }

4

What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(1, "Tony"); for (String s : names) { System.out.print(s + ", "); }

Ann, Tony

Which one of the following code snippets accepts the integer input in an array list named num1 and stores the even integers of num1 in another array list named evennum?

ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { evennum.add(num1.get(i)); } }

Which statements are true regarding the differences between arrays and array lists? I. Arrays are better if the size of a collection will not change. II. Array lists are more efficient than arrays. III. Array lists are easier to use than arrays.

I and III only

Which statements about the enhanced for loop are true? I. It is suitable for all array algorithms. II. It does not allow the contents of the array to be modified. III. It does not require the use of an index variable.

II and III only

Which of the following is correct about a local variable?

It is declared within the scope of any method.

Which of the following is not legal in a method definition?

Multiple return values

What is the output of the following code snippet? public static void main(String[] args) { int gvar = 0; gvar = gvar + 10; if (gvar > 0) { int lvar = gvar + 1; } System.out.println(lvar); }

No output due to compilation error.

What is wrong with the following code? public static char grade(int score) { if (score >= 9) { return 'A'; } else if (score >= 8) { return 'B'; } else if (score >= 6) { return 'C'; } else if (score >= 4) { return 'D'; } }

No return statement for all possible logic paths

What is wrong with the following code? public static double div2(int n1, int n2) { if (n2 != 0) { return (double) n1 / n2; } }

No return statement for all possible logic paths

Which of the following options represents the output of the given code snippet? public static int addsub(int a, boolean isSub) { if (isSub) { return sub(a); } else {return a + 1; } } public static int sub(int a) { return a - 1; } public static void main(String[] args) { System.out.println("Sub 5 = " + addsub(5, false) + ", Add 6 = " + addsub(6, true)); }

Sub 5 = 6, Add 6 = 5

What should you check for when calculating the largest value in an array list?

The array list contains at least one element.

What is the result of the following code? for (double element : values) { element = 0; }

The array values is not modified.

Which statement is true about the code snippet below? public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Double> inputs = new ArrayList<Double>(); int ind = 0; while (in.hasNextDouble()) { inputs.set(ind, in.nextDouble()); ind++; } }

The code has a run-time error.

Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = new ArrayList<String>(names); friends.add("Harry");

The final size of names is 2; the final size of friends is 3

Which one of the following statements is correct about the given code snippet? int[] somearray = new int[6]; for (int i = 1; i < 6; i++) { somearray[i] = i + 1; }

The for loop initializes all the elements except the first element.

What is the result of the following definition of an array list? ArrayList<Double> points;

The statement defines an array list reference but leaves its value as null.

What is the syntax error in the following method definition? public static String parameter(double r) { double result; result = 2 * 3.14 * r; return result; }

The value that is returned does not match the specified return type.

What statement is true about the following code? int[] values = {2, 3, 5, 8, 11}; int[] data = values; data[4] = 13;

The variable data references an initialized array.

What is wrong with the following code snippet? String[] data = { "abc", "def", "ghi", "jkl" }; String searchedValue = "ghi"; int pos = 0; boolean found = false; while (pos < data.length) { if (data[pos].equals(searchedValue)) { found = true; } else { found = false; } pos++; } if (found) { System.out.println("Found at position: " + pos); } else { System.out.println("Not found"); }

There is a logic error.

When an array reading and storing input runs out of space, __________________.

When an array reading and storing input runs out of space, __________________.

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


Conjuntos de estudio relacionados

Barron's GRE with sentences & Arabic Definitions - Complete Wordlist - Part 1

View Set

Changes in Period and Phase Shift of Sine and Cosine Functions

View Set

Chapter 8 Nutrition Book Questions

View Set

Dutton Review Questions: Chapter 16 - Shoulder

View Set

Ch. 10- Stress, health, and human flourishing

View Set

CIT 105: Intro to Computers: Powerpoint

View Set