csc exam three

Ace your homework & exams now with Quizwiz!

What is the output of the following code? int[] list = new int[5]; System.out.println(list[3]);

0

What are the contents of the items array after the following code is executed? int[] items = {10, 20, 30, 40, 50}; for (int item : items) { item = item * 2; }

10, 20, 30,40,50 the for loop does not change the values stored in the array

What is the output of the following code? Note that it uses a for-each statement to read each value from list. int[] list = {61,2,99,13,8,25,49}; int num = 999; for (int val : list) { if (val < num) num = val; } System.out.println(num);

2

What list of prime values is produced if the PrimeSieve program is executed with an input value of 20?

2 3 5 6 11 13 17 19

What is the output of the following code? int[] list = {2, 4, 6, 8, 10}; int num = 0; for (int val : list) { if (val != 6) num = num + val; } System.out.println(num);

24

How many comparisons are necessary to find the value 43 in the following list using a binary search? 13 25 28 30 39 43 44 58 66 70 78 81 86 88 95

3 the elements examined in order 58, 30, 43

How many comparisons are necessary to find the value 86 in the following list using a binary search? 13 25 28 30 39 43 44 58 66 70 78 81 86 88 95

4, the elements are examined in order: 58, 81,88, 86

How many elements can be stored in a two-dimensional array with dimensions 5 and 10?

50

What is the proper type designation for an ArrayList of Person objects

ArrayList<Person>

T/F: A one-dimensional array has only one element type, but a multidimensional array can hold several types of elements.

False, every array has a single element type. the difference is in how the values are arranged

T/F: A BufferedReader object handles the details of reading data from the disk in large chunks.

False, the FileReader object deals with the disk

What is the element type of the athletes array in the ArrayOfPersonDemo program?

Person

What is the type of the variable athletes in the ArrayOfPersonDemo program?

Person[]

Which of the following declares a valid PrintWriter object that represents an output file named myOutput.txt? PrintWriter out = new File("myOutput.txt"); PrintWriter out = new Scanner("myOutput.txt"); PrintWriter out = "myOutput.txt"; PrintWriter out = new PrintWriter("myOutput.txt");

PrintWriter out = new PrintWriter("myOutput.txt");

Which of the following declares a valid Scanner object to read an input file named myData.txt? Scanner in = new File("myData.txt"); Scanner in = new Scanner(new File("myData.txt")); Scanner in = new Scanner("myData.txt"); Scanner in = new Scanner(new PrintWriter("myData.txt"));

Scanner in = new Scanner(new File("myData.txt"));

What is the element type of the adjectives array in the ArrayOfStringDemo program?

String

Which line of code demonstrates a valid and error-free way to access the following array?String[][] chessboard = new String[8][8]; String chessPiece = chessboard[1][8]; String chessPiece = chessboard[5][2]; String chessPiece = chessboard["2"]["3"]; String chessPiece = chessboard[4][]; String chessPiece = chessboard[1];

String chessPiece = chessboard[5][2];

What is the type of the variable adjectives in the ArrayOfStringDemo program?

String[]

What is the output of the following code? int sum = 0; int[] values = {7, 6, 5, 4, 3, 2, 1}; for (int i = 1; i < values.length; i++) sum += values[i]; System.out.println("Sum: " + sum);

Sum: 21

T/F: A BufferedInputStream object is used to read binary file data.

True

Which of the following parts of a try-catch statement corresponds to an exception handler? try block catch block finally block handler block

catch block

Assuming table is a two-dimensional array of integers, what does the following code do? int sum = 0; for (int i = 0; i < table.length; i++) for (int j = 0; j < table[i].length; j++) sum = sum + table[i][j];

computes the sum of all values in the table

which is the following is the preferred way to declare an array double prices[] = new double[10]; double[] prices = new double[10];

double[] prices = new double[10];

T/F: A binary search only works if the number of elements to search is odd.

false

T/F: It's important to understand array processing to understand how to use an ArrayList.

false

T/F: Our implementation of a prime sieve uses an array of integers and sets the values to 0 when they are found not to be prime.

false

T/F: To be sure you have found all possible prime numbers up to a certain number N, you need to repeat the sieve algorithm all the way up to N.

false

T/F: You cannot define your own exception class.

false

T/F: you cannot use command-line arguments in IDEs like Eclipse and NetBeans.

false

T/F:Arrays can hold primitive types, but not objects

false

T/F: A Scanner object can be used to both read and write text files in Java.

false a scanner is object only used for input

T/F: A variable-length parameter list is specified by inserting a colon (:) between the type and the parameter name.

false a variable-length parameter is specified using an ellipses(...) between the type and parameter name

T/F: Dividing by zero is a problem represented by a DivideByZeroException object.

false it is represented by an ArithmeticException object

T/F: In the ExceptionPropagationDemo program, the exception is thrown in method1 and caught in method3.

false it was thrown in method 3 and caught in method 1

T/F: A program will crash if a thrown exception is not caught in the method that throws it.

false the exception may be caught in a method higher in the call stack

T/F: If an array can hold N values, the valid indexes for that array range from 0 to N

false the valid range is 0 to N-1

T/F: A for-each loop can be used to fill the values in an array.

false, a for each loop provides access to the elements in the array but not to the array itself

T/F: An exception handler must be defined for each exception a program may throw.

false, a program might throw many exceptions that it is not designed to handle explicitly

T/F: An exception has to be caught and handled when it occurs.

false, an exception does not have to have to be caught, though an uncaught exception will cause the program to crash

T/F: Calling the System.exit method is the best way to terminate a program.

false, best to exit "cleanly" but System.exit can be convenient when no further processing is required

T/F: File input can be made more efficient by buffering, but file output cannot.

false, both input and output can benefit form buffering

T/F: An array of objects cannot be created with an initialization list.

false, each element in the list has to be an object of the appropriate type

T/F: A binary search works best on an unsorted list of values.

false, for a binary search to work all the list of values must be already sorted

T/F: A thrown exception can be caught by any method in a program.

false, it can only be caught in a method in the call stack

T/F: A linear search examines every element in the array.

false, once the target element is found, there is no need to keep searching the array

T/F: change the algorithm for finding a minimum value into one that finds a maximum value, the condition of the for loop would change

false, the for loop could stay the same -- the condition of the if statement would change to find the biggest element

T/F: The point of a buffered reader is to reduce the effort of the user typing information on the keyboard.

false, the point of a buffered reader is to improve the efficiency of I/O operations to and from the disk

T/F: It's best to catch an exception at the highest level of the call stack if possible.

false, where an exception is caught and handled is a design decision that will vary according to the situation

Which of the following correctly shows the general structure of a for-each loop header? for(type variable-name : array-or-collection) foreach (value array : condition; array-length-specifier) for (initialization; condition; increment) foreach (type variable-name : array-or-collection)

for(type variable-name : array-or-collection)

When performing a binary search, how many comparisons are required to find a value in a list of N values, in the worst case?

log2N +1

What is the output of the following code? int sum = 0; int[] values = {1, 2, 3, 4, 5}; for (int i = 0; i <= 5; i++) sum = sum + values[i]; System.out.println("Sum: " + sum);

no output a bounds error occurs, the loop tries to access scores[5], but the last element in the array is scores[4].

What output is produced when the CommandLineTest program below is run from the command line as follows? > java CommandLineTest apple grape orange lemon kiwi public class CommandLineTest { public static void main(String[] args) { System.out.println(args[2]); } }

orange

Which of the following demonstrates the proper way to specify a variable-length argument list? public void printValues(String str, int... list, int size) public void printValues(int... list, int x, int y) public void printValues(int... list, String... str) public void printValues(String str, int... list)

public void printValues(String str, int... list)

Which method call will replace the element at index 3 of an ArrayList of String objects called stuff?

stuff.set(3, "new stuff")

What will happen if you try to add a String object to an ArrayList created to hold Person objects?

the compiler will issue an error message

In what circumstances will the code of a finally block be executed

the finally block will be executed no matter what

What happens when an element is removed from an ArrayList?

the remaining elements are shifted to close the gap

T/F: A for-each loop can be used to compute the sum of all values in an array.

true

T/F: A method that accepts a variable-length argument list can be passed a previously declared array.

true

T/F: A stack trace lists the methods that were called leading up to the exception

true

T/F: A thrown exception is represented by an object.

true

T/F: A two-dimensional array is really a one-dimensional array of one-dimensional arrays.

true

T/F: An ArrayList is a collection that can be processed using a for-each loop.

true

T/F: An array of objects is really an array of object references.

true

T/F: An exception represents a problem that occurs during program execution.

true

T/F: An exception with a specialized message can be created from an existing exception

true

T/F: An output file should always be closed when you're done using it or data may be lost.

true

T/F: Arguments passed to a variable-length argument list are accessed in the method using an array.

true

T/F: By convention, Java command-line options begin with a dash, such as -o or -b.

true

T/F: Command-line arguments are commonly used to specify program options when a program is run.

true

T/F: Finding a minimum value requires that every element in the array be considered.

true

T/F: It is possible to create an array with more than two dimensions.

true

T/F: The Greek mathematician Eratosthenes came up with the idea of using a sieve algorithm to find prime numbers.

true

T/F: The printf method is an example from the Java API that uses a variable-length parameter list.

true

T/F: Traversing an array or collection means accessing each element one at a time.

true

T/F: When a throw statement is executed, the normal flow of a method is interrupted as it would be for any thrown exception.

true

T/F: When reading large amounts of data, it can be more efficient to "buffer" that data by reading many bytes ahead.

true

T/F: exceptions are represented by classes in the Java APi

true

T/F: swapping two elements in an array requires three assignment statement

true

T/F:A for-each loop cannot be used to set the values in an array.

true

T/F:An array that holds objects is, itself, an object.

true

T/F:If you try to access an element outside of the valid index range for an array, an exception will be thrown.

true

T/F: The element type of an ArrayList<E> cannot be a primitive type such as int or double.

true instead, use wrapper classes to create ArrayList<Integer> or ArrayList<Double> objects.

T/F: The Scanner class provides several variations of hasNext and next methods for reading a variety of data types.

true the Scanner has hasNextint and nextInt to check for and read integer values

T/F: Programmers commonly need to read data from a file, process it, and output results of an analysis of that data.

true, common to read and analyze data from a file in order to discover something about that data

T/F: The maximum indexes of a two-dimensional array with 6 rows and 10 columns are 5 and 9, respectively.

true, each dimension is indexed from 0 to N-1 where N represents the number of elements in that dimension

T/F : An exception will stop propagating after it is caught.

true, it it is never caught, then the program will terminate abnormally

T/F: File I/O operations may throw checked exceptions.

true, since they are checked your code must either handle them explicitly or at least acknowledge they may occur

T/F: A for-each loop cannot be used to print the elements in an array backwards.

true, the for-each loop only processes array elements starting at index 0

T/F: A generic class operates on a generic type, which is specified when an object is created.

true, the generic type is specified by a placeholder such as E until instantiation

T/F: When an exception is thrown, control transfers immediately to the appropriate exception handler

true, the rest of the code in the try block is skipped

T/F: The print and println methods can be used to write data to a PrintWriter object.

true, they work just as they do in the System.out object

T/F: A two-dimensional array is suitable for storing tabular data.

true, two dimensions of the array mimic the row and column layout of the table


Related study sets

Chapter 12.8: Nervous System: Anatomy and Physiology of the Nervous System: Central Nervous System II: Brain

View Set

Introduction to Management Accounting Revision

View Set

DIC mylab NCLEX questions 16-5.1

View Set

IXL - Choose punctuation to avoid fragments and run-ons.

View Set

Applied Pharmacology for Veterinary Technicians: Chapter 13 Antiparasitic Drugs

View Set

Sociology fourteenth edition Chapter 1 test notes

View Set

NCLEX Practice, Taylor (8th ed.) Chapter 14 Implementing

View Set