Exam 13: File Input and Output
Exam 1: Creating Java Programs68 Questions
Exam 2: Using Data74 Questions
Exam 3: Using Methods, Classes, and Objects68 Questions
Exam 4: More Object Concepts67 Questions
Exam 5: Making Decisions70 Questions
Exam 6: Looping72 Questions
Exam 7: Characters, Strings, and the Stringbuilder73 Questions
Exam 8: Arrays74 Questions
Exam 9: Advanced Array Concepts74 Questions
Exam 10: Introduction to Inheritance70 Questions
Exam 11: Advanced Inheritance Concepts70 Questions
Exam 12: Exception Handling65 Questions
Exam 13: File Input and Output74 Questions
Exam 14: Introduction to Swing Components74 Questions
Exam 15: Advanced Gui Topics69 Questions
Exam 16: Graphics74 Questions
Exam 17: Applets, Images, and Sound72 Questions
Select questions type
Any of the file input or output methods in a Java program might throw an exception, so all the relevant code in the class is placed in a ____ block.
(Multiple Choice)
4.8/5
(31)
A data file can be used as a(n) ____ file when each record is stored in order based on the value in some field.
(Multiple Choice)
4.9/5
(35)
____________________ is an abstract class that contains methods for performing output.
(Short Answer)
4.8/5
(42)
When you store records in a file, it is often more useful to be able to access the 34th record rather than the 34th byte.
(True/False)
4.8/5
(43)
Match each term with the correct statement below.
-Writes text to an output stream, buffering the characters
(Multiple Choice)
4.9/5
(30)
Permanent storage is usually called computer memory or random access memory (RAM).
(True/False)
4.8/5
(37)
When a String is written, placing each record on a new line makes the output file easier to read and interpret. Since not all platforms use '\n' to separate lines, the BufferedWriter class has two alternatives: the newLine() method and the System.getProperty() method. Describe these two methods and how they operate.
(Essay)
4.9/5
(35)
When you perform input and output operations in an application, what is actually occurring with the bytes of information?
(Essay)
4.8/5
(38)
Explain the difference between an absolute path and a relative path. Provide an example of an absolute path to a file on a system, and then provide an example of a relative path.
(Essay)
4.9/5
(34)
Even if you simply want to display records in order based on their key field, you still need to create a random access file.
(True/False)
4.9/5
(39)
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
public class CreateOneRandomAccessRecord
{
public static void main(String[] args)
{
Path file =
Paths.get("C: \\Java\\Chapter.13\\RandomEmployees.txt");
String s = "002,Newmann,12.25" +
System.getProperty("line.separator");
______________________________________
byte[] data = s.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
FileChannel fc = null;
try
{
fc = (FileChannel)Files.newByteChannel(file, READ, WRITE);
fc.position(2 * RECSIZE);
fc.write(buffer);
fc.close();
}
catch (Exception e)
{
System.out.println("Error message: " + e);
}
}
}
The above program creates a single employee record for employee number 002 with a last name of Newmann and a pay rate of 12.25. In the shaded line provided, create a statement that assigns the length of the string to RECSIZE.
(Essay)
4.8/5
(40)
Match each term with the correct statement below.
-A collection of related records
(Multiple Choice)
4.9/5
(39)
class Rectangle
{
int length, breadth;
void show(int x, int y)
{
length = x;
breadth = y;
}
int calculate()
{
return(length * breadth);
}
}
public class EnteredValues
{
public static void main(String[] args)
{
Rectangle rectangle = new Rectangle();
_____________________________
_____________________________
rectangle.show(a, b);
System.out.println("You entered: " + a + " and " + b);
int area = rectangle.calculate();
System.out.println("The area of the rectangle is: " + area);
}
}
In the above program, the area of a rectangle is calculated by using two classes named Rectangle and EnteredValues. In the first class, two methods are used: show(int x, int y) and calculate(). The show() method takes two variables as input, and the calculate() method calculates the area of a rectangle.
In the second class, the main() method is declared. Inside this method, an object of a Rectangle class is created. The user will input two values and store the values in the variables. The entered values will be changed into integers using the parseInt() method. The variables are then passed in the show()method, and the area will be calculated by the calculate() method.
On the lines provided, create the parseInt() method statements to reformat the two String values.
(Essay)
4.7/5
(38)
What is the difference between volatile and nonvolatile storage in Java programming? Give examples of different storage situations.
(Essay)
4.8/5
(40)
Match each term with the correct statement below.
-The memory location where bytes are held after they are logically output but before they are sent to the output device
(Multiple Choice)
4.9/5
(39)
Match each term with the correct statement below.
-Returns the default line separator for a system
(Multiple Choice)
4.9/5
(35)
import java.nio.file.*;
import java.io.*;
public class ReadEmployeeFile2
{
public static void main(String[] args)
{
Path file =
Paths.get("C:\\Java\\Chapter.13\\Employees.txt");
String[] array = new String[3];
String s = "";
String delimiter = ",";
int id;
String name;
double payRate;
double gross;
final double HRS_IN_WEEK = 40;
double total = 0;
try
{
InputStream input = new
BufferedInputStream(Files.newInputStream(file));
BufferedReader reader = new
BufferedReader(new InputStreamReader(input));
System.out.println();
s = reader.readLine();
while(s != null)
{
________________________
id = Integer.parseInt(array[0]);
name = array[1];
payRate = Double.parseDouble(array[2]);
gross = payRate * HRS_IN_WEEK;
System.out.println("ID#" + id + " " + name +
" $" + payRate + " $" + gross);
total += gross;
s = reader.readLine();
}
reader.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
System.out.println(" Total gross payroll is $" + total);
}
}
The above code demonstrates a retrieved file where Strings are split into usable fields. In the blank line provided, write a String class split() method to split the String s that accepts an argument that identifies the field delimiter (in this example, a comma) and returns an array of Strings. The result should be assigned to the String[] array.
(Essay)
4.8/5
(37)
Match each term with the correct statement below.
-Clears any bytes that have been sent to a buffer for output but have not yet been output to a hardware device
(Multiple Choice)
4.7/5
(39)
A(n) ____________________ is a collection of fields that contain data about an entity.
(Short Answer)
4.8/5
(29)
Showing 41 - 60 of 74
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)