CS 1054 Exam 2

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

Behavior

The list of services that an object will perform

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

True The for each loop automatically extracts the values already in the array it can't be used to set values in the array

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 The return type of a method is not part of the method signature.

True The return value of a method can be ignored, so it doesn't help identify the method declaration to use

T/F A constructor often sets the initial values of an object's instance data.

True The role of the constructor is to set up a newly created object

T/F Constants can be declared with public visibility without violating encapsulation.

True The value of a constant cannot be changed, so allowing other objects to have direct access to it does not violate encapsulation

T/F Objects made from wrapper classes are immutable.

True The value of a wrapper object can't be changed once it has been created.

T/F Classes should be designed to have well-defined and limited interactions with other classes.

True These interactions make up the public interface to an object

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

True They work just as they do with the System.out object

T/F The Java keyword this is an explicit reference to the object on which a method is invoked.

True This allows an object to refer to itself in methods declared inside that object

T/F The this reference can be used to call one constructor from another.

True This makes it easier to write the setup code in only one constructor

Write a getter method for a String firstName

public String getFirstName() { return firstName; }

Write a toString method that will produce the output "Count is: (value of count)" using int count

public String toString() { return "Count is: " + count; }

Write a getter method for an int height

public int getHeight() { return height; }

What would you have to add to this method header so it would throw a FileNotFoundException? public static void main(String[] args)

public static void main(String[] args) throws FileNotFoundException

Write a setter method for a string firstName using a string parameter fName

public void setFirstName(String fName) { firstName = fName; }

write a setter method for an int height

public void setHeight(int newHeight) { height = newHeight; }

Checked Exception

the program must deal with it one way or another example: FileNotFoundException

public interface

the set of methods that the client can call. Each method in the public interface should perform one well-defined task. It should be clear to the client what effects calling the method will have, but that's it.

4 Access modifiers

1) Public - Accessible within its class Accessible from any class in the same package Accessible from any subclass Accessible from any classes in other packages 2) Protected - Accessible within its class Accessible from any class in the same package Accessible from any subclass 3) Default - Accessible within its class Accessible from any class in the same package 4) Private - Accessible within its class

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 1 3 4 6

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 2 3 4 5

4

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

A An input file scanner is created by passing a File object to the Scanner constructor

What's wrong with this version of the Square constructor? public Square(double side) { side = this.side; } a) It assigns the current value of the instance variable to the parameter, which is useless. b) It assigns the parameter value to the instance data. c) A syntax error occurs. d) It causes a runtime error.

A Instead the parameter (side) should be assigned to the instance data (this.side)

Which one of the following constants and methods is NOT part of the Integer wrapper class? a) POSITIVE_INFINITY b) MIN_VALUE c) toBinaryString d) SIZE e) parseInt

A Only Float and Double have this constant.

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; } a) 10, 20, 30, 40, 50 b) 100, 80, 60, 40, 20 c) 10, 20, 30, 40, 50, 10, 20, 30, 40, 50 d) 20, 40, 60, 80, 100

A The for-each loop does not change the values stored in the array.

Which method signature could NOT represent an overloaded version of the following method? public int max(int x, int y) { return (x > y) ? x : y; } a) max(int, int) b) max(int, int, int) c) max(String, String) d) max(Object, Object) e) max(double, double)

A the signatures match exactly so this couldn't be an overloaded version of the method.

toString Method

A method that returns a textual description of an object.

Instance

A particular object.

Class

A programming construct that contains data and methods.

Array initialization list

A technique for creating an array and assigning it some initial values.

access modifiers/visibility modifiers

A variable's access modifier determines which classes can reference (and possibly change) it. A method's access modifier determines which classes can invoke it.

Identity

A way to refer to a specific object

Client (what does a client have to do with encapsulation)

Any code that interacts with an object You should make it difficult, if not impossible, for a client to "reach in" and change the value of a instance variable directly. An object should be a BLACK BOX - a mystery from the client's point of view

mutator method

Any method that modifies the values of instance data (that is, changes the object's state) Setter methods

accessor method

Any method that provides the client with the value of an instance variable Getter methods

Suppose a class called Crate has an integer instance variable called width. Which of the following methods accurately sets the value of width? a) public void setWidth(int width) { width = this.width; } b) public void setWidth(int width) { this.width = width; } c) public void setWidth(int width) { this.width = this.width; } d) public void setWidth(int width) { width = width; }

B

What happens when an element is removed from an ArrayList? a) The element is replaced with a null reference. b) The remaining elements are shifted to close the gap. c) The element is moved to the end of the list. d) The element remains in the list but is marked as deleted

B

When performing a binary search, how many comparisons are required to find a value in a list of N values, in the worst case? a) 1 b) log2N + 1 c) N / 2 d) N2

B

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

B On each iteration the next element can be accessed using the specified variable.

What is the proper type designation for an ArrayList of Person objects? a) ArrayList of Person b) ArrayList<Person> c) Person ArrayList d) ArrayList[Person]

B The element type is surrounded by < >

Where is instance data defined? a) in the method that it is used b) at class level c) at the end of the method

B they are defined above all other methods however, getter and setter methods can change instance data from when they were initialized are always defined as private data

Method overloading is helpful in which of the following situations? a) When two classes have the same name. b) When two methods have different return types. c) When two methods perform a similar task on different types of data. d) When a method has the same name as its enclosing class. e) When two methods perform a different task on the same types of data

C Method overloading lets you use the same method name but with different parameter types

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

C The printwriter constructor only needs the name of the file

Bounds Error

Caused when you try to access an array element that doesn't exist.

T/F Encapsulation makes is difficult for hackers to access your data.

False Encapsulation does not deal with security breeches. Instead it is about good software design

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

False For binary search to work at all, the list of values must already be sorted.

T/F Getter methods always return a string

False Getter methods return whatever data type being retrieved

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

False In that case, the "middle" element will be the first of the two middle elements.

T/F A method can only be an accessor or a mutator but not both.

False It can be both, such as a method that changes an instance value and then returns the new value

T/F The toString method prints a text representation of the object in the class

False It returns a string it doesn't print it

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 Using the this reference is mandatory with all instance data references inside a method.

False Sometimes it is necessary, but most often the 'this' is helpful for making code more readable

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

False The element type of an array can be either a primitive type or an object reference

T/F To 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 classic format for a getter method is to use the word "access" in front of the variable name.

False The name of a getter method starts with "get". A method that returns a variable width is called getWidth

T/F All Java wrapper classes are part of the java.util package.

False They are part of the java.lang package, and therefore don't have to be imported.

T/F A class must have a getter and setter method for each instance variable

False They often do but they don't have to

Create a code that will scan data from the file input.txt

File file = new File("input.txt"); Scanner in = new Scanner(file);

Encapsulation

a fundamental principle of object-oriented programming class can and should protect its data from unnecessary access.

Object

a program component that represents something (physical or otherwise) and performs related tasks.

Method Declaration

The code of an individual method, which defines the behaviors of an object.

Instance Data

The data managed by an object The attributes of an object Variables declared at the class level that define the state of an object.

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 Scanner object can be used to both read and write text files in Java

False A Scanner object is only used for input

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 Traversing an array or collection means accessing each element one at a time.

True It's a common activity that is facilitated by a for-each loop

T/F A wrapper class "wraps" a primitive value as an object.

True So it can be used in situations that are only appropriate for objects.

T/F The signatures of all methods within a class must be unique.

True The compiler must be able to tell them apart when the method is called

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.

Array

An object that holds a set of values that can be accessed using a numeric index.

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

False Although the ArrayList class uses an array internally, the details aren't necessary to use the class.

Index

An integer value that is used to access a particular element in an array.

What describes an object?

Class

Constructor

Code that is executed to set up a newly-created object

Which method call will replace the element at index 3 of an ArrayList of String objects called stuff? a) stuff.add(3, "new stuff") b) stuff.replace(3, "new stuff") c) stuff.change(3, "new stuff") d) stuff.set(3, "new stuff")

D

What is the output of the following code? BankAccount account1 = new BankAccount(1234, 100.0); BankAccount account2 = new BankAccount(5678, 50.0); double transfer = 25.0; account1.withdraw(transfer); account2.deposit(transfer); System.out.println(account1); System.out.println(account2); a) 1234: $125.00 5678: $50.00 b) 1234: $125.00 5678: $75.00 c) 5678: $75.00 1234: $75.00 d) 1234: $75.00 5678: $75.00

D $25 is transferred from account1 to account2

What are the two terms that mean to automatically convert from a primitive value to a corresponding wrapper object and vice versa. a) rapping and unrapping b) casting and uncasting c) wrapping and unwrapping d) autoboxing and unboxing

D Converting a primitive type to an object is called autoboxing and going the other way is unboxing.

What is the output of the following code? int[] list = {2, 4, 6, 8, 10}; int num = 0; for (int val : list) { num = num + val; } System.out.println(num); a) 0 b) 5 c) 10 d) 20 e) 30

E The loop computes the sum of all values in the array

Public interface

The methods in a class that can be called from other classes.

Encapsulation

The principle that an object should control the values of its own data.

Main method

The starting point or "driver" of a Java program.

Element type

The type of data that an array is declared to hold.

State

The values of an object's instance data.

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

True

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

True

T/F The constructor of a class will always have the same name as the class itself

True

T/F The instance data of an object cannot be changed once the object has been created.

True

T/F The toString method is automatically called when an object is printed.

True

T/F The toString method does not accept parameters

True Accepts no parameters but does return a string

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

True An ArrayList is a common and useful collections.

T/F Swapping two elements in an array requires three assignment statements.

True An extra variable is used to hold one value temporarily.

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

True Any of the values could be the minimum.

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

True Each cell of an array of objects holds only the address of the object.

T/F An encapsulated object keeps other objects from changing the values of its instance data

True Each object should control the values of its own data

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

True Each value can be added into a running sum as the for-each loop iterates over the elements.

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

True In Java, all arrays are objects.

T/F An ArrayList cannot store primitive types such as int or double.

True Instead, use wrapper classes to create ArrayList<#dq8Integer>#dq8 or ArrayList<#dq8Double>#dq8 objects.

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

True It will thorw an ArrayIndexOutOfBounds Exception


Conjuntos de estudio relacionados

Marketing Segmentation and Target Market

View Set

Sport Management Exam 1 Chapters 1-5

View Set

Lacharity Chapter 5: Safety and Infection

View Set

Dispensing from ADS Cabinets (Ch 9)

View Set

Maternal Newborn Success: High Risk Antepartum Reveiw Questions

View Set