java test 2

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the output of the following code? int[] list = new int[5]; System.out.println(list[3]);

0. The values of an array type int are initialized to 0.

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

10, 20, 30, 40, 50

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: $75.00 5678: $75.00 (b) 5678: $75.00 1234: $75.00 (c)gfx (d;

1234: $75.00 5678: $75.00

If the value of num is 7 and the value of max is 10, what is the value of num after the following statement is executed? num = (max < num) ? max : max - num;

3

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

3. 58 then 30 then 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

4. 58 then 81 then 88 then 86

How many elements can be stored in a two-dimensional array with dimensions 5 and 10?

50. 5 rows 10 columns therefore room for 50 elements.

Which of the following is an appropriate analogy?

Class: concept of a dog Object: my dog Fido. Class is an idea, the object is the realization of that idea.

Assuming table is a two-dimensional array of integers, what does the following code do? int sum = 0; for (int i = 0; i < table.length; i++) for (int j = 0; j < table[i].length; j++) sum = sum + table[i][j];

Computes the sum of all values in the table.

Which statement is true about the following two circles?Circle circle1 = new Circle(50, 120, 50); Circle circle2 = new Circle(70, 120, 20); (a) They have the same size. (b) They have the same center point. (c) They partially overlap. (d) One fully surrounds the other (but they don't have the same center point).

D. The first circle completely surrounds the second.

Which of the following demonstrates the proper way to specify a variable-length argument list? (a) public void printValues(String str, int... list, int size) (b) public void printValues(int... list, String... str) (c) public void printValues(int... list, int x, int y) (d) public void printValues(String str, int... list)

D. There can only be one variable-length parameter and it must be at the end of the parameter list.

Strings containing non-alphabetic characters cannot be compared using the compareTo method.

False. A Unicode characters can be compared, including punctuation and digits.

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

False. A binary search can be performed on any number of elements.

The constructor of the Card class is called CardConstructor.

False. A constructor always has the same name as the class itself.

A for-each loop can be used to fill the values in an array.

False. A for-each loop provides access to elements in the array, but not to the array itself.

All Java classes have a main method.

False. A program can be made up of many classes and only one will likely have a main method.

The expression in a switch statement can be an int or double value.

False. A switch expression cannot be a floating point value (like a double)

If an attempt is made to deal a card from an empty deck, an exception is thrown.

False. A value of null is returned.

A variable-length parameter list is specified by inserting a colon (:) between the type and the parameter name.

False. A variable length parameter is specified using ellipses (...) between the type and the parameter name.

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

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

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.

The leftmost end point of a line segment must be specified as the start point of the line.

False. Either point can be considered the start or end point.

Encapsulation makes is difficult for hackers to access your data.

False. Encapsulation is not about guarding against security breaches.

A one-dimensional array has only one element type, but a multidimensional array can hold several types of elements.

False. Every array has a single element type. The difference is how the values are arranged.

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

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

You cannot use command-line arguments in IDEs like Eclipse and NetBeans.

False. In IDEs, program arguments are specified using the menu option or a separate tab in the Run window.

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.

The compareTo method returns one of three values: -1, 0, or 1.

False. It returns a negative integer, positive integer, or 0 (not necessarily -1 or 1)

The Card class represents a complete Java program.

False. It's a program element that can be used in multiple programs as needed.

Calling the System.exit method is the best way to terminate a program.

False. It's best to exit "cleanly," but System.exit can be convenient when no further processing is required.

Java represents a color using the CMYK color model.

False. Java uses the RGB color model.

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.

A null string and an empty string are equal.

False. One is a reference that doesn't point to any object, while the other is a valid string that contains no characters.

A Scanner object can be used to both read and write text files in Java.

False. Only used for input.

Constructors cannot be overloaded.

False. Overloaded constructors provide different ways to set up an object.

A String object is immutable, which means it is created using the new operator.

False. Strings are immutable, but that means they cannot be changed once created.

Text color is determined by the font applied to a Text object.

False. Text color is specified using the setFill method of the Text class.

A constructor is a programming construct that contains data and methods.

False. That describes a class.

A HashMap keeps the map keys in the order in which they were added.

False. That's LinkedHashMap.

The Die class is part of the Java API.

False. The Die class was written specifically for this example.

When creating a Text object, you specify the position on which you want the text centered.

False. The coordinates specify where the leading (left) edge of the text begins.

If left unspecified, the font applied to a Text object is 12 point Lucinda Grande.

False. The default font used depends on the operating system running the program.

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.

The parameters to the Rectangle constructor represent the upper left corner and lower right corner of the rectangle.

False. The first two represent the upper left corner, but the last two represent the rectangle's width and length, respectively.

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.

The JavaFX Image class does not currently support the PNG image format.

False. The image class supports the GIF, JPG, PNG, and BMP formats.

All values stored in a map must be unique.

False. The map keys must be unique, but the values could be duplicated.

The Java coordinate system has the origin point (0, 0) in the lower left corner of a container.

False. The origin is in the upper left corner.

Getter methods always return a String.

False. The return type of each getter method must match the type of the value being retrieved.

The toString method of the Card class prints a text representation of the card.

False. The toString method returns a string, it doesn't print it.

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

Each value of a switch statement case can be a constant, variable, or expression.

False. The value of each case can only be a constant (literal or named constant)

The maximum number of sides on a Die object is 20.

False. There is no maximum for the number of sides on a Die.

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.

A class must have a getter and setter method for each instance variable.

False. They sometimes do, but they don't have to.

The withdraw method of the BankAccount class ensures that the amount withdrawn is less than the balance.

False. Though it can be written that way, or perhaps that validation is done somewhere else.

The font applied to a Text object determines whether that text is underlined or not.

False. Underlying text is accomplished using the setUnderline method of the text class.

Internally, the elements in all Java collections are managed using the same data structure.

False. Usually multiple classes implement a particular collection type using different data structures.

What output is produced by the following code? String str1 = new String("Rephactor"); String str2 = "Rephactor"; if (str1 == str2) System.out.println("Bilbo"); else System.out.println("Frodo"); if (str1.equals(str2)) System.out.println("Gandalf"); else System.out.println("Aragorn");

Frodo Gandalf

What is the output of the following code? int sum = 0; int[] values = {1, 2, 3, 4, 5}; for (int i = 0; i <= 5; i++) sum = sum + values[i]; System.out.println("Sum: " + sum);

No output. A bounds error occurs. Tries to access scores[5], but the last element in the array is scores[4]

Which of the following ellipses is thinner than it is tall? (a) new Ellipse(270, 300, 180, 100) (b) new Ellipse(150, 135, 80, 60) (c) new Ellipse(140, 100, 20, 50) (d) new Ellipse(90, 90, 60, 55)

C. The X radius of the ellipse is smaller than the Y radius.

Which of the following rectangles extends furthest down? (a) new Rectangle(100, 50, 50, 100) (b) new Rectangle(80, 25, 100, 130) (c) new Rectangle(50, 85, 70, 75) (d) new Rectangle(150, 90, 70, 45)

C. The rectangle extends to 160 on the y-axis, further than any others. (y + length)

Which of the following ellipses is also a circle? (a) new Ellipse(150, 150, 150, 100) (b) new Ellipse(95, 120, 95, 120) (c) new Ellipse(100, 200, 55, 55) (d) new Ellipse(70, 70, 100, 70)

C. The width and height of the ellipse are the same.

Which of the following lines is horizontal? (a) new Line(40, 20, 100, 70) (b) new Line(30, 30, 50, 50) (c) new Line(25, 70, 125, 70) (d) new Line(75, 20, 75, 90)

C. The y coordinates of both end points are the same.

Which one of the following constants and methods is NOT part of the Integer wrapper class?

POSITIVE_INFINITY

What is the element type of the athletes array in the ArrayOfPersonDemo program?

Person

What is the type of the variable athletes in the ArrayOfPersonDemo program?

Person[]

What output is produced by the following code? Integer first = new Integer(7); Integer second = first; if (first == second) System.out.println("Sneezy"); else System.out.println("Grumpy"); if (first.equals(second)) System.out.println("Sleepy"); else System.out.println("Doc");

Sneezy Sleepy

What is the element type of the adjectives array in the ArrayOfStringDemo program?

String

Which line of code demonstrates a valid and error-free way to access the following array? String[][] chessboard = new String[8][8];

String chessPiece = chessboard[5][2];

What is the type of the variable adjectives in the ArrayOfStringDemo program?

String[]

What is the output of the following code? int sum = 0; int[] values = {7, 6, 5, 4, 3, 2, 1}; for (int i = 1; i < values.length; i++) sum += values[i]; System.out.println("Sum: " + sum);

Sum: 21. scores[0] is not included in the sum.

What does the fourth parameter in this method call represent? Color.rgb(100, 100, 200, 0.65);

The alpha channel (transparency)

Which of the following is an example of the state of a Car object?

The car's color. Could also be its make, model, and current speed.

What will happen if you try to add a String object to an ArrayList created to hold Person objects?

The compiler will issue an error message.

What does the following statement print? System.out.println((count < 0) ? "Invalid" : count)

The value of count, or "Invalid" if count is negative

If the value of weight is 7, what does the following statement print? System.out.println("The weight is " + ((weight % 2 == 0) ? "even" : "odd"));

The weight is odd

The parameter to the constructor of the Die class determines how many sides are on the die.

True. A die of any number of sides (2 or more) can be created.

The data structure used internally in a map implementation is designed for efficient value retrieval.

True. A map is designed to find a value quickly given its key.

It is possible to create an array with more than two dimensions.

True. A multidimensional array can have any number of dimensions, though more than three dimensions is rare.

Switch statements can only test equality; they cannot make relational comparisons like greater than (>).

True. A switch sees which case value matches its expression.

Swapping two elements in an array requires three assignment statements.

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

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

True. Any of the values could be a minimum.

Unlike other collections, a for-each loop is not used to traverse the map collection itself.

True. Because of its nature, a map provides several "views" of the collection that can be traversed.

Dealing a card causes all other cards in the deck to be shifted within the ArrayList.

True. But the efficiency impact is negligible in this situation.

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.

The maximum indexes of a two-dimensional array with 6 rows and 10 columns are 5 and 9, respectively.

True. Each dimension is indexed from 0 to N-1, where N represents the numbers of elements in that dimension.

A two-dimensional array is really a one-dimensional array of one-dimensional arrays.

True. Each dimension of a multidimensional array is really a single-dimensional array.

A switch statement can always be rewritten as a series of nested if statements.

True. Each if statement would correspond to a case in the switch statement.

Each Person object has its own last name.

True. Each instance (object) has its own instance variable, so that each object can store a unique value.

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.

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.

Whether text is displayed in bold or italic type is a function of the font object applied to the text.

True. Font weight and posture are both attributes of the Font class.

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

True. Forgetting to close window to output file is a common error.

An array that holds objects is, itself, an object.

True. In Java, all arrays are objects.

Dealing a card removes the Card object from the ArrayList.

True. In the sense, it mimics the "real life" deck object.

The printf method is an example from the Java API that uses a variable-length parameter list.

True. It accepts a format string followed by a variable number of objects to print.

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

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

Encapsulation ensures that an object controls its own data.

True. It keeps other code from "reaching in" and changing data values directly.

The == operator tests for reference equality.

True. It produces a true result if both references point to the same object in memory.

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

True. It will throw an ArrayOutofBounds exception.

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.

An object's toString method is called automatically when an object is printed.

True. It's also called when an object is concatenated with a String.

The Swing API is an older approach to developing Java GUI programs that is no longer supported by Oracle.

True. JavaFX is now the preferred framework for Java graphics or GUIs (graphical user interfaces)

A map collection is a good choice to use when implementing a dictionary.

True. Looking up a word's direction is the classic key/value scenario.

The methods of a class that can be called from other classes make up its public interface.

True. Public interface defines the interaction that can occur between objects.

Each object has its own instance data.

True. Separate instance data lets an object have a unique statement.

An ImageView can be used to set the size at which an image is presented.

True. Setting the width and/or height of the view's bounding box will change the viewing size.

Setting a viewport does not change the underlying image.

True. Settings on an ImageView object do not affect the image it displays.

File I/O operations may throw checked exceptions.

True. Since they are checked, the code must either be able to handle them explicitly or at least acknowledge they may occur.

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

True. That's the role of the constructor: to set up a newly created object.

If obj1 and obj2 point to the same object, then obj1 == obj2 is true.

True. The == operator tests for reference equality.

The human eye has three types of color receptors that correspond to wavelengths of red, green, and blue.

True. The Java color model is based on this mechanism.

A break statement is used at the end of each case of a switch statement to keep processing from continuing into the next case.

True. The break statement causes processing to jump out of the switch statement.

Arial is an example of a font family.

True. The font family specifies the general look of the characters.

Arguments passed to a variable-length argument list are accessed in the method using an array.

True. The parameter name is the array name and the parameter type is the array element type.

An image can be loaded into an Image object from a web page.

True. The parameter to the Image constructor can be a full URL.

An object created from a class is called an instance of that class.

True. The process of creating an object is called instantiation.

Each time the roll method is called, a random value is picked as the new face value.

True. The random value is in the range of 1 to the number of sides on the die.

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.

A two-dimensional array is suitable for storing tabular data.

True. The two dimensions of the array mimic the row and column layout of the table.

Constants can be declared with public visibility without violating encapsulation.

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

Objects made from wrapper classes are immutable.

True. The value of a wrapper object cannot be changed once it has been created.

A rectangle's corners can be rounded using calls to the setArcWidth and setArcHeight methods.

True. Their parameters determine the shape of the curve.

Objects created from the Color class are immutable.

True. Their values cannot be changed once the color is created.

The println method in the Java API is overloaded.

True. There are many versions of println. Each one accepts a single data element to print.

By convention, Java command-line options begin with a dash, such as -o or -b.

True. There are other conventions for specifying options, but the most common for Java program options is the dash.

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.

The Text class and the Font class are part of the same package.

True. They are both part of the javafx.scene.text package.

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

True. They both are System.out.

An alpha channel value of 1.0 represents a fully opaque color.

True. This is the default. Alpha values less than 1.0 make it translucent.

The element type of a collection cannot be a primitive type.

True. To store a primitive value, you use the wrapper class such as Integer.

Value equality refers to the contents of two objects being the same.

True. Two objects have value equality if their instance variables (or at least some of them) hold the same values.

A method that accepts a variable-length argument list can be passed a previously declared array.

True. Variable-length argument lists are flexible.

Command-line arguments are commonly used to specify program options when a program is run.

True. When the program is run, the command-line argument list is used to determine which options were selected.

An Image object cannot be displayed directly in a scene.

True. You can use the ImageView object to display an image.

If thisObject is less than (comes before) thatObject, what is the result of the expression thisObject.compareTo(thatObject)?

a negative integer

What analogy does JavaFX use for its high-level framework elements?

a theater analogy

What are the two terms that mean to automatically convert from a primitive value to a corresponding wrapper object and vice versa.

autoboxing and unboxing

Which of the following is the preferred way to declare an array?

double[] prices = new double[10];

When performing a binary search, how many comparisons are required to find a value in a list of N values, in the worst case?

log2N + 1

Which collection lets you look up a value based on a key?

map. used to organize key/value pairs.

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

max(int, int). The signatures match so it couldn't be overloaded.

Which of the following statements is NOT true regarding the use of the compareTo method to compare character strings?

"fantastic" comes before "Great"

Which method call will replace the element at index 3 of an ArrayList of String objects called stuff?

stuff.set(3, "new stuff")

What is a JavaFX layout pane?

A container that manages the visual presentation of the nodes it contains.

What is the output of the following code? Die die = new Die(20); die.roll(); die.roll(); System.out.println(die.getValue());

A random number between 1 and 20. Roll assigns a random number to the die in the range of one to the number of sides (20 in this case).

What type of object is used to store the cards in the DeckOfCards class?

ArrayList<Card>

What is the proper type designation for an ArrayList of Person objects?

ArrayList<Person>

Which of the following expressions represents the color white? (a) Color.rgb(100, 0, 0) (b) Color.rgb(255, 255, 255) (c) Color.rgb(0, 0, 0) (d) Color.rgb(100, 100, 100)

B. Full contribution of red, green, and blue corresponds to white.

Which of the following circles is largest? (a) new Circle(150, 175, 45) (b) new Circle(50, 50, 50) (c) new Circle(30, 80, 25) (d) new Circle(20, 100, 30)

B. This circle, with a radius of 50, is larger than any of the others.

What is the output of the following code? Die die = new Die(6); System.out.println("Die value: " + die);

Die value: 1. Initialized to 1.

Which assignment statement is functionally equivalent to the following if statement? if (num1 > total) num1 = total; else num1 = 0;

num1 = (num1 > total) ? total: 0;

What output is produced when the CommandLineTest program below is run from the command line as follows? > java CommandLineTest apple grape orange lemon kiwi public class CommandLineTest { public static void main(String[] args) { System.out.println(args[2]); } }

orange

Which type of JavaFX node corresponds to a window?

stage

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

True. An ArrayList is a common and useful collection.

A class represents a group of similar objects.

True. Class comes from word classification.

The element type of an ArrayList<E> cannot be a primitive type such as int or double.

True. Instead, use the wrapper class to create ArrayList<Integer> or ArrayList<Double> objects

The toString method does not accept parameters.

True. It accepts no parameters and returns a String.

The shuffle method in the DeckOfCards class relies on a method from the Java API.

True. It calls collections.shuffle which randomizes the order of the ArrayList elements.

The instance data of a Card object cannot be changed once the object has been created.

True. The card class doesn't provide any methods that let you change the instance data, though it could have.

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.

One BankAccount constructor assumes an initial balance of $0, and the other sets it explicitly.

True. The first constructor calls the second constructor passing it a zero balance.

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

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

A for-each loop cannot be used to print the elements in an array backwards.

True. The for-each loop processes each array elements starting at index 0.

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.

The ArrayList class represents a list collection that stores the elements in an array.

True. The name of the class indicates both the collection type and its internal data structure.

The toString method returns a textual description of an object.

True. Usually a good idea to include a toString method when designing a class.

You cannot use relational operators (such as < or >) on objects.

True. You have to use a compareTo method instead.

Method overloading is helpful in which of the following situations?

When two methods perform a similar task on different types of data.

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

(c) PrintWriter out = new PrintWriter("myOutput.txt");

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 File("myData.txt"); (c) Scanner in = new Scanner(new File("myData.txt")); (d) Scanner in = new Scanner("myData.txt");

(c) Scanner in = new Scanner(newFile("myData.txt"));

What are the top four cards in the deck right after a DeckOfCards object is created?

2 of Clubs, 2 of Diamonds, 2 of Hearts, 2 of Spades. Starts with first rank 2 for all four suits.

What is the output of the following code? int[] list = {2, 4, 6, 8, 10}; int num = 0; for (int val : list) { if (val != 6) num = num + val; } System.out.println(num);

24. The loop computes the sum of all elements in the array except 6.

What number is printed by the following code? int num = 20; switch (num) { case 1: num = num + 1; break; case 12: num = num + 1; num = num * 2; case 20: num = num * 2; case 25: num = num + 1; break; case 999: num = 20; break; default: num = 999; } System.out.println(num);

41. Starts at case 20 falls through into case 25 and then breaks out of the switch.

The amount deposited into a BankAccount is held as instance data of the BankAccount class.

False. It's just passed in as a parameter to the deposit method. Only the account number and balance are instance data.

Instance data is shared by all objects of a class.

False. It's not shared-each object has its own memory space for instance data.

Collection classes are generic, meaning that you can't use a for-each loop to process them.

False. Java collection classes are generic, but they can be processed using a for-each loop.

The values of an object's instance data represent the object's identity.

False. The instance values of two objects could be the same.

The constructor of the Person class is called constructPerson.

False. The name of a constructor always matches the name of the classes.

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.

The toString method of the BankAccount class calls the printf method to format the output.

False. The toString method doesn't print anything. In this case it uses the String.format method to format data.

What string is printed by the following code if it is executed when the value of finger is 3? switch (finger) { case 1: System.out.println("Thumb"); break; case 2: System.out.println("Index finger"); break; case 3: System.out.println("Middle finger"); break; case 4: System.out.println("Ring finger"); break; case 5: System.out.println("Pinky"); break; default: System.out.println("Not a finger!"); }

Middle finger. Processing jumps to the matching case value 3 and then breaks out of the switch.

What happens when an element is removed from an ArrayList?

The remaining elements are shifted to close the gaps.

Which of the following correctly shows the general structure of a for-each loop header?

for (type variable-name : array-or-collection)

Which collection represents a list of objects that are added and removed from the same end?

stack. most recent element put on the stack is the first element removed.


संबंधित स्टडी सेट्स

CFA 48: Overview of Equity Securities FIN3013

View Set

Research Methods Study Questions

View Set

unit 3: political parties, elections, and elected officials

View Set