Thursday, October 27, 2011

Java Interview Questions part-5(IO streams)



Q1. Given:
import java.io.*;

public class Forest implements Serializable {

private Tree tree = new Tree();

public static void main(String [] args) {

Forest f = new Forest();

try{
FileOutputStream fs = new FileOutputStream("Forest.txt");
ObjectOutputStream os = new ObjectOutputStream(fs);

os.writeObject(f);
os.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
class Tree { }

What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. An instance of Forest is serialized.
D. An instance of Forest and an instance of Tree are both serialized.

Q2. Assuming that the serializeBanana() and the deserializeBanana() methods will correctly use Java serialization and given:
import java.io.*;

class Food implements Serializable { int good = 3; }

class Fruit extends Food { int juice = 5; }

public class Banana extends Fruit {

int yellow = 4;

public static void main(String [] args) {

Banana b = new Banana();
Banana b2 = new Banana();

b.serializeBanana(b); // assume correct serialization
b2 = b.deserializeBanana(); // assume correctdeserialization
System.out.println("restore "+b2.yellow + b2.juice + b2.good);
}
// more Banana methods go here 50.
}

What is the result?

A. restore 400 B. restore 403 C. restore 453 D. Compilation fails.
E. An exception is thrown at runtime.
Q3. Which three statements concerning the use of the java.io.Serializable interface are true? (Choose three.)

A. Objects from classes that use aggregation cannot be serialized.
B. An object serialized on one JVM can be successfully deserialized on a different JVM.
C. The values in fields with the volatile modifier will NOT survive serialization and deserialization.
D. The values in fields with the transient modifier will NOT survive serialization and deserialization.
E. It is legal to serialize an object of a type that has a supertype that does NOT implement Serializable.
Q4. Assuming that the serializeBanana2() and the deserializeBanana2() methods will correctly use Java serialization and given:
import java.io.*;

class Food {
Food() { System.out.print("1"); }
}

class Fruit extends Food implements Serializable {
Fruit() { System.out.print("2"); }
}

public class Banana2 extends Fruit {

int size = 42;

public static void main(String [] args) {

Banana2 b = new Banana2();

b.serializeBanana2(b); // assume correct serialization
b = b.deserializeBanana2(b); // assume correct deserialization

System.out.println(" restored " + b.size + " ");
}
// more Banana2 methods
}

What is the result?

A. Compilation fails. B. 1 restored 42
C. 12 restored 42 D. 121 restored 42
E. 1212 restored 42 F. An exception is thrown at runtime.

Q5. When comparing java.io.BufferedWriter to java.io.FileWriter, which capability exists as a method in only one of the two?

A. closing the stream
B. flushing the stream
C. writing to the stream
D. marking a location in the stream
E. writing a line separator to the stream

Q6. Given:
10. class MakeFile {
11. public static void main(String[] args) {
12. try {
13. File directory = new File("d");
14. File file = new File(directory,"f");
15. if(!file.exists()) {
16. file.createNewFile();
17. }
18. } catch (IOException e) {
19. e.printStackTrace();
20. }
21. }
22. }

The current directory does NOT contain a directory named "d" Which three are true? (Choose three.)

A. Line 16 is never executed.
B. An exception is thrown at runtime.
C. Line 13 creates a File object named "d".
D. Line 14 creates a File object named "f".
E. Line 13 creates a directory named “d” in the file system.
F. Line 16 creates a directory named “d” and a file ‘f’ within it in the file system.
G. Line 14 creates a file named ‘f’ inside of the directory named“d” in the file system.

Q7. Given:
10. public class Foo implements java.io.Serializable {

11. private int x;
12. public int getX() {return x;}
13. public Foo(int x) {this.x=x;}

14. private void writeObject( ObjectOutputStream s) throws IOException {

15. // insert code here

16. }

17. }

Which code fragment, inserted at line 15, will allow Foo objects to be correctly serialized and deserialized?

A. s.writeInt(x); B. s.serialize(x); C. s.writeObject(x); D. s.defaultWriteObject();

Q8. Given:

import java.io.*;

class Player {
Player() {
System.out.print("p");
}
}

public class CardPlayer extends Player implements Serializable {
CardPlayer() {
System.out.print("c");
}

public static void main(String[] args) {
CardPlayer c1 = new CardPlayer();

try {
FileOutputStream fos = new FileOutputStream("play.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(c1);
os.close();

FileInputStream fis = new FileInputStream("play.txt");
ObjectInputStream is = new ObjectInputStream(fis);
CardPlayer c2 = (CardPlayer) is.readObject();
is.close();
}
catch(Exception x){}
}
}

What is the result?

A. pc B. pcc C. pcp
D. pcpc E. Compilation fails. F. An exception is thrown at runtime.

Q9. Given:
bw is a reference to a valid BufferedWriter And the snippet:

15. BufferedWriter b1 = new BufferedWriter(new File("f"));
16. BufferedWriter b2 = new BufferedWriter(new FileWriter("f1"));
17. BufferedWriter b3 = new BufferedWriter(new PrintWriter("f2"));
18. BufferedWriter b4 = new BufferedWriter(new BufferedWriter(bw));

What is the result?

A. Compilation succeeds.
B. Compilation fails due only to an error on line 15.
C. Compilation fails due only to an error on line 16.
D. Compilation fails due only to an error on line 17.
E. Compilation fails due only to an error on line 18.
F. Compilation fails due to errors on multiple lines.

Q10. Given:

import java.io.*;

class Keyboard{}

public class Computer implements Serializable {

private Keyboard k = new Keyboard();

public static void main(String[] args) {

Computer c = new Computer();
c.storeIt(c);
}

void storeIt(Computer c) {
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("myFile"));
os.writeObject(c);
os.close();

System.out.println("done");
}
catch (Exception x) {
System.out.println("exc");
}
}
}

What is the result? (Choose all that apply.)
A. exc B. done C. Compilation fails.
D. Exactly one object is serialized. E. Exactly two objects are serialized.

Q11. Given:

import java.io.*;

public class Directories {

static String [] dirs = {"dir1", "dir2"};

public static void main(String [] args) {

for (String d : dirs) {
// insert code 1 here
File file = new File(path, args[0]);
// insert code 2 here
}
}
}

and that the invocation
java Directories file2.txt
is issued from a directory that has two subdirectories, "dir1" and "dir2", and that "dir1" has a file "file1.txt" and "dir2" has a file "file2.txt", and the output is "false true", which set(s) of code fragments must be inserted? (Choose all that apply.)

A. String path = d;
System.out.print(file.exists() + " ");
B. String path = d;
System.out.print(file.isFile() + " ");
C. String path = File.separator + d;
System.out.print(file.exists() + " ");
D. String path = File.separator + d;
System.out.print(file.isFile() + " ");

Q12. Given that the current directory is empty, and that the user has read and write permissions, and the following:

import java.io.*;

public class DOS {

public static void main(String[] args) {

File dir = new File("dir");

dir.mkdir();

File f1 = new File(dir, "f1.txt");

try {
f1.createNewFile();
}
catch (IOException e)
{ ; }

File newDir = new File("newDir");

dir.renameTo(newDir);
}
}

Which statement is true?

A. Compilation fails.
B. The file system has a new empty directory named dir.
C. The file system has a new empty directory named newDir.
D. The file system has a directory named dir, containing a file f1.txt.
E. The file system has a directory named newDir, containing a file f1.txt.

Q13. Given that the current directory is empty, and that the user has read and write privileges to the current directory, and the following:

import java.io.*;

public class Maker {

public static void main(String[] args) {

File dir = new File("dir");

File f = new File(dir, "f");
}
}

Which statement is true?

A. Compilation fails.
B. Nothing is added to the file system.
C. Only a new file is created on the file system.
D. Only a new directory is created on the file system.
E. Both a new file and a new directory are created on the file system.

Q14. Given:

import java.io.*;

public class TestSer {
public static void main(String[] args) {
SpecialSerial s = new SpecialSerial();
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("myFile"));

os.writeObject(s); os.close();
System.out.print(++s.z + " ");
ObjectInputStream is = new ObjectInputStream(new FileInputStream("myFile"));
SpecialSerial s2 = (SpecialSerial)is.readObject();
is.close();

System.out.println(s2.y + " " + s2.z);
}
catch (Exception x)
{
System.out.println("exc");
}
}
}

class SpecialSerial implements Serializable {
transient int y = 7;
static int z = 9;
}

Which are true? (Choose all that apply.)

A. Compilation fails. B. The output is 10 0 9
C. The output is 10 0 10 D. The output is 10 7 9
E. The output is 10 7 10
F. In order to alter the standard deserialization process you would override the readObject() method in SpecialSerial.
G. In order to alter the standard deserialization process you would override the defaultReadObject()
method in SpecialSerial.

Q15. Can data flow through a given stream in both directions?

A. No; a stream has one direction only, input or output.
B. No; streams only work for output.
C. Yes; only one stream is needed to read and write a file.
D. Yes; but only one direction at a time.

Q16. What is the name of a stream that connects two running programs?

A. Program stream B. Conduit C. Pipe D. Channel

Q17. Can a stream act as a data source for another stream?

A. No. Streams must be connected directly to physical devices.
B. No. Streams do not deal with data.
C. Yes. Some streams can be connected together.
D. Yes. An input stream can be connected to an output stream.

Q18. What is a buffer?

A. A section of memory used as a staging area for input or output data.
B. The cable that connects a data source to the bus.
C. Any stream that deals with character IO.
D. A file that contains binary data.

Q19. What is a stream that transforms data in some way called?

A. Processing stream. B. Transformer. C. UTF stream. D. Pipe

Q20. A stream that is intended for general purpose IO, not usually character data, is called...

A. Reader B. Writer C. Byte Stream D. Character Stream

Q21. What is the name of the abstract base class for streams dealing with character input?

A. InputStream B. OutputStream C. Reader D. Writer

Q22. Is the character data used inside a Java program exactly the same format as the character data in a text file written by Java?

A. No. Internally Java uses 8-bit character data, but text files always use ASCII.
B. No. Internally Java programs always use 16-bit char data; but text files are written in UTF format, which is different.
C. Yes. Java uses the same format for characters inside a program and in text files.
D. Yes. Character data has always used the same format for all computers and all files.

Q23. What is the name of the abstract base class for streams dealing with general purpose (non-character) input?

A. InputStream B. OutputStream C. Reader D. Writer

Q24. Which of the following strings can be used as mode strings for creating a RandomAccessFile object?

(A) "r" (B) "w" (C) "rw" (D) "wr" (E) "0"

  1. (A), (B), (C) & (E)
  2. (A) & (C)
  3. (A), (B) & (C)
  4. (B), (D) & (E)
  5. (C) & (D)

Q25. DataInput is

  1. an abstract class defined in java.io.
  2. an interface that defines methods to read primitive data types.
  3. a class we can use to read primitive data types.
  4. an interface that defines methods to open files.

Q26. Which of the following statements are true?

(A) Unicode characters are all 16 bits.
(B) UTF characters are all 24 bits.
(C) Reader class has methods that can read integers and floats.
(D) File class may be used to rename a file.
(E) DataOutputStream objects are used to write primitive data to a file.

  1. (A), (C), (D) & (E)
  2. (A), (B) & (D)
  3. (B), (D) & (E)
  4. (A), (D) & (E)
  5. (A) & (D)

Q27. Which are the valid ways to create DataInputStream streams?

  1. new DataInputStream();
  2. new DataInputStream("in.dat", "r");
  3. new DataInputStream("in.dat");
  4. new DataInputStream(new FileInputStream("in.dat"));
  5. new DataInputStream(new File("in.dat"));

Q28. Which exception is thrown by the read() method of InputStream class?

  1. Exception
  2. FileNotFoundException
  3. IOException
  4. ReadException
  5. None of these

Q29. Given file is a File object, which of the following are legal statements to create a new file?

(A) file.create();
(B) FileOutputStream fos = new FileOutputStream(file);
(C) FileWriter out = new FileWriter(file);
(D) FileInputStream fis = new FileInputStream(file);
(E) RandomAccessFile raf = new RandomAccessFile(file);

  1. (B), (C) & (E)
  2. (A), (B), (D) & (E)
  3. (B), (C) & (D)
  4. (B), (D) & (E)
  5. (B) & (C)

Q30. Click the Exhibit button.
Which code, inserted at line 14, will allow this class to correctly serialize and deserialize?


A. s.defaultReadObject(); B. this = s.defaultReadObject();

C. y = s.readInt(); x = s.readInt(); D. x = s.readInt(); y = s.readInt();








Note -- Any doubts or need any clarifications please feel free to comment

6 comments:

gowsalya said...

Really you have done great job,There are may person searching about that now they will find enough resources by your post
Selenium Training in Chennai Tamil Nadu | Selenium Training Institute in Chennai anna nagar | selenium training in chennai velachery
Selenium Training in Bangalore with placements | Best Selenium Training in Bangalore marathahalli
java training in chennai | java training in chennai Velachery |java training in chennai anna nagar

haripriya said...

Pleasant Tips..Thanks for Sharing….We keep up hands on approach at work and in the workplace, keeping our business pragmatic, which recommends we can help you with your tree clearing and pruning in an invaluable and fit way.
Data Science course in Chennai
Data science course in bangalore
Data science course in pune
Data science online course
Data Science Interview questions and answers
Data Science Tutorial

jefrin said...

Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
Data science Course Training in Chennai | Data Science Training in Chennai
RPA Course Training in Chennai | RPA Training in Chennai
AWS Course Training in Chennai | AWS Training in Chennai

nisha said...

Really thanks for Providing this Article. the article is very impressive.easily satisfying the queries for the Beginners.

Data Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery

Revathi said...

very nice blog!!

Android Training in Chennai

Android Online Training in Chennai

Android Training in Bangalore

Android Training in Hyderabad

Android Training in Coimbatore

Android Training

Android Online Training

devi said...

Excellent blog with lots of information, keep sharing. I am waiting for your more posts like this or related to any other informative topic. Amazing web journal I visit this blog it's extremely marvellous. Interestingly, in this blog content composed plainly and reasonable. The substance of data is educationalData Science Training In Chennai

Data Science Online Training In Chennai

Data Science Training In Bangalore

Data Science Training In Hyderabad

Data Science Training In Coimbatore

Data Science Training

Data Science Online Training

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Vamshi krishnam raju | Bloggerized by Vamshi krishnam raju - Vamshi krishnam raju | Vamshi krishnam raju