Copyright © 3S Technologies

Email: info@3s-technologies.com

Best viewed in Internet Explorer ver. 9.0 - 1024 X 768 resolution.

Java Objectives - Part 5

The reason for documenting this topic is to provide important technical questions and answers on Java language.  These questions can appear in an aptitude test, organized by different recruiting IT organizations.

 

Some questions and answers on Input/Output or the Java.IO package:

 

1) 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.

 

2) Which of the following statements are true?

a) UTF characters are all 8-bits.

b) UTF characters are all 16-bits.

c) UTF characters are all 24-bits.

d) Unicode characters are all 16-bits.

e) Bytecode characters are all 16-bits.

Ans: d

 

3) 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

 

4) The File class contains a method that changes the current working directory.

a) True

b) False

Ans: b

 

5) It is possible to use the File class to list the contents of the current working directory.

a) True

b) False

Ans: a

 

6) Readers have methods that can read and return floats and doubles.

a) True

b) False

Ans: b

 

7) 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 new directory called dirname is created in the current working directory.

b) A new directory called dirname is created in the current working directory. A new file called filename is created in directory dirname.

c) A new directory called dirname and a new file called filename are created, both in the current working directory.

d) A new file called filename is created in the current working directory.

e) No directory is created, and no file is created.

Ans: e

 

8) 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.

 

9) 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.

 

10) 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.

 

11) 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.

 

12) 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.

 

13) What class allows you to read objects directly from a stream?

Ans: The ObjectInputStream class supports the reading of objects from input streams.

 

14) 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.

 

15) 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.

 

16) 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.

 

17) 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

 

18) 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

 

19) 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

 

20) 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

 

21) Which of the following are true about the File class?

a) A File object can be used to change the current working directory.

b) A File object can be used to access the files in the current directory.

c) When a File object is created, a corresponding directory or file is created in the local file system.

d) File objects are used to access files and directories on the local file system.

e) File objects can be garbage collected.

f) When a File object is garbage collected, the corresponding file or directory is deleted.

Ans: b, d and e

 

22) How do you create a Reader object from an InputStream object?

a) Use the static createReader( ) method of InputStream class.

b) Use the static createReader( ) method of Reader class.

c) Create an InputStreamReader object, passing the InputStream object as an argument to the InputStreamReader constructor.

d) Create an OutputStreamReader object, passing the InputStream object as an argument to the OutputStreamReader constructor.

Ans: c

 

23) 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

 

24) The isFile( ) method returns a boolean value depending on whether the file object is a file or a directory.

a) True.

b) False.

Ans: a

 

25) Reading or writing can be done even after closing the input/output source.

a) True.

b) False.

Ans: b

 

26) The ________ method helps in clearing the buffer.

Ans: flush( ).

 

27) The System.err method is used to print error message.

a) True.

b) False.

Ans: a

 

28) 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.

 

27) 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.

 

28) 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

Ans: b and c

 

29)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

 

 

Some questions and answers on Event Handling:

1) The event delegation model, introduced in release 1.1 of the JDK, is fully compatible with the event model.

a) True

b) False

Ans: b

 

2) A component subclass that has executed enableEvents( ) to enable processing of a certain kind of event cannot also use an adapter as a listener for the same kind of event.

a) True

b) False

Ans: b

 

3) What is the highest-level event class of the event-delegation model?

Ans: The java.util.eventObject class is the highest-level class in the event-delegation hierarchy.

 

4) What interface is extended by AWT event listeners?

Ans: All AWT event listeners extend the java.util.EventListener interface.

 

5) What class is the top of the AWT event hierarchy?

Ans: The java.awt.AWTEvent class is the highest-level class in the AWT event class hierarchy.

 

6) What event results from the clicking of a button?

Ans: The ActionEvent event is generated as the result of the clicking of a button.

 

7) What is the relationship between an event-listener interface and an event-adapter class?

Ans: An event-listener interface defines the methods that must be implemented by an event

handler for a particular kind of event.

An event adapter provides a default implementation of an event-listener interface.

 

8) In which package are most of the AWT events that support the event-delegation model defined?

Ans: Most of the AWT–related events of the event-delegation model are defined in the java.awt.event package. The AWTEvent class is defined in the java.awt package.

 

9) What is the advantage of the event-delegation model over the earlier event-inheritance model?

Ans: The event-delegation has two advantages over the event-inheritance model. They are :

It enables event handling by objects other than the ones that generate the events. This

allows a clean separation between a component’s design and its use.

It performs much better in applications where many events are generated. This

performance improvement is due to the fact that the event-delegation model does not

have to repeatedly process unhandled events, as is the case of the event-inheritance

model.

 

10) What is the purpose of the enableEvents( ) method?

Ans:The enableEvents( ) method is used to enable an event for a particular object.

 

11)Which of the following are true?

a) The event-inheritance model has replaced the event-delegation model.

b) The event-inheritance model is more efficient than the event-delegation model.

c) The event-delegation model uses event listeners to define the methods of event-handling classes.

d) The event-delegation model uses the handleEvent( ) method to support event handling.

Ans: c

 

12) Which of the following is the highest class in the event-delegation model?

a) java.util.EventListener

b) java.util.EventObject

c) java.awt.AWTEvent

d) java.awt.event.AWTEvent

Ans: b

 

13) When two or more objects are added as listeners for the same event, which listener is first invoked to handle the event?

a) The first object that was added as listener.

b) The last object that was added as listener.

c) There is no way to determine which listener will be invoked first.

d) It is impossible to have more than one listener for a given event.

Ans: c

 

14) Which of the following components generate action events?

a) Buttons

b) Labels

c) Check boxes

d) Windows

Ans: a

 

15) Which of the following are true?

a) A TextField object may generate an ActionEvent.

b) A TextArea object may generate an ActionEvent.

c) A Button object may generate an ActionEvent.

d) A MenuItem object may generate an ActionEvent.

Ans: a,c and d

 

16) Which of the following are true?

a) The MouseListener interface defines methods for handling mouse clicks.

b) The MouseMotionListener interface defines methods for handling mouse clicks.

c) The MouseClickListener interface defines methods for handling mouse clicks.

d) The ActionListener interface defines methods for handling the clicking of a button.

Ans: a and d

 

17) Suppose that you want to have an object eh handle the TextEvent of a TextArea object t. How should you add eh as the event handler for t?

a) t.addTextListener(eh);

b) eh.addTextListener(t);

c) addTextListener(eh.t);

d) addTextListener(t,eh);

Ans: a

 

18) What is the preferred way to handle an object’s events in Java 2?

a) Override the object’s handleEvent( ) method.

b) Add one or more event listeners to handle the events.

c) Have the object override its processEvent( ) methods.

d) Have the object override its dispatchEvent( ) methods.

Ans: b

 

19) Which of the following are true?

a) A component may handle its own events by adding itself as an event listener.

b) A component may handle its own events by overriding its event-dispatching method.

c) A component may not handle oits own events.

d) A component may handle its own events only if it implements the handleEvent( ) method.

Ans: a and b

3S Technologies

Online Education for all