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();
}
}
}

a. The program does not compile because the Scanner input = new Scanner(System.in); statement is inside the loop.
b. The program compiles, but does not run because the Scanner input = new Scanner(System.in); statement is inside the loop.
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.
d. The program compiles, but does not run because there is not prompting message for entering the input.


c To receive input from the keyboard, you need to create an input object from the Scanner class. You should create this object only once in the program. Placing the statement Scanner input = new Scanner(System.in) in the loop causes it to be created multiple times, which is a bad practice and could lead to potential errors. So, the correct answer is C.

Computer Science & Information Technology

You might also like to view...

Each object has a set of events and methods, called __________ __________ associated with it.

Fill in the blank(s) with correct word

Computer Science & Information Technology

Provide steps in verifying hashes.

As mentioned before, a common use for hashes is to verify file integrity. Follow the steps below to use SHA-2-256 hashes to verify the integrity of sample.img, a file downloaded from the Internet.

Computer Science & Information Technology

When pointer notation is used in place of subscripts to access individual characters in a C-string, the resulting statements are ____.

A. more compact but less efficient B. less compact but more efficient C. more compact and more efficient D. less compact and less efficient

Computer Science & Information Technology

Style rules are prioritized by their specificity or ____________________.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology