In: Computer Science
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
Question 1 :
Answer :c. minimum
Explanation :
Below screen shows the demonstration
******************************
Question 2 :
Answer :d. a comment
Explanation :
******************************
Question 3:
Answer :b. [0, 0, 0, 0, 0]
Explanation :
Below screen shows the output
******************************
Question 4:
Answer :a. Only "i"
Explanation :
******************************
Question 5:
Answer :b. 3
Explanation :
Below screen shows the output
******************************
Question 6 :
Answer :d. sum
Explanation :
Below screen shows the demonstration
******************************
Question 7:
Answer :c. method call
Explanation :
******************************
Question 8:
Answer :e. return type
Explanation :
******************************
Question 9:
Answer :d. 45
Explanation :
Below screen shows the output
******************************
Question 11:
Answer :d. spicy
Explanation :
Below screen shows the output
******************************