B &C Complete Study Questions And Answers Updated
Tests
Attempt A
Question 1
Correct
Mark 1.00 out of 1.00
Question text
What is the output of the following Java program?
import java.util.*;
class Array Games {
public static void main(String[] args) {
Array List<Integer> a = new ArrayList<Integer>();
for (int i = 1; i <= 5; i++) a.add(i);
System.out.println(a.get(a.get(2)));
}
}
Select one:
b.
c.
d.
Feedback
Your answer is correct.
The ArrayList has elements {1,2,3,4,5}. Because indices start at 0, "a.get(2)" returns the 3rd element, 3. Then
"a.get(3)" returns 4. See Section 7.3.1.
The correct answer is: 4
Question 2
Incorrect
Mark 0.00 out of 1.00
Question text
Consider the following Java method, which term best describes "'("Hello, World!")"?
,public static void main(String[] args) {
System.out.println("Hello, World!"); }
Select one:
actual parameter or argument
b. formal parameter
c. method call
d.
Feedback
Your answer is incorrect.
See Section 4.3.2 of Eck (2014).
The correct answer is: actual parameter or argument
Question 3
Correct
Mark 1.00 out of 1.00
Question text
What is output by the following Java program?
class Zap { static boolean zap() { return
true; } static int zap(boolean x) { return 0; }
static double zap(int x) { return 0.5; } static
String zap(double x) { return "Zap!"; } static
boolean zap(String x) { return false; } public
static void main(String[] args) {
System.out.println(zap(zap(zap(zap()))));
}
}
Select one:
true
b.
c. 0.5
d. Zap!
Feedback
Your answer is correct.
,The innermost call to "zap" has no argument, so it uses the first method definition, which returns the boolean
value true. The next call to "zap" thus gets a boolean argument, so it uses the second method definition,
which returns the int 0. The next call to "zap" gets this int argument, so it uses the third method definition,
which returns the double 0.0. The outmost call to "zap" thus gets a double argument, so it uses the fourth
method definition, which returns the String "Zap!" See Section 4.3.3 of Eck (2014).
The correct answer is: Zap!
Question 4
Correct
Mark 1.00 out of 1.00
Question text
Which one of the following claims about Java is INCORRECT?
Select one:
b. An object belongs to a class.
c.
d.
Feedback
Your answer is correct.
See Sections 5.1.1, 5.1.2, and 5.3.2 of Eck (2014).
The correct answer is: An object is a type.
Question 5
Correct
Mark 1.00 out of 1.00
Question text
What is the output of the following Java program?
interface Food {
public void printFlavor();
}
class Pepper extends Food {
public void printFlavor() { System.out.println("spicy"); }
}
public class Lunch { public static void
main(String[] args) { Food pepper =
new Pepper();
pepper.printFlavor();
, }
}
Select one:
a. bland
b. bland
spicy
c. no output
d. spicy
e. the program does not compile
Feedback
Your answer is correct.
"Food" is an interface, not a class. "Pepper" must implement it, not extend it. See Section 5.7.1.
The correct answer is: the program does not compile
Question 6
Incorrect
Mark 0.00 out of 1.00
Question text
Which one of the following values can a Java variable NOT have?
Select one:
a floating-point number
b. an integer
c. an object
d. the memory location of an object
Feedback
Your answer is incorrect.
Java object variables store references to objects, not the objects themselves. See Section 5.1.2 of Eck (2014).
The correct answer is: an object
Question 7
Incorrect
Mark 0.00 out of 1.00
Question text