1) 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
2) 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.
3) 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
4) 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
5) 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
6) 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
7) 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.
8) 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"
9) 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.
10) 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
11) 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
12) 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
13) 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
14) 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
15) 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: e. a method
setFlavour() is method used to set the data to the
private instance data floavor
Wrong Answers:
A: it is not static variable
B: Construcot name should be same as class name
C: it is not object
D: it is a method not
Answer 2:
a. At least once, at the beginning of each iteration.
Do while will check the condiition post iteration so
it will execute atleast once
Answer 3:
d. an event method
Event methods wil handle the evetnts
like actionPerformed ,mouseClicked
Answer 4:
a. java.awt.event
Wrong Answers:
JFrame is class
MouseEvent is class
MouseListener is interface
this is keyword
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.