Final Review CS162
Inheritance involves a subclass, which is the general class, and a superclass, which is the specialized class.
False
You must call a method to get the value of a wrapper class object.
False
In GUI terminology, a container that can be displayed as a window is known as a:
Frame
This is a basic window that has a border around it, a title bar, and a set of buttons for minimizing, maximizing, and closing the window.
Frame
Which one of the following GUI components is considered to be a container?
Frame
You can use this component to display images in an application's GUI.
ImageView
If two methods have the same name but different signatures, they are:
Overloaded
Oracle provides a free design tool named ________ that you can use to visually create a GUI.
Scene Builder
This variable references the window that the GUI will be displayed in.
stage
What would be the results of executing the following code? StringBuilder str = new StringBuilder("Little Jack Horner "); str.append("sat on the "); str.append("corner");
str would reference "Little Jack Horner sat on the corner".
Unchecked exceptions are those that inherit from:
the Error class or the RuntimeException class
If the following is from the method section of a UML diagram, which of the following statements is TRUE? + add(object2:Stock): Stock
This is a public method named add that accepts and returns references to objects in the Stock class
What does the following statement do? Double number = new Double(8.8);
All of the above
Which of the following is NOT a rule for the FlowLayout manager?
All of these are rules for the FlowLayout manager.
In JavaFX, this component is commonly used as a GUI's root node.
AnchorPane
Enum constants have a toString method.
True
If a non-letter is passed to the toLowerCase or toUpperCase method, it is returned unchanged.
True
If you are using characters other than whitespaces as delimiters, you will probably want to trim the string before tokenizing; otherwise, the leading and/or following whitespaces will become part of the first and/or last token.
True
It is not possible for a superclass to call a subclass's method.
True
To include Swing and AWT components in your program, use the following import statements:
import javax.swing; import java.awt;
When a subclass overloads a superclass method:
Both methods may be called with a subclass object
Look at the following code: FileInputStream fstream = new FileInputStream("MyInfo.dat"); DataInputStream inputFile = new DataInputStream(fstream); This code can also be written as:
DataInputStream inputFile = new DataInputStream(new FileInputStream("MyInfo.dat"));
Once you have created a GUI with Scene Builder, and saved it to an FXML file, you need to write a Java class that performs which of the following?
All of the above
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?
SavingsAccount.getNumberOfAccounts();
Enumerated types have this method, which returns the position of an enum constant in the declaration list.
Ordinal
One of the small dots that make up a screen display is known as what?
Pixel
A ________ is a tree-like, hierarchical data structure that contains nodes.
Scene graph
Sometimes a string will contain a series of words or other items of data separated by spaces or other characters. In programming terms, items such as these are known as what?
Tokens
A class must implement the Serializable interface in order for objects of the class to be serialized.
True
A class's static methods do not operate on the fields that belong to any instance of the class.
True
A common technique for writing an event listener class is to write it as a private inner class inside the class that creates the GUI.
True
A single copy of a class's static field is shared by all instances of the class
True
An abstract class is not instantiated, but serves as a superclass for other classes.
True
Beginning in Java 7, multi-catch can reduce a lot of duplicated code in a try statement that needs to catch multiple exceptions, but perform the same operation for each one.
True
Private members of the superclass cannot be accessed by the subclass.
True
When an exception is thrown by a method that is executing under several layers of method calls, a stack trace indicates the method executing when an exception occurred and all of the methods that were called in order to execute that method.
True
When deserializing an object using the readObject method, you must cast the return value to the desired class type.
True
When the code in a try block may throw more than one type of exception, you need to write a catch clause for each type of exception that could potentially be thrown.
True
This is the process of converting a wrapper class object to a primitive type.
Unboxing
The following catch statement can: catch (Exception e) {...}
handle all exceptions that are instances of the Exception class or a subclass of Exception
Event listeners must:
implement an interface
A functional interface is simply an interface that has one abstract method.
ture
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
When using the StringBuilder class's insert method, you can insert:
All of the above
What does the following statement do? Float number = new Float(8.8);
All of the above (It creates a Float object, It initializes that object to 8.8, & It assigns the object's address to the number variable)
This layout manager arranges components in rows.
FlowLayout
Use the following import statement when using the Character wrapper class:
No import statement is needed
The numeric classes' "parse" methods all throw an exception of this type if the string being converted does not contain a convertible numeric value.
NumberFormatException
Assuming the following declaration exists: enum Tree { OAK, MAPLE, PINE } What will the following code display? System.out.println(Tree.OAK);
OAK
A subclass can directly access:
Only public and protected members of the superclass
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.
________ are useful when you want the user to select one choice from several possible options.
Radio Buttons
Which of the following statements is NOT true?
Radio buttons and check boxes both implement the ActionListener interface
Which of the following statements will print the maximum value an int variable may have?
System.out.println(Integer.MAX_VALUE) with capital I in integer
What will be the result of executing the following statement? panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 5));
The JPanel referenced by panel will have a blue line border that is 5 pixels thick.
What will be the result of executing the following statement? panel.setBorder(BorderFactory.createTitleBorder("Title"));
The JPanel referenced by panel will have an etched border with the title "Title" displayed on it.
2/2 Assume the class BankAccount has been created, and the following statement correctly creates an instance of the class: BankAccount account = new BankAccount(5000.0); What is TRUE about the following statement? System.out.println(account);
The account object's toString method will be implicitly called.
When an application uses many components, rather than deriving just one class from the JFrame class, it is often better to encapsulate smaller groups of related components and their event listeners into their own class. A commonly used technique to do this is:
To extend a class from the JPanel class to contain other components and their related code
Look at the following declaration: enum Tree { OAK, MAPLE, PINE } What is the fully-qualified name of the PINE enum constant?
Tree.PINE
A compiler error will result if an anonymous inner class tries to use a variable that is not final, or not effectively final.
True
A single copy of a class's static field is shared by all instances of the class.
True
Both instance fields and instance methods are associated with a specific instance of a class, and they cannot be used until an instance of the class is created.
True
Which of the following does not belong in a JavaFX scene graph?
Trunk node
Two ways of concatenating two Strings are:
Use the concat() method or use the + between the two Strings
If button1 is a JButton object, which of the following statements will make its background blue?
button1.setBackground(Color.BLUE);
An exception's default error message can be retrieved using this method.
getMessage()
Look at the following statement: StringBuilder str = new StringBuilder(25); What will the StringBuilder constructor do?
give the object, str, 25 bytes of storage and not store anything in them
Assume that the variable checkbox references a JCheckBox object. To determine whether the check box has been selected, use the following code:
if (checkBox.isSelected()) {/code to execute, if selected/}
To use the ActionListener interface, as well as other event listener interfaces, you must have the following import statement in your code:
import java.awt.event.*;
Protected class members are denoted in a UML diagram with the symbol
#
What will be the value of loc after the following code is executed? int loc; String str = "The cow jumped over the moon."; loc = str.lastIndexOf("ov", 14);
-1
Look at the following declaration: enum Tree { OAK, MAPLE, PINE } What is the ordinal value of the MAPLE enum constant?
1
When using the BorderLayout manager, how many components can each region hold?
1
Look at the following code. The method in line ________ will override the method in line ________.
10, 4
What will be the value of loc after the following code is executed? int loc; String str = "The cow jumped over the moon."; loc = str.indexOf("ov");
15
The no-arg constructor for a StringBuilder object gives the object enough storage space to hold this many characters.
16 characters
Look at the following code. Which line has an error? Line 1 public interface Interface1 Line 2 { Line 3 int FIELDA = 55; Line 4 pub Line 5 }
4
A protected member of a class may be directly accessed by:
All of the above
If a class contains an abstract method:
All of the above
What will be printed after the following code is executed String str = "abc456"; int m = 0; while ( m < 6 ) { if (Character.isLetter(str.charAt(m))) System.out.print( Character.toUpperCase(str.charAt(m))); m++; }
ABC
Although these are often displayed in groups, they are not usually grouped in a toggle group like RadioButtons are.
CheckBoxes
Given the following code which of the following is TRUE? public class ClassB implements ClassA{}
ClassB must override each method in ClassA
This section of the Inspector Panel allows you to register the controller class and event listeners with the GUI.
Code
To place a component in a GUI, you simply drag it from the Library Panel, and drop it into the:
Content Panel
What does the following statement do? addButton.addActionListener(new AddButtonListener());
Creates an AddButtonListener object and registers the AddButtonListener object with the addButton
An anonymous inner class must:
Either A or B
All of the exceptions that you will handle are instances of classes that extend this class.
Exception
This is a section of code that gracefully responds to exceptions when they are thrown.
Exception handler
This is a markup language that describes the components in a JavaFX scene graph.
FXML
All methods in an abstract class must also be declared abstract.
False
If a class has a method named finalize, it is called automatically just before a data member that has been identified as final of the class is destroyed by the garbage collector.
False
If two methods in the same class have the same name but different signatures, the second overrides the first.
False
To end an application, pass this as the argument to the JFrame class's setDefaultCloseOperation() method.
JFrame.EXIT_ON_CLOSE
In Swing, labels are created with this class:
JLabel
The exception classes are in packages in the ________.
Java API
JFC stands for:
Java Foundation Classes
If a subclass constructor does not explicitly call a superclass constructor:
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
This is a special type of expression used to create an object that implements a functional interface.
Lambda
This is a graphic image that is displayed while an application loads into memory and starts up.
Splash screen
To convert the double variable, d = 543.98, to a string, use the following statement:
String str = Double.toString(d);
To convert the int variable, number to a string, use the following statement:
String str = Integer.toString(number);
The term ________ commonly is used to refer to a string that is part of another string.
Substring
What key word can you use to call a superclass constructor explicitly?
Super
These components have a consistent look and predictable behavior on any operating system.
Swing
What will be the result of the following statements? FileInputStream fstream = new FileInputStream("DataIn.dat"); DataInputStream inFile = new DataInputStream(fstream);
The inFile variable will reference an object that is able to read binary data from the Input.dat file.
Given the following statement, which of the following is NOT true? str.insert(8, 32);
The insert will start at position 32
Why doesn't the following code compile correctly? Huge block of code here
The itemStateChanged method cannot be coded here.
What is required for an interface method that has a body?
The method header must begin with the key word default.
What is wrong with the following code? IntCalculator square = new IntCalculator() { public int calculate(int number) { return number * number; }}
The statement does not end with a semicolon.
What will be the tokens given the following statements? String str = ("January 1, 2016"); String[] tokens = str.split(" ");
The tokens will be: January 1, 2016
Look at the following code: Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(int a){} Line 5 } Line 6 public class ClassB extends ClassA Line 7 { Line 8 public ClassB(){} Line 9 public void method1(){} Line 10 } Line 11 public class ClassC extends ClassB Line 12 { Line 13 public ClassC(){} Line 14 public void method1(){} Line 15 } Which method will be executed when the following statements are executed? ClassC item1 = new ClassA(); item1.method1();
This is an error and will cause the program to crash
All exceptions are instances of classes that extend this class.
Throwable
Each of the numeric wrapper classes has a static ________ method that converts a number to a string.
ToString
The process of breaking a string down into tokens is known as what?
Tokenizing
The ability to display splash screens was introduced in Java 6.
True
What will be the value of str after the following statements are executed? StringBuilder str = new StringBuilder("We have lived in Chicago, " + "Trenton, and Atlanta."); str.replace(17, 24, "Tampa");
We have lived in Tampa, Trenton, and Atlanta.
In UML diagrams, inheritance is shown:
With a line that has an open arrowhead at one end that points to the superclass
To serialize an object and write it to the file, use this method of the ObjectOutputStream class.
WriteObject
Like a family tree, a ________ shows the inheritance relationship between classes.
class hierarchy
If your code does not handle and exception when it is thrown, this prints an error message and crashes the program.
default exception handler
This is a variable whose value is never changed, but it isn't declared with the final key word.
effectively final variable
You can declare an enumerated data type inside of a method.
false
To convert the string, str = "285" to an int, use the following statement:
int x = Integer.parseInt(str);
A deep copy of an object:
is an operation that copies an aggregate object, and all the objects it references
When the this variable is used to call a constructor:
it must be the first statement in the constructor making the call
You would use this command at the operating system command line to execute the code in the MyApplication class and display the graphic image Logo.jpg as a splash screen.
java -splash:Logo.jpg MyApplication
The ability to catch multiple types of exceptions with a single catch is known as ________, and was introduced in Java 7.
multi-catch
A subclass may call an overridden superclass method by:
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
In an interface all methods have:
public access
If ClassA extends ClassB, then:
public members in ClassB are public in ClassA, but private members in ClassB cannot be directly accessed in ClassA
When a component is added to a region in the BorderLayout manager:
the component is stretched so it fills up the entire region
When an exception is thrown by code in the try block, the JVM begins searching the try statement for a catch clause that can handle it and passes control of the program to:
the first catch clause that can handle the exception.
A static field is created by placing:
the key word static after the access specifier and before the field's data type
When a reference variable is passed as an argument to a method:
the method has access to the object that the variable references
If the program does not handle an unchecked exception:
the program is halted and the default exception handler handles the exception
In a try/catch construct, after the catch statement is executed:
the program resumes at the statement that immediately follows the try/catch construct
If you attempt to perform an operation with a null reference variable:
the program will terminate
If a superclass does not have a default constructor or a no-arg constructor:
then a class that inherits from it, must call one of the constructors that the superclass does have.
Every class is either directly or indirectly derived from the Object class.
true
If a non-letter is passed to the toLowerCase or toUpperCase method, it is returned unchanged
true
In a scene graph, the root node and branch nodes can have children, but leaf nodes cannot.
true
It is not possible for a superclass to call a subclass's method.
true
The wrapper classes in Java are immutable, which means that once you create an object, you cannot change the object's value
true
When a subclass extends a superclass, the public members of the superclass become public members of the subclass.
true
You can use JavaFX to create standalone graphics applications that run on your local computer, applications that run from a remote server, or applications that are embedded in a Web page.
true
If, within one try statement you want to have catch clauses that catch exceptions of the following types, in which order should they appear in your program? (1) Throwable (2) Exception (3) RuntimeException (4) NumberFormatException
4, 3, 2, 1
What will be printed after the following code is executed? String str = "abc456"; int m = 0; while ( m < 6 ) { if (!Character.isLetter(str.charAt(m))) System.out.print( Character.toUpperCase(str.charAt(m))); m++; }
456
For the following code, how many times would the for loop execute? String str = ("Ben and Jerry's ice cream is great."); String[] tokens = str.split(" "); for (String s : tokens)
7
Why does the following code cause a compiler error? Try { number = Integer.parseInt(str); } catch (IllegalArgumentException e) { System.out.println("Bad number format."); } catch (NumberFormatException e) { System.out.println(str + " is not a number."); }
Because NumberFormatException inherits from IllegalArgumentException.
In an inheritance relationship, the subclass constructor always executes before the superclass constructor.
False
The following statement adds the FlowLayout manager to the container, centers the components, and separates the components with a gap of 10 pixels. setLayout(new FlowLayout());
False
The this key word is the name of a reference variable that is available to all static methods.
False
The throw statement informs the compiler that a method throws one or more exception.
False
To write data to a binary file you create objects from the following classes:
FileOutputStream and DataOutputStream
When a method is declared with the ________ modifier, it cannot be overridden in a subclass.
Final
The try statement may have an optional ________ clause, which must appear after all of the catch clauses.
Finally
Which of the following statements is NOT true?
If this includes the answer Buttons are round and boxes are square All of these are true.
You use the ________ key word in a class header to indicate that it implements an interface.
Implements
Look at the following code. What is missing from ClassA? Line 1 public interface MyInterface Line 2 { Line 3 int FIELDA = 55; Line 4 public int methodA(double); Line 5 } Line 6 public class ClassA implements MyInterface Line 7 { Line 8 FIELDA = 60; Line 9 public int methodB(double) { } Line 10 }
It does not override methodA.
Replacing inadequate superclass methods with more suitable subclass methods is known as what?
Method overriding
When writing a string to a binary file or reading a string from a binary file, it is recommended that you use:
Methods that use UTF-8 encoding
You cannot use the == operator to compare the contents of:
Objects
The variable panel references a JPanel object. The variable bGroup references a ButtonGroup object, which contains several button components. If you want to add the buttons to the panel:
add each button to panel one at a time, e.g. panel.add(button1);
The catch clause:
all of the above
The IllegalArgumentException class extends the RuntimeException class, and is therefore:
an UNchecked exception class
All fields declared in an interface:
are final and static
Protected members are:
both A and B
When you write an action listener class for a JButton component, it must:
both B and C
If the code in a method can potentially throw a checked exception, then that method must:
either A or B
When an application uses many components, instead of extending just one class from the JFrame class, a better approach is to:
encapsulate smaller groups of related components and their event listeners into their own classes
If str is declared as: String str = "ABCDEFGHI"; What will be returned from Character.toLowerCase(str.charAt(5))?
f
The String class's valueOf() method accepts a string representation as an argument and returns its equivalent integer value.
false
You cannot assign a value to a wrapper class object
false
You must use the statement import java.swing.*; in order to use the ItemListener interface.
false