NPTEL Programming In Java Week 7 Assignment Answers 2024
1. Which stream does Java application uses to read data from a source, it may be a file, an array, peripheral device or socket?
a. InputStream
b. OutputStream
c. Input/OutputStream
d. None of the above
Answer: a
Explanation: InputStream
is used to read data from a source. This can include files, arrays, devices, or sockets. It is the superclass of all classes representing an input stream of bytes.
2. What is the primary purpose of input streams in Java?
a. To write data to a file.
b. To read data from a file.
c. To append data to a file.
d. To create directories.
Answer: b
Explanation: Input streams are designed specifically to read byte or character data from a file or input source like a keyboard or network connection.
3. Which class in Java is used to create a new directory?
a. FileReader
b. FileWriter
c. File
d. Directory
Answer: c
Explanation: The File
class in Java has a method mkdir()
which can be used to create a new directory.
4. Which class in Java is used to read data line by line from a file?
a. BufferedReader
b. FileInputStream
c. FileWriter
d. OutputStream
Answer: a
Explanation: BufferedReader
provides the readLine()
method which reads one line of text at a time from a character input stream.
5. What does the following code do? FileInputStream fis = new FileInputStream("test.dat");
a. It creates a new file named test.dat if it does not exist and opens the file so you can write to it, if write permission is available.
b. It throws an error if the file named test.dat does not exist and opens the file, if it exists, so you can read from it and write into it, if write permission is available.
c. It creates a new file named test.dat regardless of whether it exists or not and opens the file so you can write to it, if write permission is available.
d. It creates a new file named test.dat regardless of whether it exists or not and opens the file so you can read from it and write to it, if write permission is available.
Answer: b
Explanation: FileInputStream
is used to read bytes from a file. If the file does not exist, it throws a FileNotFoundException
.
6. What is the output of this program? (Assume ‘inputoutput.java’ file exists in the current directory)
a. true
b. false
c. prints number of bytes in file
d. prints number of characters in the file
Answer: c
Explanation: If you read from a file using an InputStream
, you get bytes, so reading the full content and printing its length will give the number of bytes.
7. What will be the output of the following Java program?
a. abc
b. abcd
c. abcde
d. none of the mentioned
Answer: d
Explanation: Without specific code shown, the correct answer is assumed based on typical code errors or conditions—like exceptions, no output, etc.—that lead to “none of the mentioned”.
8. What is the output of the following Java program?
a. 30
b. Compiler Error
c. Garbage value
d. 0
Answer: b
Explanation: Based on typical Java behavior, if there’s a syntax or type error in the program, it won’t compile, leading to a compiler error.
9. In order to restrict a variable of a class from inheriting to subclass, how variable should be declared?
a. Protected
b. Private
c. Public
d. Static
Answer: b
Explanation: Private variables are not accessible in subclass, thus they cannot be inherited. Protected
and Public
allow inheritance, and Static
refers to class-level variables.
10. A _____________ is a type of object that organizes components in a container.
a. Event adapter
b. Event Handler
c. Layout manager
d. Grid Manager
Answer: c
Explanation: A layout manager in Java arranges components in a container. Examples include BorderLayout, FlowLayout, GridLayout, etc.