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’