ITP 120 quizzes for final

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

When writing the documentation comments for a method, you can provide a description of each parameter by using a:

@param tag

Which of the following will open a file named MyFile.txt and allow you to append data to its existing contents?

FileWriter fwriter = new FileWriter(MyFile.txt, true); PrintWriter outFile = new PrintWriter(fwriter);

The numeric classes' "parse" methods all throw an exception of this type if the string being converted does not contain a convertible numeric value.

NumberFormatException

In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner (System.in); System.out.print("Enter a number "); int number = keyboard.nextInt(); while (number <100 || number >500) { System.out.print("Enter another number: "); number = keyboard.nextInt(); }

Numbers in the range 100 - 500

Java allows you to create objects of this class in the same way you would create primitive variables.

String

Which of the following will format 12.7801 to display as $12.78?

System.out.printf("$%,.2f", 12.7801);

All of the exceptions that you will handle are instances of classes that extend this class.

Throwable

When the continue statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration

True

When you open a file with the PrintWriter class, the class can potentially throw an IOException.

True

You can use the PrintWriter class to open a file for writing and write data to it.

True

The sequential search algorithm:

Uses a loop to sequentially step through an array, starting with the first element

A search algorithm:

a method of locating a specific item in a larger collection of data

A constructor

a method that initializes a newly instantiated object

When an array is passed to a method

a reference to the array is passed it is passed just as an object the method has direct access to the original array

In the following code, Integer.parseInt(str) (as used below in code) is an example of: int num; string str = "555"; num = Integer.parseInt(str) + 5;

a value-returning method

What will be the result of the following code? int num; string str = "555"; num = Integer.parseInt(string str) + 5;

a value-returning method

What would be the results of the following code? int[] x = { 55, 33, 88, 22, 99, 11, 44, 66, 77 }; int a = 10; if(x[2] > x[5]) a = 5; else a = 8;

a=5

Assuming that inputFile references a Scanner object that was used to open a file, which of the following statements will read an int from the file?

int number = inputFile.nextInt();

Assume that the following method header is for a method in class A. public void displayValue (int value) Assume that the following code segments appear in another method, also in class A. Which contains a legal call to the displayValue method?

int x = 7; displayValue(x);

In memory, an array of String objects

consists of elements, each of which is a reference to a String object

A loop that repeats a specific number of times is known as a(n):

count controlled loop

To return an array of long values from a method, use this as the return type for the method

long[]

This is a control structure that causes a statement or group of statements to repeat.

loop

What is stored by a reference variable?

memory address

This part of a method is a collection of statements that are performed when the method is executed.

method body

Class objects normally have ________ that perform useful operations on their data.

methods

Look at the following statement. import java.util.Scanner; This is an example of

explicit import

The do-while loop is a pre-test loop.

false

A class specifies the ________ and ________ that a particular type of object has.

fields and methods

It is common practice in object-oriented programming to make all of a class's:

fields private

Methods that operate on an object's fields that are known also as instance variables are called:

nstance methods

To document the return value of a method, use this in a documentation comment.

the @return tag

You can use this method to determine whether a file exists.

the file class exists method

When an exception is thrown by code in the try block, the JVM begins searching the try statement for a catch clause that can handle it and passes control of the program to:

the first catch clause that can handle the exception.

The scope of a private instance field is:

the instance methods of the same class

When an object is passed as an argument to a method, what is passed into the method's parameter variable?

the object's memory address

If the program does not handle an unchecked exception:

the program is halted and the default exception handler handles the exception.

Two or more methods in a class may have the same name as long as

they have different parameter lists

A file must always be opened before using it and closed when the program is finished using it.

true

Instance methods do not have the key word static in their headers.

true

The do-while loop must be terminated with a semicolon.

true

When you pass the name of a file to the PrintWriter constructor, and the file already exists, it will be erased and a new empty file with the same name will be created.

true

What would be the result of the following code? int[] array 1 = new int[25]; ...//Code that will put values in array1 int value = array1[0]; for (int a = 1; a < array1.length; a++) { if (array1[a] < value) value = array1[a]; }

value contains the lowest value in array1

What would be the result of the following code? final int SIZE = 25; int[] array1 = new in [SIZE]; ...//Code that will put values in array1 int value = 0; for (int a = 0; a < array1.length; a++) { value += array1[a]; }

value contains the sum of all the values in array1

Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached?

while (inputFile.hasNext()) { ... }

Which of the following are pre-test loops?

while, for

What would be the results after the following code was executed? int[] x = {23, 55, 83, 19}; int[] y = {36, 78, 12, 24}; x = y; y = x;

x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24}

If, within one try statement you want to have catch clauses of the following types, in which order should they appear in your program: (1) Exception (2) IllegalArgumentException (3) RuntimeException (4) Throwable

2 3 1 4

What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x < 5);

200

What will be the value of x[1] after the following code is executed? int[] x = {22, 33, 44}; arrayProcess(x[1]; ... public static void arrayProcess(int a) { a = a + 5; }

33

What will be printed after the following code is executed? for (int number = 5; number <=15; number +=3) System.out.print(number+", ");

5,8,11,14

In the following code, System.out.println(num) (as used below in code) is an example of: double num = 5.4; System.out.println(num); num = 0.0

A void method

When an object, such as a String, is passed as an argument, it is:

Actually a reference to the object that is passed

The binary search algorithm:

An algorithm that searches for a target value in a sorted list by checking at the midpoint and then repeatedly cutting the search section in half.

Look at the following statement. import java.util.*; This is an example of:

Wildcard import

A ragged array is:

a two-dimensional array where the rows are of different lengths

When the break statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.

False

What will be the results of the following code? final int ARRAY_SIZE = 5; float[] x = float[ARRAY_SIZE]; for(int i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10; }

an error will occur

Each repetition of a loop is known as what?

an iteration

One or more objects may be created from a(n):

class

In the cookie cutter metaphor, think of the ________ as a cookie cutter and ________ as the cookies.

class, objects

This type of loop is ideal in situations where you always want the loop to iterate at least once.

do-while

It is common practice to use a ________ variable as a size declarator.

final

The exception classes are in packages in the ________

Java API

This variable controls the number of times that the loop iterates.

Loop control variable

The ability to catch multiple types of exceptions with a single catch is known as ________, and was introduced in Java 7

Multi catch

A special variable that holds a value being passed into a method is called what?

Parameter

A constructor is a method that:

Performs initialization or setup operations.

This is a sum of numbers that accumulates with each iteration of a loop

Running total

Which of the following are classes from the Java API?

Scanner Random PrintWriter

Before entering a loop to compute a running total, the program should first do this.

Set the accumulator where the total will be kept to an initial value, usually zero

Which of the following will format 12.78 to display as 12.8%?

System,out.printf("%1f%%", 12.78);

In order to do a binary search on an array:

The array must first be sorted in ascending order

Given the following two-dimensional array declaration, which statement is TRUE? int [][] numbers = new int [6] [9];

The array numbers has 6 rows and 9 columns.

An object can store data.

true

The java.lang package is automatically imported into all Java programs.

true

Data hiding, which means that critical data stored inside the object is protected from code outside the object, is accomplished in Java by:

using the private access specifier on the class fields

What will be the values of x and y as a result of the following code? int x = 12, y = 5; x += y--;

x=17 y=4

In a try/catch construct, after the catch statement is executed:

the program resumes at the statement that immediately follows the try/catch construct.

If the code in a method can potentially throw a checked exception, then that method must:

throws clause in its header

Which one of the following is the not equal operator?

!=

For the following code, what would be the value of str[2]? String[] str = {"abc", "def", "ghi", "jkl"};

"ghi"

The increment operator is:

++

If you are using a block of statements, don't forget to enclose all of the statements in a set of:

Braces

Methods are commonly used to

Break a problem down into small manageable pieces

If method A calls method B, and method B calls method C, and method C calls method D, when method D finishes, what happens?

Control is returned to method C.

To create a method you must write its:

Definition

This refers to the combining of data and code into a single object.

Encapsulation

You should not define a class field that is dependent upon the values of other class fields:

in order to avoid having stale data

________ is the process of inspecting data given to the program by the user and determining if it is valid.

input validation

Another term for an object of a class is:

instance

When an object is created, the attributes associated with the object are called:

instance fields

Which of the following is a correct method header for receiving a two-dimensional array as an argument?

public static void passArray(int [] [])

Which of the following would be a valid method call for the following method? public static void showProduct (int num1, double num2) { int product; product = num1 * (int)num2; System.out.println("The product is " + product); }

showProduct(10, 4.5);

Instance methods do not have this key word in their headers:

static

How many times will the following do-while loop be executed? int x = 11; do { x+=20; } while (x> 100);

1

Which of the following is the correct boolean expression to test for: int x being a value less than or equal to 500 or greater than 650, and int y not equal to 1000?

(x <= 500 || x > 650) && !(y ==1000))

By default, Java initializes array elements with what value?

0

Subscript numbering always starts at what value?

0

What will the following code display? String input = "99#7"; int number; try { number = Integer.parseInt(input); } catch(NumberFormatException ex) { number = 0; } catch(RuntimeException ex) { number = 1; } catch(Exception ex) { number = -1; }

0

If final int SIZE = 15 and int[] x = new int[SIZE], what would be the range of subscript values that could be used with x[]?

0 through 14

What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 10; }

100

How many times will the following for loop be executed? for (int count = 10; count <= 21; count ++) System.out.println("Java is great!!!");

11

What will be printed when the following code is executed double x = 45678.259; System.outprintf("%,.2f", x);

12.38

What will be returned from the following method? public static double methodA() { double a = 8.5 + 9.5; return a; }

18

What will be the value of x[8] after the following code has been executed? final int SUB = 12; int[] x = new int[SUB]; int y = 100; for(int i = 0; i < SUB; i++) { x[i] = y; y += 10; }

180

What will be the value of x[1] after the following code is executed? int[] x = {22, 33, 44}; arrayProcess(x); ... public static void arrayProcess(int[] a) { for(int k = 0; k < 3; k++) { a[k] = a[k] + 5; } }

38

If, within one try statement you want to have catch clauses that catch exceptions of the following types, in which order should they appear in your program? (1) Throwable (2) Exception (3) RuntimeException (4) NumberFormatException

4 3 2 1

Given the following code, what will be the value of finalAmount when it is displayed? public class Order { private int orderNum; private double orderAmount; private double orderDiscount; public Order(int orderNumber, double orderAmt, { double orderDisc) { orderNum = orderNumber; orderAmount = orderAmt; orderDiscount = orderDisc; } public double finalOrderTotal() { return orderAmount - orderAmount * orderDiscount; } } public class CustomerOrder { public static void main(String[] args) { Order order; int orderNumber = 1234; double orderAmt = 580.00; double orderDisc = .1; order = new Order(orderNumber, orderAmt, orderDisc); double finalAmount = order.finalOrderTotal(); System.out.println("Final order amount = $" + } } finalAmount); } }

522.00

What is the value of scores[2][3] in the following array? int [] [] scores = { {88, 80, 79, 92}, {75, 84, 93, 80}, {98, 95, 92, 94}, {91, 84, 88, 96} };

94

A(n) ________ is an object that is generated in memory as the result of an error or an unexpected event.

Exception

All exceptions are instances of classes that extend this class.

Exception

Which of the following will open a file named MyFile.txt and allow you to read data from it?

File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);

catch clause

The portion of a try-catch statement that handles a particular category of exception. ( all of the Above)

What do you normally use with a partially-filled array?

an accompanying integer value that holds the number of items stored in the array

What will be the result of executing the following code? int[] x = {0, 1, 2, 3, 4, 5};

an array of 6 values ranging from 0 through 5 and referenced by the variable x will be created

What will be returned from the following method? public static float[] getValue(int x)

an array of float values

the header of a value-returning method must specify this.

any valid data type

Values that are sent into a method are called ____________.

arguments

Java performs ________, which means that it does not allow a statement to use a subscript that is outside the range of valid subscripts for the array.

array bounds checking

Which of the following statements will create a reference, str, to the string, "Hello, world"? String str = new String("Hello, world"); String str = "Hello, world";

both

After the header, the body of the method appears inside a set of:

braces

This is an item that separates other items.

delimiter

Given the following statement, which statement will write "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt");

diskOut.println("Calvin");

Given the following method header, which of the method calls would be an error? public void displayValues(int x, int y)

displayValue(a,b); // where a is a short and b is a long

If numbers is a two-dimensional int array that has been initialized and total is an int that has been set to 0, which of the following will sum all the elements in the array?

for (int row = 0; row < numbers.length; row++) { for int col = 0; col < numbers[row].length; col++) total += numbers[row][col]; }

Classes that inherit from the Error class are:

for exceptions that are thrown when a critical error occurs, and the application program should not try to handle them.

This type of loop is ideal in situations where the exact number of iterations is known.

for loop

An exception's default error message can be retrieved using this method.

getMessage()

The following catch statement can: catch (Exception e) {...}

handle all exceptions that are instances of the Exception class or a subclass of Exception.

Overloading means multiple methods in the same class:

have the same name, but different parameter lists

Which of the following correctly tests the char variable chr to determine whether it is NOT equal to the character B?

if (chr != 'B')

Quite often you have to use this statement to make a group of classes available to a program.

import

When using the PrintWriter class, which of the following import statements would you write near the top of your program?

import java.io

Which of the following is a valid declaration for a ragged array

int[][] ragged = new int [5][];

A sentinel value ________ and signals that there are no more values to be entered.

is a special value that cannot be mistaken as a member of the list

What would be the result of the following code? final int SIZE = 25; int[] array1 = new in [SIZE]; ...//Code that will put values in array1 int value = 0; for (int a = 0; a <= array1.length; a++) { value += array1[a]; }

it would cause the program to crash

When an argument is passed to a method:

its value is copied into the method's parameter variable

The following package is automatically imported into all Java programs.

java.lang

You should always document a method by writing comments that appear:

just before the method's definition

Each array in Java has a public field named ________ that contains the number of elements in the array.

length

If numbers is a two-dimensional array, which of the following would give the length of row r?

numbers[r].length

Most programming languages that are in use today are:

object-oriented

This is a group of related classes.

package

When you are working with a ________, you are using a storage location that holds a piece of data.

primitive variable

In a catch statement, what does the following code do? System.out.println(e.getMessage());

t prints the error message for an exception

What do you call the number that is used as an index to pinpoint a specific element within an array?

subscript

For the following code, which statement is NOT true? public class Sphere { private double radius; public double x; private double y; private double z; }

z is available to code that is written outside the circle class.

In a multi-catch, (introduced in Java 7) the exception types are separated in the catch clause by this symbol:

|


Conjuntos de estudio relacionados

Music Appreciation 215 Chapter 16: Singing in Friendship- The Renaissance Madrigal

View Set

Lesson 8 Quiz 9 Social class in the US

View Set

MGMT 371 Iowa state Final review

View Set

Intro To Psych Exam 2 Quiz Reviews

View Set

A.D. Banker - Life Insurance Basics Ch. 14 Part 2

View Set