Docsity

CPS 180 - Java Programming Exercises and Solutions, Exams of Computer Science

Toggle sidebar
bg1
CPS 180 - E2 questions with complete solutions
How many times will the following code print "Welcome to
Java"?
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
} correct answer: 10
Analyze the following code.
int count = 0;
while (count < 100) {
// Point A
System.out.println("Welcome to Java!");
count++;
// Point B
}
// Point C correct answer: A. count < 100 is always true at
Point A
and
E. count < 100 is always false at Point C
How many times will the following code print "Welcome to
Java"?
int count = 0;
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CPS 180 - Java Programming Exercises and Solutions and more Exams Computer Science in PDF only on Docsity!

CPS 180 - E2 questions with complete solutions

How many times will the following code print "Welcome to Java"? int count = 0; while (count < 10) { System.out.println("Welcome to Java"); count++; } correct answer: 10 Analyze the following code. int count = 0; while (count < 100) { // Point A System.out.println("Welcome to Java!"); count++; // Point B } // Point C correct answer: A. count < 100 is always true at Point A and E. count < 100 is always false at Point C How many times will the following code print "Welcome to Java"? int count = 0;

while (count++ < 10) { System.out.println("Welcome to Java"); } correct answer: 10 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); correct answer: 10 How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 10); correct answer: 11 How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (++count < 10); correct answer: 10 What is the value in count after the following loop is executed? int count = 0;

for (int count = 0; count <= 10; count++) { System.out.println("Welcome to Java"); } correct answer: E. AB Which of the following loops correctly computes 1/2 + 2/3 + 3/

  • ... + 99/100? A: double sum = 0; for (int i = 1; i <= 99; i++) { sum = i / (i + 1); } System.out.println("Sum is " + sum); B: double sum = 0; for (int i = 1; i < 99; i++) { sum += i / (i + 1); } System.out.println("Sum is " + sum); C: double sum = 0; for (int i = 1; i <= 99; i++) { sum += 1.0 * i / (i + 1); } System.out.println("Sum is " + sum); D: double sum = 0; for (int i = 1; i <= 99; i++) { sum += i / (i + 1.0);

System.out.println("Sum is " + sum); E: double sum = 0; for (int i = 1; i < 99; i++) { sum += i / (i + 1.0); } System.out.println("Sum is " + sum); correct answer: E. CD The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; } correct answer: D. 1 3 5 7 9 Analyze the following code. double sum = 0; for (double d = 0; d < 10; sum += sum + d) { d += 0.1; } correct answer: D. The program complies and runs fine. What is y after the following for loop statement is executed? int y = 0; for (int i = 0; i < 10; ++i) { y += 1; } correct answer: B. 10

} correct answer: C. The program compiles despite the semicolon (;) on the for loop line, and displays 14. and D. The for loop in this program is same as for (i = 0; i < 10; i+ +) { }; System.out.println(i + 4); How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < i; j++) System.out.println(i * j) correct answer: D. 45 To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy? correct answer: A. add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0. What is the output after the following loop terminates? int number = 25; int i; boolean isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } } correct answer: D. i is 6 isPrime is false What is the output after the following loop terminates?

int number = 25; int i; boolean isPrime = true; for (i = 2; i < number; i++) { if (number % i == 0) { isPrime = false; break; } } correct answer: B. i is 5 isPrime is false What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; if (sum >= 4) continue; sum += item; } while (item < 5); correct answer: A. 6 Will the following program terminate? int balance = 10; while (true) { if (balance < 9) continue; balance = balance - 9;

// iteration } correct answer: B. n 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.print("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"); } } correct answer: D. i is 4 followed by 9 is not prime

Analyze the following code: import java.util.Scanner; public class Test { public static void main(String[] args) { int sum = 0; for (int i = 0; i < 100000; i++) { Scanner input = new Scanner(System.in); sum += input.nextInt(); } } } correct answer: C. The program compiles and runs, but it is not efficient and unnecessary to execute the Scanner input = new Scanner(System.in); statement inside the loop. You should move the statement before the loop. a continued statement can be used only in a loop correct answer: true in a for statement, if the continuation condition is blank, the condition is assumed to be correct answer: true you can always write a program without using break or continue in a loop correct answer: true a variable declared in the for loop control can be used after the loop exits correct answer: false