Friday, 10 November 2017

TCS Tech Lounge File IO

If we deleting a directory using File class delete method, the Directory can be full with files?
ANS: FALSE

which of the following stream read data in the form of bytes?
Ans: FileInputStream

Which methods of  InputStreamReader are declared as synchronized?
ANS: reset() & mark()

What is a buffer?
Ans: A section of main memory used as work area for input or output operations.

What is the difference between input.read() and input.read(array,offset,length) of BufferedInputStream?
ANS: Reads up to len bytes of data. A smaller number may be read using input.read(array,offset,length)

bufferedinputstream, which is the method inherited from FilterInputStream?
Ans:  read()

BufferedInputStream and BufferedOutputStream are the sub classes of _ _ _ _
ANS: FilterInputStream & FilterOutputStream

What id the return type of mark() method in BufferedInputStream?
ANS: void

Return Type of ensure Open() method in BufferedWriter Class ?.
ANS: void

The FileWriter assumes that you want to encode the bytes in the files using the default character encoding for the computer your application is running  on. You cannot change it.
ANS: False

For characters from the English (Roman) alphabet, how many bytes are used per character in the Program and how many are used on a disk file?
Ans:  program: 2 disk file: 1 byte    

File class implements the interfaces
Ans: Serializable and comparable

Import java.io.*;
Public class Dos{
Public static void main(String argv[]){
FileOutputStream fos = new FileOutputStream(“fos.dat”);
DataOutputStream dos = new DataOutputStream(fos);
BufferedOutputStream bos = new BufferedOutputStream(dos);
Dos.write(‘8’);
}
}

Ans:  The code will not compile because there is no try catch block. A BufferedOutputStream may take a DataOutPutStream as a constructor.


what class should be used to buffer the characters sent to a FileWriterStream?
Ans: BufferedWriter

Which is the legal ways of accessing a File named “file.tst” for reading.
Ans : FileReader fr = new FileReader(“file.tst”);

What is the disk file format for characters that is intended to work for all human languages?
ANS: UTF

___ method of File Class creates the directory named by this Abstract pathname
Ans: makdir()

What is an advantage in using the PrintWriter class ?
Ans: Its methods use the appropriate line termination character for your operating system

To set the write permission only for the owner of the file Use the ……………..method of the File class.
Ans: setWritable(true,true)

Subclass of Reader class is ?
Ans: FileReader

What are the constructors available in BufferedOutputStream ?
Ans: BufferedOutputStream(OutputStream), BufferedOutputStream(OutputStream,size)

Which of these is a method to clear all the data present in output buffers ?
Ans: flush()

Is the character data used inside a Java program exactly the same format as the character data in a text file written by Java?
Ans : No. Internally Java programs always use 16 bit char data, but text files are written in UTF format, which is different.

When the BufferedInputStream is created, what kind of buffer data structure is created ?
Ans: Array

Which character encoding is used in Reader class?
Ans: Unicode

Can a FileReader object be constructed that is not connected to any file ?
Ans: No—the constructor must specify a file

What can be appended to Writer class using append()?
Ans: character Array

Public class A{
Public A(){
System.out.println(“A”);
}
}
Public class B extends A implements Serializable{
Public B(){
System.out.println(“B”);
}
}
Public class Test{
Public static void main(String… args) throws Exception{
B b= new B();
ObjecOutputStream save = new ObjectOutputStream(new FileOutputStream(“datafile”));
Save.writeObject(b);
Save.flush();
ObjectInputStream restore =new ObjectInputStream(new FileInputStreamm(“datafile”));
B z =(B) restore.readObject();
}
}
What is the output?
Ans: ABA

Reader class is for reading Byte streams.
Ans: false
What is the data type of parameter passed in min()method of BufferedWriter Class ?
Ans: (int,int)
What does the following constructor do: FileWriter fw =new FileWriter(“myfile.text”);
Ans: Any previous myfile.txt in the current directory will be deleted the new one will be created
1.      ______method of File class tests whether the file denoted by this abstract pathname is a normal file.
Ans: isFile()

2. The Reader/ Writer class hierarchy is______

Ans: Character-oriented

3. Return type of ensureOpen() method in BufferedWriter class is void

Ans: true

4. How many parameter min() method takes in BufferedWriter class?

Ans: 2

5. FileReader is meant for reading streams of characters. For reading streams of raw bytes, use ____

Ans: FileInputStream

6. What class should be used to buffer the characters sent to a FileWriter stream?

Ans: BufferedWriter

7. Writer class is designed specifically for______characters

Ans: Unicode

8. In which Package Reader classes are defined?

Ans: java.io

9. Which of these is a method to clear all the data present in output buffers?

Ans: flush()

10. BufferedInputStream and BufferedOutputStream are the sub classes of_____

Ans: FilterInputStream & FilterOutputStream

11. Which of these method of InputStream is used to read integer representation of next available byte input?

Ans: read()

12. Which Exception is thrown by methods of Reader class?

Ans: IOException

13. When a file is opened for appending where does write() put next text?

Ans: At the end of the file

14. Which of these exception is thrown by close() and read() methods?

Ans: IOException 

15. Reader class is for reading Character streams

Ans: true

16. What will be the output of the below code


import java.io.*;

public class filesinputoutput{
public static void main(String[] args){
String obj="abc";
byte b[]=obj.getBytes();
ByteArrayInputStream obj1=new ByteArrayInputStream(b);
for(int i=0;i<2;i++){
int c;
while((c=obj1.read())!=-1){
if(i==0){
System.out.print((char)c);
}
}
}
}
}
Ans: abc

import java.io.*;
public class filesinputoutput{
public static void main(String[] args){
String obj="abc";
byte b[]=obj.getBytes();
ByteArrayInputStream obj1=new ByteArrayInputStream(b);
for(int i=0;i<2;i++){
int c;
while((c=obj1.read())!=-1){
if(i==0){
System.out.print(Character.toUpperCase(char)c);
obj2.write(1);
}
}
System.out.print(obj2);
}
}
}
ANS: AaBaaCaaa


17. How many parameter write() method takes in BufferedWriter class?

Ans: 1 or 3

18. _____method creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

Ans: createNewFile()

19. What class does readLine() belong to?

Ans: BufferedReader

20. Should FileReader be used to read a Java bytecode (*.class) file?

Ans: No---the bytes in bytecode files are not intended to be interpreted as characters

21. _____metho of File class creates the directory named by this Abstract pathname

Ans: mkdir()

22. File class implements the interfaces

Ans: Comparable

23. What file method creates a new disk directory

Ans: mkdir()

24. To set the write permission only for the owner of the file use the ____method of the File class

Ans: setWritable(true,true)

25. Which operation is usually more reliable: input or output?

Ans: Output

26. _____method creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist

Ans: createNewFile()

27. To set the write permission only for all the users of the file use the_____method of the File class

Ans: setWritable(true,false)

28. Which package holds the file class?

Ans: java.io

29. Which of these is used to perform all input and output operations in java?

Ans: streams

30. _____method of File class enables read permission for all the users of the file

Ans: setReadable(true,false)

31. What method of file is used to test if a file or directory exists?

Ans: exists()

32. Which of the following is used in a C-style input loop?

Ans: while((line=source.readLine())!=null)

33. _____method of File class is used to rename the file

Ans: renameTo(File dest)

34. If deleting a directory using File class delete method the Directory must be empty

Ans: True

35. _____method of File class enables read permission for only owner of the file

Ans: setReadable(true,true)

36. ______method of File class returns the time that the file denoted by this abstract pathname was last modified

Ans: lastModified()

37.______method of File class tests whether the file named by this abstract pathname is a hidden file

Ans: isHidden()

38.______method of File class is used to retrieve the Absolute path of the file

Ans: getAbsolutePath()

39. Which of these is a method to clear all the data present in output buffers?
Ans: flush()

40. BufferedInputStream and BufferedOutputStream are the sub-classes of_______
Ans: FilterInputStream and FilterOutputStream

41. Which of these method of InputStream is used to read integer representation of next available byte input?
Ans: read()

42. Which Exception is thrown by methods of Reader class?
Ans: IOException

43. When a file is opened for appending where does the write() put new text?
Ans: At the end of the file

44. How many parameter write() method takes in BufferedWriter Class?
Ans: 1 or 3

45. _____method creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
Ans: createNewFile()

46. What is the return type of skip() method in BufferedReader class?
Ans: long

47. For characters from the English (Roman) alphabet, how many bytes are used per character in the program and how many are used on a disk file?

Ans: Program: 2 bytes Disk file: 1 byte

48. What does the following constructor do: FileWriter fw=new FileWriter("myfile.txt");
Ans: Any previous myfile.txt in the current directory will be deleted and the new one will be created

49. It is possible to use the File class to list the contents of the current working directory

Ans: True

50. The decoding layer of InputStream transforms bytes to chars according to an _______standard

Ans: encoding

51. What is the return type of min() method in BufferedWriter class?
Ans: int

52. _______method of File class tests whether the file denoted by this abstract pathname is a Directory
Ans: isDirectory()

53. Reader class is designed specifically for Unicode characters
Ans: true

54. Which of these is a type of stream in Java?

Ans: Byte stream

55. What is the data type of parameter passed in min() method of BufferedWriter class?

Ans: (int, int)

56. What is the name of the abstract base class for streams dealing with general purpose input
Ans: InputStream

When does FileOutputStream(File  file) throw FileNotFoundException
ANS:  Both statements 1 & 2 ( if the file exists but is a directory rather than a regular file, if file does not exist but cannot be created )



Given file is a File object, which of the following are legal statements to create a new file?
(A)   File.crearte();
(B)   FileOutputStream fos=new FileOutputStream(file);
(C)   FileWriter out = new FileWriter(file);
(D)  FileInputStream fis =new FileInputStream(file);
(E)   RandomAccessFile raf = new RandomAccessFile(file);
ANS: (B), (C) & (E)

Class Input_Output{
Public static void main(String args[]) throws IOException{
String str;
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
Do{
Str = (char)obj.readLine();
System.out.print(str);
}while(!str.equals(“strong”));
}
}
ANS: Indefinitely Print hello stop world

Which abstract class is the super class of all classes used for reading bytes?
ANS: InputStream

Is the following statement true? “Only object that support the java.io.Serializable” or java.io.Externalizable interface cann be read from streams
ANS: TRUE

If the read() method of InputStreamReader returns -1, it means
ANS: It reached at end of file

Is input and output with binary files slower or faster than with character-oriented files?
ANS: Faster--because binary data is more compact and does not need translation.

What is the size of writeBufferSize?
ANS: 1024.0


Which of these methods is used to write into a file?
ANS: write()

The File class contains a method that changes the current working directory
ANS: FALSE

Java.io.File is an abstract representation of file and directory path names.
ANS: TRUE

An InputStreamReader is a bridge from byte streams to character streams
ANS: TRUE
_ _ _ _  signals that an I/O exception of some sort has ocured.
ANS: IOException

The classes which contain the word Byte are byte oriented streams.
ANS: false

Method to close a I/O stream?
ANS: close()

Reader class is a _ _ _ _ _.
ANS: Abstract class

Public class A{}
Public class B implements Serializable{
A a = new A();
Public static void main(String..args){
B b = new B();
Try{
FileOutputStream fs = new FileFormatStream(“b.ser”);
ObjectOutputStream os=new ObjectoutputStream(fs);
Os.writeObject(b);
Os.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
What is the output for the above code?

ANS:  Compilation fails

String[] list() method of File class returns_ _ _ _ if the path name Does not denote a directory
ANS: null

Which of these class can be used to implement input stream that uses a character array as the source?
ANS: CharArrayReader

What is the ancestor class of streams that do character-oriented output?
ANS:  Writer

If not defined, the default encoding identifier is 8859_1(ISO-LATIN-1).
ANS: TRUE

Value returned by the read method in Reader class, on reaching end of file?
ANS:  -1

Which of the following is not a constructor in Reader class?
ANS: Reader(char[]ch)

The mark operation(java.io.BufferedInputStream) remembers a point in the input stream. What does reset operation do?
ANS: put b not exactly correct

What method is used to signify that a file is no longer needed in the program?
ANS: close()

_ _ _ method of File class tests whether the application can modify the file denoted by this abstract pathname.
ANS: canWrite()

_ _ _ method of File class deletes the file or directory denoted by this Abstract pathname.
ANS:  delete()

LineNumberReader is subclass of BufferedReader
ANS: True

public long skip(long n) Of FileinputStream throws IoException  when _ _ _
ANS: if n is negative

Can a stream act as a data source for another stream?
ANS:  Yes. Some streams can be connected together.

import java.util.*;
class filesinputoutput{
            public static void main(String[]arg){
                        InputStream obj = new FileInputStream("inputoutput.java");
                        System.out.print(obj.available());
            }
}
ANS: printsnumber of bytes in file

Name the class that allows reading of binary representation of Java primitives from an input byte stream?
ANS: DataInputStream

Can data flow through a given stream in both direction?
ANS: No; a stream has one direction only, input or output.

Can a text file written by a Java program be read by programs written in other languages?
ANS:  Yes---usually there will be no problem.

import java.io.FileOutputStream;
import java.io.RandomAccessFile;
public class MainClass{
            public static void main(String[]args) throws Exception{
                        FileOutputStream fos = new FileOutputstream("xx");
                        for(byte b=10;b<50;b++)
                                    fos.write(b);
                        fos.close();
                        RandomAccessFileraf = new RandomAccessFile("xx","r");
                        raf.seek(10);
                        int i = raf.read();
                        raf.seek(10);
                        int i= read();
                        raf.close();
                        System.out.println("i="+i);
            }
}
ANS: i=20


When the BufferedInputStream is created, an internal buffer array is created. True/false?
ANS: True

What method a buffered character stream sends the correct characters for termination a line of text on the computer running program?
ANS: buf.newLine();

You execute the code below in an empty directory. What is the result?
File(“dirname”); File f2 = new File(f1, “filename”);
ANS: No directory is created, and no file is created.

What does closing a file do?
ANS: Flushes pending output and finishes processing the file.

_ _ _ method of File class tests whether the file or directory denoted by this Abstract pathname Exists.
ANS:  exists
Which of these classes is not abstract?
ANS:  BufferedReader

FileReader implements reader directly?
ANS: false

What happens if a FileWriter constructor is given an illegal file name?
ANS: An IOException is thrown.

Which class is implemented by DataInputStream?
ANS: FilterInputStream



3 comments:

  1. Great blog..it was so informative and helpful..Thanks for sharing..keep update.check this also http://www.bestseleniumtraininginchennai.in/

    ReplyDelete
  2. Such an interesting and informative piece of guidance imparted by you. I am glad to discover this information here and I am sure that this might be beneficial for us.
    Web delopment company in India

    ReplyDelete
  3. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Product prototyping during this lockdown period everyone can use it wisely.
    icp-cat training

    ReplyDelete