COP2800 MidtermStudyGuide
COP2800 MidtermStudyGuide
End-of-line comments that should be ignored by the compiler are denoted using _____:
>>> Two forward slashes (//).
cd Welcome.java
javac Welcome.java
java Welcome.java
compile Welcome.java
Which of the following statements would display the phase Java is fun?
System.out.println("Java is fun");
Which of the following statements does not alter the value stored in a memory location?
int a;
You can declare new classes as needed; this is one reason Java is known as a(n) ________ language.
portable
incremental
extensible
virtual
object-oriented
inherited
caller
driver
The javac command can compile multiple classes at once; simply list the source-code filenames after the
command with each filename separated by a comma from the next.
If the directory containing the app includes only one app's files, you can compile all of its classes with the
command javac *.java.
The asterisk (*) in javac *.java indicates that all files in the current directory ending with the filename
extension ".java" should be compiled.
saved
copied
restored
lost
There's a special relationship between classes that are compiled in the same directory. By default, such
classes are considered to be in the same package-known as the default package.
An import declaration is not required when one class in a package uses another in the same package.
%f
%d
%fd
%r
a copy of an object
Empty parentheses following a method name indicate that the method does not require any parameters
to perform its task.
When a method that specifies a return type other than void is called and completes its task, the method
must return a result to its calling method
Classes often provide public methods to allow the class's clients to set or get private instance variables;
the names of these methods must begin with set or get.
By convention class names begin with an uppercase letter, and method and variable names begin with a
lowercase letter.
Instance variables exist before methods are called on an object, while the methods are executing and
after the methods complete execution.
A class normally contains one or more methods that manipulate the instance variables that belong to
particular objects of the class.
consumer
servant
caller
client
centered
A primitive-type variable can store exactly one value of its declared type at a time.
Variables of types byte, char, short, int, long, float and double are initialized to 0.
Every instance variable has a default initial value-a value provided by Java when you do not specify the
instance variable's initial value.
The argument types in the method call must be identical to the types of the corresponding parameters in
the method's declaration.
Flag question: Question 41
Question 411 pts
An import declaration is not required if you always refer to a class with its ________ name, which
includes its package name and class name.
compile-time
default package
paired
package
instance
library
type.
public methods
main
static methods
private methods
If a class does not define constructors, the compiler provides a default constructor with no parameters.
If you declare a constructor for a class, the compiler will not create a default constructor for that class.
To distinguish a constructor from a class's operations, the UML places the word "constructor" between
double quotes before the constructor's name.
zero
null
false
float
double
real
decimal
Each class declaration that begins with the access modifier private must be stored in a file that has the
same name as the class and ends with the .java filename extension.
Every class declaration contains keyword class followed immediately by the class's name.
An object has attributes that are implemented as instance variables and carried with it throughout its
lifetime.
Flag question: Question 48
Question 481 pts
Java requires a ________ call for every object that's created.
constructor
destructor
parameterless
parameterized
stable
private
static
public
Variables declared in the body of a particular method are local variables and can be used only in that
method.
Every method's body is delimited by left and right braces ({ and }).
Keyword null indicates that a method will perform a task but will not return any information.
secure
private
static
masked
static
reference
declared
source
Each object (instance) of the class shares the class's instance variables.
Most instance-variable declarations are preceded with the keyword public, which is an access modifier.
Variables or methods declared with access modifier private are accessible only to methods of the class in
which they're declared.
Scanner method next reads characters until any white-space character is encountered, then returns the
characters as a String.
To call a method of an object, follow the object name with a comma, the method name and a set of
parentheses containing the method's arguments.
A class instance creation expression begins with keyword new and creates a new object.
A constructor is similar to a method but is called implicitly by the new operator to initialize an object's
instance variables at the time the object is created.
The conditional operator is a ternary operator, meaning that it takes three operands.
The second operand is the result value if the condition evaluates to false.
The second operand is the result value if the condition evaluates to true.
do…while
for
if…else
if
Initialization phase
Processing phase
Action phase
To ensure that the operands in a mixed-type expression are of the same type, Java performs implicit
conversion on selected operands.
Cast operators associate from right to left and are one level lower in precedence than the multiplicative
operators.
Cast operators are formed by placing parentheses around the name of a type.
Sequence structure.
Selection structure.
Iteration structure.
Declaration structure.
do
next
while
for
double k = 0.0;
while (k != 4) {
readData();
k = k + 1;
}
int i = 0;
while (i <= 4) {
readData();
i = i + 1;
}
int i = 0;
while (i < 4) {
readData();
}
int i = 0;
while (i < 4) {
readData();
i = i + 1;
}
19.
20.
21.
0.
?:
%=
int
long
float
double
One problem with calling methods from constructors is that it can lead to duplicated validation code.
val += 3;
val = val + 1;
val = val + 1;
val = val + 1;
c = 3;
val = val + (c == 3 ? 2 : 3);
char
float
String
int
Decision symbol.
Action symbol.
Transition arrows.
Initial state.
Diamond.
Circle.
Rectangle with left and right sides replaced with arcs curving outward.
Rounded rectangle.
Neglecting to include an action in the body of a while statement that will eventually cause the condition
to become false.
An infinite loop.
Both syntax errors and logic errors are caught by the compiler.
Both syntax errors and logic errors have effects at execution time.
Syntax errors are caught by the compiler. Logic errors have effects at execution time.
Logic errors are caught by the compiler. Syntax errors have effects at execution time.
Semicolon ;
Parentheses ()
Braces {}
Colon :
Unless directed otherwise, the computer executes Java statements one after the other in the order in
which they're written.
Activity diagrams normally show the Java code that implements the activity.
Like pseudocode, activity diagrams help programmers develop and represent algorithms.
The arrows in the activity diagram represent transitions, which indicate the order in which the actions
represented by the action states occur.
The primitive types in Java are portable across all computer platforms that support Java.
>>> Java requires all variables to have a type before they can be used in a program.
Instance variables of the primitive types are automatically assigned a default value.
Which of the following terms is not used to refer to a sentinel value that breaks out of a while loop?
>>> maximum value.
What is the result value of c at the end of the following code segment?
int c = 8;
c++;
++c;
c %= 5;
>>> 0.
The control variable of a counter-controlled loop should be declared as ________to prevent errors.
>>> int.
Which of the following can NOT be used in a switch statement in the expression after keyword case?
>>> an if-statement
Which formatting flag indicates that the floating-point values should be output with a thousands
separator?
>>> comma (,).
Suppose variable gender contains MALE and age equals 60, how is the expression
(gender == FEMALE) && (age >= 65) evaluated?
>>> The condition (gender == FEMALE) is evaluated first and the evaluation stops immediately.
Which statement prints the floating-point value 123.456 right justified with a field width of 10?
>>> System.out.printf("%10.3f", 123.456);
boolean values can be displayed as the words true and false with the ________ format specifier.
>>> %b.
Which case of the following would warrant using the boolean logical inclusive OR (|) rather than the
conditional OR (||)?
>>> Testing if at least one of two conditions is true when the right operand has a required side effect.
Method log takes the logarithm of its argument with respect to what base?
>>> e
Which statement below could be used to simulate the outputs of rolling a six-sided die? Suppose
randomNumbers is a SecureRandom object.
>>> 1 + randomNumbers.nextInt(6);
Which statement below could be used to simulate the outputs of tossing a quarter to get heads or tails?
Suppose randomNumbers is a SecureRandom object.
>>> randomNumbers.nextInt(2);
A set of named constants that start with the value 0 for the first constant and increment by 1 for each
subsequent constant can be declared as a(n) ________.
>>> enum
If more method calls occur than can have their activation records stored on the program execution stack,
an error known as a ________ occurs.
>>> stack overflow.
The parameter list in the method header and the arguments in the method call must agree in:
>>> number, type, and order
In a class containing methods with the same name, the methods are distinguished by ________.
>>> Both number of arguments and types of arguments
Which of the following methods are overloaded with respect to one another?
[A] public int max (int a, int b) { … }
[B] public double max (double a, double b) { … }
[C] public int max (int a, int b, int c) { … }
[D] public double max (double a, double b, double c) { … }
>>> All four methods are overloaded.
The java.text package contains classes for manipulating all of the following items except ________:
>>> classes.
To declare a method as static, place the keyword static before ________ in the method's declaration.
>>> the return type
Which of the following primitive types is never promoted to another primitive type?
>>> double and boolean.
Math static method random generates a random double value in the range from 0.0
>>> up to but not including 1.0
Which statement creates a random value from the sequence 2, 5, 8, 11 and 14. Suppose
randomNumbers is a SecureRandom object.
>>> 2 + 3 * randomNumbers.nextInt(5);
Which set of statements totals the items in each row of two-dimensional array items, and displays each
row's total?
>>> for (int row = 0; row < items.length; row++) {
int total = 0;
for (int column = 0; column < items[row].length; column++) {
total += items[row][column];
System.out.printf("Row %d's total is %d%n", row, total);
}
Which of the following initializer lists would correctly set the elements of array n?
>>> int[] n = {1, 2, 3, 4, 5};
When you pass an array or an individual array element of a reference type to a method, the called
method receives ________. When you pass an individual element of a primitive type, the called method
receives ________.
>>> a copy of the element's reference, a copy of the element's value
Attempting to access an array element outside of the bounds of an array, causes a(n) .
>>> ArrayIndexOutOfBoundsException.
Which flag in a format specifier indicates that values with fewer digits than the field width should begin
with a leading 0?
>>> 0
Which of the following statements creates a multidimensional array with 3 rows, where the first row
contains 1 element, the second row contains 4 elements and the final row contains 2 elements?
>>> int[][] items = {{1}, {2, 3, 4, 5}, {6, 7}};
In array items, which expression below accesses the value at row 3 and column 4?
>>> items[3][4]
What kind of application tests a class by creating an object of that class and calling the class's methods?
>>> Test harness.
Which command below runs TestProgram, and passes in the values files.txt and 3?
>>> java TestProgram files.txt 3.
Consider integer array values, which contains 5 elements. Which statements successfully swap the
contents of the array at index 3 and index 4?
>>> int temp = values[3];
values[3] = values[4];
values[4] = temp;
Which of the following statements about creating arrays and initializing their elements is false?
>>> The elements of an array of integers have a value of null before they are initialized.
Consider array items, which contains the values 0, 2, 4, 6 and 8. If method changeArray is called with the
method call changeArray(items, items[2]), what values are stored in items after the method has finished
executing?
public static void changeArray(int[] passedArray, int value) {
passedArray[value] = 12;
value = 5;
}
>>> 0, 2, 4, 6, 12.
Which of the following sets of statements creates a multidimensional array with 3 rows, where the first
row contains 1 value, the second row contains 4 items and the final row contains 2 items?
>>> int[][] items;
items = new int[3][];
items[0] = new int[1];
items[1] = new int[4];
items[2] = new int[2];
Which of the following tasks cannot be performed using an enhanced for loop?
>>> Incrementing the value stored in each element of the array.
Which statement below initializes array items to contain 3 rows and 2 columns?
>>> int[][] items = {2, 4}, {6, 8}, {10, 12};
Which set of statements totals the values in two-dimensional int array items?
>>> int total = 0;
for (int[] subItems : items) {
for (int item : subItems) {
total += item;
}
}
Class Arrays provides method __________ for comparing arrays to determine whether they have the
same contents.
>>> equals
Which statement correctly passes the array items to method takeArray? Array items contains 10
elements.
>>> takeArray(items).
An argument type followed by a(n)__________ in a method's parameter list indicates that the method
receives a variable number of arguments of that particular type.
>>> ellipsis (...)
Consider the code segment below. Which of the following statements is false?
int[] g;
g = new int[23];
>>> The value of g[3] is -1.
Class Arrays methods sort, binarySearch, equals and fill are overloaded for primitive-type arrays and
Object arrays. In addition, methods __________ and __________ are overloaded with generic versions.
>>> binarySearch, equals.
Assume array items contains the integer values 0, 2, 4, 6 and 8. Which of the following uses the
enhanced for loop to display each value in array items?
>>> for (int i : items) {
System.out.prinf("%d%n", i);
}
For the array that was the correct answer in the previous question, what is the value returned by
items[1][0]?
>>> 6.