JAVA INTERVIEW QUESTIONS - EXPLORING JAVA.LANG
EXPLORING JAVA.LANG
1.java.lang
package is automatically imported into all programs.
True
False
Ans
: a
2.What
are the interfaces defined by java.lang?
Ans
: Cloneable, Comparable and Runnable.
3.What
are the constants defined by both Flaot and Double classes?
Ans
: MAX_VALUE,
MIN_VALUE,
NaN,
POSITIVE_INFINITY,
NEGATIVE_INFINITY
and
TYPE.
4.What
are the constants defined by Byte, Short, Integer and Long?
Ans
: MAX_VALUE,
MIN_VALUE
and
TYPE.
5.What
are the constants defined by both Float and Double classes?
Ans
: MAX_RADIX,
MIN_RADIX,
MAX_VALUE,
MIN_VALUE
and
TYPE.
6.What
is the purpose of the Runtime class?
Ans
: The purpose of the Runtime class is to provide access to the Java runtime
system.
7.What
is the purpose of the System class?
8.Which
class is extended by all other classes?
Ans
: Object class is extended by all other classes.
9.Which
class can be used to obtain design information about an object?
Ans
: The Class class can be used to obtain information about an object’s design.
10.Which
method is used to calculate the absolute value of a number?
Ans
: abs( ) method.
11.What
are E and PI?
Ans
: E is the base of the natural logarithm and PI is the mathematical value pi.
12.Which
of the following classes is used to perform basic console I/O?
System
SecurityManager
Math
Runtime
Ans
: a.
13.Which
of the following are true?
The
Class class is the superclass of the Object class.
The
Object class is final.
The
Class class can be used to load other classes.
The
ClassLoader class can be used to load other classes.
Ans
: c and d.
14.Which
of the following methods are methods of the Math class?
absolute(
)
log(
)
cosine(
)
sine(
)
Ans
: b.
15.Which
of the following are true about the Error and Exception classes?
Both
classes extend Throwable.
The
Error class is final and the Exception class is not.
The
Exception class is final and the Error is not.
Both
classes implement Throwable.
Ans
: a.
16.Which
of the following are true?
The
Void class extends the Class class.
The
Float class extends the Double class.
The
System class extends the Runtime class.
The
Integer class extends the Number class.
Ans
: d.
17)
Which of the following will output -4.0
System.out.println(Math.floor(-4.7));
System.out.println(Math.round(-4.7));
System.out.println(Math.ceil(-4.7));
d) System.out.println(Math.Min(-4.7));
Ans
: c.
18)
Which of the following are valid statements
a)
public class MyCalc extends Math
b) Math.max(s);
c) Math.round(9.99,1);
d) Math.mod(4,10);
b) Math.max(s);
c) Math.round(9.99,1);
d) Math.mod(4,10);
e)
None of the above.
Ans
: e.
19)
What will happen if you attempt to compile and run the following code?
Integer
ten=new Integer(10);
Long
nine=new Long (9);
System.out.println(ten
+ nine);
int
i=1;
System.out.println(i
+ ten);
19
followed by 20
19
followed by 11
Error:
Can't convert java lang Integer
d)
10 followed by 1
Ans
: c.
INPUT / OUTPUT : EXPLORING JAVA.IO
20.What
is meant by Stream and what are the types of Streams and classes of the
Streams?
Ans
: A Stream is an abstraction that either produces or consumes information.
There
are two types of Streams. They are:
Byte
Streams :
Byte Streams provide a convenient means for handling input and output of bytes.
Character
Streams :
Character Streams provide a convenient means for handling input and output of
characters.
Byte
Stream classes :
Byte Streams are defined by using two abstract classes. They are:InputStream
and OutputStream.
Character
Stream classes :
Character Streams are defined by using two abstract classes. They are : Reader
and Writer.
21.Which
of the following statements are true?
UTF
characters are all 8-bits.
UTF
characters are all 16-bits.
UTF
characters are all 24-bits.
Unicode
characters are all 16-bits.
Bytecode
characters are all 16-bits.
Ans
: d.
22.Which
of the following statements are true?
a. When you construct an
instance of File, if you do not use the filenaming semantics of the local
machine, the constructor will throw an IOException.
b. When you construct an
instance of File, if the corresponding file does not exist on the local file
system, one will be created.
c. When an instance of File is
garbage collected, the corresponding file on the local file system is deleted.
d. None of the above.
Ans
: a,b and c.
23.The
File class contains a method that changes the current working directory.
True
False
Ans
: b.
24.It
is possible to use the File class to list the contents of the current
working directory.
True
False
Ans
: a.
25.Readers
have methods that can read and return floats and doubles.
True
False
Ans
: b.
26.You
execute the code below in an empty directory. What is the result?
File
f1 = new File("dirname");
File
f2 = new File(f1, "filename");
a a. A new directory called
dirname is created in the current working directory.
b b. A new directory called
dirname is created in the current working directory. A new file called filename
is created in directory dirname.
c c. A new directory called
dirname and a new file called filename are created, both in the current working
directory.
d d. A new file called filename
is created in the current working directory.
e e. No directory is created,
and no file is created.
Ans
: e.
27.What
is the difference between the Reader/Writer class hierarchy and the
InputStream/OutputStream
class hierarchy?
Ans
: The Reader/Writer class hierarchy is character-oriented and the
InputStream/OutputStream class hierarchy is byte-oriented.
28.What
is an I/O filter?
Ans
: An I/O filter is an object that reads from one stream and writes to another,
usually altering the data in some way as it is passed from one stream to
another.
29.What
is the purpose of the File class?
Ans
: The File class is used to create objects that provide access to the files and
directories of a local file system.
30.What
interface must an object implement before it can be written to a stream as an
object?
Ans
: An object must implement the Serializable or Externalizable interface before
it can be written to a stream as an object.
31.What
is the difference between the File and RandomAccessFile classes?
Ans
: The File class encapsulates the files and directories of the local file
system. The RandomAccessFile class provides the methods needed to directly
access data contained in any part of a file.
32.What
class allows you to read objects directly from a stream?
Ans
: The ObjectInputStream class supports the reading of objects from input
streams.
33.What
value does read( ) return when it has reached the end of a file?
Ans
: The read( ) method returns – 1 when it has reached the end of a file.
34.What
value does readLine( ) return when it has reached the end of a file?
Ans
: The readLine( ) method returns null when it has reached the
end of a file.
35.How
many bits are used to represent Unicode, ASCII, UTF-16 and UTF-8 characters?
Ans
: Unicode requires 16-bits and ASCII requires 8-bits. Although the ASCII
character set uses only 1-bits, it is usually represented as 8-bits. UTF-8
represents characters using 8, 16 and 18-bit patterns. UTF-16 uses 16-bit and
larger bit patterns.
36.Which
of the following are true?
a. The
InputStream and OutputStream classes are byte-oriented.
b. The
ObjectInputStream and ObjectOutputStream do not support serialized object input and output.
c. The
Reader and Writer classes are character-oriented.
d. The
Reader and Writer classes are the preferred solution to serialized object
output.
Ans
: a and c.
37.Which
of the following are true about I/O filters?
a. Filters
are supported on input, but not on output.
b. Filters
are supported by the InputStream/OutputStream class hierarchy, but not by the Reader/Writer class hierarchy.
c. Filters
read from one stream and write to another.
d. A
filter may alter data that is read from one stream and written to another.
Ans
: c and d.
38.Which
of the following are true?
a. Any
Unicode character is represented using 16-bits.
b. 7-bits
are needed to represent any ASCII character.
c. UTF-8
characters are represented using only 8-bits.
d. UTF-16
characters are represented using only 16-bits.
Ans
: a and b.
39.Which
of the following are true?
a. The
Serializable interface is used to identify objects that may be written to an
output stream.
b. The
Externalizable interface is implemented by classes that control the way in
which their objects are serialized.
c. The
Serializable interface extends the Externalizable interface.
d. The
Externalizable interface extends the Serializable interface.
Ans
: a, b and d.
40.Which
of the following are true about the File class?
a a. A File object can be used
to change the current working directory.
b b. A File object can be used
to access the files in the current directory.
c c. When a File object is
created, a corresponding directory or file is created in the local file system.
d d. File objects are used to
access files and directories on the local file system.
e e. File objects can be garbage
collected.
f. f. When a File object is
garbage collected, the corresponding file or directory is deleted.
Ans
: b, d and e.
41.How
do you create a Reader object from an InputStream object?
a a. Use the static
createReader( ) method of InputStream class.
b b. Use the static
createReader( ) method of Reader class.
c c. Create an InputStreamReader
object, passing the InputStream object as an argument to the InputStreamReader
constructor.
d d. Create an
OutputStreamReader object, passing the InputStream object as an argument to the
OutputStreamReader constructor.
Ans
: c.
42.Which
of the following are true?
a. Writer
classes can be used to write characters to output streams using different
character encodings.
b. Writer
classes can be used to write Unicode characters to output streams.
c. Writer
classes have methods that support the writing of the values of any Java
primitive type to output streams.
d. Writer
classes have methods that support the writing of objects to output streams.
Ans
: a and b.
43.The
isFile( ) method returns a boolean value depending on whether the file object
is a file or a directory.
True.
False.
Ans
: a.
44.Reading
or writing can be done even after closing the input/output source.
True.
False.
Ans
: b.
45.The
________ method helps in clearing the buffer.
Ans
: flush( ).
46.The
System.err method is used to print error message.
True.
False.
Ans
: a.
47.What
is meant by StreamTokenizer?
Ans
: StreamTokenizer breaks up InputStream into tokens that are delimited by sets
of characters.
It
has the constructor : StreamTokenizer(Reader inStream).
Here
inStream must be some form of Reader.
48.What
is Serialization and deserialization?
Ans
: Serialization is the process of writing the state of an object to a byte
stream.
Deserialization
is the process of restoring these objects.
30)
Which of the following can you perform using the File class?
a)
Change the current directory
b) Return the name of the parent directory
c) Delete a file
d) Find if a file contains text or binary information
b) Return the name of the parent directory
c) Delete a file
d) Find if a file contains text or binary information
Ans
: b and c.
31)How
can you change the current working directory using an instance of the File
class called FileName?
a. FileName.chdir("DirName").
b. FileName.cd("DirName").
c. FileName.cwd("DirName").
d. The
File class does not support directly changing the current directory.
Ans
: d.