String, Files, IO, Date and Serializable
String, Files, IO, Date and Serializable
6 (CX-310-065 , CX-310-066)
Prepared by : http://www.techfaq360.com
Explanation :
A is the correct answer.
== compare the addresses and equals() methods checks for content. In the "das" ==
new String("das") , new String("das") point to different address then "das".
Question - 2
Which of the following is correct?
Explanation :
D is the correct answer.
Question - 3
What is the output?
Explanation :
A is the correct answer.
StringBuffer class DOES NOT override the equals() method. Therefore, it uses Object
class' equals(), which only checks for equality of the object references
Question - 4
What is the output?
Explanation :
A is the correct answer.
String's equals() method checks if the argument if of type string, if not it returns false
Question - 5
What is true about StringBuilder ?
Explanation :
D is the correct answer.
Question - 6
What is the output ?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
System.out.println(b);
}
}
1.true
2.false
3.compile error
4.None of the error
Explanation :
A is the correct answer.
Question - 7
What is the output ?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("b");
boolean b = m.matches();
System.out.println(b);
}
}
1.true
2.false
3.Compile error
4.None of the above
Explanation :
A is the correct answer.
Question - 8
What is the output ?
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaa");
boolean b = m.matches();
System.out.println(b);
}
}
1.true
2.false
3.Compile error
4.None of the above
Explanation :
B is the correct answer.
Pattern p = Pattern.compile("a+b?");
Matcher m = p.matcher("aaaa");
boolean b = m.matches();
System.out.println(b);
}
}
1.true
2.false
3.Compile error
4.None of the above
Explanation :
A is the correct answer.
Question - 10
What is output ?
Pattern p = Pattern.compile("a+b?");
Matcher m = p.matcher("aaaabb");
boolean b = m.matches();
System.out.println(b);
}
}
1.true
2.false
3.Compile error
4.None of the above
Explanation :
B is the correct answer.
Question - 11
What is the output ?
Pattern p = Pattern.compile("a+b?");
Matcher m = p.matcher("b");
boolean b = m.matches();
System.out.println(b);
}
}
1.true
2.false
3.Compile error
4.None of the above
Explanation :
B is the correct answer.
Question - 12
What is the output ?
Pattern p = Pattern.compile("a+b?c*");
Matcher m = p.matcher("ab");
boolean b = m.matches();
System.out.println(b);
}
}
1.true
2.false
3.Compile error
4.None of the above
Explanation :
A is the correct answer.
Question - 13
What is the output ?
Pattern p = Pattern.compile("a{3}b?c*");
Matcher m = p.matcher("aaab");
boolean b = m.matches();
System.out.println(b);
}
}
1.true
2.false
3.Compile Error
4.None of the above
Explanation :
A is the correct answer.
X? X, once or not at all X* X, zero or more times X+ X, one or more times X{n} X,
exactly n times X{n,} X, at least n times X{n,m} X, at least n but not more than m
times
Question - 14
What is the output ?
Pattern p = Pattern.compile("a{3,}b?c*");
Matcher m = p.matcher("aab");
boolean b = m.matches();
System.out.println(b);
}
}
1.true
2.false
3.Compile error
4.None of the above
Explanation :
B is the correct answer.
X? X, once or not at all X* X, zero or more times X+ X, one or more times X{n} X,
exactly n times X{n,} X, at least n times X{n,m} X, at least n but not more than m
times
Question - 15
What is the output ?
public class Test {
Pattern p = Pattern.compile("a{1,3}b?c*");
Matcher m = p.matcher("aaab");
boolean b = m.matches();
System.out.println(b);
}
}
1.true
2.false
3.Compile error
4.None of the above
Explanation :
A is the correct answer.
X? X, once or not at all X* X, zero or more times X+ X, one or more times X{n} X,
exactly n times X{n,} X, at least n times X{n,m} X, at least n but not more than m
times
Question - 16
What is the output ?
public class Test {
Pattern p = Pattern.compile("a{1,3}b?c*");
Matcher m = p.matcher("aaaab");
boolean b = m.matches();
System.out.println(b);
}
}
1.true
2.false
3.Compile error
4.None of the above
Explanation :
B is the correct answer.
X? X, once or not at all X* X, zero or more times X+ X, one or more times X{n} X,
exactly n times X{n,} X, at least n times X{n,m} X, at least n but not more than m
times
Question - 17
What is the output ?
Pattern p = Pattern.compile("[,\\s]+");
}
}
Explanation :
A is the correct answer.
[,\\s]+ means comma or white space one or more time so split based on the criteria.
Question - 18
What is the output ?
Pattern p = Pattern.compile("[\\s]+");
}
}
Explanation :
A is the correct answer.
[\\s]+ means white space one or more time so split based on the criteria.
Question - 19
"Instances of Pattern class are immutable and are safe for use by
multiple concurrent threads.
Instances of the Matcher class are not safe for such use. "
1.true
2.false
3.Can't say
4.None of the above
Explanation :
A is the correct answer.
Instances of Pattern class are immutable and are safe for use by multiple concurrent
threads. Instances of the Matcher class are not safe for such use.
Question - 20
"str.split (" "); is equal to str.split ("\\s")"
Is the above statement true ?
1.true
2.false
3.can't say
4.none of the above
Explanation :
A is the correct answer.
Question - 21
What is the output ?
}
}
Explanation :
A is the correct answer.
The split() method takes a parameter giving the regular expression to use as a
delimiter and returns a String array containing the tokens so delimited. Using split()
function:
Question - 22
What is the output ?
}
}
Explanation :
B is the correct answer.
java.util.Scanner is a simple text scanner which can parse primitive types and strings
using regular expressions
Question - 23
What is the output ?
}
}
Explanation :
A is the correct answer.
The split() method takes a parameter giving the regular expression to use as a
delimiter and returns a String array containing the tokens so delimited. Using split()
function
Question - 24
Which statement is true about "java.io.File";
1.Files and directories are accessed and manipulated via the java.io.File class
2.Files accessed and manipulated via the java.io.File class not directories
3.both are true
4.None of the above.
Explanation :
A is the correct answer.
Files and directories are accessed and manipulated via the java.io.File class. The File
class does not actually provide for input and output to files. It simply provides an
identifier of files and directories.
Question - 25
Which statement is true?
Explanation :
A and B is the correct answer.
FileReader is meant for reading streams of characters. For reading streams of raw
bytes, consider using a FileInputStream.
Question - 26
Which statement is true?
1.FileWriter is meant for writing streams of characters
2.FileOutputStream is meant for writing streams of raw bytes
3.FileReader is meant for writing streams of characters and raw bytes both
4.None of the above
Explanation :
A and B is the correct answer.
FileReader is meant for reading streams of characters. For reading streams of raw
bytes, consider using a FileInputStream.
Question - 27
Is the bellow statement is true?
"Only objects that support the java.io.Serializable or
java.io.Externalizable interface can be read from streams"
1.true
2.false
3.can't say
4.none of the above
Explanation :
A is the correct answer.
Question - 28
What is the output for the below code?
public class A {
public A() {
System.out.println("A");
}
}
1.A B A
2.A B A B
3.B B
4.B
Explanation :
A is the correct answer.
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 - 29
What is the output for the below code?
public class A {
public A() {
System.out.println("A");
}
}
1.A A
2.A
3.java.io.NotSerializableException
4.None of the above
Explanation :
C is the correct answer.
Question - 30
What is the output for the below code?
1.A A
2.A
3.Runtime Exception
4.Compile with error
Explanation :
B is the correct answer.
On the time of deserialization , the Serializable object not create new object. So
constructor of class A does not called.
Question - 31
Can static variables are Serialized ?
1.yes
2.No,static can't be Serialized.
3.may or may not Serialized
4.None of the above
Explanation :
B is the correct answer.
Question - 32
Which statement is true?
Explanation :
A is the correct answer.
Implementing the Serializable interface allows object serialization to save and restore
the entire state of the object
Question - 33
Which statement is true?
Explanation :
A is the correct answer.
static and transient fields are NOT serialized
Question - 34
public class A {}
}catch(Exception e){
e.printStackTrace();
}
1.Compilation Fail
2.java.io.NotSerializableException: Because class A is not Serializable.
3.No Exception
4.None of the above
Explanation :
B is the correct answer.
Question - 35
public class A {
public A() {
System.out.println("A");
}
}
}
What is the output?
1.A B A
2.A B A B
3.B B
4.B
Explanation :
A is the correct answer.
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 - 36
What is the output for the below code?
public class A {
public A() {
System.out.println("A");
}
}
1.A A
2.A
3.java.io.NotSerializableException
4.None of the above
Explanation :
C is the correct answer.
Question - 37
What is the output for the below code?
1.A A
2.A
3.Runtime Exception
4.Compile with error
Explanation :
B is the correct answer.
On the time of deserialization , the Serializable object not create new object. So
constructor of class A does not called.
Question - 38
public class A implements Serializable {
transient int a = 7;
static int b = 9;
}
What is the output?
1.9 0 9
2.9 7 9
3.Runtime Exception
4.Compile with error
Explanation :
A is the correct answer.
static and transient variables are not serialized when an object is serialized.
Question - 39
Which is the correct way of Instantiate BufferedWriter object?
Explanation :
B is the correct answer.
Question - 40
What will be the result of compiling and run the following code:
1.true
2.false
3.Compile error
4.None of the above
Explanation :
B is the correct answer.
equals() method for the integer wrappers will only return true if the two primitive
types and the two values are equal.
Question - 41
What will be the result of compiling and run the following code:
1.true
2.false
3.Compile error
4.None of the above
Explanation :
A is the correct answer.
equals() method for the integer wrappers will only return true if the two primitive
types and the two values are equal.
Question - 42
When comparing java.io.BufferedWriter and java.io.FileWriter, which
capability exist as a method in only one of two ?
Explanation :
D is the correct answer.
Question - 43
What will be the result of compiling and run the following code:
public class Test {
Explanation :
B is the correct answer.
creating a new instance of the class File, you're not yet making an actual file, you're
just creating a filename
Question - 44
What will be the result of compiling and run the following code:
public class Test {
1.true true
2.false true
3.false true
4.None of the above
Explanation :
B is the correct answer.
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 - 45
What will be the result of compiling and run the following code:
public class Test {
}
1.true true
2.false true
3.false true
4.None of the above
Explanation :
B is the correct answer.
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 - 46
What is the way to create a actual file ?
Explanation :
C is the correct answer.
Question - 47
What will be the result of compiling and run the following code:
public class Test {
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
Explanation :
B is the correct answer.
Question - 48
public class A {}
}catch(Exception e){
e.printStackTrace();
}
1.Compilation Fail
2.java.io.NotSerializableException: Because class A is not Serializable.
3.No Exception
4.None of the above
Explanation :
C is the correct answer.
Question - 49
What is the output for the below code ?
public class A {}
}catch(Exception e){
e.printStackTrace();
}
1.Compilation Fail
2.java.io.NotSerializableException: Because class A is not Serializable.
3.No Exception at Runtime
4.None of the above
Explanation :
C is the correct answer.
Question - 50
Which statement is true ?
Explanation :
C is the correct answer.
serialization applies only to OBJECTS. static variables are related to class not
OBJECT. so static variables are not Serializable.
Question - 51
public class Test {
Explanation :
B is the correct answer.
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 - 52
public class Test {
Explanation :
C is the correct answer.
Question - 53
Which statement is true?
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
Explanation :
D is the correct answer.
Question - 54
Which statement is true about java.io.Console class ?
1.Console has readPassword() method that disables console echo and returns a char
array
2.Console has readLine() method that disables console echo and returns a char array]
3.Both of the above statements are true
4.None of the above
Explanation :
A is the correct answer.
Console has readPassword() method that disables console echo and returns a char
array. You can't see the password in console.
Question - 55
Which statement is true about java.io.Console class ?
Explanation :
A and C is the correct answer.
public final class Console implements Flushable {}. you can't extend Console class
because it is final.
Question - 56
What is the output?
import java.io.Console;
if (con != null)
{
int count = 0;
do
{
String uname = con.readLine(null);
char[] pwd = con.readPassword("Enter %s's password: ",
uname);
con.writer().write("\n\n");
} while (!auth && ++count < 3);
}
}
}
1.NullPointerException
2.It works properly
3.Compile Error : No readPassword() method in Console class.
4.None of the above
Explanation :
A is the correct answer.
F passing a null argument to any method in Console class will cause a
NullPointerException to be thrown.
Question - 57
How to get instance of java.io.Console class ?
Explanation :
A is the correct answer.
If this virtual machine has a console then it is represented by a unique instance of this
class which can be obtained by invoking the System.console() method. If no console
device is available then an invocation of that method will return null.