Intro to Java Programming Chapter 6

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

How many times will the following loop run? int i = 0; while (i < 10) { System.out.println(i); i++; } A. 0 B. 8 C. 9 D. 10

10

What is the output of the code snippet given below? String s = "12345"; int i = 1; while (i < 5) { System.out.print(s.substring(i, i + 1)); i+=2; } A. no output B. 13 C. 24 D. 2345

24

Which of the following loops executes 8 times? A. for (years = 8; years >= 0; years--) B. for(i = 0; i <= 7; i++) C. for(i = 1; i <= 10; i++) D. for (int i = 19; i >= 11; i--)

B. for(i = 0; i <= 7; i++)

Which of the statements about hand-tracing is true? A. You need a working program in order to do hand-tracing. B. Hand-tracing can be done with pseudocode. C. Hand-tracing will clearly show how long a program will take to execute. D. The goal of hand-tracing is to double check the answers of the running code.

Hand-tracing can be done with pseudocode.

Which of the following loop(s) could possibly not enter the loop body at all? I. the for loop II. the while loop III. the do loop A. I only B. I and II only C. II and III only D. I and III only

I and II only

Which loop does not check a condition at the beginning of the loop? I. the do loop II. the while loop III. the for loop A. I and II B. I and III C. I only D. III only

I only

The code snippet below checks whether a given number is a prime number. What will be the result of executing it? public static void main(String[] args) { int j = 2; int result = 0; int number = 0; Scanner reader = new Scanner(System.in); System.out.println("Please enter a number: "); number = reader.nextInt(); while (j <= number / 2) { if (number % j == 0) { result = 1; } j++; } if (result == 1) { System.out.println("Number: " + number + " is Not Prime."); } else { System.out.println("Number: " + number + " is Prime. "); } } A. The code snippet will not compile. B. The code snippet will display the desired result. C. The code snippet will display an incorrect result if the number is 1. D. The code snippet will loop forever.

The code snippet will display the desired result.

What is the best strategy for avoiding off-by-one errors? A. Randomly insert +1 or -1 until the program seems to work. B. Think through a couple of test cases and use the results to come up with a rationale for decisions. C. Always start a loop at 0. D. Never use the < operator by itself in a comparison test.

Think through a couple of test cases and use the results to come up with a rationale for decisions.

In the __________ loop header, you can include multiple update expressions, separated by commas, but it is not recommended. A. do B. while C. for D. if

for

Which of the following for loops is illegal? A. for (int i = 0; ; ) { } B. for (int i = 0) { } C. for (int i = 0, k = 1; ; i++) { } D. for ( ; ; )

for (int i = 0) { }

What are the values of i and j after the following code fragment runs? int i = 60; int j = 50; int count = 0; while (count < 5) { i = i + i; i = i + 1; j = j - 1; j = j - j; count++; } System.out.println("i=" + i + ", j=" + j); A. i = 1951, j = 0 B. i = 1951, j = 45 C. i = 65, j = 1 D. i = 65, j = 45

i = 1951, j = 0

How many times does the following code snippet display "Loop Execution"? for (int i = 0; i < 10; i++); { System.out.println("Loop Execution"); } A. ten times B. The code snippet does not run because of a compile error. C. infinite loop D. only one time

only one time

Which error type does the "off-by-one" error belong to? A. syntax error B. compile-time error C. run-time error D. infinite loop

run-time error


Kaugnay na mga set ng pag-aaral

ATI Maternal Newborn Practice A & B

View Set

Abeka 4th Grade, History Test 12, (Ch. 14-15)

View Set

Cellular Respiration Formative Assessment

View Set

Endocrine glands, hormones, and functions

View Set

Chapter 7 pretest Thinking and Intelligence

View Set

NC3 Exam 2 Practice Questions (3)

View Set