BCIS 3630 Exam 3

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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

+

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

-

arraylist

-Automatically expands when a new item is added -Automatically shrinks when items are removed

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

0 through 14

1. numbers[0] = 20 2. numbers[3] = 30

1. numbers sub zero is assigned 20 2. numbers sub 3 is assigned 30

int[ ] numbers = {3,6,9} for (int val : numbers) System.out.println(val);

3 6 9

What is stored by a reference variable?

A memory address

What do you call a constructor that accepts no arguments?

A no-arg constructor

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

A wildcard import

Bounds Checking

-Occurs at runtime -if invalid subscripts, program throws exception and terminates

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

String[] words = { "Hello", "Goodbye" };System.out.println(words.toUpperCase());

A subscript should be used with words, such as words[0].toUpperCase().

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

Both 1 & 2

Instance methods should be declared static.

F

The public access specifier for a field indicates that the attribute may not be accessed by statements outside the class.

F

The term "default constructor" is applied to the first constructor written by the author of a class.

F

When a local variable in an instance method has the same name as an instance field, the instance field hides the local variable.

F

True or False: A class may not have more than one constructor.

False

True or False: An array's sitze declarator can be a negative integer expression.

False

You can use the == operator to compare two array reference variables and determine whether the arrays are equal. T/F

False

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

Fields private

How does method overloading improve the usefulness of a class?

Several different versions of the same method can be created, each performing an operation in a different manner. This makes the class more flexible.

An object can store data.

T

Find the error in the following class:public class MyClass{ private int x; private double y; public void MyClass(int a, double b) { x = a; y = b; }}

The constructor cannot have a return type, not even void.}

int[] hours = 8, 12, 16;

The initialization list must be enclosed in braces.

Find the error in the following class:public class FindTheError{ public int square(int number) { return number * number; } public double square(int number) { return number * number; }}

The square methods must have different parameter lists. Bothaccept an int.

What does the following UML diagram entry mean? + setHeight(h : double) : void

This is a public method with a parameter of data type double and does not return a value

Individual elements are processed like any other type of variable

True

Methods may return arrays and accept as arguments

True

Once an array is created, it cannot be changed

True

TRUE/FALSE: A class is not an object, but a description of an object.

True

TRUE/FALSE: A constructor is a method that is automatically called when an object is created.

True

True or False: The Java compiler does not display an error message when it processesa statement that uses an invalid subscript.

True

True or False: The new operator creates an instance of a class.

True

arrays

a list of data elements. •can store any type of data but only one type of data at a time.

constructors

a method that is automatically called when an instance of a class is created.

subscripts

a number that identifies which number in the array to access

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

a wildcard import

Which of the following are classes from the Java API?

a. Scanner b. Random c. PrintWriter

This is a method that gets a value from a class's field, but does not change it. a. accessor b. constructor c. void d. mutator

a. accessor

This search algorithm repeatedly divides the portion of an array being searched in half. a. binary search b. sequential search c. selection search d. iterative search

a. binary search

When initializing a two-dimensional array, you enclose each row's initialization list in___________. a. braces b. parentheses c. brackets d. quotation marks

a. braces

This is a collection of programming statements that specify the fields and methods that a particular type of object may have. a. class b. method c. parameter d. instance

a. class

To delete an item from an ArrayList object, you use this method. a. remove b. delete c. erase d. get

a. remove

When a local variable has the same name as a field, the local variable's name does this to the field's name. a. shadows b. complements c. deletes d. merges with

a. shadows

To determine the number of items stored in an ArrayList object, you use this method. a. size b. capacity c. items d. length

a. size

Each element of an array is accessed by a number known as a(n) __________. a. subscript b. size declarator c. address d. specifier

a. subscript

string arrays

an array of references to String objects - each element of this array must be initialized

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

an explicit import

The process of matching a method call with the correct method is known as__________. a. matching b. binding c. linking d. connecting

b. binding

A class is analogous to a(n) __________. a. house b. blueprint c. drafting table d. architect

b. blueprint

This is a method that is automatically called when an instance of a class is created. a. accessor b. constructor c. void d. mutator

b. constructor

This key word causes an object to be created in memory. a. create b. new c. object d. construct

b. new

This search algorithm steps through an array, comparing each item with the search value. a. binary search b. sequential search c. selection search d. iterative search

b. sequential search

When the value of an item is dependent on other data, and that item is not updated when the other data is changed, what has the value become? a. bitter b. stale c. asynchronous d. moldy

b. stale

A class's responsibilities include:

both A and B

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

braces, {}

array bounds checking happens when the program is a) compiled b) saved c) run d) loaded into memory

c

this array field holds the number of elements that the array has a) size b) elements c) length d) width

c

to insert an item at a specific location in an ArrayList object, you use this method a) store b) insert c) add d) get

c

To insert an item at a specific location in an ArrayList object, you use this method. a. store b. insert c. add d. get

c. add

This is automatically provided for a class if you do not write one yourself. a. accessor method b. default instance c. default constructor d. variable declaration

c. default constructor

This is a class member that holds data. a. method b. instance c. field d. constructor

c. field

This array field holds the number of elements that the array has. a. size b. elements c. length d. width

c. length

Two or more methods in a class may have the same name, as long as this is different. a. their return values b. their access specifier c. their parameter lists d. their memory address

c. their parameter lists

Array bounds checking happens __________. a. when the program is compiled b. when the program is saved c. when the program runs d. when the program is loaded into memory

c. when the program runs

Array

can hold multiple values of the same dat type simultaniously

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

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

the last subscript in an array is always a) 100 b) 0 c) -1 d) 1 less than the number of elements

d

this is the typical number of comparisons performed by the sequential search on an array of N elements a) 2N b) N c) Nsquared d) N/2

d

The last subscript in an array is always __________. a. 100 b. 0 c. 21 d. 1 less than the number of elements

d. 1 less than the number of elements

This is the typical number of comparisons performed by the sequential search on anarray of N elements (assuming the search values are consistently found). a. 2N b. N c. N2 d. N/2

d. N/2

A class's responsibilities are __________. a. the objects created from the class b. things the class knows c. actions the class performs d. both b and c

d. both b and c

An object is a(n) __________. a. blueprint b. primitive data type c. variable d. instance of a class

d. instance of a class

This is a method that stores a value in a field or in some other way changes the value of a field. a. accessor b. constructor c. void d. mutator

d. mutator

String Arrays

each string must be created individually if the array is uninitialized

T/F an array's size declarator can be a negative integer expression

false

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

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

import

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

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

instance methods

A constructor is a method that:

returns an object of the class.

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

they have different parameter lists

T/F Java does no allow a statement to use a subscript that is outside the range of valid subscripts for an array

true

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

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

A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses?

Classes are analogous to the blueprint.

A method that gets a value from a class's field but does not change it is known as a mutator method.

F

TRUE/FALSE: Instance methods should be declared static.

False

Overloading means multiple methods in the same class:

Have the same name, but different parameter lists

Another term for an object of a class is:

Instance

Assume a program named MailList.java is stored in the DataBase folder on your hard drive. The program creates objects of the Customer and Account classes. Describe the steps that the compiler goes through in locating and compiling the Customer andAccount classes.

It looks in the current folder or directory for the file Customer.class.If that file does not exist, the compiler searches for the file Customer.java and compiles it. This creates the file Customer. class,which makes the Customer class available. The same procedure is followed when the compiler searches for the Account class.

TRUE/FALSE: A method that stores a value in a class's field or in some other way changes the value of a field is known as a mutator method.

True

TRUE/FALSE: An access specifier indicates how the class may be accessed.

True

TRUE/FALSE: An object can store data.

True

TRUE/FALSE: Instance methods do not have the key word static in their headers.

True

TRUE/FALSE: Shadowing is the term used to describe where the field name is hidden by the name of a local or parameter variable.

True

TRUE/FALSE: The java.lang package is automatically imported into all Java programs.

True

True or False: A two-dimensional array has multiple length fields.

True

By default, Java initializes array elements as..

Zero

when initializing a two-dimensional array, you enclose each row's initialization list in a) braces b) parenthesis c) brackets d) quotation marks

a

An element's subscript and the value stored in the element (are/are not) ________ the same thing.

are not

When using a constructor that has parameter variables, you must provide ___________ for them.

arguments

1. In an array declaration, this indicates the number of elements that the array will have a) subscript b) size declarator c) element sum d) reference variable

b

the first subscript in an array is always a) 1 b) 0 c) -1 d) 1 less than the number of elements

b

this search algorithm steps through an array comparing each item with the search value a) binary search b) sequential search c) selection search d) iterative search

b

The first subscript in an array is always ___________. a. 1 b. 0 c. 21 d. 1 less than the number of elements

b. 0

Java does not allow you to use a _______ value that is outside the range of valid ________ for an array

subscript

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

the variable name followed by a colon and the data type

passing arguments in arrays

-There is no difference between passing a single or two-dimensional array as an argument to a method. -The method must accept a two-dimensional array as a parameter

Passing an array as an argument to a method

-pass the value in the variable that references an array -when a single elements is passed, it is handled like any other argument

You cannot use the enhanced for loop when..

-you need to change the contents of array element -you need to work through elements in reverse -if you're not accessing all of the elements -if you need to work with two or more arrays -if you need to refer to the subscript number of a certain element

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

1 and 2

What is the difference between a class and an instance of a class?

A class is a collection of programming statements that specify the attributes and methods that a particular type of object may have. You should think of a class as a"blueprint" that describes an object. An instance of a class is an actual object that exists in memory.

boolean

A single value of either TRUE or FALSE

Design a class named Pet, which should have the following fields:• name. The name field holds the name of a pet.• animal. The animal field holds the type of animal that a pet is. Example values are"Dog", "Cat", and "Bird".• age. The age field holds the pet's age. The Pet class should also have the following methods:• setName. The setName method stores a value in the name field.• setAnimal. The setAnimal method stores a value in the animal field.• setAge. The setAge method stores a value in the age field.• getName. The getName method returns the value of the name field.• getAnimal. The getAnimal method returns the value of the animal field.• getAge. The getAge method returns the value of the age field.a. Draw a UML diagram of the class. Be sure to include notation showing each field and method's access specification and data type. Also include notation showing any method parameters and their data types.b. Write the Java code for the Pet class.

A. UML diagram B. Class code

What is an accessor method? What is a mutator method?

An accessor method is a method that gets a value from a class's field but does not change it. A mutator method is a method that stores a value in a field or in some other way changes the value of a field.

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

An explicit import

Why are constructors useful for performing "start-up" operations?

Because they execute when an object is created.

When the same name is used for two or more methods in the same class, how doesJava tell them apart?

By their signatures, which includes the method name and the data types of the method parameters, in the order that they appear.

One or more objects may be created from a:

Class

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

Encapsulation

A semicolon always appears at the end of a method header. T/F

False

TRUE/FALSE: A method that gets a value from a class's field but does not change it is known as a mutator method.

False

TRUE/FALSE: The public access specifier for a field indicates that the attribute may not be accessed by statements outside the class.

False

TRUE/FALSE: The term "default constructor" is applied to the first constructor written by the author of a class.

False

TRUE/FALSE: When a local variable in an instance method has the same name as an instance field, the instance field hides the local variable.

False

True or False: The first size declarator in the declaration of a two-dimensional arrayrepresents the number of columns. The second size declarator represents the numberof rows.

False

True or False: To find the classes needed for an object-oriented application, you identify all of the verbs in a description of the problem domain.

False

True or False: When you write a constructor for a class, it still has the default constructor that Java automatically provides.

False

Under what circumstances does Java automatically provide a default constructor fora class?

If you do not write a constructor for a class,Java automatically provides one.

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

Import

What is the purpose of the new key word?

It creates an object (an instance of a class) in memory.

If a class has a private field, what has access to the field?

Methods that are members of the same class.

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

String

Which of the following statements will create a reference, str, to the String, "Hello, World"?

String str = "Hello, World";

A class in not an object, but a description of an object.

T

A constructor is a method that is automatically called when an object is created.

T

A method that stores a value in a class's field or in some other way changes the value of a field is known as a mutator method.

T

An access specifier indicates how the class may be accessed.

T

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

T

Shadowing is the term used to describe where the field name is hidden by the name of a local or parameter variable.

T

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

T

The term "no-arg constructor" is applied to any constructor that does not accept arguments.

T

To copy an array, you should copy the individual elements of one array to another. T/F

T

Find the error in the following class:public class TwoValues{ private int x, y; public TwoValues() { x = 0; } public TwoValues() { x = 0; y = 0; }}

The constructors must have different parameter lists. Both are empty.

int[] table = new int[10];for (int x = 1; x <= 10; x++){ table[x] = 99;}

The loop uses the values 1 through 10 as subscripts. It should use 0 through 9.

Assume that the following method is a member of a class. Find the error.public void total(int value1, value2, value3){ return value1 + value2 + value3;}

The method should have int as its return type

The following statement attempts to create a Rectangle object. Find the error.Rectangle box = new Rectangle;

The parentheses are missing. The statement should read:Rectanglebox = new Rectangle();

Overloading

The process of creating multiple methods of the same name.

int[] collection = new int[-20];

The size declarator cannot be negative.

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

The variable name followed by a colon and the data type

TRUE/FALSE: The term "no-arg constructor" is applied to any constructor that does not accept arguments.

True

True or False: An ArrayList automatically expands in size to accommodate the itemsstored in it.

True

True or False: Both of the following declarations are legal and equivalent:int[] numbers;int numbers[];

True

True or False: Each instance of a class has its own set of instance fields

True

True or False: Java does not allow a statement to use a subscript that is outside therange of valid subscripts for an array

True

True or False: The subscript of the last element in a single-dimensional array is oneless than the total number of elements in the array.

True

True or False: The values in an initialization list are stored in the array in the orderthat they appear in the list.

True

True or False: When an array is passed to a method, the method has access to the original array

True

You cannot change the value of an array's length field

True

Is it a good idea to make fields private? Why or why not?

When an object's fields are hidden from outside code, the fields are protected from accidental corruption. It is good idea to make all of a class's fields private and to provide access to those fields through methods.

String[] names = { "George", "Susan" };int totalLength = 0;for (int i = 0; i < names.length(); i++) totalLength += names[i].length;

With an array, length is a field. With a String, length is a method. This code uses length as if it were a method in the array, and a field in the String.

Which of the following is not involved in finding the classes when developing an object-oriented application?

Write the code.

each element of an array is accessed by a number known as a a) subscript b) size declarator c) address d) specifier

a

this search algorithm repeatedly divides the portion of an array being searched in half a) binary search b) sequential search c) selection search d) iterative search

a

to delete an item from an ArrayList object, you use this method a)remove b)delete c)erase d)get

a

to determine the number of items stored in an ArrayList object, you use this method a)size b) capacity c) items d) length

a

Look at the following description of a problem domain: The bank offers the following types of accounts to its customers: savings accounts, checking accounts, and money market accounts. Customers are allowed to deposit money into an account (thereby increasing its balance), withdraw money from an account (thereby decreasing its balance), and earn interest on the account. Each account has an interest rate. Assume that you are writing an application that will calculate the amount of interest earned for a bank account.a. Identify the potential classes in this problem domain.b. Refine the list to include only the necessary class or classes for this problem.c. Identify the responsibilities of the class or classes.

a) After eliminating duplicates, objects, and primitive values, the potential classes are: bank, account, and customer b) The only class needed for this particular problem is account. c) The account class knows its balance and interest rate. The account can calculate interest earned.

Consider the following class declaration:public class Square{ private double sideLength; public double getArea() { return sideLength * sideLength; } public double getSideLength() { return sideLength; }}a. Write a no-arg constructor for this class. It should assign the sideLength field the value 0.0.b. Write an overloaded constructor for this class. It should accept an argument that is copied into the sideLength field.

a) public Square() { sideLength = 0.0; } b) public Square(double s) { sideLength = s; }

Look at the following partial class definition, and then respond to the questions that follow it:public class Book{ private String title; private String author; private String publisher; private int copiesSold;}a. Write a constructor for this class. The constructor should accept an argument foreach of the fields.b. Write accessor and mutator methods for each field.c. Draw a UML diagram for the class, including the methods you have written.

a. Constructor: b. Accessor and mutator methods c. UML diagram:

T/F the first size declarator in the declaration of a two-dimensional array represents the number of columns. the second size declarator represents the number of rows

false

When a method's local variable has the same name as a _______ in the same class, the local variable's name _______ the field's name.

field;shadows

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

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

in order to avoid having stale data

Reference Copy

int [ ] array1 = { 2,4,6]; int [] array2 = array2 -only the address of the array is copied, not the contents of the array object -after execution, both array variables will reference the same array

enhanced for loop

iterates once for every element in an array

The following package is automatically imported into all Java programs.

java.lang

When you use the ==operator with reference variables, the operator compares the ________ _______ that the variables contain, not the contents of the objects referenced by the variables.

memory addresses

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

methods

Mutators

methods that modify the data of fields (setter)

accessors

methods that retrieve data from the field (getter)

Declaring a variable to reference an object does not create an object. You must use the _______ key word to create the object.

new

If you write a constructor that accepts arguments, you must also write a ________ constructor for the same class if you want to be able to create __________ of the class without passing ________ to the constructor

no-arg;instances;arguments

A UML diagram does not contain:

object names

Most programming languages that are in use today are:

object-oriented

When processing arrays, the subscripts start at zero and end at ____________ than the number of elements in the array

one less

This is a group of related classes.

package

Overloaded methods must have unique ____________ ____.

parameter lists

The name of a class appears after the new key word, and a set of _____________ appears after the class name. You must write the ____________ even if no arguments are passed to the constructor.

parenthesis

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

primitive variable

In an array declaration, this indicates the number of elements that the array will have. a. subscript b. size declarator c. element sum d. reference variable

size declarator

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

static

The scope of a public instance field is:

the instance methods and methods outside the class

The scope of a private instance field is:

the instance methods of the same class

Size Declarator

the number inside the brackets ex: numbers = new int[6]

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

What does the following UML diagram entry mean? + setHeight(h : double) : void

this is a public method with a parameter of data type double and does not return a value

T/F a two-dimensional array has multiple length fields

true

T/F an arraylist automatically expands in size to accommodate the items stored in it

true

T/F both of the following declarations are legal and equivalent int[] numbers; in numbers[];

true

T/F the java compiler does not display an error message when it processes a statement that uses an invalid subscript

true

T/F the subscript of the last element in a single-dimensional array is one less than the total number of elements in the array

true

T/F the values in an initialization list are stored in the array in the order that they appear in the list

true

T/F when an array is passed to a method, the method has access to the original array

true

Subscript

used as an index to pinpoint a specific element within an array i.e. 6 elements, subscripts 0-5 ex: numbers[0] pronounced "numbers sub zero"

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

The subscript identifies an element, which holds a _____

value

Partially filled arrays passed as an argument to a method

variable that holds the count of items must also be passed. If not, method would not be able to determine number of items stored

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.


Set pelajaran terkait

Chemistry Midterm (chapters 2-4,6-10)

View Set

Предмет психології

View Set

A & P 1 Lab Week 4 Exercises 6-7

View Set

Reproduction: Postpartum Newborn and Complications

View Set