Week 07assignment PDF
Week 07assignment PDF
PROGRAMMING IN JAVA
Assignment7
TYPE OF QUESTION: MCQ
Number of questions: 15 Total mark: 15× 1 = 15
______________________________________________________________________________
QUESTION 1:
Which are the ways to read data from the keyboard?
Correct Answer: d
Detailed Solution:
Note: Command line input provides data to the running program form the keyboard only.
________________________________________________________________________
QUESTION 2:
Which of the following streams contains the classes which can work on character stream?
a. InputStream
b. OutputStream
c. FileReader
d. FileWriter
Correct Answer: c, d
Detailed Solution:
Note: InputSteam and OutputStram classes work on byte streams.
________________________________________________________________________
QUESTION 3:
Which of the following methods of DataInputStraem class is used to read characters from a file?
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
a. read()
b. readChar()
c. readLine()
d. readUTF()
Correct Answer: b, d
Detailed Solution:
read() method reads a byte from a file.readLine() is used to read the entire buffer from the
keyboard (as standard input file) and readUTF() read a character in unicode character encoding
format.
_________________________________________________________________________
QUESTION 4:
Which of the following classes can be used to implement the input stream that uses a character
array as the source?
a. BufferedReader
b. FileReader
c. CharArrayReader
d. FileArrayReader
Correct Answer:c
Detailed Solution:
Note: FileArrayReader creates a stream to read an array from a file, whereas CharArrayReader
class read an array of characters in memory (main memory).
_________________________________________________________________
QUESTION 5:
Which of the following methods help in clearing the contents of the buffer:
a. flush()
b. clear()
c. close()
d. exit()
Correct Answer: a
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Note: Clear() and exit() are not any valid methods defined in any class in java.io package. The
close() method closed a stream.
______________________________________________________________________________
QUESTION 6:
In which Java APIs the classes for handling all IO-streams are defined?
a. java.lang
b. java.util
c. java.io
d. java.awt
Correct Answer: c
Detailed Solution:
java.io package is meant for handling io-streams in Java program.
______________________________________________________________________________
QUESTION 7:
Which of the following statements is/ are NOT true?
a. While you are creating an instance of File class, and if you do not use the file naming
according to the convention of the local machine, the constructor will throw an exception
of class IOException.
b. A File object can be opened for both reading and writing.
c. When you create an object of Fileclass for writing, if the corresponding file does not exist
on the local file system, then it will be created.
d. Closing operation of a File object never faces any run-time error and hence need not to be
placed under try-catch block.
Correct Answer: b
Detailed Solution:
You can open a File object either in read or write mode, but not both.
______________________________________________________________________________
QUESTION 8:
Which of the following code is correct?
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
a.
FileWriterfileWriter = new FileWriter("../file.txt");
File file = new File(fileWriter );
BufferedWriterbufferedOutputWriter = new BufferedWriter(fileWriter);
b.
BufferedWriterbufferedOutputWriter = new
BufferedWriter("../file.txt");
File file = new File(bufferedOutputWriter );
FileWriterfileWriter = new FileWriter(file);
c.
File file = new File("../file.txt");
FileWriterfileWriter = new FileWriter(file);
BufferedWriterbufferedOutputWriter = new BufferedWriter(fileWriter);
d.
File file = new File("../file.txt");
BufferedWriterbufferedOutputWriter = new BufferedWriter(file);
FileWriterfileWriter = new FileWriter(bufferedOutputWriter );
Correct Answer: c
Detailed Solution:
The correct procedure would be to create a File object first, then create aFileWriter object with
File object and finally create a stream object for writing in to the File object.
______________________________________________________________________________
QUESTION 9:
Should FileReader be used to read a Java bytecode (e.g, a .class) file? Which of the following is
an appropriate answer?
Correct Answer: c
Detailed Solution:
Once can open a .class file and read its content using byte-stream class. However, the similar
attempt with the FileReader class character stream is useless.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
______________________________________________________________________________
QUESTION 10:
Which of the following statement is NOT true for RandomSAccessFile object?
Correct Answer: d
Detailed Solution:
The RandoAccessFile is related to secondary storage only.
QUESTION 11:
What method of an applet is called by the browser when it wishes to draw anything in the applet
on the display screen?
a. paint()
b. drawLine()
c. drwaString()
d. setBackground()
Correct Answer: a
Detailed Solution:
The paint() method is the method, which calls any object of class Graphics to draw in it. For
g.DrawLIne(…) to draw a line.
______________________________________________________________________________
QUESTION 12:
Which of the followingststements sets the background color of an applet to white?
a. setColor( white );
b. setColor( Color.white );
c. setBackground( Color.white );
d. setBackground( white );
Correct Answer:c
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Detailed Solution:
The method setBackground(…) is used to change the back ground color of an applet. The
argument value for this method should a constant, which is defined in class Color.
____________________________________________________________________________
QUESTION 13:
Which of the following sequence of method calls take place when an applet begins?
Correct Answer:c
Detailed Solution:
If init() is defined, then it will be executed followed by the start() and then paint()
method.
_____________________________________________________________________________
QUESTION 14:
Which of the following methods is/are must and to be override?
a. init()
b. start()
c. stop()
d. paint()
Correct Answer:d
Detailed Solution:
The init() and paint() methods are to be defined, as these two methods are abstract
methods in the class Applet. The start() method cannot be overridden as it is final.
However, an applet program can be executed successfully without any one of the applet method
in it. In this case, it will draw a blank applet.
______________________________________________________________________________
QUESTION 15:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Which of the following is a valid code to be written in an HTML file for the applet program store
in MyApplet.java file?
a.
<html>
<body>
<applet code="MyApplet.class">
</applet>
</body>
</html>
b.
<html>
<body>
<applet code="MyApplet.java" width=250 height=200>
</body>
</html>
c.
<html>
<applet code="MyApplet.class" width=250 height=200>
</applet>
</html>
d.
<html>
<body>
<applet code="MyApplet.class" height=”250”width =”200”>
</applet>
</body>
</html>
Correct Answer: c, d
Detailed Solution:
The Applet tage should include .class file with height and width parameters. They are not
necessaruily in a fixed order or to be mentioned within double codes “ “.
______________________________________________________________________________
************END************