COSC 240 Java Exam 2

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

having two or more methods with the same name but different signatures.

Overloading is

15

What will be the value of x after the following code is executed? int x, y = 15; x = y--;

size

Which method is used to determine the number of items stored in an ArrayList object?

Tree.PINE

Given the following declaration: enum Tree ( OAK, MAPLE, PINE ) What is the fully-qualified name of the PINE enum constant?

True

When an object is passed as an argument, it is actually a reference to the object that is passed.

True

The String[] args parameter in the main method header allows the program to receive arguments from the operating system command-line.

for

The ________ loop is ideal in situations where the exact number of iterations is known.

long[ ]

To return an array of long values from a method, which return type should be used for the method?

The code contains a syntax error and will not compile.

What will be the result after the following code is executed? final int ARRAY_SIZE = 5; float[] x = float[ARRAY_SIZE]; for (i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; }

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

A partially filled array is normally used

All of these (-do not enclose the enum constants in quotation marks. -should use the standard convention of writing the enum constants in uppercase. -are actually creating a special kind of class.)

When you write an enumerated type declaration, you

F both of these (-create your own data type. -specify the values that belong to the type.)

You can use the enum key word to

a way to terminate.

In all but very rare cases, loops must contain, within themselves

Class, Responsibilities, Collaborations.

CRC stands for

a reference to the String object containing "ghi"

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

True

enum constants have a toString method.

5

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

1

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

False

The for loop is a posttest loop that has built-in expressions for initializing, testing, and updating.

It specifies that only String objects may be stored in the ArrayList object.

What does specify in the following statement? ArrayList nameList = new ArrayList();

"Has a"

________ is the term for the relationship created by object aggregation.

a two-dimensional array where the rows have different numbers of columns.

A ragged array is

is used to locate a specific item in a collection of data.

A search algorithm

numbers[r].length

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

write a method for the class that will make a field by field copy of object1 data members into object2 data members.

If object1 and object2 are objects of the same class, to make object2 a copy of object1

False

In a for loop, the control variable is always incremented.

whole-part relationship

The "has a" relationship is sometimes called a(n) ________ because one object is part of a greater whole.

garbage collection

The JVM periodically performs the ________ process to remove unreferenced objects from memory.

do-while

The ________ loop is ideal in situations where you always want the loop to iterate at least once.

remove

The ________ method removes an item from an ArrayList at a specific index.

they cannot refer to nonstatic members of the class.

The only limitation that static methods have is

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

The sequential search algorithm

only one copy of the field in memory.

When a field is declared static there will be

delimiter.

An item that separates other items is known as a

SavingsAccount.getNumberOfAccounts();

If you have defined a class, SavingsAccount, with a public static method, getNumberOfAccounts, and created a SavingsAccount object referenced by the variable account20, which of the following will call the getNumberOfAccounts method?

text and binary.

In general, there are two types of files which are

the array must first be sorted.

In order to do a binary search on an array

True

The do-while loop is ideal in situations where you always want the loop to iterate at least once.

100

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

40

What will be the value of x after the following statements are executed? int x = 10; for (int y = 5; y < 20; y +=5) x += y;

x = 33, y = 9

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

The value variable will contain the lowest value in the numbers array.

What would be the result after the following code is executed? int[] numbers = {40, 3, 5, 7, 8, 12, 10}; int value = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] < value) value = numbers[i]; }

a reference to an object of that class

When a method's type is an object, what is actually returned by the calling program?

the File class's exists method

Which of the following is the method you can use to determine whether a file exists?

public Rectangle()

Of the following, which would be considered the no-arg constructor for the Rectangle class?

False

The names of the enum constants in an enumerated data type must be enclosed in quotation marks.

ordinal

Enumerated types have the ________ method which returns the position of an enum constant in the declaration list.

True

If a class has a method named finalize, it is called automatically just before an instance of the class is destroyed by the garbage collector.

public static void passMyArray(int[][] myArray)

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

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

Which of the following is a valid declaration for a ragged array with five rows but no columns?

toString

Which of the following methods returns a string representing all of the items stored in an ArrayList object?

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

Which of the following statements opens a file named MyFile.txt and allows you to read data from it?

a case expression.

You cannot use the fully-qualified name of an enum constant for

The value variable will contain the sum of all the values in the numbers array.

What would be the result after the following code is executed? int[] numbers = {50, 10, 15, 20, 25, 100, 30}; int value = 0; for (int i = 1; i < numbers.length; i++) value += numbers[i];

False

When an array of objects is declared but not initialized, the array values are set to 0.

set

You can use the ________ method to replace an item at a specific location in an ArrayList.

accumulator.

The variable used to keep a running total in a loop is called a(n)

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

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

12

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

94

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} };

null

By default, a reference variable that is an instance field is initialized to the value

set the accumulator variable to an initial value, often zero.

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

True

Any items typed on the command-line, separated by space, after the name of the class, are considered to be one or more arguments that are to be passed into the main method.

The account object's toString method will be implicitly called.

Assume the class BankAccount has been created and the following statement correctly creates an instance of the class. BankAccount account = new BankAccount(5000.00); What is true about the following statement? System.out.println(account);

int number = inputFile.nextInt();

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?

All of these are true. (-It declares array1 to be a reference to an array of long values. -It creates an instance of an array of ten long values. -It will allow valid subscripts in the range of 0 through 9.)

Which of the following statements is(are) true about this code? final int ARRAY_SIZE = 10; long[] array1 = new long[ARRAY_SIZE];

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

Which of the following statements opens a file named MyFile.txt and allows you to append data to its existing contents?

False

You can declare an enumerated data type inside a method.

True

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

use the toString method.

To get the name of a calling enum constant

sentinel

A ________ is a value that signals when the end of a list of values has been reached.

posttest

A ________ loop will always be executed at least once.

inner class.

A class that is defined inside another class is called a(n)

True

A class's static methods do not operate on the fields that belong to any instance of the class.

enum

A declaration for an enumerated type begins with the ________ key word.

True

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

conditional

A loop that executes as long as a particular condition exists is called a(n) ________ loop.

count-controlled.

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

the method name and the parameter list.

A method's signature consists of

True

A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order.

False

A sorting algorithm is used to locate a specific item in a larger collection of data.

after the access specifier and before the field's data type.

A static field is created by placing the key word static

sentinel

A(n) ________ is a special value that cannot be mistaken as a member of a list of data items and signals that there are no more data items to be processed.

subscript

A(n) ________ is used as an index to pinpoint a specific element within an array.

True

An ArrayList object automatically expands in size to accommodate the items stored in it.

consists of an array of references to String objects.

An array of String objects

True

An enumerated data type is actually a special type of class.

True

An instance of a class does not have to exist in order for values to be stored in a class's static fields.

while (inputFile.hasNext())

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

OAK

Assume the following declaration exists: enum Tree ( OAK, MAPLE, PINE ) What will the following code display? System.out.println(Tree.OAK);

the address of an object of the Rectangle class

Given the following method header, what will be returned from the method? public Rectangle getRectangle()

diskOut.println("Calvin");

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

The numbers array has 6 rows and 9 columns.

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

infinite

If a loop does not contain, within itself, a valid way to terminate, it is called a(n) ________ loop

This is a public method that accepts a Stock object as its argument and returns a boolean value.

If the following is from the method section of a UML diagram, which of the statements shown is true? + equals(object2:Stock) : boolean

True

If you write a toString method for a class, Java will automatically call the method any time you concatenate an object of the class with a string.

True

Java does not limit the number of dimensions an array may have.

variable-length argument list

Java provides a mechanism known as a ________ which makes it possible to write a method that takes a variable number of arguments.

True

Objects in an array are accessed with subscripts, just like any other data type in an array.

-update the control variable during each iteration -initialize the control variable to a starting value -test the control variable by comparing it to a maximum or minimum value -terminate when the control variable reaches its maximum or minimum value (all the above)

Select all that apply. Which of the following steps is normally performed by a for loop?

False

The Java compiler will display an error message when it processes a statement that uses an invalid subscript.

will cut the portion of the array being searched in half each time it fails to locate the search value.

The binary search algorithm

True

The key word this is the name of a reference variable that an object can use to refer to itself.

False

The while loop is always the best choice in situations where the exact number of iterations is known.

True

To determine if two arrays are equal you must compare each of the elements of the two arrays.

It allows the user to enter the name of the file that data will be written to.

What does the following code do? Scanner keyboard = new Scanner(System.in); String filename; System.out.print("Enter the filename: "); filename = keyboard.readString(); PrintWriter outFile = new PrintWriter(filename);

It does all of these. (-It creates an instance of an array of ten double values. -It declares array1 to be a reference to an array of double values. -It will allow valid subscripts in the range of 0 through 9.)

What does the following statement do? double[] array1 = new double[10];

210

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

aggregate

When a method in the ________ class returns a reference to a field object, it should return a reference to a copy of the field object to prevent security holes.

All of these are true. (-a reference to the array is passed. -it is passed just as any other object would be passed. -the method has direct access to the original array.)

When an array is passed to a method

False

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.

True

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.

it must be the first statement in the constructor making the call.

When the this variable is used to call a constructor

import java.io.*;

When working with the PrintWriter class, which of the following import statements should you have near the top of your program?

you are performing a deep copy.

When you make a copy of the aggregate object and of the objects that it references,

True

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

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.

add

Which of the following ArrayList class methods is used to insert an item at a specific location in an ArrayList?

the equals and compareTo methods

Which of the following can you use to compare two enum data values?

import java.util.ArrayList;

Which of the following import statements is required in order to use the ArrayList class?


Kaugnay na mga set ng pag-aaral

Psychology in Everyday Life Chapter 1 3 4 and 5

View Set

History Final: The United States as a World Power

View Set

SLP ETS Practice Test Form 3 + VOCAB and CONCEPTS

View Set

Timeline Covert FIRC in Iran 1953

View Set