Examine the following file processing program. import java.io.*; import java.uti
ID: 3821112 • Letter: E
Question
Examine the following file processing program.
import java.io.*;
import java.util.Scanner;
public class FileProcessing
{
public static void main(String args[]) throws IOException
{
FileOutputStream outFile = null;
PrintWriter pWriter;
try
{
File inFile = new File("input.txt");
outFile = new FileOutputStream("output.txt");
pWriter = new PrintWriter(outFile);
int salary = 0;
Scanner sc = new Scanner (inFile);
while (sc.hasNextLine())
{
String line = sc.nextLine();
salary = Integer.parseInt(line) + 100;
pWriter.println(salary);
System.out.println(salary);
}
sc.close();
pWriter.close();
}
finally
{
if (outFile != null) {
outFile.close();
}
}
}
}
Contents of input.txt file: 1500
2200
7230
6500
1275
Concerning the aforementioned program code, select the correct answer.
(1) The pWriter object is associated with the file named input.txt .
(a) True (b) False
(2) The outFile file object uses the output.txt file in the APPEND mode.
(a) True (b) False
(3) When the above program is executed, the contents of the output.txt file will be the following.
1600
2300
7330
6600
1375
(a) True (b) False
(4) The outFile object is associated with the FileInputStream class.
(a) True (b) False
(5) The println() method is used by the PrintWriter object.
(a) True (b) False
import java.io.*;
import java.util.Scanner;
public class FileProcessing
{
public static void main(String args[]) throws IOException
{
FileOutputStream outFile = null;
PrintWriter pWriter;
try
{
File inFile = new File("input.txt");
outFile = new FileOutputStream("output.txt");
pWriter = new PrintWriter(outFile);
int salary = 0;
Scanner sc = new Scanner (inFile);
while (sc.hasNextLine())
{
String line = sc.nextLine();
salary = Integer.parseInt(line) + 100;
pWriter.println(salary);
System.out.println(salary);
}
sc.close();
pWriter.close();
}
finally
{
if (outFile != null) {
outFile.close();
}
}
}
}
Explanation / Answer
(1) The pWriter object is associated with the file named input.txt .
Ans) b) false
Reason: PrintWriter object is associated with outfile.txt file.
______________
(2) The outFile file object uses the output.txt file in the APPEND mode.
Ans) b) false
Reason: every time when we executes the program it will create output.txt file which overwrites if already existing .
______________
(3) When the above program is executed, the contents of the output.txt file will be the following.
Ans) True
Reason:
for every Iteration this statement will be executed .salary = Integer.parseInt(line) + 100;
so every time the value 100 is added to the salary and write that salary to the outfile.txt and as well as on console.
__________________
(4) The outFile object is associated with the FileInputStream class.
Ans) (b) False
FileInputStream class is associated with input.txt file
__________________
(5) The println() method is used by the PrintWriter object.
Ans) (a) True
Reason: In order to print the each value of the salary in new line we are using println() method.
__________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.