Saturday, 15 March 2014

Interview Questions and Answers on File Operations in C#

1) Why we use files?

Variables and arrays only provide temporary storage, they lost when
 program terminates. But the files are permanent and they are available
any time for the application.

2)What are advantage and disadvantage using file operation?

The main advantage of files are, they are persistent and they are readily
available for the use of application and the disadvantage is, If the
number of users accessing the same file it will degrades the performance
of the application.

3)Under which name space file operations can be done?

System.IO name space.

4)Name some classes in System.IO name space?

These are the some important classes under System.IO name space

a) File                                             
b) FileInfo                                      
c) Directory                  
d) DirectoryInfo          
         e) DriveInfo                 
         f) StreamReader
         g) StreamWriter
         h) StringReader
         i) StringWriter
         j) BinaryReader
         k) BinaryWriter
         l) Path etc.
5) What is stream in file operations?

Stream is a sequence of bytes traveling from a source to a destination
over a communication path.

6)Why Dispose ( ) comes into picture?

Garbage Collector is responsible for the automatic memory management
but the Garbage Collector is under control of CLR. Here file
operations are under control of OS so the garbage collector will not
come into picture. To release memory used by the instances in file
operation we need to call the Dispose ( ).

7)Which Name Space addded in .net4.5 regarding file operations?

System.IO.Compression name space
This name Space contains classes that provide basic compression
and decompression services for streams.

8) What is the difference between Dispose ( ) & Flush ( )?

Dispose ( ) is used to release all resources used by the current instances
 where as Flush ( ) is used to clear all buffer for the current stream.

9) What is the use of Seek ( )?

We can seek to a specific location in our file with the Seek ( ) method.

10) How to get the total size of a physical drive in your computer?

By using TotalSize property of DriveInfo class in System.IO name space.

11)What is the difference between ReadDecimal ( ) and ReadInt32 ( )?

ReadDecimal ( ) is used to read a decimal value from the current stream
where as ReadInt32 ( ) is used to read an integer value from the current
stream.

12)What is the use of BinaryWriter & BinaryReader class?

BinaryWriter class is responsible for write primitive data types in
binary to a stream and BinaryReader class is responsible for read
primitive data types as binary value.

13)How to get the file names present in  a drive using directory class?

Directory.GetFiles(String)->Returns an array(upto .net3.5)

Directory.EnumerateFiles(String)->Returns an enumerable
collection(Newly added in .net4.0).

14)How to check whether the directory and file are exists or not?

Directory.Exists(path)
File.Exists(path)

15)What is the difference between Peek ( ) & Seek ( )?

Peek ( ) is used to Returns the next available character but does not
consume it where as Seek ( ) is used to set the position within the
current stream.

16)How to get the directories names present in  system using directory
class?

Directory.GetDirectories(String)->Returns an array(upto .net3.5)

Directory.EnumerateDirectories(String)->Returns an enumerable
collection(Newly added in .net4.0).

17) What are the different types of files?

There are 2 kinds of files. They are Text files & Binary files.

18)How to create a gip file?

By using ZipClass present under the System.IO.Compression name space.

Ex:using System;

    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }

19)Why we use Close ( ) method?

By using Close ( ) we can close the current class object and underlying
stream.

20)What is a Stream?

 We can think of a stream as a channel on which data is passed from
senders to receivers. As far as the programs we will use are concerned,
streams allow travel in only one direction. The stream is basically the
sequence of bytes passing through the communication path. There are two
 main streams: the input stream and the output stream.

The input stream is used for reading data from file (read operation)
and the output stream is used for writing into the file (write operation).

21)What type of data ZipStream Compress?

Any data less than 4gb in size

22)How we can deal with the audio and video files?

By using Audio and video class.we create the instances of these
classes and provides(path)as the argument.In windows applications with
the help of MediaElement control we deal with audio and video files.

23)how we will get the creation time of a file ?

By using the method GetCreationTime of file class.

Ex:DateTime CreatedDate=File.GetCreationTime(@"C:\Example\MyTest.txt");

Console.WriteLine("file created: " + fileCreatedDate);

24)How to get last access time of a file ?

By using GetLastAccessTime method of file class.

Ex:DateTime dt = File.GetLastAccessTime(path);
Console.WriteLine("The last access time for this file was {0}.", dt);
25)Which methods directory and directoryInfo classes have?

Directory class:Static methods
DirectoryInfo:Instance methods and is a sealed class .

26)In directory and directoryInfo class  to get directories and files information
which methods we have upto .net3.5?

GetDirectories for directories information and getfiles for files information.
These will gives the result in an array format.

27)In directory and directoryInfo class  to get directories and files information
which methods are introduced in .net4.0?

EnumerateDirectories for directories information and enumeratefiles for
files information.These will gives the result in Enumerable collection,this will
give better performance than GetDirectories and GetFiles methods.

28)Why didn’t I have to use the Close () method to close the file after I used File. ReadAllText () and File.WriteAllText ()?

A: The File class has several very useful static methods that automatically
open up a file, read or write data, and then close it automatically. In
addition to the ReadAllText () and WriteAllText () methods,
there are ReadAllBytes () and WriteAllBytes (), which work with byte
arrays, and ReadAllLines () and WriteAllLines (), which read and write
string arrays, where each string in the array is a separate line in the file.
All of these methods automatically open and close the streams, so you
can do your whole file operation in a single statement.

29)How can you change an object into a remote object?
Any object can be changed into a remote object by deriving it
 from MarshalByRefObject Class.

30)What is considered as remote object?
Any object outside the application domain of the calling application
is considered remote object,even if the objects are executing on
the same machine.


No comments:

Post a Comment