Programming Languages Chapter 07 Consider Array Items Which Contains The

subject Type Homework Help
subject Pages 12
subject Words 3307
subject Authors Harvey Deitel, Paul Deitel

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
Chapter 7 Arrays and ArrayLists
Section 7.1 Introduction
7.1 Q1: Arrays are ________.
a. variable-length entities
b. fixed-length entities
c. data structures that contain up to 10 related data items
d. used to draw a sequence of lines, or “rays”
7.2 Q1: Which of the following statements about arrays are true?
A. An array is a group of variables containing values that all have the same type.
B. Elements are located by index.
C. The length of an array c is determined by the expression c.length();.
D. The zeroth element of array c is specified by c[0].
a. A, C, D.
b. A, B, D.
c. C, D.
d. A, B, C, D.
7.2 Q2: Consider the array:
s[0] = 7
s[1] = 0
s[2] = -12
s[3] = 9
s[4] = 10
s[5] = 3
s[6] = 6
The value of s[s[6] - s[5]] is:
a. 0.
b. 3.
c. 9.
d. 0.
page-pf2
7.3 Q1: A programmer must do the following before using an array:
a. declare then reference the array.
b. create then declare the array.
c. create then reference the array.
d. declare then create the array.
7.3 Q2: Consider the code segment below. Which of the following statements is false?
int[] g;
g = new int[23];
a. The value of g[3] is -1.
b. The first statement declares an array reference.
c. The second statement creates the array.
d. g is a reference to an array of integers.
7.4.1 Q1: Which of the following statements about creating arrays and initializing their
elements is false?
a. The new keyword should be used to create an array.
b. When an array is created with operator new, the number of elements must be placed in
square brackets following the type of element being stored.
c. The elements of an array of integers have a value of null before they are initialized.
d. A for loop is commonly used to set the values of the elements of an array.
7.4.1 Q2: What do the following statements do?
double[] array;
array = new double[14];
a. Create a double array containing 13 elements.
b. Create a double array containing 14 elements.
c. Create a double array containing 15 elements.
d. Declare but do not create a double array.
page-pf3
7.4.2 Q1: Which of the following initializer lists would correctly set the elements of array
n?
a. int[] n = {1, 2, 3, 4, 5};.
b. array n[int] = {1, 2, 3, 4, 5};.
c. int n[5] = {1; 2; 3; 4; 5};.
d. int n = new int(1, 2, 3, 4, 5);.
7.4.3 Q1: Constant variables also are called .
a. write-only variables
b. finals
c. named constants
d. All of the above
7.4.3 Q2: Which of the following will not produce a compiler error?
a. Changing the value of a constant after it is initialized.
b. Changing the value at a given index of an array after it is created.
c. Using a final variable before it is initialized.
d. All of the above will produce compiler errors.
7.4.4 Q1: Consider the program below:
public class Test
{
public static void main(String[] args)
{
int[] a;
a = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = i + 2;
page-pf4
int result = 0;
for (int i = 0; i < a.length; i++)
result += a[i];
System.out.printf("Result is: %d%n", result);
}
}
The output of this program will be:
a. Result is: 62.
b. Result is: 64.
c. Result is: 65.
d. Result is: 67.
7..4.4 Q2: Consider the class below:
public class Test
{
public static void main(String[] args)
{
int[] a = {99, 22, 11, 3, 11, 55, 44, 88, 2, -3};
int result = 0;
for (int i = 0; i < a.length; i++)
{
if (a[i] > 30)
result += a[i];
}
System.out.printf("Result is: %d%n", result);
}
}
The output of this Java program will be:
a. Result is: 280.
b. Result is: 286.
c. Result is: 154.
d. Result is: 332.
7.4.5 Q1: Which flag in a format specifier indicates that values with fewer digits than the
page-pf5
field width should begin with a leading 0?
a. p.
b. l.
c. w.
d. 0.
7.4.6 Q1: Invalid possibilities for array indices include .
a. Positive integers.
b. Negative integers.
c. Zero.
d. None of the above.
7.4.7 Q1: Which expression adds 1 to the element of array arrayName at index i?
a. ++arrayName[i].
b. arrayName++[i].
c. arrayName[i++].
d. None of the above.
7.4.7 Q2: Attempting to access an array element outside of the bounds of an array, causes
a(n) .
a. ArrayOutOfBoundsException.
b. ArrayElementOutOfBoundsException.
c. ArrayIndexOutOfBoundsException.
d. ArrayException.
7.4.7 Q3: Which of the following statements is false?
a. An exception indicates a problem that occurs while a program executes.
b. Exception handling enables you to create fault-tolerant programs that can resolve (or
handle) exceptionsin many cases, this allows a program to continue executing as if no
problems were encountered.
c. The catch block contains the code that might throw an exception, and the try block
contains the code that handles the exception if one occurs.
page-pf6
d. Inside the catch block, you can use the parameter’s identifier to interact with a caught
exception object.
7.5 Q1: A(n) ________ indicates a problem that occurs while a program executes.
a. syntax error
b. omitted import
c. missing semicolon
d. exception
7.5 Q2: Exception handling helps you create ________ programs.
a. high-performance
b. logic-error-free
c. fault-tolerant
d. compilation-error-free
7.5.1 Q1: Which of the following statements is true?
a. The catch block contains the code that might throw an exception.
b. The try block contains the code that handles the exception if one occurs.
c. You can have many catch blocks to handle different types of exceptions.
d. When a try block terminates, any variables declared in the try block are preserved.
7.5.2 Q1 Which of the following statements is false?
a. A catch block declares a type and an exception parameter name.
b. Inside the catch block, you can use the parameter’s name to interact with a caught
exception object.
c. When a program is executed, array element indices are checked for validityall indices
must be greater than 0 and less than or equal to the length of the array.
d. If an attempt is made to use an invalid index to access an element, an
ArrayIndexOutOfBoundsException exception occurs.
page-pf7
7.5.3 Q1: An exception objects ________ method returns the exception’s error message.
a. String
b. Message
c. Error
d. toString
7.6 Q1: Consider integer array values, which contains 5 elements. Which statements
successfully swap the contents of the array at index 3 and index 4?
a.
values[3] = values[4];
values[4] = values[3];
b.
values[4] = values[3];
values[3] = values[4];
c.
int temp = values[3];
values[3] = values[4];
values[4] = temp;
d.
int temp = values[3];
values[3] = values[4];
values[4] = values[3];
page-pf8
7.6 Q2: Assume class Book has been declared. Which set of statements creates an array of
Books?
a.
Book[] books;
books = new Book[numberElements];
b.
Book[] books];
books = new Book()[numberElements];
c.
new Book() books[];
books = new Book[numberElements];
d. All of the above.
7.6 Q3: How many Book objects are created by the following statement?
Book[] books = new Book[10];
a. 10
b. 0
c. 5
d. None of the above.
7.7 Q1: Assume array items contains the integer values 0, 2, 4, 6 and 8. Which of the
following uses the enhanced for loop to display each value in array items?
a.
for (int i = 0; i < items.length; i++)
System.out.prinf("%d%n", items[i]);
b.
for (int i : items)
System.out.prinf("%d%n", items[i]);
page-pf9
c.
for (int i : items)
System.out.prinf("%d%n", i);
d.
for (int i = 0 : items.length)
System.out.prinf("%d%n", items[i]);
7.7 Q2: Which of the following tasks cannot be performed using an enhanced for loop?
a. Calculating the product of all the values in an array.
b. Displaying all even element values in an array.
c. Comparing the elements in an array to a specific value.
d. Incrementing the value stored in each element of the array.
7.8 Q1: Which statement correctly passes the array items to method takeArray? Array
items contains 10 elements.
a. takeArray(items[]).
b. takeArray(items).
c. takeArray(items[9]).
d. Arrays cannot be passed to methodseach item must be sent to the method separately.
7.8 Q2: Consider array items, which contains the values 0, 2, 4, 6 and 8. If method
changeArray is called with the method call changeArray(items, items[2]), what
values are stored in items after the method has finished executing?
public static void changeArray(int[] passedArray, int value)
{
passedArray[value] = 12;
value = 5;
}
a. 0, 2, 5, 6, 12.
b. 0, 2, 12, 6, 8.
page-pfa
c. 0, 2, 4, 6, 5.
d. 0, 2, 4, 6, 12.
7.8 Q3: When an argument is passed by reference, ________.
a. a copy of the argument’s value is passed to the called method
b. changes to the argument do not affect the original variable’s value in the caller
c. the called method can access the argument’s value in the caller directly and modify that
data
d. the original value is removed from memory
7.9 Q1: Which of the following statements is false?
a. When an argument is passed by reference, the called method can access the argument’s
value in the caller directly but cannot modify it.
b. All arguments in Java are passed by value.
c. To pass an individual array element to a method, use the indexed name of the array.
d. To pass an object reference to a method, simply specify in the method call the name of
the variable that refers to the object.
7.9 Q2: When you pass an array or an individual array element of a reference type to a
method, the called method receives ________. When you pass an individual element of a
primitive type, the called method receives ________.
a. a copy of the element’s reference, a copy of the element’s reference
b. a copy of the element’s value, a copy of the element’s reference
c. a copy of the element’s value, a copy of the element’s value
d. a copy of the element’s reference, a copy of the element’s value
page-pfb
7.10 Q1: What kind of application tests a class by creating an object of that class and
calling the class’s methods?
a. Pseudo application.
b. Debugger.
c. Declarator.
d. Test harness.
7.11 Q1: In Java, multidimensional arrays ________.
a. are not directly supported.
b. are implemented as arrays of arrays.
c. are often used to represent tables of values.
d. All of the above.
7.11 Q2: In array items, which expression below accesses the value at row 3 and column
4?
a. items[3].[4]
b. items[3[4]]
c. items[3][4]
d. items[3, 4]
7.11 Q3: An array with m rows and n columns is not ________.
A. an m-by-n array.
B. an n-by-m array.
C. a two-dimensional array.
D. a dual-transcripted array.
a. A and C.
b. A and D.
c. B and D.
d. B and C.
page-pfc
7.11 Q4: Which statement below initializes array items to contain 3 rows and 2 columns?
a. int[][] items = {{2, 4}, {6, 8}, {10, 12}};
b. int[][] items = {{2, 6, 10}, {4, 8, 12}};
c. int[][] items = {2, 4}, {6, 8}, {10, 12};
d. int[][] items = {2, 6, 10}, {4, 8, 12};
7.11 Q5: For the array that was the correct answer in the previous question, what is the
value returned by items[1][0]?
a. 4.
b. 8.
c. 12.
d. 6.
7.11 Q6: Which of the following statements creates a multidimensional array with 3 rows,
where the first row contains 1 element, the second row contains 4 elements and the final
row contains 2 elements?
a. int[][] items = {{1, null, null, null}, {2, 3, 4, 5}, {6, 7, null, null}};
b. int[][] items = {{1}, {2, 3, 4, 5}, {6, 7}};
c. int[][] items = {{1}, {2, 3, 4, 5}, {6, 7}, {});
d. int[][] items = {{1}, {4}, {2}};
7.11 Q7: Which of the following sets of statements creates a multidimensional array with 3
rows, where the first row contains 1 value, the second row contains 4 items and the final
row contains 2 items?
a.
int[][] items;
items = new int[3][?];
items[0] = new int[1];
items[1] = new int[4];
items[2] = new int[2];
b.
page-pfd
int[][] items;
items = new int[3][];
items[0] = new int[1];
items[1] = new int[4];
items[2] = new int[2];
c.
int[][] items;
items = new int[?][?];
items[0] = new int[1];
items[1] = new int[4];
items[2] = new int[2];
d.
int[][] items;
items[0] = new int[1];
items[1] = new int[4];
items[2] = new int[2];
7.11 Q8: The preferred way to traverse a two-dimensional array is to use .
a. a do while statement.
b. a for statement.
c. two nested for statements.
d. three nested for statements.
7.11 Q9: Which set of statements totals the items in each row of two-dimensional array
items, and displays each row’s total?
a.
for (int row = 0; row < items.length; row++)
page-pfe
{
int total = 0;
for (int column = 0; column < items[row].length; column++)
total += items[row][column];
System.out.printf("Row %d's total is %d%n", row, total);
}
b.
int total = 0;
for (int row = 0; row < items.length; row++)
{
for (int column = 0; column < items[row].length; column++)
total += items[row][column];
System.out.printf("Row %d's total is %d%n", row, total);
}
c.
int total = 0;
for (int row = 0; row < items.length; row++)
{
for (int column = 0; column < items[column].length; column++)
total += items[row][column];
System.out.printf("Row %d's total is %d%n", row, total);
}
d.
for (int row = 0; row < items.length; row++)
{
int total = 0;
for (int column = 0; column < items[column].length; column++)
total += items[row][column];
System.out.printf("Row %d's total is %d%n", row, total);
}
page-pff
7.12 Q1: Which set of statements totals the values in two-dimensional int array items?
a.
int total = 0;
for (int subItems : items)
for (int item : subItems)
total += item;
b.
int total = 0;
for (int item: int[] subItems : items)
total += item;
c.
int total = 0;
for (int[] subItems : items)
for (int item : items)
total += item;
d.
int total = 0;
for (int[] subItems : items)
for (int item : subItems)
total += item;
page-pf10
7.13 Q1: An argument type followed by a(n) in a method’s parameter list
indicates that the method receives a variable number of arguments of that particular type.
a. square brackets ([])
b. ellipsis (...)
c. varargs keyword
d. All of the above are acceptable to indicate a variable number of arguments.
7.14 Q1: Which command below runs TestProgram, and passes in the values files.txt
and 3?
a. java TestProgram files.txt 3.
b. java TestProgram files.txt, 3.
c. java TestProgram "files.txt", "3".
d. None of the above.
7.14 Q2: Which method call converts the value in variable stringVariable to an integer?
a. Convert.toInt(stringVariable)
b. Convert.parseInt(stringVariable)
c. Integer.parseInt(stringVariable)
d. Integer.toInt(stringVariable)
7.15 Q1: Class Arrays methods sort, binarySearch, equals and fill are overloaded for
primitive-type arrays and Object arrays. In addition, methods __________ and
__________ are overloaded with generic versions.
a. sort, binarySearch.
b. sort, fill.
c. binarySearch, equals.
d. binarySearch, fill.
page-pf11
7.15 Q2: Class Arrays provides method __________ for comparing arrays to determine
whether they have the same contents.
a. compare
b. compares
c. equal
d. equals
7.16 Q1: Class ________ represents a dynamically resizable array-like data structure.
a. Array
b. ArrayList
c. Arrays
d. None of the above.
7.16 Q2: Which of the following is false?
a. The size of an ArrayList can be determined via its length instance variable.
b. The size of an ArrayList can be determined via its size method.
c. You can add a new item to the end of an ArrayList with its add method.
c. You can get an item from a specified index in an ArrayList with its get method.
7.17 Q1: Which method sets the background color of a JPanel?
a. setBack.
b. setBackground.
c. setBackgroundColor.
d. setColor.
7.17 Q2: Which of the following statements about an arc is false?
a. An arc is a section of an oval.
b. The sweep is the amount of arc to cover.
c. Method drawArc draws the edges of an arc.
d. The fillArc method draws an oval, with the section that is an arc filled in.
page-pf12

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.