1. In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print(“Enter a number: “); int number = keyboard.nextInt(); while (number < 100 || number > 500) { System.out.print(“Enter another number: “); number = keyboard.nextInt(); } a. Numbers less than 100 or greater than 500 b. Numbers in the range 100 – 499 c. Numbers in the range 100 – 500 d. The boolean condition can never be true 2. Given the following statement, which statement will write “Calvin” to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter(“DiskFile.txt”); System.out.println(diskOut, “Calvin”); b. DiskFile.println(“Calvin”); PrintWriter.println(“Calvin”); diskOut.println(“Calvin”); a. C. d. 3. Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached? a. while (inputFile != null) b. while (linputFile.EOF) C. while (inputFile.hasNext()) while (inputFile.nextLine == “”) d. {…}
4. Assuming that inputFile references a Scanner object that was used to open a file, which of the following statements will read an int from the file? a. int number = inputFile.next(); b. int number =inputFile.integer(); int number = inputFile.readint(); d. int number = inputFile.nextInt(); C. 5. Which of the following statements opens a file named MyFile.txt and allows you to read data from it? a Scanner inputFile = new Scanner(“MyFile.txt”); b. File file = new File(“MyFile.txt”); Scanner inputFile = new Scanner(file); c. File Scanner = new File(“MyFile.txt”); d. PrintWriter inputFile = new PrintWriter(“MyFile.txt”);
Answer
1)
2)
3)
4)
5)
NOTE: Please upvote the answer if you found it
helpful.
C. Numbers in the range 100 – 500
d. diskOut.println(“Calvin”);
C. while (inputFile.hasNext())
d. int number = inputFile.nextInt();
b. File file = new File(“MyFile.txt”); Scanner inputFile = new Scanner(file);