Final Exam (part 2) Flashcards
What is the value in count after the following loop is executed?
int count = 0;
do {
System.out.println(“Welcome to Java”);
} while (count++ < 9);
System.out.println(count);
10
Suppose the input for number is 9. What is the output from running the following program?
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(“Enter an integer: “);
int number = input.nextInt();
int i; boolean isPrime = true; for(i = 2; i < number && isPrime; i++) { if(number % i == 0) { isPrime = false; } } System.out.println("i is " + i); if (isPrime) System.out.println(number + " is prime"); else System.out.println(number + " is not prime"); } }
i is 4 followed by 9 is not prime
A break statement can be used only in a loop. T/F
false
What is sum after the following loop terminates?
int sum = 0;
int item = 0;
do {
item++;
sum += item;
if (sum > 4) break;
}
while (item < 5);
6
Suppose cond1 is a Boolean expression. When will this while condition be true?
while (cond1) …
in case cond1 is true
Is the following loop correct?
for(; ; );
yes
How many times will the following code print “Welcome to Java”?
int count = 0;
do {
System.out.println(“Welcome to Java”);
count++;
} while (count < 10);
10
A variable declared in the for loop control can be used after the loop exits. T/F
False
Each Java class must contain a main method. T/F
false
You may have a return statement in a void method.
True
(REPEAT QUESTION):
You may have a return statement in a void method.
True
The signature of a method consists of _____.
method name and parameter list
Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion.
a stack
Methods can be declared in any order in a class. T/F
true
(char)(‘a’ + Math.random() * (‘z’ - ‘a’ + 1)) returns a random character _______.
between ‘a’ and ‘z’
Analyze the following code.
public class Test {
public static void main (String[] args) {
System.out.println(m(2));
}
public static int m (int num) { return num; } public static void m (int num) { System.out.println(num); } }
The program has a compile error because the two methods m have the same signature.
Which of the following is correct to create an array?
int[] m = {1, 2, };
int[] m = {{1, 2}};
int[] m = {{1, 2}, {3, 4}};
int[] m = {1, 2, 3};
int[] m = {1, 2, 3};
Which correctly creates an array of five empty Strings?
String[] a = new String[5];
String[] a = {“”, “”, “”, “”, “”};
String[5] a;
String[] a = new String[5]; for (int i = 0; i < 5; a[i++] = null);
String[] a = {“”, “”, “”, “”, “”};
For the binarySearch method in Section 7.10.2, what is low and high after the first iteration of the while loop when invoking binarySearch(new int[]{1, 4, 6, 8, 10, 15, 20}, 11)?
low = 4
high = 26
Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 3) return?
-2
What is the output of the following code?
double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < myList.length: i++) {
if (myList[i] > max) {
max = myList[i];
indexOfMax = i;
}
}
System.out.println(indexOfMax);
1
When you pass an array to a method, the method receives ______.
the reference of the array
Show the output of the following code:
public class Test {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4, 5};
increase(x);
int[] y = {1, 2, 3, 4, 5}; increase(y[0]); System.out.println(x[0] + " " + y[0]); } public static void increase (int[] x) { for (int i = 0; i < x.length; i++) x[i]++; } public static void increase (int y) { y++; } }
2 1
Which of the following are valid array declarations?
char[] charArray = new char[26];
int[] words = new words[10];
char[] charArray = “Computer Science”;
double[3] nums = {3.5, 35.1, 32.0};
char[] charArray = new char[26];