Graded Quiz cs1102 3 6
Graded Quiz cs1102 3 6
Graded Quiz cs1102 3 6
Select one:
a. Exactly once.
The correct answer is: At least once, at the beginning of each iteration.
Question 2 Correct Mark 1.00 out of 1.00
Consider the following block of Java code. How many times will it output "Hello"?
for (int i = 1; i < 10; i--)
{
System.out.println("Hello");
}
Select one:
a. 0
b. 9
c. 1
d. 10
Notice the update, "i--". "i" starts less than 10 and just keeps getting smaller. (The loop is technically not
in nite. "i" will eventually reach the most-negative value that "int" can represent, and then it will wrap
around to the largest possible "int", ending the loop.) See Section 3.4.1 of Eck (2014).
Select one:
a. a thread
b. a program counter
c. an object
d. a message
e. an address
Question 4 Correct Mark 1.00 out of 1.00
Consider the following class de nition. Which variables can be used in the missing "println"
expression on line 20?
public class PrintStu
2 {
3 public static void main()
4 {
6 {
7 int i = -1;
8 System.out.println(_____);
9 }
10 int j = 1;
11 for (j = 0; j < 10; j++) {
12 System.out.println(_____);
13 }
14 {
15 int k;
16 for (k = 0; k < 10; k++) {
17 System.out.println(_____);
18 }
19 }
20 System.out.println(_____);
21 }
22 }
Select one:
a. Only "k"
c. Only "j"
e. Only "i"
"i" and "k" are no longer in scope. Only "j" is still in scope. See Section 3.1.1 of Eck (2014).
Question 5 Correct Mark 1.00 out of 1.00
Select one:
a. short
b. oat
c. double
d. int
e. long
Java automatically converts values from short, int, and long to oat. It does not automatically convert
double to oat. See Section 2.5.6 of Eck (2014).
Question 6 Correct Mark 1.00 out of 1.00
class Sum {
static int sum = 0;
static void add(int i) { i++; }
public static void main(String[] args) {
for (int i = 0; i < 10; i++) add(sum);
System.out.println(sum);
}
}
Select one:
a. 10
b. 9
c. 45
d. 0
e. 100
The "add" method increments the parameter "i", which is a copy of the argument "sum". The value of "sum"
never changes. See Section 4.3.2 of Eck (2014).
Which of the following keywords is useful for loops that should always execute at least once?
Select one:
a. switch
b. break
c. continue
d. do
e. while
Select one:
a. a class de nition
c. a comment
d. a variable declaration
e. a statement
Assume "test" is a boolean variable. Which of the following expressions is equivalent to "test ==
false"?
Select one:
a. test = true
b. test
c. test.equals(true)
d. !test
Question 10 Correct Mark 1.00 out of 1.00
Select one:
a. a class
b. a method (subroutine)
c. an object
d. a parameter
e. a statement
"println" is a method of the object "out". See Section 2.3.2 of Eck (2014).
Select one:
a. String
b. short
c. double
d. boolean
e. char
Question 12 Incorrect Mark 0.00 out of 1.00
Assume that the variables "x" and "y" both have type "int". Which one of the following is NOT a
boolean expression?
Select one:
a. x = y
b. x >= y
"x = y" is an int assignment statement. The other choices are all legal boolean expressions. See Sections
2.5.4 and 2.5.6 of Eck (2014).
Which of the following keywords is useful for skipping to the next iteration of a loop?
Select one:
a. break
b. do
c. continue
d. while
e. switch
Question 14 Incorrect Mark 0.00 out of 1.00
class Compute {
static int compute() { return 42; }
static int compute(int i) { return i+1; }
public static void main(String[] args) {
System.out.println(compute(compute(0)));
}
}
Select one:
a. 0
b. 42
c. 1
d. 2
e. 43
The inner call to "compute" has an argument, so it uses the second method de nition, which returns 0+1, or
1. This 1 becomes the argument to the outer call, which then returns 1+1, or 2. The rst de nition of
"compute" is never used. See Section 4.3.3 of Eck (2014).
Which one of the following lines of Java code does NOT include a method call?
Select one:
a. int x = compute(y);
b. if (i < 1) { compute(); }
c. compute(x,y,z);
d. compute();
e. void compute() {}
Answer is a method de nition, not a method call. See Sections 4.2.1 and 4.2.2 of Eck (2014).
1. Consider the following class de nition. Which variables can be used in the missing "println"
expression on line 12?
1 public class PrintStu
2 {
3 public static void main()
4 {
6 {
7 int i = -1;
8 System.out.println(_____);
9 }
10 int j = 1;
11 for (j = 0; j < 10; j++) {
12 System.out.println(_____);
13 }
14 {
15 int k;
16 for (k = 0; k < 10; k++) {
17 System.out.println(_____);
18 }
19 }
20 System.out.println(_____);
21 }
22 }
Select one:
b. Only "k"
c. Only "j"
d.
1. Only "i"
"i" is no longer in scope. "k" has not been declared yet. See Section 3.1.1 of Eck (2014).
Question 17 Correct Mark 1.00 out of 1.00
Select one:
c. Exactly once.
The correct answer is: Zero or more times, at the end of each iteration.
Select one:
c. a primitive type
Primitive types are not classes. See Section 2.3 of Eck (2014).
Question 19 Incorrect Mark 0.00 out of 1.00
Select one:
a. a method (subroutine)
b. an object
c. a parameter
d. a class
e. a statement
"out" is a static member object of the class "System". See Section 2.3.2 of Eck (2014).
Which one of the following is used in Java programming to handle asynchronous events?
Select one:
a. pragmatics
b. event handlers
c. reserved words
d. short circuits
e. protocols
← Self-Quiz Unit 3
Jump to...
Raymond Enokela
Consider the following Java program. Which one of the following does NOT describe
"MouseWhisperer"?
import java.awt.event.*;
import javax.swing.*;
public class MouseWhisperer extends JFrame implements MouseListener {
MouseWhisperer() {
super("COME CLOSER");
setSize(300,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
setVisible(true);
}
public void mouseClicked(MouseEvent e) { setTitle("OUCH"); }
public void mousePressed(MouseEvent e) { setTitle("LET GO"); }
public void mouseReleased(MouseEvent e) { setTitle("WHEW"); }
public void mouseEntered(MouseEvent e) { setTitle("I SEE YOU"); }
public void mouseExited(MouseEvent e) { setTitle("COME CLOSER"); }
public static void main(String[] args) { new MouseWhisperer(); }
}
Select one:
A "MouseWhisperer" object responds to mouse events ("addMouseListener"), not action events. See
Sections 6.1.3 and 6.3.1.
The correct answer is: A "MouseWhisperer" object listens for action events.
Question 2 Correct Mark 1.00 out of 1.00
Consider the following Java program, which one of the following best describes " avor"?
Select one:
b. a constructor
c. a method
d. an instance variable
e. a class variable
class Food {
Food() { System.out.println(flavor); }
String flavor = "bland";
}
class Pepper extends Food {
String flavor = "spicy";
}
public class Lunch {
public static void main(String[] args) {
Food lunch = new Pepper();
}
}
Select one:
a. spicy
c. bland
spicy
d. no output
e. bland
Unlike methods, constructors are NOT polymorphic. The compiler automatically calls the "Food"
constructor, which uses the " avor" member for class "Food", not the one for the subclass "Pepper". See
Sections 5.6.2 and 5.6.3.
import java.awt.event.*;
import javax.swing.*;
public class MouseWhisperer extends JFrame implements MouseListener {
MouseWhisperer() {
super("COME CLOSER");
setSize(300,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
setVisible(true);
}
public void mouseClicked(MouseEvent e) { setTitle("OUCH"); }
public void mousePressed(MouseEvent e) { setTitle("LET GO"); }
public void mouseReleased(MouseEvent e) { setTitle("WHEW"); }
public void mouseEntered(MouseEvent e) { setTitle("I SEE YOU"); }
public void mouseExited(MouseEvent e) { setTitle("COME CLOSER"); }
public static void main(String[] args) { new MouseWhisperer(); }
}
Select one:
a. this
b. java.awt.event
c. MouseListener
d. JFrame
e. MouseEvent
"this" is registered as a listener by the "addMouseListener" call. (Also, "this" is the only choice that is an
object. The other choices are packages, classes, or interfaces.) See Section 6.3.1.
import java.awt.event.*;
import javax.swing.*;
public class MouseWhisperer extends JFrame implements MouseListener {
MouseWhisperer() {
super("COME CLOSER");
setSize(300,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
setVisible(true);
}
public void mouseClicked(MouseEvent e) { setTitle("OUCH"); }
public void mousePressed(MouseEvent e) { setTitle("LET GO"); }
public void mouseReleased(MouseEvent e) { setTitle("WHEW"); }
public void mouseEntered(MouseEvent e) { setTitle("I SEE YOU"); }
public void mouseExited(MouseEvent e) { setTitle("COME CLOSER"); }
public static void main(String[] args) { new MouseWhisperer(); }
}
Select one:
a. addMouseListener(this);
b. super("COME CLOSER");
The correct answer is: public void mouseEntered(MouseEvent e) { setTitle("I SEE YOU"); }
Question 6 Correct Mark 1.00 out of 1.00
class Food {
String flavor = "bland";
void printFlavor() { System.out.println(flavor); }
}
class Pepper extends Food {
String flavor = "spicy";
}
public class Lunch {
public static void main(String[] args) {
Food lunch = new Pepper();
lunch.printFlavor();
}
}
Select one:
a. bland
spicy
b. spicy
d. no output
e. bland
The "Pepper" class inherits the "printFlavor" method but does not override it. Because member variables
are NOT polymorphic, the "printFlavor" method de ned in "Food" sees the member variable " avor" in
"Food", not the one in "Pepper". See Sections 5.6.1 and 5.6.2.
Consider the following Java program, which one of the following best describes "setFlavor"?
Select one:
a. a setter method
c. a class method
d. a static method
e. a public method
Consider the following Java program, which one of the following best describes "main"?
Select one:
a. a public method
b. a class method
d. a setter method
e. a static method
The correct answer is: gets executed when the program runs
Question 9 Incorrect Mark 0.00 out of 1.00
import java.awt.event.*;
import javax.swing.*;
public class Clicker extends JFrame implements ActionListener {
int count;
JButton button;
Clicker() {
super("Click Me");
button = new JButton(String.valueOf(count));
add(button);
button.addActionListener(this);
setSize(200,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
count++;
button.setText(String.valueOf(count));
}
public static void main(String[] args) { new Clicker(); }
}
Select one:
a. button
b. super
c. count
d. e
e. this
The "button" object registers "this" as an event listener in the call "addActionListener". See Section 6.1.3.
interface Food {
public void printFlavor();
}
class Pepper implements 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. no output
b. bland
spicy
c. bland
e. spicy
The "Pepper" class implements the "Food" interface. In particular, it de nes the "printFlavor" method, which
prints "spicy". See Section 5.7.1.
Consider the following Java program. Which line declares an instance object variable?
import java.awt.event.*;
import javax.swing.*;
public class Clicker extends JFrame implements ActionListener {
int count;
JButton button;
Clicker() {
super("Click Me");
button = new JButton(String.valueOf(count));
add(button);
button.addActionListener(this);
setSize(200,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
count++;
button.setText(String.valueOf(count));
}
public static void main(String[] args) { new Clicker(); }
}
Select one:
a. int count;
e. JButton button;
"int count;" also declares an instance variable, but it is a primitive type, not an object. See Sections 2.2.2 and
5.1.2.
A subclass method can _____ a superclass method with the same name and parameter types.
Select one:
a. implement
b. abstract
c. inherit
d. override
e. extend
Select one:
a. bland
c. spicy
d. no output
e. bland
spicy
The "Pepper" class inherits the "printFlavor" method from the abstract "Food" class. Abstract classes are
allowed to de ne concrete methods, and "Food" does de ne "printFlavor", though it de nes it to do
nothing: "{}". See Section 5.5.5.
Select one:
a. 2
b. sweet
c. spicy
d. 1
This program has a sneaky bug. Notice that the so-called setter "setFlavor" sets its formal parameter "s" to
the instance variable " avor" instead of the other way around. The value of " avor" does not change from
its initial value of "sweet". See Section 5.1.3 of Eck (2014).
Select one:
a. sweet
c. 1
d. spicy
e. 2
Select one:
a. smoky
b. sweet
c. false
d. spicy
e. true
Setting "chile = pepper" causes the two object variables to refer to the same single object. Because "chile"
and "pepper" refer to the same object, the avor set by "pepper.setFlavor" is seen by "chile.getFlavor". See
Section 5.1.2 of Eck (2014).
Select one:
a. override
b. inherit
c. implement
d. abstract
e. extend
Select one:
b. bland
c. sweet
d. no output
e. spicy
The "Pepper" constructor uses "this" to set the " avor" member variable to the " avor" parameter. See
Section 5.6.1.
The correct answers are: sweet, the program does not compile
Question 19 Incorrect Mark 0.00 out of 1.00
Consider the following Java program. Which statement sets the title of a window?
import java.awt.event.*;
import javax.swing.*;
public class Clicker extends JFrame implements ActionListener {
int count;
JButton button;
Clicker() {
super("Click Me");
button = new JButton(String.valueOf(count));
add(button);
button.addActionListener(this);
setSize(200,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
count++;
button.setText(String.valueOf(count));
}
public static void main(String[] args) { new Clicker(); }
}
Select one:
a. setVisible(true);
b. button.addActionListener(this);
c. super("Click Me");
d. add(button);
e. button.setText(String.valueOf(count));
The "JFrame" constructor takes the window name as a parameter, and "JFrame" is the superclass of
"Clicker". See Section 6.1.1.
class Food {
void flavor() { System.out.println("bland"); }
}
class Pepper extends Food {
void flavor() { System.out.println("spicy"); }
}
public class Lunch {
public static void main(String[] args) {
Pepper lunch = new Food();
lunch.flavor();
}
}
Select one:
a. spicy
b. no output
c. bland
spicy
d. bland
The program tries to initialize "lunch", a variable of type "Pepper", with an object of type "Food". A
superclass variable can be initialized to a subclass object, but not the other way around. See Section 5.5.3.
← Self-Quiz Unit 6
Jump to...