CompSci.rocks

Text Files and Wrapper Classes

Free AP Computer Science A multiple-choice practice for topics 4.6–4.7, Unit 4 (Data Collections). Answer five random questions, check your work, and review the essential knowledge behind each one.

Unit 4: Data Collections. Topics 4.6–4.7. This set has 56 questions. Each time the page loads you get five of them, chosen at random. Pick an answer, press Check answer, and read the explanation for every choice. Reload the page or use New questions for a fresh five.

Topics assessed

  • 4.6 Using Text Files
  • 4.7 Wrapper Classes

Practice questions

Loading questions…

Essential knowledge for this set

These are the essential knowledge (EKS) statements from the AP Computer Science A Course and Exam Description that the 56 questions in this set assess, grouped by topic and learning objective. The table under each question links here.

Topic 4.6: Using Text Files

4.6.A — Develop code to read data from a text file.

  • 4.6.A.1 A file is storage for data that persists when the program is not running. The data in a file can be retrieved during program execution.
  • 4.6.A.2 A file can be connected to the program using the File and Scanner classes.
  • 4.6.A.3 A file can be opened by creating a File object, using the name of the file as the argument of the constructor.
    • File(String str) is the File constructor that accepts a String file name to open for reading, where str is the pathname for the file.
  • 4.6.A.4 When using the File class, it is required to indicate what to do if the file with the provided name cannot be opened. One way to accomplish this is to add throws IOException to the header of the method that uses the file. If the file name is invalid, the program will terminate.
  • 4.6.A.5 The File and IOException classes are part of the java.io package. An import statement must be used to make these classes available for use in the program.
  • 4.6.A.6 The following Scanner methods and constructor—including what they do and when they are used—are part of the Java Quick Reference:
    • Scanner(File f) is the Scanner constructor that accepts a File for reading.
    • int nextInt() returns the next int read from the file or input source if available. If the next int does not exist or is out of range, it will result in an InputMismatchException.
    • double nextDouble() returns the next double read from the file or input source. If the next double does not exist, it will result in an InputMismatchException.
    • boolean nextBoolean() returns the next boolean read from the file or input source. If the next boolean does not exist, it will result in an InputMismatchException.
    • String nextLine() returns the next line of text as a String read from the file or input source; can return the empty string if called immediately after another Scanner method that is reading from the file or input source.
    • String next() returns the next String read from the file or input source.
    • boolean hasNext() returns true if there is a next item to read in the file or input source; returns false otherwise.
    • void close() closes this scanner.
  • 4.6.A.8 The following additional String method— including what it does and when it is used—is part of the Java Quick Reference:
    • String[] split(String del) returns a String array where each element is a substring of this String, which has been split around matches of the given expression del.
  • 4.6.A.9 A while loop can be used to detect if the file still contains elements to read by using the hasNext method as the condition of the loop.
  • 4.6.A.10 A file should be closed when the program is finished using it. The close method from Scanner is called to close the file.

Topic 4.7: Wrapper Classes

4.7.A — Develop code to use Integer and Double objects from their primitive counterparts and determine the result of using these objects.

  • 4.7.A.1 The Integer class and Double class are part of the java.lang package. An Integer object is immutable, meaning once an Integer object is created, its attributes cannot be changed. A Double object is immutable, meaning once a Double object is created, its attributes cannot be changed.
  • 4.7.A.2 Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an int to an Integer and a double to a Double. The Java compiler applies autoboxing when a primitive value is:
    • passed as a parameter to a method that expects an object of the corresponding wrapper class
    • assigned to a variable of the corresponding wrapper class
  • 4.7.A.3 Unboxing is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer to an int and a Double to a double. The Java compiler applies unboxing when a wrapper class object is:
    • passed as a parameter to a method that expects a value of the corresponding primitive type
    • assigned to a variable of the corresponding primitive type
  • 4.7.A.4 The following class Integer method— including what it does and when it is used—is part of the Java Quick Reference:
    • static int parseInt(String s) returns the String argument as an int.
  • 4.7.A.5 The following class Double method— including what it does and when it is used—is part of the Java Quick Reference:
    • static double parseDouble(String s) returns the String argument as a double.