Question

In: Computer Science

1) Consider the following Java method. Which term best describes what this method computes? static int...

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

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

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

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

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

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

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

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

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

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

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

Solutions

Expert Solution

Question 1 :

Answer :c. minimum

Explanation :

  • Given method doSomething() takes integer array as a parameter a[0]
  • will consider zeroth element into the array as smallest element that is a[0]
  • using if check for any element that is less than b then assign that element to the b and lastly return b

Below screen shows the demonstration

******************************

Question 2 :

Answer :d. a comment

Explanation :

  • In Java comment is declared with either // or /**/
  • variable declaration starts with any valid data type followed by variable name
  • line 3 that is main() is a method
  • Line 1 which is class declaration
  • and line 4 is valid statement

******************************

Question 3:

Answer :b. [0, 0, 0, 0, 0]

Explanation :

  • In java all array elements are initilized with 0

Below screen shows the output

******************************

Question 4:

Answer :a. Only "i"

Explanation :

  • Because i is the only variable that is declared before line no 8
  • All other variables j, k etc are declared after the line 8 hence which is not available on line 8
  • Hence all other options are invalid.

******************************

Question 5:

Answer :b. 3

Explanation :

  • In the given code snippet a variable i is declared and initilized with 3
  • x=x++; will first assign 3 to the x and the increment will take place because this is post increment.

Below screen shows the output

******************************

Question 6 :

Answer :d. sum

Explanation :

  • Given code snippet will take each element from the array a and will add that element and return sum of the all array elements

Below screen shows the demonstration

******************************

Question 7:

Answer :c. method call

Explanation :

  • ("Hello, World!") is the method call because parenthesis is used
  • "Hello, World!" Actual parameter or arguments are passed when method is called
  • Formal parameters are parameters given at the time of method definition
  • There is no any return type
  • public , private , protected etc are all modifiers

******************************

Question 8:

Answer :e. return type

Explanation :

  • void means main() method does not return any thing
  • Actual parameter or arguments are passed when method is called
  • Formal parameters are parameters given at the time of method definition
  • public , private , protected etc are all modifiers

******************************

Question 9:

Answer :d. 45

Explanation :

  • Given code snippet will add the value from 1 to 9 which is 45
  • variable sum is accessible in main() method because it is declared outside of main() method like globallly

Below screen shows the output

******************************

Question 11:

Answer :d. spicy

Explanation :

Below screen shows the output

******************************


Related Solutions

Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
How is this method coded in java? int differential(int[] post) differential computes the virtual differential for...
How is this method coded in java? int differential(int[] post) differential computes the virtual differential for a single post record and returns the differential. The differential is simply the difference between ups and downs. differential is virtual because it is not stored in a post record. /// Compute the differential between "ups" (at index 1) and "downs"   /// (at index 2). The differential is not maintained in the data but is a   /// virtual field derived by the calculation performed...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
In Java 1. Which term best describes a "A uses B" class relationship? word answer: 2....
In Java 1. Which term best describes a "A uses B" class relationship? word answer: 2. choose correct one: What are some of the ways a class can exhibit dependency? A. A class can invoke the methods of another class. B. A method within a class can accept an object of the same class. C. A class can rely on a constant defined in another class. D. All of the above. Which if the following statements is FALSE regarding arrays...
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method calculates grade/totalMarks and returns a letter grade based on the following scale: 0-50 = F 51-60 = D 61-70 = C 71-80 = B 81-100 = A
Which one of the following best describes: With the following data gathering method, it may be...
Which one of the following best describes: With the following data gathering method, it may be difficult to interpret the previously developed policies and procedures. Interviews Questionnaires Requirements workshops Document collection and analysis None of the above
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT