JAVA EXAM 3
________ is the term for the relationship created by object aggregation. "Has a" Inner class "Is a" One-to-many
"Has a"
The whole-part relationship created by object aggregation is more often called a(n) ________ relationship. "has a" inner class extra class inside class
"has a"
Protected class members can be denoted in a UML diagram with the ________ symbol. + - * #
#
Which of the following methods compile? (Choose all that apply) A, public void methodA() { return;} B, public void methodB() { return null;} C, public void methodD() {} D, public int methodD() { return 9;} E, public double methodE() { return 9;} F, public int methodF() { return;} G, public int methodG() { return null;}
A,C,D,E
If you have defined a class, SavingsAccount, with a public static method, getNumberOfAccounts, and created a SavingsAccount object referenced by the variable account20, which of the following will call the getNumberOfAccounts method? account20.getNumberOfAccounts(); SavingsAccount.getNumberOfAccounts(); getNumberOfAccounts(); SavingsAccount.account20.getNumberOfAccounts();
SavingsAccount.getNumberOfAccounts();
Assume the class BankAccount has been created and the following statement correctly creates an instance of the class. BankAccount account = new BankAccount(5000.00); What is true about the following statement? System.out.println(account); A runtime error will occur. The method will display unreadable binary data on the screen. The account object's toString method will be implicitly called. A compiler error will occur.
The account object's toString method will be implicitly called.
The JVM periodically performs the ________ process to remove unreferenced objects from memory. memory shuffling system restore garbage collection memory sweeping
garbage collection
If object1 and object2 are objects of the same class, to make object2 a copy of object1 ________. write a method for the class that will make a field by field copy of object1 data members into object2 data members use the copy method that is a part of the Java language use the default constructor to create object2 with object1 data members use an assignment statement to make object2 a copy of object1
write a method for the class that will make a field by field copy of object1 data members into object2 data members
To compare two objects in a class, ________. use the == operator (for example, object1 == object2) write a method to do a byte-by-byte compare of the two objects write an equals method that will make a field by field compare of the two objects This cannot be done since objects consist of several fields.
write an equals method that will make a field by field compare of the two objects
When you make a copy of the aggregate object and of the objects that it references, ________. you are performing a shallow copy you are performing a nested copy you are performing a deep copy a compiler error will occur
you are performing a deep copy
What will be displayed after the following code is executed? String str = "RSTUVWXYZ"; System.out.println(str.charAt(5)); W X V U
W
If a method in a subclass has the same signature as a method in the superclass, the subclass method ________ the superclass method. inherits overloads overrides implements
overrides
If you don't provide an access specifier for a class member, the class member is given ________ access by default. private public protected package
package
Java automatically stores a ________ value in all uninitialized static member variables. 0 -1 null false
0
Given the following declaration: enum Tree ( OAK, MAPLE, PINE ) What is the ordinal value of the MAPLE enum constant? 0 1 2 3
1
_______ tells the Java compiler that a method is meant to override a method in the superclass. @Override @Overload @Protected @Inherited
@Override
If a class contains an abstract method ________. you must create an instance of the class the method will only have a header, but not a body, and will end with a semicolon the method cannot be overridden in subclasses All of these are true.
All of these are true.
A protected member of a class may be directly accessed by ________. methods of the same class methods of a subclass methods in the same package Any of these
Any of these
In Java it is possible to write a method that will return ________. a whole number a reference to an object a string of characters Any of these
Any of these
Given the following code, which statement is true? public class ClassB implements ClassA{ } ClassA must override each method in ClassB. ClassB must override each method in ClassA. ClassB inherits from ClassA. ClassA inherits from ClassB.
ClassB must override each method in ClassA.
All methods specified by an interface are ________. private public protected static
static
Static methods can only operate on ________ fields. instance static global local
static
Which of the following is not true about static methods? They are created by placing the key word static after the access specifier in the method header. It is necessary for an instance of the class to be created to execute the method. They are called directly from the class. They are often used to create utility classes that perform operations on data but have no need to store and collect data.
It is necessary for an instance of the class to be created to execute the method.
If a subclass constructor does not explicitly call a superclass constructor ________. it must include the code necessary to initialize the superclass fields the superclass fields will be set to the default values for their data types Java will automatically call the superclass's default or no-arg constructor immediately after the code in the subclass's constructor executes Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes
Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes
Autoboxing is ________. Java's process of automatically "boxing up" a value inside an object the automatic allocation of array elements the process of assigning a default value to primitive data types the process of identifying tokens in a string
Java's process of automatically "boxing up" a value inside an object
________ is a special type of expression used to create an object that implements a functional interface. Lambda Beta Alpha Sigma
Lambda
What is the term used for a class that is "wrapped around" a primitive data type and allows you to create objects instead of variables? intrinsic class enclosed object wrapper class transitional object
wrapper class
Which of the following is true about protected access? Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package. Protected members may be accessed by methods in the same package or in a subclass, but only if the subclass is in the same package. Protected members cannot be accessed by methods in any other classes. Protected members are actually named constants.
Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package.
What is required for an interface method that has a body? The method header must begin with the key word default. A class that implements the interface must override the method. The @Default annotation must precede the method header. All of these are true.
The method header must begin with the key word default.
Which of the following is not true about static methods? It is not necessary for an instance of the class to be created to execute a static method. They are called by placing the key word static after the access specifier in the method header. They are called from an instance of the class. They are often used to create utility classes that perform operations on data but have no need to collect and store data.
They are called from an instance of the class.
If the following is from the method section of a UML diagram, which of the statements below is true? + add(object2:Stock) : Stock This is a private method named add that accepts and returns objects of the Stock class. This is a private method named Stock that adds two objects. This is a public method named add that accepts and returns references to objects in the Stock class. This is a public method named Stock that adds two objects.
This is a public method named add that accepts and returns references to objects in the Stock class.
If the following is from the method section of a UML diagram, which of the statements below is true? + equals(object2:Stock) : boolean This is a public method that accepts a Stock object as its argument and returns a boolean value. This is a public method that returns a reference to a String object. This is a private method that receives two objects from the Stock class and returns a boolean value. This is a private method that returns a boolean value.
This is a public method that accepts a Stock object as its argument and returns a boolean value.
Each of the numeric wrapper classes has a static ________ method that converts a number to a string. GetString Parse ToString Convert
ToString
Given the following declaration: enum Tree ( OAK, MAPLE, PINE ) What is the fully-qualified name of the PINE enum constant? enum.PINE PINE Tree.PINE enum.Tree.PINE
Tree.PINE
If the this variable is used to call a constructor, ________. a compiler error will result if it is not the first statement of the constructor a compiler error will result if it is the first statement of the constructor nothing will happen the this variable cannot be used as a constructor call
a compiler error will result if it is not the first statement of the constructor
When a method's return type is a class, what is actually returned to the calling program? an object of that class a reference to an object of that class the values in the object that the method accessed nothing - the return type is simply for documentation in this situation
a reference to an object of that class
A class becomes abstract when you place the ________ key word in the class definition. super extends final abstract
abstract
A(n) ________ method is a method that appears in a superclass but expects to be overridden in a subclass. abstract protected static overloaded
abstract
A static field is created by placing the key word static ________. after the access specifier and the field's data type after the access specifier and before the field's data type after the field name in brackets, before the field's data type
after the access specifier and before the field's data type
When an "is a" relationship exists between objects, the specialized object has ________. some of the characteristics of the general class, but not all, plus additional characteristics some, but not all, of the characteristics of the general object none of the characteristics of the general object all of the characteristics of the general object plus additional characteristics
all of the characteristics of the general object plus additional characteristics
All fields declared in an interface ________. have protected access must be initialized in the class implementing the interface have private access are treated as final and static
are treated as final and static
When a subclass overloads a superclass method ________. both methods may be called with a subclass object only the subclass method may be called with a subclass object only the superclass method may be called with a subclass object neither method may be called with a subclass object
both methods may be called with a subclass object
Which of the following shows the inheritance relationships among classes in a manner similar to that of a family tree? UML diagram CRC card flowchart class hierarchy
class hierarchy
Which key word indicates that a class inherits from another class? final super implements extends
extends
When a method is declared with the ________ modifier, it cannot be overridden in a subclass. final Super void public
final
Which of the following compile? (Choose all that apply) final static void method4( ) { } public final int void method() { } private void int method() { } static final void method3() { } void final method() {} void public method() { }
final static void method4( ) { } static final void method3() { }
Which of the following can fill in the blank in this code to make it compile? (Choose all that apply) public class Ant { _____ void method() { } } default final private Public String
final, private
Which of the following is the operator used to determine whether an object is an instance of a particular class? equals instanceOf >> isa
instanceOf
Which of the following statements converts a String object variable named str to an int and stores the value in the variable x? int x = Integer.integer(str); int x - str; int x = Integer.parseInteger(str); int x = Integer.parseInt(str);
int x = Integer.parseInt(str);
Which of the following is an example of a lambda expression? int x = x * factor; IntCalculator = new divider(x, 2); IntCalculator multiplier = x -> x * factor; Any of these are examples of a lambda expression.
int x = x * factor;
A deep copy of an object ________. is an assignment of that object to another object is an operation that copies an aggregate object and all the objects that it references is a bogus term and means nothing is always a private method
is an operation that copies an aggregate object and all the objects that it references
If you have defined a class, SavingsAccount, with a public static data member named numberOfAccounts, and created a SavingsAccount object referenced by the variable account20, which of the following will assign numberOfAccounts to numAccounts? numAccounts = account20.numAccounts; numAccounts = numOfAccounts; numAccounts = SavingsAccount.numberOfAccounts; numAccounts = account20;
numAccounts = SavingsAccount.numberOfAccounts;
You cannot use the == operator to compare the contents of ________. objects strings integers Boolean values
objects
When a field is declared static there will be ________. a copy of the field for each method in the class a copy of the field in each class object only one copy of the field in memory two reference copies of the field for each method in the class
only one copy of the field in memory
A subclass can directly access ________. only protected and private members of the superclass all members of the superclass only public and private members of the superclass only public and protected members of the superclass
only public and protected members of the superclass
If two methods have the same name but different signatures they are ________. overridden overloaded superclass methods subclass methods
overloaded
In Java, a reference variable is ________ because it can reference objects of types different from its own, as long as those types are related to its type through inheritance. static dynamic polymorphic public
polymorphic
A subclass may call an overridden superclass method by ________. prefixing its name with the super key word and a dot (.) prefixing its name with the name of the superclass in parentheses using the extends key word before the method is called calling the superclass method first and then calling the subclass method
prefixing its name with the super key word and a dot (.)
When declaring class data members it is best to declare them as ________. private members public members protected members restricted members
private members
A ________ member's access is somewhere between public and private. package protected static final
protected
All methods specified by an interface are ________. private public protected static
public
Which of the following statements correctly specifies two interfaces? public class ClassA implements [Interface1, Interface2] public class ClassA implements (Interface1, Interface2) public class ClassA implements Interface1, Interface2 public class ClassA implements Interface1 | Interface2
public class ClassA implements Interface1, Interface2
Which of the following statements declares Salaried as a subclass of PayType? public class Salaried implements PayType public class PayType derives Salaried public class Salaried extends PayType public class Salaried derivedFrom(PayType)
public class Salaried extends PayType
The ________ key word is used to call a superclass constructor explicitly. goto this super extends
super
Given the following method header, what will be returned from the method? public Rectangle getRectangle() the address of an object of the Rectangle class the values stored in the data members of the Rectangle object a graph of a rectangle an object of the class Rectangle
the address of an object of the Rectangle class
When a reference variable is passed as an argument to a method _________. a copy of the variable's value is passed into the method's parameter the method has access to the object that the variable references the method becomes a static method the program terminates
the method has access to the object that the variable references
If you attempt to perform an operation with a null reference variable ________. the resulting operation will always be zero the results will be unpredictable the program will terminate Java will create an object to reference the variable
the program will terminate
In an inheritance relationship ________. the subclass constructor always executes before the superclass constructor the superclass constructor always executes before the subclass constructor the constructor with the lowest overhead always executes first regardless of inheritance in subclasses the unified constructor always executes first regardless of inheritance
the superclass constructor always executes before the subclass constructor
The only limitation that static methods have is ________. they must be declared outside of the class they cannot refer to nonstatic members of the class they can only be called from static members of the class they can refer only to nonstatic members of the class
they cannot refer to nonstatic members of the class
Which of the following is true? this() can be called from anywhere in a constructor this() can be called from any instance method in the class this.variableName can be called from any instance method in the class this.variableName can be called from any static method in the class You must include a default constructor in the code if the compiler does not include one You can call the default constructor written by the compiler using this()
this.variableName can be called from any instance method in the class
The process of converting a wrapper class object to a primitive type is known as ________. simplifying unboxing parsing devaluating
unboxing
The String class's ________ method accepts a value of any primitive data type as its argument and returns a string representation of the value. trim getChar toString valueOf
valueOf
