Multiple Choice Question Bank With Java
Multiple Choice Question Bank With Java
[Link]
Comparing and Testing Characters ............................................................................................... 32
Getting Characters from a String ................................................................................................... 32
Converting Strings ........................................................................................................................... 33
Comparing Strings........................................................................................................................... 33
Finding a Character or a Substring in a String ............................................................................... 34
Conversions between Strings and Numbers ................................................................................ 35
Formatting Console Output ........................................................................................................... 35
Loops .................................................................................................................................................... 37
The while Loop ................................................................................................................................. 37
Case Study: Guessing Numbers .................................................................................................... 37
Controlling a Loop with User Confirmation or a Sentinel Value ................................................. 38
The do-while Loop ........................................................................................................................... 38
The for Loop ..................................................................................................................................... 39
Which Loop to Use? ........................................................................................................................ 42
Nested Loops................................................................................................................................... 43
Minimizing Numerical Errors .......................................................................................................... 44
Case Studies .................................................................................................................................... 44
Keywords break and continue ........................................................................................................ 45
Case Study: Checking Palindromes .............................................................................................. 47
Case Study: Displaying Prime Numbers ....................................................................................... 48
Methods ............................................................................................................................................... 50
Defining a Method ........................................................................................................................... 50
Calling a Method.............................................................................................................................. 50
Passing Parameters by Values ....................................................................................................... 53
Overloading Methods ...................................................................................................................... 54
The Scope of Variables ................................................................................................................... 56
Method Abstraction and Stepwise Refinement ............................................................................ 57
Single-Dimensional Arrays ................................................................................................................. 58
Array Basics ..................................................................................................................................... 58
Case Study: Analyzing Numbers .................................................................................................... 59
Case Study: Deck of Cards ............................................................................................................. 60
Copying Arrays ................................................................................................................................. 63
Passing Arrays to Methods ............................................................................................................. 66
Sorting Arrays................................................................................................................................... 71
The Arrays Class .............................................................................................................................. 71
[Link]
Command-Line Arguments ............................................................................................................ 72
Multidimensional Arrays ..................................................................................................................... 74
Two-Dimensional Array Basics ...................................................................................................... 74
Processing Two-Dimensional Arrays ............................................................................................. 76
Passing Two-Dimensional Arrays to Methods .............................................................................. 78
Multidimensional Arrays ................................................................................................................. 79
Objects and Classes ........................................................................................................................... 81
Defining Classes for Objects .......................................................................................................... 81
Constructing Objects Using Constructors.................................................................................... 81
Accessing Objects via Reference Variables ................................................................................. 83
Using Classes From the Java Library ............................................................................................. 86
Visibility Modifiers ........................................................................................................................... 90
Data Field Encapsulation ............................................................................................................... 91
Passing Objects to Methods........................................................................................................... 92
Array of Objects ............................................................................................................................... 95
Immutable Objects and Classes ................................................................................................... 95
Scope of Variables .......................................................................................................................... 95
The this Keyword ............................................................................................................................. 97
[Link]
Elementary Programming
Writing a Simple Program
2.1 _______ is the code with natural language mixed with Java code.
A. Java program
B. A Java statement
C. Pseudocode
D. A flowchart diagram
2.5 If you enter 1 2 3, when you run this program, what will be the output?
import [Link];
public class Test1 {
public static void main(String[] args) {
[Link]
Scanner input = new Scanner([Link]);
[Link]("Enter three numbers: ");
double number1 = [Link]();
double number2 = [Link]();
double number3 = [Link]();
// Compute average
double average = (number1 + number2 + number3) / 3;
// Display result
[Link](average);
}
}
A. 1.0
B. 2.0
C. 3.0
D. 4.0
Identifiers
2.6 Every letter in a Java keyword is in lowercase?
A. true
B. false
Variables
2.8 Which of the following are correct names for variables according to Java naming
conventions?
A. radius
B. Radius
C. RADIUS
D. findArea
E. FindArea
[Link]
Section 2.6 Assignment Statements and Assignment Expressions
2.10 ____________ is the Java assignment operator.
A. ==
B. :=
C. =
D. =:
Named Constants
2.13 To declare a constant MAX_LENGTH inside a method with value 99.98, you write
A. final MAX_LENGTH = 99.98;
B. final float MAX_LENGTH = 99.98;
C. double MAX_LENGTH = 99.98;
D. final double MAX_LENGTH = 99.98;
2.15 To improve readability and maintainability, you should declare _________ instead
of using literal values such as 3.14159.
A. variables
B. methods
C. constants
D. classes
[Link]
Naming Conventions
2.16 According to Java naming convention, which of the following names can be
variables?
A. FindArea
B. findArea
C. totalLength
D. TOTAL_LENGTH
E. class
Numeric Types
2.17 Which of these data types requires the most amount of memory?
A. long
B. int
C. short
D. byte
2.18 When assigning a literal to a variable of the byte type, if the literal is too large to be
stored as a byte value, it _____________.
A. causes overflow
B. causes underflow
C. causes no error
D. cannot happen in Java
E. receives a compile error
2.21 25 % 1 is _____
A. 1
B. 2
C. 3
D. 4
E. 0
[Link]
2.22 -25 % 5 is _____
A. 1
B. 2
C. 3
D. 4
E. 0
2.23 24 % 5 is _____
A. 1
B. 2
C. 3
D. 4
E. 0
2.24 -24 % 5 is _____
A. -1
B. -2
C. -3
D. -4
E. 0
2.25 -24 % -5 is _____
A. 3
B. -3
C. 4
D. -4
E. 0
Exponent Operations
2.26 How do you write 2.5 ^ 3.1 in Java?
A. 2.5 * 3.1
B. [Link](2.5, 3.1)
C. [Link](3.1, 2.5)
D. 2.5 ** 3.1
E. 3.1 ** 2.5
[Link]
2.28 [Link](4, 1 / 2) returns __________.
A. 2
B. 2.0
C. 0
D. 1.0
E. 1
Numeric Literals
2.31 To declare an int variable number with initial value 2, you write
A. int number = 2L;
B. int number = 2l;
C. int number = 2;
D. int number = 2.0;
[Link]
2.33 Which of the following is incorrect?
A. 1_2
B. 0.4_56
C. 1_200_229
D. _4544
[Link]
Case Study: Displaying the Current Time
2.37 The [Link]() returns ________________ .
A. the current time.
B. the current time in milliseconds.
C. the current time in milliseconds since midnight.
D. the current time in milliseconds since midnight, January 1, 1970.
E. the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix
time).
[Link]
Augmented Assignment Operators
2.41 To add a value 1 to variable x, you write
A. 1 + x = x;
B. x += 1;
C. x := 1;
D. x = x + 1;
E. x = 1 + x;
int x = 2;
int y = 1;
x *= y + 1;
A. x is 1.
B. x is 2.
C. x is 3.
D. x is 4.
[Link]
2.46 What is x after the following statements?
int x = 1;
x *= x + 1;
A. x is 1.
B. x is 2.
C. x is 3.
D. x is 4.
(A) x -= x + 4
(B) x = x + 4 - x
(C) x = x - (x + 4)
A. (A) and (B) are the same
B. (A) and (C) are the same
C. (B) and (C) are the same
D. (A), (B), and (C) are the same
[Link]
2.50 What is i printed in the following code?
[Link]
Numeric Type Conversions
2.53 To assign a double variable d to a float variable x, you write
A. x = (long)d
B. x = (int)d;
C. x = d;
D. x = (float)d;
double x = 5.5;
int y = (int)x;
[Link]("x is " + x + " and y is " + y);
A. x is 5 and y is 6
B. x is 6.0 and y is 6.0
C. x is 6 and y is 6
D. x is 5.5 and y is 5
E. x is 5.5 and y is 5.0
[Link]
2.59 Which of the following expression results in 45.37?
A. (int)(45.378 * 100) / 100
B. (int)(45.378 * 100) / 100.0
C. (int)(45.378 * 100 / 100)
D. (int)(45.378) * 100 / 100.0
2.61 If you attempt to add an int, a byte, a long, and a double, the result will be a(n)
__________ value.
A. byte
B. int
C. long
D. double
Software Life Cycle
2.62 _____________ is a formal process that seeks to understand the problem and
document in detail what the software system needs to do.
A. Requirements specification
B. Analysis
C. Design
D. Implementation
E. Testing
2.63 _____________ seeks to analyze the data flow and to identify the system?s input
and output. When you do analysis, it helps to identify what the output is first, and then
figure out what input data you need in order to produce the output.
A. Requirements specification
B. Analysis
C. Design
D. Implementation
E. Testing
Common Errors and Pitfalls
2.64 Analyze the following code:
public class Test {
public static void main(String[] args) {
int n = 10000 * 10000 * 10000;
[Link]("n is " + n);
}
}
[Link]
A. The program displays n is 1000000000000.
B. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This
causes an overflow and the program is aborted.
C. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This
causes an overflow and the program continues to execute because Java does not report
errors on overflow.
D. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This
causes an underflow and the program is aborted.
E. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This
causes an underflow and the program continues to execute because Java does not
report errors on underflow.
[Link]
Selections
boolean Data Type
3.1 The "less than or equal to" comparison operator in Java is __________.
A. <
B. <=
C. =<
D. <<
E. !=
3.3 What is 1 + 1 + 1 + 1 + 1 == 5?
A. true
B. false
C. There is no guarantee that 1 + 1 + 1 + 1 + 1 == 5 is true.
if Statements
3.6 Which of the following code displays the area of a circle if the radius is positive?
A. if (radius != 0) [Link](radius * radius * 3.14159);
B. if (radius >= 0) [Link](radius * radius * 3.14159);
C. if (radius > 0) [Link](radius * radius * 3.14159);
D. if (radius <= 0) [Link](radius * radius * 3.14159);
[Link]
3.7 What is the output of the following code?
int x = 0;
if (x < 4) {
x = x + 1;
}
[Link]("x is " + x);
A. x is 0
B. x is 1
C. x is 2
D. x is 3
E. x is 4
[Link]
Common Errors and Pitfalls
3.10 Suppose x = 1, y = -1, and z = 1. What is the output of the following statement?
(Please indent the statement correctly first.)
if (x > 0)
if (y > 0)
[Link]("x > 0 and y > 0");
else if (z > 0)
[Link]("x < 0 and z > 0");
A. x > 0 and y > 0;
B. x < 0 and z > 0;
C. x < 0 and z < 0;
D. no output.
3.12 Suppose isPrime is a boolean variable, which of the following is the correct and
best statement for testing if isPrime is true?
A. if (isPrime = true)
B. if (isPrime == true)
C. if (isPrime)
D. if (!isPrime = false)
E. if (!isPrime == false)
[Link]
3.14 Analyze the following code:
Code 1:
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
int number = 45;
boolean even = (number % 2 == 0);
A. Code 1 has compile errors.
B. Code 2 has compile errors.
C. Both Code 1 and Code 2 have compile errors.
D. Both Code 1 and Code 2 are correct, but Code 2 is better.
[Link]
Case Study: Computing Body Mass Index
3.18 Suppose you write the code to display "Cannot get a driver's license" if age is less
than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the
following code is correct?
I:
if (age < 16)
[Link]("Cannot get a driver's license");
if (age >= 16)
[Link]("Can get a driver's license");
II:
if (age < 16)
[Link]("Cannot get a driver's license");
else
[Link]("Can get a driver's license");
III:
if (age < 16)
[Link]("Cannot get a driver's license");
else if (age >= 16)
[Link]("Can get a driver's license");
IV:
if (age < 16)
[Link]("Cannot get a driver's license");
else if (age > 16)
[Link]("Can get a driver's license");
else if (age == 16)
[Link]("Can get a driver's license");
A. I
B. II
C. III
D. IV
3.19 Suppose you write the code to display "Cannot get a driver's license" if age is less
than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the
following code is the best?
I:
if (age < 16)
[Link]("Cannot get a driver's license");
if (age >= 16)
[Link]("Can get a driver's license");
[Link]
II:
if (age < 16)
[Link]("Cannot get a driver's license");
else
[Link]("Can get a driver's license");
III:
if (age < 16)
[Link]("Cannot get a driver's license");
else if (age >= 16)
[Link]("Can get a driver's license");
IV:
if (age < 16)
[Link]("Cannot get a driver's license");
else if (age > 16)
[Link]("Can get a driver's license");
else if (age == 16)
[Link]("Can get a driver's license");
A. I
B. II
C. III
D. IV
Logical Operators
3.21 Which of the Boolean expressions below is incorrect?
A. (true) && (3 => 4)
B. !(x > 0) && (x > 0)
C. (x > 0) || (x < 0)
D. (x != 0) || (x = 0)
E. (-10 < x < 0)
[Link]
3.22 Which of the following is the correct expression that evaluates to true if the
number x is between 1 and 100 or the number is negative?
A. 1 < x < 100 && x < 0
B. ((x < 100) && (x > 1)) || (x < 0)
C. ((x < 100) && (x > 1)) && (x < 0)
D. (1 > x > 100) || (x < 0)
[Link]
3.12 Lottery
3.29 Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x-- >
10)?
A. 9
B. 10
C. 11
3.30 Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x++
> 10).
A. 9
B. 10
C. 11
3.31 Suppose x=10 and y=10. What is x after evaluating the expression (y >= 10) || (x-- >
10).
A. 9
B. 10
C. 11
3.32 Suppose x=10 and y=10. What is x after evaluating the expression (y >= 10) || (x++ >
10).
A. 9
B. 10
C. 11
[Link]
switch Statements
3.35 What is y after the following switch statement is executed?
int x = 3; int y = 4;
switch (x + 3) {
case 6: y = 0;
case 7: y = 1;
default: y += 1;
}
A. 1
B. 2
C. 3
D. 4
E. 0
switch (d) {
case 1.0: x = 1;
case 1.5: x = 2;
case 2.0: x = 3;
}
A. The program has a compile error because the required break statement is missing in
the switch statement.
B. The program has a compile error because the required default case is missing in the
switch statement.
C. The switch control variable cannot be double.
D. No errors.
Conditional Expressions
3.37 What is y after the following statement is executed?
x = 0;
y = (x > 0) ? 10 : -10;
A. -10
B. 0
C. 10
D. 20
E. Illegal expression
[Link]
3.38 Analyze the following code fragments that assign a boolean value to the variable
even.
Code 1:
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
even = (number % 2 == 0) ? true: false;
Code 3:
even = number % 2 == 0;
A. Code 2 has a compile error, because you cannot have true and false literals in the
conditional expression.
B. Code 3 has a compile error, because you attempt to assign number to even.
C. All three are correct, but Code 1 is preferred.
D. All three are correct, but Code 2 is preferred.
E. All three are correct, but Code 3 is preferred.
[Link]
3.41 What is y displayed in the following code?
public class Test1 {
public static void main(String[] args) {
int x = 1;
int y = x = x + 1;
[Link]("y is " + y);
}
}
A. y is 0.
B. y is 1 because x is assigned to y first.
C. y is 2 because x + 1 is assigned to x and then x is assigned to y.
D. The program has a compile error since x is redeclared in the statement int y = x = x +
1.
[Link]
Mathematical Functions, Characters, and Strings
Trigonometric Methdos
4.1 To obtain the sine of 35 degrees, use _______.
A. [Link](35)
B. [Link]([Link](35))
C. [Link]([Link](35))
D. [Link]([Link](35))
E. [Link]([Link](35))
[Link]
C. 4.0
D. 5.0
4.13 The Unicode of 'a' is 97. What is the Unicode for 'c'?
A. 96
B. 97
C. 98
D. 99
[Link]
A. [Link]("smith\exam1\[Link]");
B. [Link]("smith\\exam1\\[Link]");
C. [Link]("smith\"exam1\"[Link]");
D. [Link]("smith"\exam1"\[Link]");
[Link]
C. 2
D. 3
4.23 Which of the following is not a correct method in the Character class?
A. isLetterOrDigit(char)
B. isLetter(char)
C. isDigit()
D. toLowerCase(char)
E. toUpperCase()
Concatenating Strings
4.26 The expression "Java " + 1 + 2 + 3 evaluates to ________.
A. Java123
B. Java6
C. Java 123
D. java 123
E. Illegal expression
4.27 Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to
________.
[Link]
A. 66
B. B
C. A1
D. Illegal expression
4.28 Note that the Unicode for character A is 65. The expression 'A' + 1 evaluates to
________.
A. 66
B. B
C. A1
D. Illegal expression
Converting Strings
4.29 Which of the following is the correct statement to return JAVA?
A. toUpperCase("Java")
B. "Java".toUpperCase("Java")
C. "Java".toUpperCase()
D. [Link]("Java")
Comparing Strings
4.30 Suppose s1 and s2 are two strings. Which of the following statements or
expressions is incorrect?
A. String s3 = s1 - s2;
B. boolean b = [Link](s2);
C. char c = s1[0];
D. char c = [Link]([Link]());
4.31 Suppose s1 and s2 are two strings. What is the result of the following code?
[Link](s2) == [Link](s1)
A. true
B. false
4.32 "abc".compareTo("aba") returns ___________.
A. 1
B. 2
C. -1
D. -2
E. 0
[Link]
C. -1
D. -2
E. 0
4.38 To check if a string s contains the suffix "Java", you may write
A. if ([Link]("Java")) ...
B. if ([Link]("Java") >= 0) ...
C. if ([Link]([Link]() - 4).equals("Java")) ...
D. if ([Link]([Link]() - 5).equals("Java")) ...
E. if ([Link]([Link]() - 4) == 'J' && [Link]([Link]() - 3) == 'a' && [Link]([Link]()
- 2) == 'v' && [Link]([Link]() - 1) == 'a') ...
[Link]
Conversions between Strings and Numbers
4.39 The __________ method parses a string s to an int value.
A. [Link](s);
B. [Link](s);
C. [Link](s);
D. [Link](s);
[Link]
int i = 3434; double d = 3434;
[Link]("%5.1f %5.1f", i, d);
A. The code compiles and runs fine to display 3434.0 3434.0.
B. The code compiles and runs fine to display 3434 3434.0.
C. i is an integer, but the format specifier %5.1f specifies a format for double value. The
code has an error.
[Link]
Loops
The while Loop
5.1 How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
[Link]("Welcome to Java");
count++;
}
A. 8
B. 9
C. 10
D. 11
E. 0
int x = 0;
[Link]
while (x < 4) {
x = x + 1;
}
[Link]("x is " + x);
A. x is 0
B. x is 1
C. x is 2
D. x is 3
E. x is 4
[Link]
5.7 How many times will the following code print "Welcome to Java"?
int count = 0;
do {
[Link]("Welcome to Java");
} while (count++ < 10);
A. 8
B. 9
C. 10
D. 11
E. 0
5.8 How many times will the following code print "Welcome to Java"?
int count = 0;
do {
[Link]("Welcome to Java");
} while (++count < 10);
A. 8
B. 9
C. 10
D. 11
E. 0
5.9 What is the value in count after the following loop is executed?
int count = 0;
do {
[Link]("Welcome to Java");
} while (count++ < 9);
[Link](count);
A. 8
B. 9
C. 10
D. 11
E. 0
[Link]
A. The program has a compile error because the adjustment is missing in the for loop.
B. The program has a compile error because the control variable in the for loop cannot
be of the double type.
C. The program runs in an infinite loop because d < 10 would always be true.
D. The program compiles and runs fine.
5.12 Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100?
A:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum = i / (i + 1);
}
[Link]("Sum is " + sum);
B:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1);
}
[Link]("Sum is " + sum);
C:
double sum = 0;
[Link]
for (int i = 1; i <= 99; i++) {
sum += 1.0 * i / (i + 1);
}
[Link]("Sum is " + sum);
D:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += i / (i + 1.0);
}
[Link]("Sum is " + sum);
E:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1.0);
}
[Link]("Sum is " + sum);
A. BCD
B. ABCD
C. B
D. CDE
E. CD
5.14 Do the following two statements in (I) and (II) result in the same value in sum?
(I):
for (int i = 0; i < 10; ++i) {
sum += i;
}
(II):
for (int i = 0; i < 10; i++) {
sum += i;
}
A. Yes
B. No
[Link]
5.15 What is the output for y?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += i;
}
[Link](y);
A. 10
B. 11
C. 12
D. 13
E. 45
[Link]
5.19 Analyze the following code:
public class Test {
public static void main (String[] args) {
int i = 0;
for (i = 0; i < 10; i++);
[Link](i + 4);
}
}
A. The program has a compile error because of the semicolon (;) on the for loop line.
B. The program compiles despite the semicolon (;) on the for loop line, and displays 4.
C. The program compiles despite the semicolon (;) on the for loop line, and displays
14.
D. The for loop in this program is same as for (i = 0; i < 10; i++) { }; [Link](i +
4);
Nested Loops
5.20 How many times is the println statement executed?
for (int i = 0; i < 10; i++)
for (int j = 0; j < i; j++)
[Link](i * j)
A. 100
B. 20
C. 10
D. 45
[Link]
5.22 How many times is the println statement executed?
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
[Link](i * j);
A. 100
B. 20
C. 10
D. 45
Case Studies
5.25 What is y after the following for loop statement is executed?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += 1;
}
A. 9
B. 10
C. 11
D. 12
[Link]
Keywords break and continue
5.26 Will the following program terminate?
int balance = 10;
while (true) {
if (balance < 9)
break;
balance = balance - 9;
}
A. Yes
B. No
[Link]
5.29 What is the output after the following loop terminates?
int number = 25;
int i;
boolean isPrime = true;
for (i = 2; i < number; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
[Link]("i is " + i + " isPrime is " + isPrime);
A. i is 5 isPrime is true
B. i is 5 isPrime is false
C. i is 6 isPrime is true
D. i is 6 isPrime is false
[Link]
5.32 What balance after the following code is executed?
int balance = 10;
while (balance >= 1) {
if (balance < 9)
continue;
balance = balance - 9;
}
A. -1
B. 0
C. 1
D. 2
E. The loop does not end
5.33 What is the value of balance after the following code is executed?
int balance = 10;
while (balance >= 1) {
if (balance < 9)
break;
balance = balance - 9;
}
A. -1
B. 0
C. 1
D. 2
[Link]
Case Study: Displaying Prime Numbers
5.36 Suppose the input for number is 9. What is the output from running the following
program?
import [Link];
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter an integer: ");
int number = [Link]();
int i;
[Link]
A. The program does not compile because the Scanner input = new
Scanner([Link]); statement is inside the loop.
B. The program compiles, but does not run because the Scanner input = new
Scanner([Link]); statement is inside the loop.
C. The program compiles and runs, but it is not efficient and unnecessary to execute
the Scanner input = new Scanner([Link]); statement inside the loop. You should
move the statement before the loop.
D. The program compiles, but does not run because there is not prompting message
for entering the input.
[Link]
Methods
Defining a Method
6.1 Suppose your method does not return any value, which of the following keywords
can be used as a return type?
A. void
B. int
C. double
D. public
E. None of the above
Calling a Method
6.4 Arguments to methods always appear within __________.
A. brackets
B. parentheses
C. curly braces
D. quotation marks
6.5 Does the return statement in the following method cause compile errors?
public static void main(String[] args) {
int max = 0;
if (max != 0)
[Link](max);
else
return;
}
A. Yes
B. No
6.6 Does the method call in the following method cause compile errors?
public static void main(String[] args) {
[Link](2, 4);
}
[Link]
A. Yes
B. No
6.7 Each time a method is invoked, the system stores parameters and local variables in
an area of memory, known as _______, which stores elements in last-in first-out fashion.
A. a heap
B. storage area
C. a stack
D. an array
6.9 You should fill in the blank in the following code with ______________.
public class Test {
public static void main(String[] args) {
[Link]("The grade is ");
printGrade(78.5);
[Link]("The grade is ");
printGrade(59.5);
}
public static __________ printGrade(double score) {
if (score >= 90.0) {
[Link]('A');
}
else if (score >= 80.0) {
[Link]('B');
}
else if (score >= 70.0) {
[Link]('C');
}
else if (score >= 60.0) {
[Link]('D');
}
else {
[Link]('F');
}
}
}
[Link]
A. int
B. double
C. boolean
D. char
E. void
6.10 You should fill in the blank in the following code with ______________.
public class Test {
public static void main(String[] args) {
[Link]("The grade is " + getGrade(78.5));
[Link]("\nThe grade is " + getGrade(59.5));
}
public static _________ getGrade(double score) {
if (score >= 90.0)
return 'A';
else if (score >= 80.0)
return 'B';
else if (score >= 70.0)
return 'C';
else if (score >= 60.0)
return 'D';
else
return 'F';
}
}
A. int
B. double
C. boolean
D. char
E. void
[Link]
C. [Link]("number");
D. return number;
6.13 Given the following method, what is the output of the call nPrint('a', 4)?
static void nPrint(String message, int n) {
while (n > 0) {
[Link](message);
n--;
}
}
A. aaaaa
B. aaaa
C. aaa
D. invalid call
[Link]
Overloading Methods
6.15 Analyze the following code:
public class Test {
public static void main(String[] args) {
[Link](xMethod(5, 500L));
}
public static int xMethod(int n, long l) {
[Link]("int, long");
return n;
}
public static long xMethod(long n, long l) {
[Link]("long, long");
return n;
}
}
A. The program displays int, long followed by 5.
B. The program displays long, long followed by 5.
C. The program runs fine but displays things other than 5.
D. The program does not compile because the compiler cannot distinguish which
xmethod to invoke.
[Link]
6.17 Analyze the following code.
[Link]
A. The program has a compile error because the two methods m have the same
signature.
B. The program has a compile error because the second m method is defined, but not
invoked in the main method.
C. The program runs and prints 2 once.
D. The program runs and prints 2 twice.
6.22 (int)('a' + [Link]() * ('z' - 'a' + 1)) returns a random number __________.
A. between 0 and (int)'z'
B. between (int)'a' and (int)'z'
C. between 'a' and 'z'
D. between 'a' and 'y'
6.23 (char)('a' + [Link]() * ('z' - 'a' + 1)) returns a random character __________.
A. between 'a' and 'z'
B. between 'a' and 'y'
C. between 'b' and 'z'
D. between 'b' and 'y'
[Link]
6.24 Which of the following is the best for generating random integer 0 or 1?
A. (int)[Link]()
B. (int)[Link]() + 1
C. (int)([Link]() + 0.5)
D. (int)([Link]() + 0.2)
E. (int)([Link]() + 0.8)
6.26 __________ is to implement one method in the structure chart at a time from the
top to the bottom.
A. Bottom-up approach
B. Top-down approach
C. Bottom-up and top-down approach
D. Stepwise refinement
[Link]
Single-Dimensional Arrays
Array Basics
7.1 What is the representation of the third element in an array called a?
A. a[2]
B. a(2)
C. a[3]
D. a(3)
7.2 If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.
A. 3.4
B. 2.0
C. 3.5
D. 5.5
E. undefined
7.4 If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array
list is __________.
A. 0
B. 1
C. 2
D. 3
E. 4
[Link]
7.5 How many elements are in array double[] list = new double[5]?
A. 4
B. 5
C. 6
D. 0
A. The program has a compile error because the size of the array wasn't specified
when declaring the array.
B. The program has a runtime error because the array elements are not initialized.
C. The program runs fine and displays x[0] is 0.
D. The program has a runtime error because the array element x[0] is not defined.
[Link]
7.10 How can you initialize an array of two characters to 'a' and 'b'?
A. char[] charArray = new char[2]; charArray = {'a', 'b'};
B. char[2] charArray = {'a', 'b'};
C. char[] charArray = {'a', 'b'};
D. char[] charArray = new char[]{'a', 'b'};
7.11 What would be the result of attempting to compile and run the following code?
public class Test {
public static void main(String[] args) {
double[] x = new double[]{1, 2, 3};
[Link]("Value is " + x[1]);
}
}
A. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong
and it should be replaced by {1, 2, 3}.
B. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong
and it should be replaced by new double[3]{1, 2, 3};
C. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong
and it should be replaced by new double[]{1.0, 2.0, 3.0};
D. The program compiles and runs fine and the output "Value is 1.0" is printed.
E. The program compiles and runs fine and the output "Value is 2.0" is printed.
[Link]
D. 3
E. 4
[Link]
C. 6 2 3 4 5 1
D. 1 1 2 3 4 5
E. 2 3 4 5 6 1
[Link]
Copying Arrays
x = new int[2];
[Link]
for (int i = 0; i < [Link]; i++)
[Link](y[i] + " ");
}
}
A. The program displays 1 2 3 4
B. The program displays 0 0
C. The program displays 0 0 3 4
D. The program displays 0 0 0 0
x = new int[2];
x = new int[2];
[Link]
A. The program displays 1 2 3 4.
B. The program displays 0 0.
C. The program has a compile error on the statement x = new int[2], because x is final
and cannot be changed.
D. The elements in the array x cannot be changed, because x is final.
[Link]
Passing Arrays to Methods
7.28 When you pass an array to a method, the method receives __________.
A. a copy of the array
B. a copy of the first element
C. the reference of the array
D. the length of the array
Program I:
public class Test {
public static void main(String[] args) {
int[] list = {1, 2, 3, 4, 5};
reverse(list);
[Link]
for (int i = 0; i < [Link]; i++)
[Link](list[i] + " ");
}
list = newList;
}
}
Program II:
public class Test {
public static void main(String[] args) {
int[] oldList = {1, 2, 3, 4, 5};
reverse(oldList);
for (int i = 0; i < [Link]; i++)
[Link](oldList[i] + " ");
}
list = newList;
}
}
A. Yes
B. No
[Link]
}
list = newList;
}
}
A. The program displays 1 2 3 4 5.
B. The program displays 1 2 3 4 5 and then raises an
ArrayIndexOutOfBoundsException.
C. The program displays 5 4 3 2 1.
D. The program displays 5 4 3 2 1 and then raises an
ArrayIndexOutOfBoundsException.
[Link]
7.33 The JVM stores the array in an area of memory, called _______, which is used for
dynamic memory allocation where blocks of memory are allocated and freed in an
arbitrary order.
A. stack
B. heap
C. memory block
D. dynamic memory
7.36 The reverse method is defined in the textbook. What is list1 after executing the
following statements?
[Link]
7.37 The reverse method is defined in this section. What is list1 after executing the
following statements?
7.39 Which of the following statements are correct to invoke the printMax method in
Listing 7.5 in the textbook?
A. printMax(1, 2, 2, 1, 4);
B. printMax(new double[]{1, 2, 3});
C. printMax(1.0, 2.0, 2.0, 1.0, 4.0);
D. printMax(new int[]{1, 2, 3});
7.41 If a key is not in the list, the binarySearch method returns _________.
A. insertion point
B. insertion point - 1
C. -(insertion point + 1)
D. -insertion point
[Link]
Sorting Arrays
7.42 Use the selectionSort method presented in this section to answer this question.
Assume list is {3.1, 3.1, 2.5, 6.4, 2.1}, what is the content of list after the first iteration of
the outer loop in the method?
A. 3.1, 3.1, 2.5, 6.4, 2.1
B. 2.5, 3.1, 3.1, 6.4, 2.1
C. 2.1, 2.5, 3.1, 3.1, 6.4
D. 3.1, 3.1, 2.5, 2.1, 6.4
E. 2.1, 3.1, 2.5, 6.4, 3.1
7.43 Use the selectionSort method presented in this section to answer this question.
What is list1 after executing the following statements?
7.45 Assume int[] scores = {1, 20, 30, 40, 50}, what value does
[Link](scores, 30) return?
A. 0
B. -1
C. 1
D. 2
E. -2
[Link]
7.46 Assume int[] scores = {1, 20, 30, 40, 50}, what value does
[Link](scores, 3) return?
A. 0
B. -1
C. 1
D. 2
E. -2
7.47 Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of
[Link]([Link](scores))?
A. {1, 20, 30, 40, 50}
B. [1, 20, 30, 40, 50]
C. {1 20 30 40 50}
D. [1 20 30 40 50]
Command-Line Arguments
7.48 How can you get the word "abc" in the main method from the following call?
java Test 1 2 3
A. 3
B. 1
C. 1 2 3
D. 1 2
[Link]
7.50 Which code fragment would correctly identify the number of arguments passed
via the command line to a Java application, excluding the name of the class that is
being invoked?
A. int count = [Link];
B. int count = [Link] - 1;
C. int count = 0; while (args[count] != null) count ++;
D. int count=0; while (!(args[count].equals(""))) count ++;
7.53 Which of the following is the correct header of the main method?
A. public static void main(String[] args)
B. public static void main(String args[])
C. public static void main(String[] x)
D. public static void main(String x[])
E. static void main(String[] args)
[Link]
Multidimensional Arrays
Two-Dimensional Array Basics
8.1 Which of the following statements are correct?
A. char[][] charArray = {'a', 'b'};
B. char[2][2] charArray = {{'a', 'b'}, {'c', 'd'}};
C. char[2][] charArray = {{'a', 'b'}, {'c', 'd'}};
D. char[][] charArray = {{'a', 'b'}, {'c', 'd'}};
8.2 Assume double[][] x = new double[4][5], what are [Link] and x[2].length?
A. 4 and 4
B. 4 and 5
C. 5 and 4
D. 5 and 5
8.3 What is the index variable for the element at the first row and first column in array
a?
A. a[0][0]
B. a[1][1]
C. a[0][1]
D. a[1][0]
8.4 When you create an array using the following statement, the element values are
automatically initialized to 0.
8.5 How many elements are in array matrix (int[][] matrix = new int[5][5])?
A. 14
B. 20
C. 25
D. 30
[Link]
8.6 Analyze the following code:
8.7 Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, what are [Link] are x[0].length?
A. 2 and 1
B. 2 and 2
C. 3 and 2
D. 2 and 3
E. 3 and 3
8.8 Assume int[][] x = {{1, 2}, {3, 4, 5}, {5, 6, 5, 9}}, what are x[0].length, x[1].length, and
x[2].length?
A. 2, 3, and 3
B. 2, 3, and 4
C. 3, 3, and 3
D. 3, 3, and 4
E. 2, 2, and 2
[Link]
Processing Two-Dimensional Arrays
8.9 What is the output of the following program?
int v = values[0][0];
for (int row = 0; row < [Link]; row++)
for (int column = 0; column < values[row].length; column++)
if (v < values[row][column])
v = values[row][column];
[Link](v);
}
}
A. 1
B. 3
C. 5
D. 6
E. 33
int v = values[0][0];
for (int[] list : values)
for (int element : list)
if (v > element)
v = element;
[Link](v);
}
}
A. 1
B. 3
C. 5
D. 6
E. 33
[Link]
8.11 What is the output of the following program?
[Link]
8.13 What is the output of the following code?
[Link]
public static int m(int[] list) {
int v = list[0];
for (int i = 1; i < [Link]; i++)
if (v < list[i])
v = list[i];
return v;
}
}
A. 3 33
B. 1 1
C. 5 6
D. 5 33
E. 33 5
Multidimensional Arrays
8.16 Assume double[][][] x = new double[4][5][6], what are [Link], x[2].length, and
x[0][0].length?
A. 4, 5, and 6
B. 6, 5, and 4
C. 5, 5, and 5
D. 4, 5, and 4
[Link](data[1][0][0]);
}
}
[Link]
A. 1
B. 2
C. 4
D. 5
E. 6
[Link](ttt(data[0]));
}
return v;
}
}
A. 1
B. 2
C. 4
D. 5
E. 6
[Link]
Objects and Classes
[Link]
9.6 Which of the following statements are true?
A. A default constructor is provided automatically if no constructors are explicitly
declared in the class.
B. At least one constructor must always be defined explicitly.
C. Every class has a default constructor.
D. The default constructor is a no-arg constructor.
class A {
String s;
A(String s) {
this.s = s;
}
void print() {
[Link](s);
}
}
A. The program has a compile error because class A is not a public class.
B. The program has a compile error because class A does not have a default
constructor.
C. The program compiles and runs fine and prints nothing.
D. The program would compile and run if you change A a = new A() to A a = new A("5").
[Link]
9.9 Analyze the following code.
class TempClass {
int i;
public void TempClass(int j) {
int i = j;
}
}
public class C {
public static void main(String[] args) {
TempClass temp = new TempClass(2);
}
}
A. The program has a compile error because TempClass does not have a default
constructor.
B. The program has a compile error because TempClass does not have a constructor
with an int argument.
C. The program compiles fine, but it does not run because class C is not public.
D. The program compiles and runs fine.
public Test(String t) {
[Link]("Test");
}
[Link]
A. The program has a compile error because test is not initialized.
B. The program has a compile error because x has not been initialized.
C. The program has a compile error because you cannot create an object from the
class that defines the object.
D. The program has a compile error because Test does not have a default constructor.
E. The program has a runtime NullPointerException because test is null while executing
test.x.
9.12 The default value for data field of a boolean type, numeric type, object type is
___________, respectively.
A. true, 1, Null
B. false, 0, null
C. true, 0, null
D. true, 1, null
E. false, 1, null
[Link]
9.15 Analyze the following code.
public Test(String t) {
[Link]("Test");
}
9.16 Suppose TestCircle and Circle in Listing 9.1 are in two separate files named
[Link] and [Link], respectively. What is the outcome of compiling
[Link] and then [Link]?
A. Only [Link] compiles.
B. Only [Link] compiles.
C. Both compile fine.
D. Neither compiles successfully.
[Link]
Using Classes From the Java Library
9.18 Which of the following code A or B, or both creates an object of the Date class:
A:
public class Test {
public Test() {
new [Link]();
}
}
B:
public class Test {
public Test() {
[Link] date = new [Link]();
}
}
A. A.
B. B.
C. Neither
9.20 To obtain the distance between the points (40, 50) and (5.5, 4.4), use _________.
A. distance(40, 50, 5.5, 4.4)
B. new Point2D(40, 50).distance(5.5, 4.4)
C. new Point2D(40, 50).distance(new Point2D(5.5, 4.4))
D. new Point2D(5.5, 4.4).distance(40, 50)
E. new Point2D(5.5, 4.4).distance(new Point2D(40, 50))
[Link]
9.22 You should add the static keyword in the place of ? in Line ________ in the
following code:
void xMethod(int n) {
n++;
[Link]
}
}
A. The code has a compile error because xMethod does not return a value.
B. The code has a compile error because xMethod is not declared static.
C. The code prints n is 1.
D. The code prints n is 2.
E. The code prints n is 3.
9.26 What is the output of the second println statement in the main method?
public Foo() {
i++;
s++;
}
}
A. f2.i is 1 f2.s is 1
B. f2.i is 1 f2.s is 2
C. f2.i is 2 f2.s is 2
D. f2.i is 2 f2.s is 1
9.27 What is the output of the third println statement in the main method?
[Link]
Foo f3 = new Foo();
[Link]("f3.i is " + f3.i + " f3.s is " + f3.s);
}
public Foo() {
i++;
s++;
}
}
A. f3.i is 1 f3.s is 1
B. f3.i is 1 f3.s is 2
C. f3.i is 1 f3.s is 3
D. f3.i is 3 f3.s is 1
E. f3.i is 3 f3.s is 3
9.28 What code may be filled in the blank without causing syntax or runtime errors:
public MyClass() {
xMethod();
}
A. a static method
B. an instance method
C. a static method or an instance method
[Link]
9.30 Suppose the xMethod() is invoked from a main method in a class as follows,
xMethod() is _________ in the class.
Visibility Modifiers
9.31 To prevent a class from being instantiated, _____________________
A. don't use any modifiers on the constructor.
B. use the public modifier on the constructor.
C. use the private modifier on the constructor.
D. use the static modifier on the constructor.
class NClass {
int t;
private NClass() {
}
}
A. The program has a compile error because the NClass class has a private
constructor.
B. The program does not compile because the parameter list of the main method is
wrong.
C. The program compiles, but has a runtime error because t has no initial value.
D. The program compiles and runs fine.
[Link]
9.33 Analyze the following code:
9.34 Analyze the following code and choose the best answer:
[Link]
9.36 Suppose you wish to provide an accessor method for a boolean property finished,
what should the signature of this method?
A. public void getFinished()
B. public boolean getFinished()
C. public boolean isFinished()
D. public void isFinished()
[Link](
"[Link] = " + [Link]);
[Link]("times = "+ times);
}
[Link]
class Count {
int count;
Count(int c) {
count = c;
}
Count() {
count = 1;
}
}
A. 101
B. 100
C. 99
D. 98
[Link](
"[Link] = " + [Link]);
[Link]("times = "+ times);
}
class Count {
int count;
Count(int c) {
count = c;
}
[Link]
Count() {
count = 1;
}
}
A. 101
B. 100
C. 99
D. 98
E. 0
import [Link];
m2(date);
[Link]([Link]());
}
[Link]
Array of Objects
9.42 Given the declaration Circle[] x = new Circle[10], which of the following statement
is most accurate?
A. x contains an array of ten int values.
B. x contains an array of ten objects of the Circle type.
C. x contains a reference to an array and each element in the array can hold a reference
to a Circle object.
D. x contains a reference to an array and each element in the array can hold a Circle
object.
Scope of Variables
9.45 What is the output for the first statement in the main method?
k = i + j;
[Link]("k is " + k);
[Link]("j is " + j);
[Link]
}
}
A. i + j is 5
B. i + j is 6
C. i + j is 22
D. i + j is 23
9.46 What is the output for the second statement in the main method?
k = i + j;
[Link]("k is " + k);
[Link]("j is " + j);
}
}
A. k is 0
B. k is 1
C. k is 2
D. k is 3
9.47 What is the output for the third statement in the main method?
[Link]
k = i + j;
[Link]("k is " + k);
[Link]("j is " + j);
}
}
A. j is 0
B. j is 1
C. j is 2
D. j is 3
9.48 You can declare two variables with the same name in __________.
A. a method, one as a formal parameter and the other as a local variable
B. a block
C. two nested blocks in a method (two nested blocks means one being inside the
other)
D. different methods in a class
class Circle {
private double radius;
class Test {
private double i;
public Test(double i) {
this.t();
this.i = i;
}
[Link]
public Test() {
[Link]("Default constructor");
this(1);
}
9.51 Which of the following can be placed in the blank line in the following code?
A. this
B. Test
[Link]