Home > Java Programming > File > Questions and Answers
01. |
try{ File f = new File("a.txt"); }catch(Exception e){ }catch(IOException io){ } Is this code create new file name a.txt ? | |||||||||||
|
02. |
What is the output for the below code? 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(); ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("datafile")); save.writeObject(b); save.flush(); ObjectInputStream restore = new ObjectInputStream(new FileInputStream("datafile")); B z = (B) restore.readObject(); } } | |||||||||||
|
03. |
What is the output for the below code? public class A { public A() { System.out.println("A"); } } public class Test { public static void main(String... args) throws Exception { A a = new A(); ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("datafile")); save.writeObject(a); save.flush(); ObjectInputStream restore = new ObjectInputStream(new FileInputStream("datafile")); A z = (A) restore.readObject(); } } | |||||||||||
|
04. |
What will be the result of compiling and run the following code: public class Test { public static void main(String... args) throws Exception { File file = new File("test.txt"); System.out.println(file.exists()); file.createNewFile(); System.out.println(file.exists()); } } | |||||||||||
|
05. |
What is the output for the below code ? public class A {} public class B implements Serializable { private static A a = new A(); public static void main(String... args){ B b = new B(); try{ FileOutputStream fs = new FileOutputStream("b.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(b); os.close(); }catch(Exception e){ e.printStackTrace(); } } } | |||||||||||
|
06. |
You have a class file name Test.class inside javaproject directory. Test.java source code is : import java.util.Properties; class Test { public static void main (String[] args){ Properties p = System.getProperties(); System.out.println(p.getProperty("key1")); } } you have issued below commands from command prompt. cd javaproject java -D key1=value1 Test What is the output ? | |||||||||||
|
07. |
Is the below statement true? "Only objects that support the java.io.Serializable or java.io.Externalizable interface can be read from streams" (Choose correct one from multiple below) | |||||||||||
|
08. |
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(); ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("datafile")); save.writeObject(b); save.flush(); ObjectInputStream restore = new ObjectInputStream(new FileInputStream("datafile")); B z = (B) restore.readObject(); } } What is the output? | |||||||||||
|
09. |
public class A { public A() { System.out.println("A"); } } public class Test { public static void main(String... args) throws Exception { A a = new A(); ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("datafile")); save.writeObject(a); save.flush(); ObjectInputStream restore = new ObjectInputStream(new FileInputStream("datafile")); A z = (A) restore.readObject(); } } What is the output? | |||||||||||
|
10. |
public class A implements Serializable{ public A() { System.out.println("A"); } } public class Test { public static void main(String... args) throws Exception { A a = new A(); ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("datafile")); save.writeObject(a); save.flush(); ObjectInputStream restore = new ObjectInputStream(new FileInputStream("datafile")); A z = (A) restore.readObject(); } } | |||||||||||
|