LearnFrenzy
Sign in Take a free test

File Handling, IO and NIO · Exercise 3 of 4

File Handling, IO and NIO Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each Java program misuses the IO or NIO API in a way javac rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) {
        String config = Files.readString(Path.of("app.properties"));
        System.out.println(config.length());
    }
}
A Path.of needs two arguments
B Files.readString throws the checked IOException, which main neither catches nor declares
C readString returns a List
D length() is not a String method
Question page & discussion
Question 2 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) {
        Path report = "reports/march.csv";
        System.out.println(report.getFileName());
    }
}
A getFileName needs an argument
B A String is not a Path; create one with Path.of("reports/march.csv")
C Paths cannot contain slashes
D report must be final
Question page & discussion
Question 3 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) {
        Path p = new Path("data.txt");
        System.out.println(p);
    }
}
A data.txt must be absolute
B Path is an interface and cannot be instantiated; use Path.of("data.txt") (or Paths.get)
C Path needs two arguments
D println cannot print paths
Question page & discussion
Question 4 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        List<String> lines = Files.lines(Path.of("notes.txt"));
        System.out.println(lines.size());
    }
}
A notes.txt must exist at compile time
B Files.lines returns a Stream<String>, not a List; use Files.readAllLines, or collect the (closed) stream with toList()
C List cannot hold Strings
D size() does not exist
Question page & discussion
Question 5 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class Main {
    public static void main(String[] args) throws IOException {
        try (Reader r = new StringReader("hi")) {
            char c = r.read();
            System.out.println(c);
        }
    }
}
A StringReader cannot be used in try
B read() returns an int (so it can return -1 at the end); check for -1, then cast: char c = (char) value
C Readers return bytes
D println cannot print chars
Question page & discussion
Question 6 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.io.IOException;
import java.io.StringWriter;

public class Main {
    public static void main(String[] args) throws IOException {
        StringWriter w = new StringWriter();
        w.write(42.5);
        System.out.println(w);
    }
}
A StringWriter cannot be printed
B Writer.write accepts an int (a single char), a String or a char array – not a double; use w.write(String.valueOf(42.5)) or a PrintWriter
C 42.5 must be 42.5f
D write must be inside try
Question page & discussion
Question 7 Hard

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

public class Main {
    public static void main(String[] args) throws IOException {
        try (BufferedReader r = new BufferedReader(new StringReader("a\nb"))) {
            System.out.println(r.readLine());
            r = new BufferedReader(new StringReader("c"));
            System.out.println(r.readLine());
        }
    }
}
A readLine cannot be called twice
B Resource variables are implicitly final, so r cannot be reassigned inside the try block
C StringReader must be closed first
D BufferedReader needs a size
Question page & discussion
Question 8 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws IOException {
        Path f = Path.of("out.txt");
        Files.write(f, "hello world");
    }
}
A out.txt must exist
B Files.write takes a byte[] or an Iterable of lines, not a String; use Files.writeString(f, "hello world")
C Files.write needs an option
D Path.of cannot be stored
Question page & discussion
Question 9 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) {
        Path video = Path.of("media/intro.mp4");
        System.out.println(video.length() + " bytes");
    }
}
A mp4 files cannot be measured
B Path has no length() method (that belongs to java.io.File); use Files.size(video), which also throws IOException
C length must be called on a String
D Path.of cannot take folders
Question page & discussion
Question 10 Hard

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        try (InputStreamReader r = new InputStreamReader(new FileReader("data.txt"))) {
            System.out.println(r.read());
        }
    }
}
A FileReader needs a charset
B InputStreamReader converts a byte stream (InputStream) to characters; a FileReader is already a Reader, so there is no such constructor – wrap a FileInputStream instead, or just use the FileReader
C read() cannot be printed
D try-with-resources needs catch
Question page & discussion
Up next · Exercise 4File Handling Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links