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. 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.
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.
You might also like to view...
Match each of the following File Explorer items to its use:
I. Quick Access Toolbar II. Ribbon III. Search box IV. Address bar V. Navigation pane A. contains icons for frequently used commands B. contains the path to the current location in the File Explorer window C. used to travel through the folders and drives available on a computer D. used to look for files located in the current File Explorer window E. used to perform common tasks on the items in the file list area
A(n) ________ uses one, two, or three letters to uniquely identify a column
Fill in the blank(s) with correct word