Question 1
When using an array, what can be used to determine the number of elements in the array?
use the array's length() method
use the array's length property
use the array's size() method
use the array's size property
Question 2
Overriding is when you:
define a class using the same class name but a different number of parameters
define two classes using the same name but different constructors
define a method in a subclass and superclass with the same name.
define two methods with the same name but different number of parameters
Question 3
To access an element in an Array object,
use the array's index value.
use the element() method.
use the get() method.
use the position property.
Question 4
To access an element in an ArrayList object,
use the array's index value.
use the element() method.
use the get() method.
use the position property.
Question 5
Which GUI component would be used to allow text to be entered?
a JTextField
a JTextBox
a JTextFrame
a JTextLabel
Question 6
Which method call would change the window title to "Hello"?
setTitle("Hello")
setWindowTitle("Hello")
setFrame("Hello")
setFrameTitle("Hello"
Question 7
When a button-click event occurs, which item below must be implemented?
ActionEvent interface
ActionPerformed interface
AactionListener interface
WindowListener interface
Question 8
Which method should be used to determine which component caused the fired event?
getInstance()
getSource()
getEvent()
getActionCommand()
Question 9
Which layout manager causes components to be positioned in the top-center position?
BorderLayout
FlowLayout
GridBagLayout
GridLayout
GridlessLayout
Question 10
Which two methods come from the Component class which is an ancestor of the JFrame class?
setTitle()
setDefaultCloseOperation()
setLayout()
setVisible()
Question 11
Which is the proper way to enable Line Wrap on a JTextArea?
setLineWrap(1);
setLineWrap(word);
setLineWrap(true);
setLineWrap(on);
True/False questions: circle the best answer.
Question 12
When using Short-circuit evaluation, if the left side of an "and" operation is false, the right side is not evaluated.
True
False
Question 13
The following is an example of a "Ternary" operator.
(a > b) ? x : y
True
False
Question 14
A "catch" block can only check one exception at a time. Subsequent catch blocks are needed for each additional exception.
True
False
Question 15
Use of the access modifier public provides for better encapsulation and ensures access is possible for any class needed direct access to the variable in question.
True
False
Question 16
When using parseInt() to convert a String to a number, a non-integer value causes a runtime error.
True
False
Question 17
The getSource method is used to determine the component causing the event that was fired from inside an actionPerformed method.
True
False
Question 18
Write the statement to access the 8th element of the array defined below?
Integer [] priceList = new Integer [10];
HTML EditorKeyboard Shortcuts
Font Sizes
Paragraph
Question 19
Add the variable called input to the ArrayList declared below.
private static ArrayList floors = new ArrayList<>();
HTML EditorKeyboard Shortcuts
Font Sizes
Paragraph
p
Question 20
Given the code below, a syntax error prevents it from compiling and running.
Which line is causing the syntax error?
HTML EditorKeyboard Shortcuts
Font Sizes
Paragraph
Question 21
Given the code below, a syntax error prevents it from compiling and running.
What is the output?
Which line is causing the syntax error?
HTML EditorKeyboard Shortcuts
Font Sizes
Paragraph
Question 22
Assume this program compiles and runs.
Assume that the user enters 1 2 3 4(separated by spaces) for input.
What is the output?
1 2 3 4
4 3 2 1
3 4 1 2
2 1 4 3
Question 23
Given the following code fragment, what is the output?
0
6
Division by zero
Other Exception
Question 24
Assume this program compiles and runs.
Assume that the user enters 5 6 (separated by a space) for input.
What is the output?
As always be precise when showing your output.
0
6
Other Exception
Division by zero
Question 25
Assume these 3 classes compile and run.
What is the output?
Robert in Driver
Robert in Person
Robert in Student
Neither of the above
Question 26
Assume these 3 classes compile and run.
What is the output?
Robert
Robert in Driver
Robert in Student
Robert in Person
Question 27
- Create a driver program that creates an instance of a dog class using the default constructor.
- Call a method called setName to set the name of your dog to "Max".
- Implement the dog class to include a default constructor and a setName method.
- Include any instance variables needed.
- Provide comments as necessary.
HTML EditorKeyboard Shortcuts
Font Sizes
Paragraph
Question 28
Provide the code for a class called Grades.java that would work with the driver class provided below.
import java.util.Random;
public class Driver
{
public static void main(String [] args)
{
Grades [] studentGrades = new Grades[25];
for (int x=0; x studentGrades[x] = new Grades();
Random rand = new Random();
for (int x=0; x {
int grade = rand.nextInt(51)+50;
studentGrades[x].addEntry(grade);
}
Grades.showResults(studentGrades) ;
}
}
When the program runs, the following random output should be produced: (numbers will vary due to random)
Lowest Grade: 60
Highest Grade: 99
Average: 78
Program specific requirements for Grades class:
- Declare 3 static variables for lowest, highest, and average grades.
- Declare 1 instance variable to hold the grade.
- addEntry Method
o provide a proper header which receives grade as input
o store received grade in instance variable
- showResults Method
o provide a proper class method header which receives array
o utilize a for loop to iterate through array
o add up grades into total static variable
o set low to lowest grade received
o set high to highest grade received
o output lowest grade
o output highest grade
o output average grade