Question

1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...

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

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
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....
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
What is the output of the following Java program? interface Food {     public void printFlavor();...
What is the output of the following Java program? 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. spicy b. no output c. the program does not compile d. bland e. bland spicy
Consider the following Java program. Describe what it does in response to specific operations of the...
Consider the following Java program. Describe what it does in response to specific operations of the mouse, and how it does it. (You are encouraged to run the program for yourself to test its behavior. Then read through the program carefully to understand how that behavior arises.) 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)...
What is the output of the following Java program? class Food { Food() { System.out.println(flavor); }...
What is the output of the following Java program? 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 b. the program does not compile c. bland spicy d. no output e. bland
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
What is the output of the following Java program? class Food { String flavor = "bland";...
What is the output of the following Java program? class Food { String flavor = "bland"; } class Pepper extends Food { String flavor = "spicy"; Pepper(String flavor) { this.flavor = flavor; } } public class Lunch { public static void main(String[] args) { Pepper pepper = new Pepper("sweet"); System.out.println(pepper.flavor); } } Select one: a. bland b. the program does not compile c. no output d. spicy e. sweet
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT