Java Questions & Answers – Java.io Introduction

This section of our 1000+ Java MCQs focuses on java.io library of Java Programming Language.

1. Which of these packages contain classes and interfaces used for input & output operations of a program?
a) java.util
b) java.lang
c) java.io
d) all of the mentioned
View Answer

Answer: c
Explanation: java.io provides support for input and output operations.

2. Which of these class is not a member class of java.io package?
a) String
b) StringReader
c) Writer
d) File
View Answer

Answer: a
Explanation: None.

3. Which of these interface is not a member of java.io package?
a) DataInput
b) ObjectInput
c) ObjectFilter
d) FileFilter
View Answer

Answer: c
Explanation: None.
advertisement
advertisement

4. Which of these class is not related to input and output stream in terms of functioning?
a) File
b) Writer
c) InputStream
d) Reader
View Answer

Answer: a
Explanation: A File describes properties of a file, a File object is used to obtain or manipulate the information associated with a disk file, such as the permissions, time date, and directories path, and to navigate subdirectories.

5. Which of these is specified by a File object?
a) a file in disk
b) directory path
c) directory in disk
d) none of the mentioned
View Answer

Answer: c
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. Which of these is method for testing whether the specified element is a file or a directory?
a) IsFile()
b) isFile()
c) Isfile()
d) isfile()
View Answer

Answer: b
Explanation: isFile() returns true if called on a file and returns false when called on a directory.

7. What will be the output of the following Java code?

advertisement
  1.     import java.io.*;
  2.     class files 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             File obj = new File("/java/system");
  7.             System.out.print(obj.getName());
  8.         }
  9.     }

a) java
b) system
c) java/system
d) /java/system
View Answer

Answer: b
Explanation: obj.getName() returns the name of the file.
Output:

advertisement
$ javac files.java
$ java files
system

8. What will be the output of the following Java program? (Note: file is made in c drive.)

  1.     import java.io.*;
  2.     class files 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             File obj = new File("c:/java/system");
  7.             System.out.print(obj.getAbsolutePath());
  8.         }
  9.     }

a) java
b) system
c) c:/java/system
d) c:\java\system
View Answer

Answer: d
Explanation: None.
Output:

$ javac files.java
$ java files
c:\java\system

9. What will be the output of the following Java program considering that the filename sanfoundry.java exists and is readable but not writable?

  1.     import java.io.*;
  2.     class files 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             File obj = new File("sanfoundry.java");
  7.             System.out.print(obj.canWrite());
  8.             System.out.print(" " + obj.canRead());
  9.         }
  10.     }

a) true false
b) false true
c) true true
d) false false
View Answer

Answer: b
Explanation: None.
Output:

$ javac files.java
$ java files
false true

10. What will be the output of the following Java program? (Note: file is made in c drive.)

  1.     import java.io.*;
  2.     class files 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             File obj = new File("/java/system");
  7.             System.out.print(obj.getParent());
  8.             System.out.print(" " + obj.isFile());
  9.         }
  10.     }

a) java true
b) java false
c) \java false
d) \java true
View Answer

Answer: c
Explanation: getparent() giver the parent directory of the file and isfile() checks weather the present file is a directory or a file in the disk.
Output:

$ javac files.java
$ java files
\java false

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.