Question

1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

1) Consider the following Java program:

1 public class HelloWorld {
2     // My first program!
3     public static void main(String[] args) {
4         System.out.println("Hello, World!");
5     }
6 }

What is on line 1?

a. a variable declaration

b. a statement

c. a method (subroutine) definition

d. a comment

e. a class definition

2) Which one of the following does NOT describe an array?

a. It can be used in a for-each loop.

b. It has a numbered sequence of elements.

c. It provides efficient random access to its elements.

d. Its elements can be a primitive type.

e. The number of its elements can change.

3) What is the output of the following Java program?

abstract class Food {
    abstract void printFlavor();
}
class Pepper extends Food {
    void printFlavor() { System.out.println("spicy"); }
}
public class Lunch {
    public static void main(String[] args) {
        Food pepper = new Food();
        pepper.printFlavor();
    }
}

a. bland

b. bland
spicy

c. no output

d. spicy

e. the program does not compile

4) Which one of the following claims about Java is INCORRECT?

a. A class is a type.

b. An object belongs to a class.

c. An object is an instance of a class.

d. An object is a type.

e. "Object" is a class.

5) 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");

6) 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]

7) 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!

8) Which of the following keywords is useful for getting out of an infinite loop?

a. break

b. continue

c. do

d. switch

e. while

9) 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

10) 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.

11) 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

12) 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

13) 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

14) 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

15) 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.

16) 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"

17) 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.

18) 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

19) 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

20) 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

21) 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

22) 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

23) 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1: E
here we use class keyword to define the class
Wrong Answers:
A: for variable declaration we dont use class keyword
B: statement ends with ;
C: method definition we dont use class keyword
D: comments starts with // or /*

Answer 2: e. The number of its elements can change.
Size of the array is fixed so we ca't change it
Wrong Answers:
A: we can use array with for each
B: indexes starts from 0 to size-1
C: we can aaccess any element with index at constant time
Answer 3: e. the program does not compile
we can't create the instance for the abstract class
Answer 4:e. "Object" is a class.

an Object is not class it is instance of the class

As per policy we can answer 1 question per post. Please post the remianing questions as separate post.Thanks

Add a comment
Know the answer?
Add Answer to:
1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • There 2 parts to complete: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Q4 extends JFrame...

    There 2 parts to complete: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Q4 extends JFrame { private int xPos, yPos; public Q4() { JPanel drawPanel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); // paint parent's background //complete this: } }; drawPanel.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { //complete this :- set the xPos, yPos }    }); setContentPane(drawPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Mouse-Click Demo"); setSize(400, 250); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() {...

  • 2016 points output of following Java Program? public class Base { public final void show() {...

    2016 points output of following Java Program? public class Base { public final void show() { . System.out.println("Base::show() called"); public class Derived extends Base { public void show() { System.out.println("Derived::show() called"); } e lor rested in order public class Main { ects from and public static void main(String[] args) { Base b = new Derived(); b.show();

  • Help with a question in Java: What is the output from the following program? public class...

    Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }

  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

  • Complete the following Java program by writing the missing methods. public class 02 { public static...

    Complete the following Java program by writing the missing methods. public class 02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number: int n = scan.nextInt(); if (isOdd (n)) { //Print n to 1 System.out.println("Print all numbers from "+n+" to 1"); printNumToOne (n); } else { System.out.println(n + " is //End of main()

  • Consider the following Java program public class Foo i static class Node Object item; Node next:...

    Consider the following Java program public class Foo i static class Node Object item; Node next: Node (Object item, Node next) this. item = item; this.next next: public static void main (String] args) ( Node dat a nu ï ï ; dat a = new Node ("hello", data): data ne Node (5, data) while (data null) String s(String) data. item; System.out.println (s)

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • the answer must be in java. thank you Question 2 [8 points] Consider the class Class...

    the answer must be in java. thank you Question 2 [8 points] Consider the class Class A below: class Class A implements Serializable{ int a; static int b; transient int c; String s; // Constructor public Class A (int a, int b, int c, Strings){ this.a = a; this.b = b; this.c = c; this.s = s; Complete the class Test to test the serialization and deserialization of the objects of class Class A. State the possible variable values following...

  • What is the output of this program? class A { public int i; private int j;...

    What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class Inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } Java language!! // include explanation!

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT