Sun Certified Java Programmer (SCJP Sample Questions)
Test SCJP Skill before Attending Final SCJP Exam
Questions are from File and Serializable topic only
Question No :4Is the bellow statement is 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)
1. true
2. false
3. can't say
4. none of the above
correct is :1
Explanations :Only objects that support the java.io.Serializable or java.io.Externalizable interface
can be read from streams
------------------------------------------------------------------------------
Question No :5
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?
(Choose correct one from multiple below)
1. A
B
A
2. A
B
A
B
3. B
B
4. B
correct is :1
Explanations :On the time of deserialization , the Serializable object not create new object. So
constructor of class B does not called.
A is not Serializable object so constructor is called.
------------------------------------------------------------------------------
Question No :6
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?
(Choose correct one from multiple below)
1. A
A
2. A
3. java.io.NotSerializableException
4. None of the above
correct is :3
Explanations :Class A does not implements Serializable interface. So throws
NotSerializableException on trying to Serialize a non Serializable object.
------------------------------------------------------------------------------
Question No :7
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();
}
}
What is the output?
(Choose correct one from multiple below)
1. A
A
2. A
3. Runtime Exception
4. Compile with error
correct is :2
Explanations :On the time of deserialization , the Serializable object not create new object. So
constructor of class A does not called.
------------------------------------------------------------------------------
Question No :8
Can static variables are Serialized ?
(Choose correct one from multiple below)
1. yes
2. No,static can't be Serialized.
3. may or may not Serialized
4. None of the above
correct is :2
Explanations :No,static and transient can't be Serialized.
------------------------------------------------------------------------------
Question No :9
Which statement is true?
(Choose correct one from multiple below)
1. Implementing the Serializable interface allows object serialization to save and restore the entire
state of the object
2. Implementing the Serializable interface allows object serialization to save state of the object
and can't restore the entire state
3. Both are true
4. None of the above
correct is :1
Explanations :Implementing the Serializable interface allows object serialization to save and
restore the entire state of the object
------------------------------------------------------------------------------
Question No :10
Which statement is true?
(Choose correct one from multiple below)
1. static and transient fields are NOT serialized
2. static and transient fields are can be serialized
3. Both are true
4. None of the above
correct is :1
Explanations :static and transient fields are NOT serialized
------------------------------------------------------------------------------
Question No :11
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 FileOutputStream("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 ?
(Choose correct one from multiple below)
1. Compilation Fail
2. java.io.NotSerializableException: Because class A is not Serializable.
3. No Exception
4. None of the above
correct is :2
Explanations :java.io.NotSerializableException:A Because class A is not Serializable.
------------------------------------------------------------------------------
Question No :12
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?
(Choose correct one from multiple below)
1. A
B
A
2. A
B
A
B
3. B
B
4. B
correct is :1
Explanations :On the time of deserialization , the Serializable object not create new object. So
constructor of class B does not called.
A is not Serializable object so constructor is called.
------------------------------------------------------------------------------
Question No :13
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?
(Choose correct one from multiple below)
1. A
A
2. A
3. java.io.NotSerializableException
4. None of the above
correct is :3
Explanations :Class A does not implements Serializable interface. So throws
NotSerializableException on trying to Serialize a non Serializable object.
------------------------------------------------------------------------------
Question No :14
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();
}
}
What is the output?
(Choose correct one from multiple below)
1. A
A
2. A
3. Runtime Exception
4. Compile with error
correct is :2
Explanations :On the time of deserialization , the Serializable object not create new object. So
constructor of class A does not called.
------------------------------------------------------------------------------
Question No :15
public class A implements Serializable {
transient int a = 7;
static int b = 9;
}
public class B implements Serializable {
public static void main(String... args){
A a = new A();
try {
ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream("test.ser"));
os.writeObject(a);
os. close();
System.out.print( + + a.b + " ");
ObjectInputStream is = new ObjectInputStream(new FileInputStream("test.ser"));
A s2 = (A)is.readObject();
is.close();
System.out.println(s2.a + " " + s2.b);
} catch (Exception x)
{
x.printStackTrace();
}
}
}
What is the output?
(Choose correct one from multiple below)
1. 9 0 9
2. 9 7 9
3. Runtime Exception
4. Compile with error
correct is :1
Explanations :static and transient variables are not serialized when an object is serialized.
------------------------------------------------------------------------------
Question No :16
Which is the correct way of Instantiate BufferedWriter object?
(Choose correct one from multiple below)
1. BufferedWriter b1 = new BufferedWriter(new File("file.txt"));
2. BufferedWriter b1 = new BufferedWriter(new FileWriter("file.txt"));
3. Both are true
4. None of the above
correct is :2
Explanations :Constructor of BufferedWriter is public BufferedWriter(Writer out) {
}
so BufferedWriter b1 = new BufferedWriter(new FileWriter("file.txt")); is correct one.
------------------------------------------------------------------------------
Question No :17
What will be the result of compiling and run the following code:
public class Test {
public static void main(String... args) throws Exception {
Integer i = 34;
long l = 34l;
if(i.equals(l)){
System.out.println(true);
}else{
System.out.println(false);
}
}
}
(Choose correct one from multiple below)
1. true
2. false
3. Compile error
4. None of the above
correct is :2
Explanations :equals() method for the integer wrappers will only return true if the two primitive
types and the two values are equal.
------------------------------------------------------------------------------
Question No :18
What will be the result of compiling and run the following code:
public class Test {
public static void main(String... args) throws Exception {
Integer i = 34;
int l = 34;
if(i.equals(l)){
System.out.println(true);
}else{
System.out.println(false);
}
}
}
(Choose correct one from multiple below)
1. true
2. false
3. Compile error
4. None of the above
correct is :1
Explanations :equals() method for the integer wrappers will only return true if the two primitive
types and the two values are equal.
------------------------------------------------------------------------------
Question No :19
When comparing java.io.BufferedWriter and java.io.FileWriter, which capability exist as a method
in only one of two ?
(Choose correct one from multiple below)
1. closing the stream
2. flushing the stream
3. writting to the stream
4. writting a line separator to the stream
correct is :4
Explanations :A newLine() method is provided in BufferedWriter which is not in FileWriter.
------------------------------------------------------------------------------
Question No :20
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");
}
}
(Choose correct one from multiple below)
1. create new actual file name as test.txt
2. no actual file will be created.
3. Compile error.
4. None of the above
correct is :2
Explanations :creating a new instance of the class File, you're not yet making an actual file, you're
just creating a filename
------------------------------------------------------------------------------
Question No :21
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());
}
}
(Choose correct one from multiple below)
1. true
true
2. false
true
3. false
true
4. None of the above
correct is :2
Explanations :creating a new instance of the class File, you're not yet making an actual file, you're
just creating a filename.
So file.exists() return false.
createNewFile() method created an actual file.so file.exists() return true.
------------------------------------------------------------------------------
Question No :22
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());
FileWriter fw = new FileWriter(file);
System.out.println(file.exists());
}
}
(Choose correct one from multiple below)
1. true
true
2. false
true
3. false
true
4. None of the above
correct is :2
Explanations :creating a new instance of the class File, you're not yet making an actual file, you're
just creating a filename.
So file.exists() return false.
FileWriter fw = new FileWriter(file) do three things:
It created a FileWriter reference variable fw.
It created a FileWriter object, and assigned it to fw.
It created an actual empty file out on the disk.
So file.exists() return true .
------------------------------------------------------------------------------
Question No :23
What is the way to create a actual file ?
(Choose correct one from multiple below)
1. Invoke the createNewFile() method on a File object.
2. Create a Reader or a Writer or a Stream.
3. Both are true
4. None of the above
correct is :3
Explanations :Whenever you create an instance of a FileReader, a FileWriter, a PrintWriter, a
FileInputStream, or a FileOutputStream
classes, you automatically create a file, unless one already exists.
------------------------------------------------------------------------------
Question No :24
What will be the result of compiling and run the following code:
public class Test {
public static void main(String... args) throws Exception {
File myDir = new File("test");
// myDir.mkdir();
File myFile = new File(
myDir, "test.txt");
myFile.createNewFile();
}
}
(Choose correct one from multiple below)
1. create directory "test" and a file name as "test.txt" within the the directory.
2. java.io.IOException: No such file or directory
3. Compile with error
4. None of the above
correct is :2
Explanations :// myDir.mkdir(); is commented so no directory, thatswhy exception.
------------------------------------------------------------------------------
Question No :25
public class A {}
public class B implements Serializable {
private transient 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();
}
}
}
What is the output for the above code ?
(Choose correct one from multiple below)
1. Compilation Fail
2. java.io.NotSerializableException: Because class A is not Serializable.
3. No Exception
4. None of the above
correct is :3
Explanations :No java.io.NotSerializableException, Because class A variable is transient.
transient variables are not Serializable.
------------------------------------------------------------------------------
Question No :26
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();
}
}
}
What is the output for the above code ?
(Choose correct one from multiple below)
1. Compilation Fail
2. java.io.NotSerializableException: Because class A is not Serializable.
3. No Exception
4. None of the above
correct is :3
Explanations :No java.io.NotSerializableException, Because class A variable is static. static
variables are not Serializable.
------------------------------------------------------------------------------
Question No :27
Which statement is true ?
(Choose correct one from multiple below)
1. serialization applies only to OBJECTS.
2. Static variables are NEVER saved as part of the object's state. So static variables are not
Serializable.
3. Both are true
4. None of the above
correct is :3
Explanations :serialization applies only to OBJECTS. static variables are related to class not
OBJECT. so static variables are not Serializable.
------------------------------------------------------------------------------
Question No :28
public class Test {
public static void main(String... args) throws Exception {
Calendar c = new Calendar();
System.out.println(c.getTimeInMillis());
}
}
What is the output for the above code ?
(Choose correct one from multiple below)
1. Returns this Calendar's time value in milliseconds.
2. Compile error : Cannot instantiate the type Calendar
3. Runtime Exception
4. None of the above
correct is :2
Explanations :Compile error : Cannot instantiate the type Calendar.
In order to create a Calendar instance, you have to use one of the overloaded getInstance() static
factory methods:
Calendar cal = Calendar.getInstance();
------------------------------------------------------------------------------
Question No :29
public class Test {
public static void main(String... args) throws Exception {
Date d = new Date(1123631685981L);
DateFormat df = new DateFormat();
System.out.println(df.format(d));
}
}
What is the output for the above code ?
(Choose correct one from multiple below)
1. An exception is thrown at runtime.
2. 1123631685981L
3. Compilation fails
4. None of the above
correct is :3
Explanations :DateFormat objects must be created using a static method such as
DateFormat.getInstance() or DateFormat.getDateInstance().
------------------------------------------------------------------------------
Question No :30
Which statement is true?
(Choose correct one from multiple below)
1. The DateFormat.getDate() is used to convert a String to a Date instance
2. If a NumberFormat instance's Locale is to be different than the current Locale, it must be
specified at creation time.
3. Both DateFormat and NumberFormat objects can be constructed to be Locale specific
4. 2 and 3 is true
correct is :4
Explanations :DateFormat.parse() is used to convert a String to a Date.
------------------------------------------------------------------------------
Question No :31
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 FileOutputStream("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 ?
(Choose correct one from multiple below)
1. Compilation Fail
2. java.io.NotSerializableException: Because class A is not Serializable.
3. No Exception
4. None of the above
correct is :2
Explanations :java.io.NotSerializableException:A Because class A is not Serializable.
------------------------------------------------------------------------------
Question No :32
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?
(Choose correct one from multiple below)
1. A
B
A
2. A
B
A
B
3. B
B
4. B
correct is :1
Explanations :On the time of deserialization , the Serializable object not create new object. So
constructor of class B does not called.
A is not Serializable object so constructor is called.