JAVA I/O & NIO MCQs

  1. Which of the following is true about Java I/O streams?
    a) Streams are used to read/write data
    b) Byte streams handle binary data
    c) Character streams handle text data
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  2. What will be printed?
    1 import java.io.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 ByteArrayOutputStream out = new ByteArrayOutputStream();
    5 out.write("Eduinq".getBytes());
    6 System.out.println(out.toString());
    7 }
    8 }
    a) Eduinq
    b) Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq

  3. Which of the following is true about FileInputStream?
    a) Reads bytes from a file
    b) Throws FileNotFoundException if file not found
    c) Can be wrapped with BufferedInputStream
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  4. What will be printed?
    1 import java.io.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 File f = new File("Eduinq.txt");
    5 System.out.println(f.exists());
    6 }
    7 }
    a) true (if file exists)
    b) false (if file does not exist)
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — true (if file exists)

  5. Which of the following is true about BufferedReader?
    a) Reads text efficiently
    b) Provides readLine() method
    c) Wraps around Reader classes
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  6. What will be printed?
    1 import java.io.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 StringReader sr = new StringReader("Eduinq Portal");
    5 BufferedReader br = new BufferedReader(sr);
    6 System.out.println(br.readLine());
    7 }
    8 }
    a) Eduinq Portal
    b) Eduinq
    c) Portal
    d) Compilation error
    View Answer

    Correct answer: A — Eduinq Portal

  7. Which of the following is true about PrintWriter?
    a) Writes formatted text
    b) Provides println() method
    c) Can wrap OutputStream or Writer
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  8. What will be printed?
    1 import java.io.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 CharArrayWriter cw = new CharArrayWriter();
    5 cw.write("Eduinq");
    6 System.out.println(cw.toString());
    7 }
    8 }
    a) Eduinq
    b) Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq

  9. Which of the following is true about NIO Channels?
    a) Support non blocking I/O
    b) Work with Buffers
    c) Provide faster data transfer
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  10. What will be printed?
    1 import java.nio.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 ByteBuffer buffer = ByteBuffer.allocate(10);
    5 buffer.put((byte)65);
    6 buffer.flip();
    7 System.out.println((char)buffer.get());
    8 }
    9 }
    a) A
    b) 65
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — A

  11. Which of the following is true about FileChannel?
    a) Reads and writes from files
    b) Supports random access
    c) Can be memory mapped
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  12. What will be printed?
    1 import java.nio.file.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 Path path = Paths.get("Eduinq.txt");
    5 System.out.println(Files.exists(path));
    6 }
    7 }
    a) true (if file exists)
    b) false (if file does not exist)
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — true (if file exists)

  13. Which of the following is true about RandomAccessFile?
    a) Supports both read and write
    b) Allows file pointer movement
    c) Useful for large files
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  14. What will be printed?
    1 import java.io.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 RandomAccessFile raf = new RandomAccessFile("Eduinq.txt","rw");
    5 raf.writeBytes("Eduinq Portal");
    6 raf.seek(0);
    7 System.out.println(raf.readLine());
    8 raf.close();
    9 }
    10 }
    a) Eduinq Portal
    b) Eduinq
    c) Portal
    d) Compilation error
    View Answer

    Correct answer: A — Eduinq Portal

  15. Which of the following is true about ObjectOutputStream?
    a) Serializes objects
    b) Requires Serializable interface
    c) Throws NotSerializableException if not serializable
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  16. What will be printed?
    1 import java.io.*;
    2 class Eduinq implements Serializable {
    3 String name = "Eduinq Portal";
    4 }
    5 public class Test {
    6 public static void main(String[] args) throws Exception {
    7 ByteArrayOutputStream bos = new ByteArrayOutputStream();
    8 ObjectOutputStream oos = new ObjectOutputStream(bos);
    9 oos.writeObject(new Eduinq());
    10 oos.close();
    11 System.out.println("Serialized Eduinq");
    12 }
    13 }
    a) Serialized Eduinq
    b) Eduinq Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Serialized Eduinq

  17. Which of the following is true about ObjectInputStream?
    a) Deserializes objects
    b) Requires same class definition
    c) Throws ClassNotFoundException if class not found
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  18. What will be printed?
    1 import java.io.*;
    2 class Eduinq implements Serializable {
    3 String name = "Eduinq Portal";
    4 }
    5 public class Test {
    6 public static void main(String[] args) throws Exception {
    7 ByteArrayOutputStream bos = new ByteArrayOutputStream();
    8 ObjectOutputStream oos = new ObjectOutputStream(bos);
    9 oos.writeObject(new Eduinq());
    10 oos.close();
    11 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    12 ObjectInputStream ois = new ObjectInputStream(bis);
    13 Eduinq e = (Eduinq)ois.readObject();
    14 System.out.println(e.name);
    15 }
    16 }
    a) Eduinq Portal
    b) Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Portal

  19. Which of the following is true about BufferedInputStream?
    a) Improves performance by buffering
    b) Wraps InputStream
    c) Provides mark/reset support
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  20. What will be printed?
    1 import java.io.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 ByteArrayInputStream bis = new ByteArrayInputStream("Eduinq".getBytes());
    5 BufferedInputStream bin = new BufferedInputStream(bis);
    6 System.out.println((char)bin.read());
    7 }
    8 }
    a) E
    b) d
    c) u
    d) Compilation error
    View Answer

    Correct answer: A — E

  21. Which of the following is true about NIO Buffers?
    a) Store data for channels
    b) Have position, limit, capacity
    c) Support flip, clear, rewind operations
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  22. What will be printed?
    1 import java.nio.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 CharBuffer cb = CharBuffer.allocate(5);
    5 cb.put('E').put('d').put('u');
    6 cb.flip();
    7 System.out.println(cb.get());
    8 }
    9 }
    a) E
    b) d
    c) u
    d) Compilation error
    View Answer

    Correct answer: A — E

  23. Which of the following is true about Files.readAllLines()?
    a) Reads all lines into a List
    b) Uses UTF 8 by default
    c) Can throw IOException
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  24. What will be printed?
    1 import java.nio.file.*;
    2 import java.util.*;
    3 public class Test {
    4 public static void main(String[] args) throws Exception {
    5 Path path = Paths.get("Eduinq.txt");
    6 Files.write(path, Arrays.asList("Eduinq Portal"));
    7 List<String> lines = Files.readAllLines(path);
    8 System.out.println(lines.get(0));
    9 }
    10 }
    a) Eduinq Portal
    b) Eduinq
    c) Portal
    d) Compilation error
    View Answer

    Correct answer: A — Eduinq Portal

  25. Which of the following is true about Files.copy()?
    a) Copies file from source to destination
    b) Can replace existing file with option
    c) Throws IOException if fails
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  26. What will be printed?
    1 import java.nio.file.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 Path path = Paths.get("Eduinq.txt");
    5 Files.write(path,"Eduinq".getBytes());
    6 byte[] data = Files.readAllBytes(path);
    7 System.out.println(new String(data));
    8 }
    9 }
    a) Eduinq
    b) Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq

  27. Which of the following is true about FileWriter?
    a) Writes characters to file
    b) Can append if specified
    c) Throws IOException
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  28. What will be printed?
    1 import java.io.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 FileWriter fw = new FileWriter("Eduinq.txt");
    5 fw.write("Eduinq Portal");
    6 fw.close();
    7 System.out.println("Written");
    8 }
    9 }
    a) Written
    b) Eduinq Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Written

  29. Which of the following is true about FileReader?
    a) Reads characters from file
    b) Can be wrapped with BufferedReader
    c) Throws IOException
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  30. What will be printed?
    1 import java.io.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 FileWriter fw = new FileWriter("Eduinq.txt");
    5 fw.write("Eduinq");
    6 fw.close();
    7 FileReader fr = new FileReader("Eduinq.txt");
    8 System.out.println((char)fr.read());
    9 fr.close();
    10 }
    11 }
    a) E
    b) d
    c) u
    d) Compilation error
    View Answer

    Correct answer: A — E

  31. Which of the following is true about BufferedWriter?
    a) Buffers characters for efficient writing
    b) Provides newLine() method
    c) Wraps Writer classes
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  32. What will be printed?
    1 import java.io.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 BufferedWriter bw = new BufferedWriter(new FileWriter("Eduinq.txt"));
    5 bw.write("Eduinq Portal");
    6 bw.newLine();
    7 bw.write("Next Line");
    8 bw.close();
    9 System.out.println("Written with BufferedWriter");
    10 }
    11 }
    a) Written with BufferedWriter
    b) Eduinq Portal
    c) Next Line
    d) Compilation error
    View Answer

    Correct answer: A — Written with BufferedWriter

  33. Which of the following is true about DataInputStream?
    a) Reads primitive data types
    b) Wraps InputStream
    c) Throws IOException
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  34. What will be printed?
    1 import java.io.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 ByteArrayOutputStream bos = new ByteArrayOutputStream();
    5 DataOutputStream dos = new DataOutputStream(bos);
    6 dos.writeInt(100);
    7 dos.close();
    8 DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    9 System.out.println(dis.readInt());
    10 }
    11 }
    a) 100
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — 100

  35. Which of the following is true about File class?
    a) Represents file and directory pathnames
    b) Provides methods like exists(), delete(), length()
    c) Does not handle content directly
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  36. What will be printed?
    1 import java.io.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 File f = new File("Eduinq.txt");
    5 System.out.println(f.getName());
    6 }
    7 }
    a) Eduinq.txt
    b) Eduinq
    c) Portal
    d) Compilation error
    View Answer

    Correct answer: A — Eduinq.txt

  37. Which of the following is true about Path in NIO?
    a) Represents file path
    b) Immutable
    c) Provides methods like getFileName(), getParent()
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  38. What will be printed?
    1 import java.nio.file.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 Path path = Paths.get("Eduinq","Portal","File.txt");
    5 System.out.println(path);
    6 }
    7 }
    a) Eduinq\Portal\File.txt
    b) Eduinq/Portal/File.txt
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — Eduinq/Portal/File.txt

  39. Which of the following is true about Files.createFile()?
    a) Creates new empty file
    b) Throws exception if file exists
    c) Returns Path
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  40. What will be printed?
    1 import java.nio.file.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 Path path = Paths.get("EduinqNew.txt");
    5 Files.createFile(path);
    6 System.out.println("File Created");
    7 }
    8 }
    a) File Created
    b) EduinqNew.txt
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — File Created

  41. Which of the following is true about Files.delete()?
    a) Deletes file or directory
    b) Throws exception if file does not exist
    c) Returns void
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  42. What will be printed?
    1 import java.nio.file.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 Path path = Paths.get("EduinqDelete.txt");
    5 Files.createFile(path);
    6 Files.delete(path);
    7 System.out.println(Files.exists(path));
    8 }
    9 }
    a) true
    b) false
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — false

  43. Which of the following is true about FileVisitor?
    a) Used for walking file tree
    b) Provides methods preVisitDirectory, visitFile, postVisitDirectory
    c) Works with Files.walkFileTree()
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  44. What will be printed?
    1 import java.nio.file.*;
    2 import java.io.IOException;
    3 public class Test {
    4 public static void main(String[] args) throws IOException {
    5 Path path = Paths.get("EduinqDir");
    6 Files.createDirectory(path);
    7 System.out.println("Directory Created");
    8 }
    9 }
    a) Directory Created
    b) EduinqDir
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Directory Created

  45. Which of the following is true about Files.walk()?
    a) Returns Stream of paths
    b) Can traverse directory tree
    c) Supports filtering
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  46. What will be printed?
    1 import java.nio.file.*;
    2 import java.util.stream.*;
    3 public class Test {
    4 public static void main(String[] args) throws Exception {
    5 Path path = Paths.get("EduinqDir");
    6 Files.createDirectories(path);
    7 Stream<Path> stream = Files.walk(path,1);
    8 stream.forEach(System.out::println);
    9 }
    10 }
    a) EduinqDir
    b) EduinqDir and its contents
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — EduinqDir and its contents

  47. Which of the following is true about Files.lines()?
    a) Returns Stream of lines from file
    b) Uses UTF 8 by default
    c) Must be closed after use
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  48. What will be printed?
    1 import java.nio.file.*;
    2 import java.util.stream.*;
    3 public class Test {
    4 public static void main(String[] args) throws Exception {
    5 Path path = Paths.get("Eduinq.txt");
    6 Files.write(path,"Eduinq Portal".getBytes());
    7 Stream<String> lines = Files.lines(path);
    8 lines.forEach(System.out::println);
    9 }
    10 }
    a) Eduinq Portal
    b) Eduinq
    c) Portal
    d) Compilation error
    View Answer

    Correct answer: A — Eduinq Portal

  49. Which of the following is true about FileChannel.transferTo() and transferFrom()?
    a) Used for fast file copy
    b) Work with channels directly
    c) Avoids intermediate buffering
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  50. What will be printed?
    1 import java.nio.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 ByteBuffer buffer = ByteBuffer.allocate(5);
    5 buffer.put((byte)'E').put((byte)'d').put((byte)'u');
    6 buffer.flip();
    7 while(buffer.hasRemaining()) {
    8 System.out.print((char)buffer.get());
    9 }
    10 }
    11 }
    a) Edu
    b) Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Edu

Quick Links to Explore