programming 4-6

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

three different looping structures

For, while, and do

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

Input validation

The following statement creates an ArrayList object. What is the purpose of the <String> notation? ArrayList<String> arr = new ArrayList<String>();

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

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

Loop control variable

If the constructor that follows is in the Employee class, what other form of constructor, if any, is implicitly provided? public Employee(String name, int salary) {...}

None

Objects have two general capabilities:

Objects can store data and perform operations

A constructor is a method that:

Performs initialization or setup operations.

This type of loop will always be executed at least once.

Post-test loop

writing data to files

PrintWriter objects

Method modifiers types

Public and static

generating random numbers

Random objects

reading input

Scanner objects

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

Sentinel

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

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

String

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?

The array numbers has 6 rows and 9 columns

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

This would cause the program to crash

This type of loop allows the user to decide the number of iterations.

User controlled loop

Classes

Where Objects Come From

The binary search algorithm:

Will cut the portion of the array being searched in half each time the loop fails to locate the search value

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

An array's length is

a field

An array is

a list of data elements

A search algorithm is

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

an arrays size declarator must be

a non negative integer expression

A ragged array is

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

Which of the statements are true about the following code?

a. Declares array1 to be a reference to an array of long values b. Creates an instance of an array of 10 long values c. Will allow valid subscripts in the range of 0 - 9 d. All of the above

Rectangle -length: double -width: double + setLength(len:double): void + setWidth(len:double): void + getLength(): double + getWidth(): double + getArea(): double Which of the following have parameter(s)? There may be more than one. Select all that apply

a. setLength() b. setWidth()

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

b. 180

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();

In memory, an array of String objects

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

________ is invoked to create an object.

constructor

Classes can have special methods called

constructors

Parentheses

contain nothing or a list of one or more variable declarations if the method is capable of receiving arguments.

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

length

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

length

Arrays are objects and provide a public field named

length that is a constant that can be tested. The length of an array can be obtained via its length constant

each array in java has a public field named

length, this field contains the number of elements in the array

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

static

method belongs to a class, not a specific object.

public

method is publicly available to code outside the class

Class objects normally have ________ that perform useful operations on their data, but primitive variables do not.

methods

The operations that an object can perform are known as

methods

An array can hold

multiple values of several different data types simultaneously.

If a loop is nested, the inner loop will

execute all of its iterations for each time the outer loop executes once. for(int i = 0; i < 10; i++) for(int j = 0; j < 10; j++) loop statements;

The default value for data field of a boolean type, numeric type, object type is ________, respectively.

false, 0, null

This is a class member that holds data

field

The pieces of data stored in an object are known as

fields

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

fields private

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

fields; methods

Method name

name that is descriptive of what the method does

Like if statements, loops can be

nested

A constructor that does not accept arguments is known as a

no-arg constructor The default constructor (provided by Java) is a no-arg constructor.

The while loop is a pretest loop, which means

that it will test the value of the condition prior to executing the loop.

You can provide a description of each parameter in your documentation comments by using

the @param tag. General format @param parameterName Description

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

the File class's exists method

This indicates the number of elements, or values, the array can hold.

the array's size declarator

In order for a while loop to end....

the condition must become false. The following loop will not end: int x = 20; while(x > 0) { System.out.println("x is greater than 0"); } The variable x never gets decremented so it will always be greater than 0. Adding the x-- above fixes the problem.

Variables declared have scope only for

the for loop

The update section of the for loop is

the last thing to execute at the end of each loop

When an individual element of an array is passed to a method:

the method does not have direct access to the original array

An array is accessed randomly

the reference name a subscript that identifies which element in the array to access.

In a UML diagram to indicate the data type of a variable enter:

the variable name followed by a colon and the data type

Strings are immutable objects, which means that

they cannot be changed

Accumulators are useful .

to keep running totals

The sequential search algorithm:

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

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 would be the results of the following code? int[] array1 = 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 results of the following code? final int SIZE = 25; int[] array1 = new int[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

Return type

void or the data type from a value-returning method

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()) { ... }

The while loop has the form:

while(condition) { statements; } While the condition is true, the statements will execute repeatedly

Which of the following are pre-test loops?

while, for

The binary search algorithm

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

A method should always be documented by

writing comments that appear just before the method's definition The documentation comments begin with /** and end with */

Given the declaration Circle x = new Circle(), which of the following statement is most accurate?

x contains a reference to a Circle object.

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}

For the following code, which statement is NOT true? public class Circle { private double radius; public double x; private double y; } a. y is available to code that is written outside the Circle class. b. radius is not available to code written outside the Circle class. c. radius, x, and y are called members of the Circle class. d. x is available to code that is written outside the Circle class.

y is available to code that is written outside the Circle class.

Methods that operate on an object's fields are called:

instance methods

An object is a(n) __________

instance of a class

In UML diagrams, this symbol indicates that a member is public.

+

The increment operator is:

++

In UML diagrams, this symbol indicates that a member is private:

-

. Subscript numbering always starts at what value?

0

By default, Java initializes array elements with what value?

0

A while loop executes

0 or more times. If the condition is false, the loop will not execute.

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

When an array is passed to a method:

1) a reference to the array is passed 2) it is passed just as an object 3) the method has direct access to the original array all of the above

.What is the output of the following loop? int count; count = 3; do { System.out.printf("%d ",count); count += 3; } while (count <=12);

3 6 9 12

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

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 = 20; for(int i = 0; i < SUB; i++) { x[i] = y; y += 5; }

60

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

________ is a construct that defines objects of the same type.

A class

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

A reference to the String "ghi"

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

A way to terminate

Which of the following statements are true? (Choose all that apply.) A) Constructors are invoked using the new operator when an object is created. B) Constructors must have the same name as the class itself. C) At least one constructor must always be defined explicitly. D) A default no-arg constructor is provided automatically if no constructors are explicitly declared in the class. E) Constructors do not have a return type, not even void.

A) Constructors are invoked using the new operator when an object is created. B) Constructors must have the same name as the class itself. D) A default no-arg constructor is provided automatically if no constructors are explicitly declared in the class. E) Constructors do not have a return type, not even void.

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

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

An error will occur when the program runs.

________ represents an entity in the real world that can be distinctly identified.

An object

What happens when this statement is executed? Automobile car = new Automobile(1);

An object of the Automobile class is created.

The method body is a

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

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

Methods are commonly used to

Break a problem down into small manageable pieces This is called divide and conquer

A loop that executes as long as a particular condition exists is called a:

Conditional loop

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

Counter-controlled loop

If there is no constructor defined by the programmer in the Employee class, what form of constructor, if any, is implicitly provided?

Employee(){...}

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 scope of a public instance field is:

The instance methods and methods outside the class

Increment and Decrement Operators

There are numerous times where a variable must simply be incremented or decremented. number = number + 1; number = number - 1;

The test section of the for statement

acts in the same manner as the condition section of a while loop.

This ArrayList class method is used to insert an item into an ArrayList.

add

You can use this ArrayList class method to insert an item at a specific location in an ArrayList

add

The initialization section of the for loop

allows the loop to initialize its own control variable.

Each object that is created from a class is called

an instance of the class

Each repetition of a loop is known as what?

an iteration

An array is an object so it needs

an object reference

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

What would be the results after the following code was executed? int[] x = {23, 55, 83, 19}; int[] y = {36, 78, 12, 24}; for(int a = 0; a < x.length; a++) { x[a] = y[a]; y[a] = x[a]; }

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

When the public access specifier is applied to a class member, the member can

be accessed by code inside the class or outside.

A class is analogous to a(n)

blueprint

A class's responsibilities include:

both the things a class is responsible for doing and the things a class is responsible for knowing

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

braces, {}

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

c. final

The break statement

can be used to abnormally terminate a loop

A sentinel value

can be used to notify the program to stop acquiring input If it is a user input, the user could be prompted to input data that is not normally in the input data range (i.e. -1 where normal input would be positive.)

When the private access specifier is applied to a class member, the member

cannot be accessed by code outside the class. The member can be accessed only by methods that are members of the same class

An object is an instance of a ________.

class

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

class

The keyword ________ is required to declare a class.

class

In your textbook the general layout of a UML diagram is a box that is divided into three sections. The top section has the ________; the middle section holds ________; the bottom section holds ________.

class name; attributes or fields; methods

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

class; objects

Methods simplify programs. If a specific task is performed in several places in the program, a method can be written once to perform that task, and then be executed anytime it is needed. This is known as

code reuse

A local variable is

declared inside a method and is not accessible to statements outside the method

This is an item that separates other items.

delimiter

A class is code that

describes a particular type of object. It specifies the data that an object can hold (the object's fields), and the actions that an object can perform (the object's methods).

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

diskOut.println("Calvin");

Which one of the following statements will execute the following method? public static void displayMenu() { System.out.println("1 - Larry"); System.out.println("2 - Moe"); System.out.println("3 - Curly"); }

displayMenu();

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

do-while loop

Which of the following for loops is valid, given the following declaration? String[] names = {"abc", "def", "ghi", "jkl"};

for (int i = 0; i < names.length; i++) System.out.println(names[i].length());

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

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

for loop

a constructor

has the same name as the class

Overloading means multiple methods in the same class:

have the same name, but different parameter lists

To create a method, you must write a definition, which consists of a

header and a body

An access specifier is a Java keyword that indicate

how a field or method can be accessed

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

import java.io.*;

If a loop does not contain within itself a way to terminate, it is called an:

infinite loop

Loops that do not end are called

infinite loops

In Java, you do not use the new operator when you use a(n):

initialization list

The for loop allows the programmer to...

initialize a control variable, test a condition, and modify the control variable all in one line of code

Another term for an object of a class is

instance

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

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

A search algorithm:

is a way to locate a specific item in a larger collection of data

A constructor is a method that

is automatically called when an object is created

Input validation

is the process of ensuring that user input is valid.

java performs array bounds checking which means that

it does not allow a statement to use a subscript that is outside the range of valid subscripts for an array

A method executes when

it is called

The following package is automatically imported into all Java programs.

java.lang

The ArrayList class is in this package

java.util

UML diagrams are

language independent and they use an independent notation to show return types, access modifiers, etc

Arrays have a final field named

length

You use this method to determine the number of items stored in an ArrayList object

numberItems

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

numbers[r].length

A UML diagram does not contain:

object names

Most programming languages that are in use today are:

object-oriented

Constructors are used to perform

operations at the time an object is created.

In Java, all arguments of the primitive data types are

passed by value, which means that only a copy of an argument's value is passed into a parameter variable

The do-while loop is a

post-test loop, which means it will execute the loop prior to testing the condition

The for loop is a

pre-test loop.

Unified Modeling Language (UML)

provides a set of standard diagrams for graphically depicting object-oriented systems

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

public static void passArray(int [][])

This ArrayList class method deletes an item from an ArrayList.

remove

A value-returning method not only performs a task, but also

sends a value back to the code that called it.

A(n) _______ is a dummy value that signals the end of the data.

sentinel value

You can use this ArrayList class method to replace an item at a specific location in an ArrayList.

set

The method header, which appears at the beginning of a method definition, lists

several important things about the method, including the method's name.

A void method is one that

simply performs a task and then terminates.

The main method is automatically called when a program

starts, but other methods are executed by method call statements.

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

static

Given that String[] str has been initialized, to get a copy of str[0] with all characters converted to upper case, use the following statement

str[0].toUpperCase();

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

subscript

each element is assigned a number known as a

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.


Kaugnay na mga set ng pag-aaral

Chapter 4 - Profitability Analysis

View Set

AICE Business Unit 3 Test "Marketing" Review

View Set

Computer Graphics Final Exam Flash Cards

View Set

positions used during a general physical exam

View Set

Isaac Rudansky - Google Ads Masterclass - Chapter 2 - Creating and Setting Up Our First Google Ads Account

View Set

Ch 51: Assessment and Management of Patients With Diabetes Mellitus

View Set

Science - Chapter 11 - Water (Water Resources)

View Set

BIOL 446 Final (Non-Cumulative Portion)

View Set

Significant Figures, Kinematics, & Dynamics

View Set