0% found this document useful (0 votes)
60 views31 pages

COP2800 MidtermStudyGuide

Intro to Java Midterm Study Guide Questions drawn from the chapter exercise questions. Includes the question and all the choices, with the correct answer highlighted. All answers are correct to the best of my knowledge (and I got a 100 on the exam, so they should all be good!)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views31 pages

COP2800 MidtermStudyGuide

Intro to Java Midterm Study Guide Questions drawn from the chapter exercise questions. Includes the question and all the choices, with the correct answer highlighted. All answers are correct to the best of my knowledge (and I got a 100 on the exam, so they should all be good!)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 31

Which of the following cannot cause a syntax error to be reported by the Java compiler?

>>> An extra blank line.

Given the Java statement


int sum = number1 + number2;
which of the following statements is false?
>>> It assigns the value of number1 to sum.

End-of-line comments that should be ignored by the compiler are denoted using _____:
>>> Two forward slashes (//).

A(n) ________ enables a program to read data from the user.


>>> Scanner.

Which of the following does not contain a syntax error?


>>> System.out.println("Hello world!");

Which of the following is a Scanner method for inputting an integer value?


>>> nextInt

Which of the following is a variable declaration statement?


>>> int total;

Which of the following is the escape character?


>>> \

Java's predefined classes are grouped into ____:


>>> packages.

Which command compiles the Java source code file Welcome.java?

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");

Portions of statements that contain calculations are called


expressions.

Which of the following escape sequences represents a carriage return?


\r.
Which of the following statements is false?
Package javac is imported in every Java program.

Which is the output of the following statements?


System.out.print("Hello ");
System.out.println("World");
Hello World

Given the Java statement


int number1 = input.nextInt();
in which input is a Scanner, which of the following occurs if the user does not enter a valid int value?
A runtime logic error occurs.

Which of the following statements is false?


Variable name identifiers must begin with a lowercase letter.

Optional parentheses in expressions are said to be


redundant.

Which of the following statements does not alter the value stored in a memory location?
int a;

Which of the following is not a Java primitive type?


real

Which of the following statements will print a single line containing


"hello there"?
System.out.print("hello");
System.out.println(" there");

Which command executes the Java class file Welcome.class?


java Welcome

All import declarations must be placed:


before the class declaration.

Which of the following is not a valid Java identifier?


my Value

The format specifier ________ is a placeholder for an int value.


%d

Which of the following statements is false?


Variables of type double represent double-precision floating-point numbers; these require twice as
much memory as float variables and provide 14 significant digits.

You can declare new classes as needed; this is one reason Java is known as a(n) ________ language.
portable

incremental

extensible

virtual

Flag question: Question 28


Question 281 pts
A class that creates an object of another class, then calls the object's methods, is called a(n) ________
class.

object-oriented

inherited

caller

driver

Flag question: Question 29


Question 291 pts
Which of the following statements is false?

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.

All of the above are true.

Flag question: Question 30


Question 301 pts
When a method terminates, the values of its local variables are ________.

saved

copied
restored

lost

Flag question: Question 31


Question 311 pts
Which of the following statements is false?

Most classes you'll use in Java programs must be imported explicitly.

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.

Classes in the same package are implicitly imported into main.

An import declaration is not required when one class in a package uses another in the same package.

Flag question: Question 32


Question 321 pts
The format specifier ________ is used to output values of type float or double.

%f

%d

%fd

%r

Flag question: Question 33


Question 331 pts
Reference-type variables (called references) store ________ in memory.

the value of an object

a copy of an object

the location of an object

the size of an object

Flag question: Question 34


Question 341 pts
Which of the following statements is false?
The method's return type specifies the type of data returned to a method's caller.

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.

Flag question: Question 35


Question 351 pts
Which of the following statements is false?

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.

Instance variables can be declared anywhere inside a class.

Flag question: Question 36


Question 361 pts
A __________ of a class called MyClass is another class whose methods call the methods of MyClass.

consumer

servant

caller

client

Flag question: Question 37


Question 371 pts
The format specifier %.2f specifies that two digits of precision should be output ________ in the floating-
point number.
to the left of the decimal point

centered

to the right of the decimal point

None of the above.

Flag question: Question 38


Question 381 pts
Which of the following statements is false?

A primitive-type variable can store exactly one value of its declared type at a time.

Primitive-type instance variables are initialized by default.

Variables of types byte, char, short, int, long, float and double are initialized to 0.

Variables of type boolean are initialized to true.

Flag question: Question 39


Question 391 pts
Which of the following statements is true?

Constructors can specify parameters and return types.

Constructors can specify parameters but not return types.

Constructors cannot specify parameters but can specify return types.

Constructors can specify neither parameters nor return types.

Flag question: Question 40


Question 401 pts
Which of the following statements is true?

Local variables are automatically initialized.

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 default value for an instance variable of type String is void.

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

fully qualified name

Flag question: Question 42


Question 421 pts
Each class you create becomes a new ________ that can be used to declare variables and create objects.

package

instance

library

type.

Flag question: Question 43


Question 431 pts
You must call most methods other than ________ explicitly to tell them to perform their tasks.

public methods

main

static methods

private methods

Flag question: Question 44


Question 441 pts
Which of the following statements is false?

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.

The UML models constructors in the third compartment of a class diagram.

To distinguish a constructor from a class's operations, the UML places the word "constructor" between
double quotes before the constructor's name.

Flag question: Question 45


Question 451 pts
If a class does not define constructors, the compiler provides a default constructor with no parameters,
and the class's instance variables are initialized to ________.

zero

null

their default values

false

Flag question: Question 46


Question 461 pts
Floating-point literals are of type ________ by default.

float

double

real

decimal

Flag question: Question 47


Question 471 pts
Which of the following statements is false?

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.

Class, method and variable names are identifiers.

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

Flag question: Question 49


Question 491 pts
Which of the following statements is false?

A reference to an object is required to invoke an object's methods.

A primitive-type variable does not refer to an object.

Reference-type instance variables are initialized by default to the value void.

A primitive-type variable cannot be used to invoke a method.

Flag question: Question 50


Question 501 pts
A key part of enabling the JVM to locate and call method main to begin the app's execution is the
________ keyword, which indicates that main can be called without first creating an object of the class in
which the method is declared.

stable

private

static

public

Flag question: Question 51


Question 511 pts
Which of the following statements is false?

Variables declared in the body of a particular method are local variables and can be used only in that
method.

A method's parameters are local variables of the 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.

Flag question: Question 52


Question 521 pts
Declaring instance variables ________ is known as data hiding or information hiding.

secure

private

static

masked

Flag question: Question 53


Question 531 pts
Types in Java are divided into two categories. The primitive types are boolean, byte, char, short, int, long,
float and double. All other types are ________ types.

static

reference

declared

source

Flag question: Question 54


Question 541 pts
Which of the following statements is true?

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.

None of the above is true.


Flag question: Question 55
Question 551 pts
Which of the following statements is false?

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.

Flag question: Question 56


Question 561 pts
Which of the following statements about the conditional operator (?:) is false?

The conditional operator is a ternary operator, meaning that it takes three operands.

The first operand is a boolean expression.

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.

Flag question: Question 57


Question 571 pts
Which of the following is a double-selection control statement?

do…while

for

if…else

if

Flag question: Question 58


Question 581 pts
Which of the following is not a common name for one of the three phases that a program often can be
split into using pseudocode?
Termination phase

Initialization phase

Processing phase

Action phase

Flag question: Question 59


Question 591 pts
Which of the following statements is false?

To ensure that the operands in a mixed-type expression are of the same type, Java performs implicit
conversion on selected operands.

Cast operators are unary operators.

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.

Flag question: Question 60


Question 601 pts
Which of the following is not a control structure:

Sequence structure.

Selection structure.

Iteration structure.

Declaration structure.

Flag question: Question 61


Question 611 pts
Which of the following is not a Java keyword?

do

next

while
for

Flag question: Question 62


Question 621 pts
Which of the following segments is a proper way to call the method readData four times?

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;
}

Flag question: Question 63


Question 631 pts
Local variables must be ________.

initialized when they're declared.

initialized before their values are used in an expression.

declared and initialized in two steps.

declared at the top of the method's body.

Flag question: Question 64


Question 641 pts
How many times is the body of the loop below executed?
int counter = 1;
while (counter > 20) {
// body of loop
counter = counter - 1;
}

19.

20.

21.

0.

Flag question: Question 65


Question 651 pts
What is output by the following Java code segment?
int temp = 200;
if (temp > 90) {
System.out.println("This porridge is too hot.");
}
if (temp < 70) {
System.out.println("This porridge is too cold.");
}
if (temp == 80) {
System.out.println("This porridge is just right!");
}

This porridge is too hot.

This porridge is too cold.

This porridge is just right!

Flag question: Question 66


Question 661 pts
Which of the following operators associates from left to right?

?:

%=

Flag question: Question 67


Question 671 pts
In an expression containing values of the types int and double, the ________ values are ________ to
________ values for use in the expression.

int, promoted, double.

int, demoted, double.

double, promoted, int.

double, demoted, int.

Flag question: Question 68


Question 681 pts
What is output by the following Java code segment?
int temp = 180;
if (temp > 90) {
System.out.println("This porridge is too hot.");
temp = temp - (temp > 150 ? 100 : 20); // cool down
}
else {
if (temp < 70) {
System.out.println("This porridge is too cold.");
temp = temp + (temp < 50 ? 30 : 20); // warm up
}
}
if (temp == 80) {
System.out.println("This porridge is just right!");
}

This porridge is too hot.

This porridge is too cold.


This porridge is just right!

This porridge is just right!

This porridge is too hot.


This porridge is just right!

Flag question: Question 69


Question 691 pts
Which primitive type can hold the largest value?

int
long

float

double

Flag question: Question 70


Question 701 pts
Which of the following statements is false?

You should not call methods from constructors.

Nested if statements can be useful for validating values.

Logical operators can express nested if statements more concisely.

One problem with calling methods from constructors is that it can lead to duplicated validation code.

Flag question: Question 71


Question 711 pts
What does the expression x %= 10 do?

Adds 10 to the value of x, and stores the result in x.

Divides x by 10 and stores the remainder in x.

Divides x by 10 and stores the integer result in x.

Flag question: Question 72


Question 721 pts
Which of the following code segments does not increment val by 3:

val += 3;

val = val + 1;
val = val + 1;
val = val + 1;

c = 3;
val = val + (c == 3 ? 2 : 3);

Flag question: Question 73


Question 731 pts
What is output by the following Java code segment?
int temp = 180;
while (temp != 80) {
if (temp > 90) {
System.out.print("This porridge is too hot! ");
temp = temp - (temp > 150 ? 100 : 20); // cool down
}
else {
if (temp < 70) {
System.out.print("This porridge is too cold! ");
temp = temp + (temp < 50 ? 30 : 20); // warm up
}
}
}
if (temp == 80) {
System.out.println("This porridge is just right!");
}

This porridge is too cold! This porridge is just right!

This porridge is too hot! This porridge is just right!

This porridge is just right!

Flag question: Question 74


Question 741 pts
Which of the following is not a primitive type?

char

float

String

int

Flag question: Question 75


Question 751 pts
In an activity diagram, the merge symbol has the same shape as what other symbol?

Decision symbol.

Action symbol.

Transition arrows.
Initial state.

Flag question: Question 76


Question 761 pts
Which of the following is the shape of an action-state symbol?

Diamond.

Circle.

Rectangle with left and right sides replaced with arcs curving outward.

Rounded rectangle.

Flag question: Question 77


Question 771 pts
Which of the following is not an error (either a syntax error or a logic error)?

Neglecting to include an action in the body of a while statement that will eventually cause the condition
to become false.

Spelling a keyword (such as while or if) with a capitalized first letter.

Using a condition for a while statement that is initially false.

An infinite loop.

Flag question: Question 78


Question 781 pts
Which of the following statements is true?

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.

Flag question: Question 79


Question 791 pts
The empty statement is denoted by what symbol?

Semicolon ;
Parentheses ()

Braces {}

Colon :

Flag question: Question 80


Question 801 pts
Which statement is false?

Dividing two integers results in integer division.

With integer division, any fractional part of the calculation is lost.

With integer division, any fractional part of the calculation is truncated.

With double division, any fractional part of the calculation is truncated.

Flag question: Question 81


Question 811 pts
Which statement is false?

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.

Flag question: Question 82


Question 821 pts
Java is considered a strongly typed language because:

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.

Flag question: Question 83


Question 831 pts
Where can local variables declared within a method's body be used?
>>> Only in that method between the line in which they were declared and the closing brace of that
method.

What is the size in bits of an int?


>>> 32

Sentinel-controlled iteration is also known as:


>>> Indefinite iteration.

Which of the following is not an algorithm?


>>> Textbook index.

Which of the following terms is not used to refer to a sentinel value that breaks out of a while loop?
>>> maximum value.

Which of the following is false?


>>> Pseudocode is an actual computer programming language.

What is the result value of c at the end of the following code segment?
int c = 8;
c++;
++c;
c %= 5;
>>> 0.

Counter-controlled iteration is also known as:


>>> Definite iteration

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 (,).

Which of the following is equivalent to this code segment?


int total = 0;
for (int i = 0; i <= 20; i += 2) {
total += i;
}
>>> int total = 0;
for (int i = 0; i <= 20; total += i, i += 2) {}

Which expression is equivalent to if (!(grade == sentinelValue))?


>>> if (grade != sentinelValue)

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 of the following statements about the break statement is false?


>>> The break statement, when executed in a while, for or do…while, skips the remaining statements in
the loop body and proceeds with the next iteration of the loop.

For the code segment below:


switch(q) {
case 1:
System.out.println("apple");
break;
case 2:
System.out.println("orange");
break;
case 3:
System.out.println("banana");
break;
case 4:
System.out.println("pear");
case 5:
System.out.println("grapes");
default:
System.out.println("kiwi");
}
Which of the following values for q will result in kiwi being included in the output?
>>> Any integer less than 1 and greater than or equal to 4.

Which of the following for-loop headers results in equivalent numbers of iterations:


[A] for (int q = 1; q <= 100; q++)
[B] for (int q = 100; q >= 0; q--)
[C] for (int q = 99; q > 0; q -= 9)
[D] for (int q = 990; q > 0; q -= 90)
>>> [C] and [D]

Which of the following statements about the continue statement is true?


>>> A continue statement proceeds with the next iteration of the immediately enclosing while, for, do…
while statement.

Which of the following statements is true?


>>> Strings can be used in a switch statement's controlling expression and in its case labels.

Which statement below is false?


>>> Structured programming requires four forms of control.
To exit out of a loop completely, and resume the flow of control at the next statement after the loop, use
a _______:
>>> break statement.

Which of the following is not a type of iteration statement in Java?


>>> loop statement.

Which statement prints the floating-point value 123.456 right justified with a field width of 10?
>>> System.out.printf("%10.3f", 123.456);

Consider the code segment below.


if (gender == 1) {
if (age >= 65) {
++seniorFemales;
}
}
This segment is equivalent to which of the following?
>>> if (gender == 1 && age >= 65) {
++seniorFemales;
}

Which of the following statements about a do…while iteration statement is true?


>>> The body of a do…while loop is always executed at least once.

For the two code segments below:


//Segment A
int q = 5;
switch(q) {
case 1:
System.out.println(1); break;
case 2:
System.out.println(2); break;
case 3:
System.out.println(3); break;
case 4:
System.out.println(4); break;
case 5:
System.out.println(5);
default:
System.out.println("default");
}
//Segment B
q = 4;
switch(q) {
case 1:
System.out.println(1); break;
case 2:
System.out.println(2); break;
case 3:
System.out.println(3); break;
case 4:
System.out.println(4);
case 5:
System.out.println(5); break;
default:
System.out.println("default");
}
Which of the following statements is true?
>>> The output for Segment A is:
5
default

boolean values can be displayed as the words true and false with the ________ format specifier.
>>> %b.

Which of the following will not help prevent infinite loops?


>>> Include braces around the statements in a do…while statement.

Which of the following will count down from 10 to 1 correctly?


>>> for (int j = 10; j >= 1; j--)

Which of the following statements about the switch statement is false?


>>> You can use a comma-separated list of Strings in a switch statement's case label.

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.

The identifiers in an enumeration ________.


>>> must be unique.

Which of the following statements is false?


>>> If a method does not return a value, the return-value-type in the method declaration can be
omitted.

Which of the following promotions of primitive types is not allowed to occur?


>>> double to float.

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);

Question 1201 pts


Any field declared with keyword ________ is constant.
>>> final

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.

When an object is concatenated with a String, ________.


>>> the object's toString method is implicitly called to obtain the String representation of the object

Which of the following can NOT be an argument to a method?


>>> if-statements.

Overloaded methods always have the same _________.


>>> method name

The parameter list in the method header and the arguments in the method call must agree in:
>>> number, type, and order

Which of the following statements is false?


>>> The Java API consists of import declarations.

Declaring main as static allows the JVM to invoke main ________.


>>> without creating an instance of the class in which main is declared.

Variables should be declared as fields only if ________.


>>> they are required for use in more than one method or their values must be saved between calls to
the class's methods

In a class containing methods with the same name, the methods are distinguished by ________.
>>> Both number of arguments and types of arguments

Method calls cannot be distinguished by ________.


>>> return type

Which of the following is not a package in the Java API?


>>> java.component

A Java class can have which of the following methods?


[A] void foo(int a)
[B] void foo(int a, int b)
[C] void foo(double a)
[D] void foo(double a, double b)
[E] void foo(int b)
>>> [A], [B], [C], [D].

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.

Information is passed to a method in ________:


>>> the arguments to the method.

Consider the following Java statements:


int x = 9;
double y = 5.3;
result = calculateValue(x, y);
Which of the following statements is false?
>>> x and y are parameters.

Which operator can be used in string concatenation?


>>> +=

Which of the following methods is not in the Math class?


>>> parseInt

Flag question: Question 140


Question 1401 pts
Stacks are known as ________ data structures.
>>> LIFO.

Which of these statements best defines scope?


>>> Scope is the portion of a program that can refer to an entity by its simple name.

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.

An overloaded method is one that ________.


>>> has the same name as another method, but different parameters (by number, types or order of the
types)

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);

Flag question: Question 147


Question 1471 pts
Identifiers in Java have ________ and ________ scopes?
>>> class, block.

Which of the following statements describes block scope?


>>> It begins at the identifier's declaration and ends at the terminating right brace (}).

A well-designed method ________:


>>> performs a single, well-defined task.

Which is a correct static method call of Math class method sqrt?


>>> Math.sqrt(900);

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};

Which of the following will not produce a compiler error?


>>> Changing the value at a given index of an array after it is created.

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 set of statements creates an array of Books?


>>> Book[] books;
books = new Book[numberElements];

An array with m rows and n columns is not ________.


[A] an m-by-n array.
[B] an n-by-m array.
[C] a two-dimensional array.
[D] a dual-transcripted array.
>>> [B] and [D].

Which of the following statements is false?


>>> When a program is executed, array element indices are checked for validity-all indices must be
greater than 0 and less than or equal to the length of the array.

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 of the following statements about arrays are true?


[A] An array is a group of variables containing values that all have the same type.
[B] Elements are located by index.
[C] The length of an array c is determined by the expression c.length();.
[D] The zeroth element of array c is specified by c[0].
>>> [A], [B], [D].

Which command below runs TestProgram, and passes in the values files.txt and 3?
>>> java TestProgram files.txt 3.

Arrays are ________.


>>> fixed-length entities

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];

Exception handling helps you create ________ programs.


>>> fault-tolerant

Consider the program below:


public class Test {
public static void main(String[] args) {
int[] a;
a = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = i + 2;
}
int result = 0;
for (int i = 0; i < a.length; i++) {
result += a[i];
}
System.out.printf("Result is: %d%n", result);
}
}
The output of this program will be:
>>> Result is: 65.

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};

Consider the class below:


public class Test {
public static void main(String[] args) {
int[] a = {99, 22, 11, 3, 11, 55, 44, 88, 2, -3};
int result = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > 30) {
result += a[i];
}
}
System.out.printf("Result is: %d%n", result);
}
}
The output of this Java program will be:
>>> Result is: 286.

Constant variables also are called .


>>> named constants

A(n) ________ indicates a problem that occurs while a program executes.


>>> exception

Which of the following statements is false?


>>> The catch block contains the code that might throw an exception, and the try block contains the
code that handles the exception if one occurs.

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;
}
}

Which of the following statements is false?


>>> When an argument is passed by reference, the called method can access the argument's value in the
caller directly but cannot modify it.

Class Arrays provides method __________ for comparing arrays to determine whether they have the
same contents.
>>> equals

Which of the following statements is true?


>>> You can have many catch blocks to handle different types of exceptions.

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 (...)

The preferred way to traverse a two-dimensional array is to use .


>>> two nested for statements.

An exception object's ________ method returns the exception's error message.


>>> toString

A programmer must do the following before using an array:


>>> declare then create the array.

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.

Which method call converts the value in variable stringVariable to an integer?


>>> Integer.parseInt(stringVariable)

Consider the array:


s[0] = 7
s[1] = 0
s[2] = -12
s[3] = 9
s[4] = 10
s[5] = 3
s[6] = 6
The value of s[s[6] - s[5]] is:
>>> 9.

Invalid possibilities for array indices include ____:


>>> Negative integers.

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);
}

How many Book objects are created by the following statement?


Book[] books = new Book[10];
>>> 0

In Java, which of the following statements is false:


>>> Multidimensional arrays are implemented as values of arrays.

Which of the following is false?


>>> The size of an ArrayList can be determined via its length instance variable.

What do the following statements do?


double[] array;
array = new double[14];
>>> Create a double array containing 14 elements.

Which expression adds 1 to the element of array arrayName at index i?


>>> ++arrayName[i].

For the array that was the correct answer in the previous question, what is the value returned by
items[1][0]?
>>> 6.

Class ________ represents a dynamically resizable array-like data structure.


>>> ArrayList

You might also like