JAVA QC Questions Exam 3
constructor
Code that is executed to set up a newly-created object.
If thisObject is less than (comes before) thatObject, what does the following expression return? thisObject.compareTo(thatObject) 1 -1 a positive integer a negative integer true false
a negative integer Yes! A negative result indicates that the first object (on whom the method is called) is less than the second object (the parameter).
instance
a particular object
T/F Encapsulation makes is difficult for hackers to access your data.
false Encapsulation is not about guarding against security breeches.
T/F Transformations can be applied to any JavaFX node
true including shapes, text, image views, controls, etc.
When the compareTo method is used to compare character strings, which of the following statements is false? -"able" comes before "baker" -"ultrasonic" comes before "ultraviolet" -"moon" comes before "moonwalk" -"fantastic" comes before "Great"
-"fantastic" comes before "Great" Right. All uppercase characters come before all lowercase characters, so 'G' comes before 'f'.
Which of the following statements is true? -An interface can contain public and private methods. -An interface can contain methods and constants. -An interface can contain methods and instance variables. -An interface can contain methods and classes.
-An interface can contain methods and constants. Correct. The constants can be used by any class that implements the interface.
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 -Frodo Aragorn -Bilbo Aragorn -Bilbo Gandalf
-Frodo Gandalf Correct. The two strings have value equality but not reference equality.
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 -Grumpy Doc -Sneezy Doc -Grumpy Sleepy
-Sneezy Sleepy Good! The two references have both reference and value equality.
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(double, double) -max(Object, Object) -max(int, int) -max(int, int, int) -max(String, String)
-max(int, int) Correct. The signatures match exactly so this couldn't be an overloaded version of the method.
How many elements can be stored in a two-dimensional array with dimensions 5 and 10? -36 -40 -45 -50 -66
50 Right. There are 5 rows and 10 columns, so room for 50 elements.
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); 1234: $75.00 5678: $75.00 5678: $75.00 1234: $75.00 1234: $125.00 5678: $50.00 1234: $125.00 5678: $75.00
5678: $75.00 1234: $75.00 Right. No, check the order of the output carefully.
toString method
A method that returns a textual description of an object.
What is the output of the following code? Die die = new Die(20); die.roll(); die.roll(); System.out.println(die.getValue()); -0 -1 -20 -A random number between 0 and 19. -A random number between 1 and 20.
A random number between 1 and 20. Correct. Each call to the roll method assigns a random number to the die in the range 1 to the number of sides (20 in this case).
In what type of object are the cards of the deck managed? -ArrayList<Object> -String[] -Card[] -ArrayList<Card>
ArratList<Card. *card objects are stored in an ArrayList
Why is the Group object that serves as the root of the scene declared at the class level in this program? -Because it needs to be accessed by multiple methods in the class. -Because it has to be declared as private. -Because it is contains all elements of the flag. -Because it contains too many elements.
Because it needs to be accessed by multiple methods in the class *variables declared at the class level can be referenced by all methods in the class
If Lockable is an interface, which statement must be true given the following declaration? Lockable obj = new Book(); -Book must contain Lockable -Book must implement Lockable -Lockable must define the methods in Book -Lockable must implement the Book class
Book must implement Lockable Good! To be type compatible, the class Book must implement the interface.
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 elements in the table except those in the last row. -Computes the sum of each column in the table. -Computes the sum of all values in the table. -Computes the sum of each row in the table. -Computes the sum of all elements in the table except those in the last column.
Computes the sum of all values in the table. The nested for loop accesses each element in the array.
What is the output of the following code? Die die = new Die(); System.out.println("Die value: " + die); -Die value: 0 -Die value: 1 -Die value: 6 -A random number will be printed.
Die value: 1 Good! The die face value is initialized to 1.
What's wrong with this version of the Square constructor? public Square(double side) { side = this.side; } -It assigns the current value of the instance variable to the parameter, which is useless. -It assigns the parameter value to the instance data. -A syntax error occurs. -It causes a runtime error.
It assigns the current value of the instance variable to the parameter, which is useless. Correct. Instead, the parameter (side) should be assigned to the instance data (this.side).
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[1][8]; -String chessPiece = chessboard["2"]["3"]; -String chessPiece = chessboard[4][]; -String chessPiece = chessboard[5][2]; -String chessPiece = chessboard[1];
String chessPiece = chessboard[5][2]; Right. That correctly accesses an element within the bounds of the two-dimensional array.
A Java interface defines which of the following? user interface printable interface keyword interface system interface
System interface Good! It defines the way one part of a system can talk to another.
method declaration
The code of an individual method, which defines the behaviors of an object.
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 mehtod
The starting point or "driver" of a Java program.
Method overloading is helpful in which of the following situations? -When two methods have different return types. -When two classes have the same name. -When two methods perform a similar task on different types of data. -When two methods perform a different task on the same types of data. -When a method has the same name as its enclosing class.
When two methods perform a similar task on different types of data. Correct. Method overloading lets you use the same method name but with different parameter types.
object
a profram component that represents something and preforms related ta
identity
a way to refer to a specific object
What Java keyword is used to create a subclass? extends inherits implements interface
extends a subclass extends its superclass
T/F A class can only implement one interface at a time.
false A class can implement multiple interfaces.
T/F The constructor of the Card class is called CardConstructor.
false A constructor always has the same name as the class itself.
T/F A call to setScale(1.5) will present a node at twice its normal size
false A scaling factor of 1.5 displays the node at 150% of its normal size
T/F If an attempt is made to deal a card from an empty deck, an exception is thrown.
false A value of null is returned.
T/F The Star class can only be used in the
false AmericanFlag program. The Star class is its own entity. It can be used in any appropriate program.
T/F Only strings with alphabetic characters can be compared using the compareTo method.
false Any Unicode characters can be compared, including punctuation and digits.
T/F 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 in how the values are arranged.
T/F 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).
T/F The Card class represents a complete Java program.
false It's a program element that can be used in multiple programs as needed.
T/F 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.
T/F 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.
T/F Each UFO object in the Aliens program is defined on its own Pane.
false Panes are not used in this program. Each UFO is a Group.
T/F An object that has been shifted must also be scaled.
false Scaling the size of an object and shifting its position are separate issues.
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 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.
T/F The Star class can also be used to display a six-pointed star.
false The coordinates that make up the star polygon form only a five-pointed star.
T/F All of the Rectangle objects that form the red stripes are the same length.
false The first four are partially covered by the blue field
T/F The constructor of the Person class is called constructPerson.
false The name of a constructor always matches the name of the class.
T/F Getter methods always return a String.
false The return type of each getter method must match the type of the value being retrieved.
T/F 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 the data.
T/F 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.
T/F A class must have a getter and setter method for each instance variable.
false They sometimes do, but don't have to.
T/F The withdraw method of the BankAccount class ensures that the amount withdrawn is less than the balance.
false Though it could be written that way, or perhaps that validation is done somewhere else.
T/F Calling setTranslateY(100) on an object causes it to be shifted 100 pixels to the right.
false Translating its position it along the Y axis causes it to be shifted down.
T/F A call to setRotate(10) rotates the node 10 degrees to the left
false positive parameters cause the node to rotate clockwise
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 Constructors cannot be overloaded.
false Sure they can, and often are. Overloaded constructors provide different ways to set up an object.
T/F The Die class is part of the Java API.
false The Die was written by the author of Rephactor Java.
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 he maximum number of sides on a Die object is 20.
false There is no maximum value for the number of sides on a Die.
Deriving one class from another establishes what kind of relationship between the two classes? aggregation uses has-a is-a
is-a the subclass is a more specific version of its superclass
Suppose a class called Crate has an integer instance variable called width. Which of the following methods accurately sets the value of width? -public void setWidth(int width) { this.width = width; } -public void setWidth(int width) { width = width; } -public void setWidth(int width) { width = this.width; } -public void setWidth(int width) { this.width = this.width; }
public void setWidth(int width) { width = this.width; } Good! No, this assigns the instance value to the parameter.
What's different about an overridden method in a subclass compared to the inherited version? -same method name and parameters but different code -different method name but the same code -different parameters but same method name and code -same method name and code but different return type
same method name and parameters but different code You're changing what the method does for the child class.
Which Java keyword is used to call a method of a superclass? parent super my this
super he super reference can be used to invoke the parent's constructor or any of its methods (particularly overridden ones).
Which two terms mean the same thing? child class and superclass superclass and parent class base class and child class parent class and subclass
superclass and parent class correct. This is the class from which a subclass (or child class) is derived.
instance data
the data managed by an object & Variables declared at the class level that define the state of an object.
behvior
the list of services that an object will preform
class
the pattern from which an object is created & A programming construct that contains data and methods.
state
the vales of an objects instance data
Which types of transformations are applied to the Star objects in this program? -scaling and rotation -shearing and scaling -translation and scaling -rotation and translation
translation and scaling *the star's position and size is set by these transformations
T/F If obj1 and obj2 point to the same object, then obj1 == obj2 is true.
true The == operator tests for reference equality.
T/F 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.
T/F The toString method is automatically called when an object is printed.
true And when an object is concatenated with a string.
T/F 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
T/F 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 number of elements in that dimension.
T/F 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.
T/F 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.
T/F Dealing a card removes the Card object from the ArrayList.
true In that sense, it mimics the "real life" deck object.
T/F The methods in a Java interface are abstract.
true Interface methods don't have a body defined for them. That's up to the class that implements the interface.
T/F The toString method does not accept parameters.
true It accepts no parameters and returns a String.
T/F he 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.
T/F The instanceof operator determines if an object reference variable is currently pointing to an object of a particular type.
true It produces a boolean (true or false) result.
T/F The == operator tests for reference equality.
true It produces a true result if both references point to the same object in memory.
T/F One BankAccount constructor assumes an initial balance of $0, and the other sets it explicitly.
true One BankAccount constructor assumes an initial balance of $0, and the other sets it explicitly. The first constructor calls the second constructor passing it a zero balance.
T/F Scaling an object by a factor of 1.2 in both dimensions will cause it to be 20% larger.
true Scaling an object by 1.0 will not change its size at all.
T/F When a Group object is shifted, all nodes in the group are shifted by the same amount.
true That makes it easier to treat a UFO object as one element.
T/F A constructor often sets the initial values of an object's instance data.
true That's the role of a constructor: to set up a newly created object.
T/F 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.
T/F the Java keyword this is an explicit reference to the object on which a method is invoked. This allows an object to refer to itself in methods declared inside that object.
true The Java keyword this is an explicit reference to the object on which a method is invoked. This allows an object to refer to itself in methods declared inside that object
T/F 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.
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.
T/F Value equality is when the contents of two objects are the same.
true Two objects have value equality if their instance variables hold the same values.
T/F A Java interface cannot be instantiated.
true Unlike a class, you cannot create an object from an interface.
T/F You cannot use relational operators (such as < or >) on objects.
true You have to use the compareTo method instead.
T/F A scaling factor greater than 1.0 will increase the presentation size of the node
true a scaling factor of 1.0 is equivalent to 100% which is the node's normal size
T/F it takes two method calls to translate (shift) a node diagonally
true translating along the X and Y axes are done separately
T/F The parameter to the second Die constructor determines how many sides are on the die.
true A die of any number of sides (2 or more) can be created.
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 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 Each time the roll method is called, a random value is assigned to the die.
true The random value is in the range 1 to the number of sides on the die.
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 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 The println method in the Java API is overloaded.
true There's many versions of println. Each one accepts a single data element to print.
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.