1) Consider the following Java program. Which statement updates the appearance of a button?
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(); }
}
a. add(button);
b. button.addActionListener(this);
c. button.setText(String.valueOf(count));
d. setVisible(true);
e. super("Click Me");
2) What is the output of the following Java program?
import java.util.*;
class ArrayGames {
public static void main(String[] args) {
ArrayList<Integer>
a = new ArrayList<Integer>();
for (int i = 1; i <=
5; i++) a.add(i);
Integer n = 3;
a.remove(n);
System.out.println(a.toString());
}
}
a. [0, 1, 2, 4, 5]
b. [1, 2, 3, 4, 5]
c. [1, 2, 3, 5]
d. [1, 2, 4, 5]
e. [4, 5]
3) 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");
}
a. 0
b. 1
c. 9
d. 10
e. Too many!
4) Which of the following keywords is useful for getting out of an infinite loop?
a. break
b. continue
c. do
d. switch
e. while
5) Consider the following Java program, which one of the following best describes "setFlavor"?
public class Food {
static int count;
private String flavor = "sweet";
Food() { count++; }
void setFlavor(String s) { flavor = s; }
String getFlavor() { return flavor; }
static public void main(String[] args) {
Food pepper = new
Food();
System.out.println(pepper.getFlavor());
}
}
a. a class variable
b. a constructor
c. a local object variable
d. an instance variable
e. a method
6) In a do-while loop, how many times does the continuation condition run (if the loop has no break, return, or System.exit calls)?
a. At least once, at the beginning of each iteration.
b. At least once, at the end of each iteration.
c. Exactly once.
d. Zero or more times, at the beginning of each iteration.
e. Zero or more times, at the end of each iteration.
7) Which one of the following is an event handler?
a. an event generator
b. an event listener
c. an event loop
d. an event method
e. java.awt.event
8) Consider the following Java program. Which one of the
following is a package?
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(); }
}
a. java.awt.event
b. JFrame
c. MouseEvent
d. MouseListener
e. this
9) Consider the following Java method. Which term best describes what this method computes?
static int doSomething(int[] a) {
int b = a[0];
for (int c : a) if (b > c) b = c;
return b;
}
a. average
b. maximum
c. minimum
d. sum
e. transpose
10) Consider the following Java program, what starts on line
2?
1 public class HelloWorld {
2 // My first program!
3 public static void main(String[] args)
{
4
System.out.println("Hello, World!");
5 }
6 }
a. a variable declaration
b. a statement
c. a method (subroutine) definition
d. a comment
e. a class definition
11) What is the output of the following Java program?
import java.util.*;
class ArrayGames {
public static void main(String[] args) {
int[] a = new
int[5];
System.out.println(Arrays.toString(a));
}
}
a. null
b. [0, 0, 0, 0, 0]
c. [5, 5, 5, 5, 5]
d. [null, null, null, null, null]
e. No output. It throws an exception.
12) Consider the following class definition. Which variables can
be used in the missing "println" expression on line 8?
1 public class PrintStuff
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 }
a. Only "i"
b. Only "j"
c. Only "k"
d. "i" and "j"
e. "j" and "k"
13) Consider the following Java statements.
int x = 3;
x = x++;
What is the value x is holding?
a. 0
b. 3
c. 4
d. 5
e. The question is moot. The statements have a syntax error.
14) Consider the following Java method. Which term best describes what this method computes?
static int doSomething(int[] a) {
int b = 0;
for (int c : a) b += c;
return b;
}
a. average
b. maximum
c. minimum
d. sum
e. transpose
15) Consider the following Java method, which term best describes "'("Hello, World!")"?
public static void main(String[] args) {
System.out.println("Hello, World!");
}
a. actual parameter or argument
b. formal parameter
c. method call
d. modifier
e. return type
16) Consider the following Java method, which term best
describes "void"?
public static void main(String[] args) {
System.out.println("Hello, World!");
}
a. actual parameter or argument
b. formal parameter
c. method call
d. modifier
e. return type
17) What is the output of the following Java program?
class Sum {
static int sum = 0;
static void add(int i) { sum += i; }
public static void main(String[] args) {
for (int i = 0; i <
10; i++) add(i);
System.out.println(sum);
}
}
a. 0
b. 9
c. 10
d. 45
e. 100
18) Consider the following Java program. Which object registers
event listeners?
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(); }
}
a. java.awt.event
b. JFrame
c. MouseEvent
d. MouseListener
e. this
19) What is the output of the following Java program?
class Food {
Food() { printFlavor(); }
void printFlavor() {
System.out.println("bland"); }
}
class Pepper extends Food {
void printFlavor() {
System.out.println("spicy"); }
}
public class Lunch {
public static void main(String[] args) {
Food lunch = new
Pepper();
}
}
a. bland
b. bland
spicy
c. no output
d. spicy
e. the program does not compile
Answer 1: D
D: this will make the button visible
Wrong Answers:
A: this will add the button
B: this will register the buttons
C: This will change the text
E: this will change the text
Answer 2:[1, 2, 4, 5]
Here we will remove the object 3 from the list
Answer 3:C
it is iterating from 1-9
Answer 4:break
if we use break we will comes out of the loop
Wrong Answer:
continue: it will skip the remaining statements in the
current iteration and move to next iteration
do: used for looping
Switch: this is select statement
while:used for looping
As per policy we can answer 1 question per post. Please post the remianing questions as separate post.Thanks
Get Answers For Free
Most questions answered within 1 hours.