Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Chapter Quizzes  arrow Chapter 10

Chapter 10

File I/O

This activity contains 15 questions.

Question 1.

After this program executes, what is in Data3.txt?
import java.io.*;

public class IO {

	IO() {
		create(3, sFile1);
		create(4, sFile2);
		try {
			boolean bFlag = false;
			BufferedReader br1 = 
				new BufferedReader(
					new FileReader(sFile1));
			BufferedReader br2 = 
				new BufferedReader(
					new FileReader(sFile2));
			PrintWriter os = 
				new PrintWriter(
					new 
				  FileOutputStream(sFile3));
			while (true) {
				String sLine =
					br1.readLine();
				if (sLine == null || 
					sLine.length() <= 0)
					break;
				int nValue1 = 
					Integer.parseInt(sLine);
				sLine = br2.readLine();
				if (sLine == null ||
					sLine.length() <= 0)
					break;
				int nValue2 = 
					Integer.parseInt(sLine);
				os.print(" " + 
					((bFlag = !bFlag) ?
						nValue1 : nValue2));
			}
			os.close();
		} catch (IOException e) {
			System.out.println(
					"IO Exception: " + e);
		}
	}

	private void create(int n, String sFile) {
		try {
			PrintWriter os = 
				new PrintWriter(
					new
				   FileOutputStream(sFile));
			int n2 = n * n;
			for (int i=-n2; i<=n2; i+= n)
				os.println(i);
			os.close();
		} catch (IOException e) {
			System.out.println(
					"IOException: " + e);
		}
	}

	private String sFile1 = "Data1.txt";
	private String sFile2 = "Data2.txt";
	private String sFile3 = "Data3.txt";

	public static void main(String[] args) {
		new IO();
	}

}

Open Hint for Question 1 in a new window.
 
End of Question 1


Question 2.

What does this program print?
import java.io.*;

public class IO {

	IO() {
		try {
			ObjectOutputStream os = 
				new ObjectOutputStream(
					new FileOutputStream(sFile));
			for (char c='A'; c<'K'; ++c)
				os.writeChar(c);
			os.close();
			ObjectInputStream is = 
				new ObjectInputStream(
					new FileInputStream(sFile));
			while (true) {
				char c = is.readChar();
				is.readChar();
				System.out.print(c);
			}
		} catch (EOFException e) {
			System.out.println();
		} catch (IOException e) {
			System.out.println(
					"IOException: " + e);
		}
	}

	private String sFile = "Data.txt";

	public static void main(String[] args) {
		new IO();
	}

}

Open Hint for Question 2 in a new window.
 
End of Question 2


Question 3.

What does this program print?
import java.io.*;

public class IO {

	final String FILE = "data.txt";

	IO() {
		write();
		read();
	}

	void write() {
		try {
			PrintWriter pw =
				new PrintWriter(
					new FileWriter(FILE));
			for (int n = 1; n <= 4; n++) {
				pw.print(n + ".");
				for (int k = 0; k < n; k++)
					pw.print(n);
				pw.println();
			}
			pw.println();
			pw.close();
		} catch (IOException e) {
			System.err.println(
				"Couldn't create " + FILE);
		}
	}

	void read() {
		try {
			String line;
			double result = 0, value;
			BufferedReader br =
				new BufferedReader(
					new FileReader(FILE));
			while ((line = br.readLine()).
					compareTo("") != 0) {
				System.out.println(
					"[" + line + "]");
				value =
				   Double.parseDouble(line);
				result += value;
			}
			br.close();
			System.out.println(result);
		} catch (IOException e) {
			System.err.println(
				"Couldn't read " + FILE);
		}
	}

	public static void main(String[] args) {
		new IO();
	}
}

Open Hint for Question 3 in a new window.
 
End of Question 3


Question 4.

What is in file "Text.txt?"
import java.io.*;

public class IO {

	IO(int n) {
		PrintWriter os = null;
		for (int i=0; i<3; i++) {
			try {
				os = new PrintWriter(
                 new FileOutputStream(sFile,
                 i >= 1));
				os.println("n=" + n + 
                           "   i=" + i);
				os.close();
			} catch (FileNotFoundException e) {
				System.out.println("File not found: " + e);
			}
		}
	}

	private String sFile = "Test.txt";

	public static void main(String[] args) {
		new IO(1);
		new IO(2);
	}

}

Open Hint for Question 4 in a new window.
 
End of Question 4


Question 5.

What is printed by this program?
import java.io.*;

class Info implements Serializable {
	private String s;
	private char   c;
	private int    n;
	Info(String ss, char cc, int nn) {
		s = ss;  c = cc;  n = nn;
	}
	public String toString() {
		return s + " | " + c + " | " + n;
	}
}

public class IO {

	final String FILE = "data.txt";

	IO() {
		write();
		read();
	}

	void write() {
		Info info1= new Info("Abc", 'x', 1);
		Info info2= new Info("Def", 'y', 2);
		Info info3= new Info("Ghi", 'z', 3);
		try {
			ObjectOutputStream os =
			  new ObjectOutputStream(
				new FileOutputStream(FILE));
			os.writeObject(info3);
			os.writeObject(info1);
			os.writeObject(info2);
		} catch (IOException e) {
			System.err.println(
				"Output Error: " + FILE);
		}
	}

	void read() {
		Info info;
		try {
			ObjectInputStream is = 
			  new ObjectInputStream(
				new FileInputStream(FILE));
			while (true) {
				info =
					(Info) is.readObject();
				System.out.println(info);
			}
		} catch (EOFException e) {
		} catch (IOException e) {
			System.err.println(
				"Input error: " + FILE);
		} catch (ClassNotFoundException e) {
			System.err.println(
				"Class error: " + FILE);
		}
	}

	public static void main(String[] args) {
		new IO();
	}
}

Open Hint for Question 5 in a new window.
 
End of Question 5


Question 6.

What is printed by this program?
import java.io.*;

class Info implements Serializable {
	private String s;
	private char   c;
	private transient int n;
	Info(String ss, char cc, int nn) {
		s = ss;  c = cc;  n = nn;
	}
	public String toString() {
		return s + " | " + c + " | " + n;
	}
}

public class IO {

	final String FILE = "data.txt";

	IO() {
		write();
		read();
	}

	void write() {
		Info info1= new Info("Abc", 'x', 7);
		Info info2= new Info("Def", 'y', 8);
		Info info3= new Info("Ghi", 'z', 9);
		try {
			ObjectOutputStream os =
			  new ObjectOutputStream(
				new FileOutputStream(FILE));
			os.writeObject(info2);
			os.writeObject(info1);
			os.writeObject(info3);
		} catch (IOException e) {
			System.err.println(
				"Output Error: " + FILE);
		}
	}

	void read() {
		Info info;
		try {
			ObjectInputStream is = 
			  new ObjectInputStream(
				new FileInputStream(FILE));
			while (true) {
				info =
					(Info) is.readObject();
				System.out.println(info);
			}
		} catch (EOFException e) {
		} catch (IOException e) {
			System.err.println(
				"Input error: " + FILE);
		} catch (ClassNotFoundException e) {
			System.err.println(
				"Class error: " + FILE);
		}
	}

	public static void main(String[] args) {
		new IO();
	}
}

Open Hint for Question 6 in a new window.
 
End of Question 6


Question 7.

What is printed?
import java.io.*;

public class IO {

	IO() {
		float[] flt = { 3, 1, 4, 1, 
						5, 9, 6, 8 };
		create(flt);
		display();
	}

	private void create(float[] flt) {
		try {
			ObjectOutputStream os =
			  new ObjectOutputStream(
				new FileOutputStream(
						sFile));
			os.writeObject(flt);
			os.close();
		} catch (IOException e) {
			System.out.println(
					   "IOException: " + e);
		}
	}

	private void display() {
		try {
			ObjectInputStream is =
			  new ObjectInputStream(
				new FileInputStream(sFile));
			float[] flt = (float[]) 
							is.readObject();
			is.close();
			for (int i=0; i

Open Hint for Question 7 in a new window.
 
End of Question 7


Question 8.

What is printed?
import java.io.*;

public class IO {

	IO() {
		int nValue = 0;
		try {
			PrintWriter os = null;
			os = new PrintWriter(
               new FileOutputStream(sFile));
			for (int i=1; i<6; i++)
			os.println(i * i);
			os.close();
			BufferedReader br = 
               new BufferedReader(
                   new FileReader(sFile));
			while (true) {
				String 
                    sLine = br.readLine();
				if (sLine == null ||
                    sLine.length() <= 0)
					break;
				nValue += Integer.
                            parseInt(sLine);
			}
		} catch (FileNotFoundException e) {
			System.out.println(
                    "File not found: " + e);
		} catch (IOException e) {
			System.out.println(
                    "IO Exception");
		}
		System.out.println("nValue=" +
                           nValue);
	}

	private String sFile = "Data.txt";

	public static void main(String[] args) {
		new IO();
	}

}

Open Hint for Question 8 in a new window.
 
End of Question 8


Question 9.

What is printed?
import java.io.*;

public class IO {

	IO() {
		long inc = create(4);
		display(inc);
	}

	private long create(final int n) {
		long inc = 0;
		try {
			RandomAccessFile ios = 
				new RandomAccessFile(sFile,
									 "rw");
			ios.setLength(0);
			double d = 1.1;
			for (int i=0; i= 0) {
				double d = ios.readDouble();
				System.out.print(d + "  ");
				if ((loc -= inc) >= 0)
					ios.seek(loc);
			}
			ios.close();
			System.out.println();
		} catch (IOException e) {
			System.out.println(
					   "IOException: " + e);
		}
	}

	private String sFile = "Data.dat";

	public static void main(String[] args) {
		new IO();
	}

}

Open Hint for Question 9 in a new window.
 
End of Question 9


Question 10.

What will be the contents of file data.txt after this code executes?
import java.io.*;

public class IO {

	IO() {
		final String FILE = "data.txt";

		try {
			PrintWriter pw =
				new PrintWriter(
					new FileWriter(FILE));
			for (int n = 1; n < 7; n++)
				pw.print(" " + n);
			pw.println();
			pw.close();
		} catch (IOException e) {
			System.err.println(
				"Couldn't create " + FILE);
		}
	}

	public static void main(String[] args) {
		new IO();
	}
}

Open Hint for Question 10 in a new window.
 
End of Question 10


Question 11.
What's the best way to check for an end-of-file condition when using the readLine method?


Open Hint for Question 11 in a new window.
 
End of Question 11


Question 12.

Which of the following are true?
  1. The principal difference between InputStreams and Readers is in the amount of processing that each does.
  2. The principal difference between OutputStreams and Writers is in the amount of processing that each does.
  3. Character streams deal with ASCII characters.
  4. Byte streams deal with Unicode bytes.
  5. System.in, System.out, System.error are the three predefined standard I/O streams.

Open Hint for Question 12 in a new window.
 
End of Question 12


Question 13.

Which statement(s) are true?
  1. The length of a file opened using RandomAccessFile is set to zero
  2. When you open a RandomAccessFile, the file pointer is set to the beginning of the file
  3. When you open a RandomAccessFile, the file pointer is set to the end of the file, ready for a write operation
  4. You can effectively empty a RandomAccessFile by using the setLength method

Open Hint for Question 13 in a new window.
 
End of Question 13


Question 14.

Which statement(s) are true?

When reading beyond the end of file in the case of a text file, using BufferedReader:

  1. an EOFException always is thrown
  2. an EOFException never is thrown

When reading beyond the end of file in the case of a binary file:

  1. an EOFException always is thrown
  2. an EOFException never is thrown

Open Hint for Question 14 in a new window.
 
End of Question 14


Question 15.
Why is it a good idea to close a file to which you have written, even though Java will close it for you when your program ends?


Open Hint for Question 15 in a new window.
 
End of Question 15





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page