Assignment Solution 7
Assignment Solution 7
PROGRAMMING IN JAVA
Assignment 7
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
import java.io.*;
class ReadFile {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("NPTEL.txt");
BufferedReader br = new BufferedReader(fr);
System.out.println(br.readLine());
br.close();
}
}
Assume NPTEL.txt contains:
a. Hello, World!
c. IOException
d. null
Correct Answer:
Detailed Solution:
BufferedReader to read the first line from the file NPTEL.txt. Since the file contains “This is
Programming in Java online course.”, it is printed.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
Which of these classes is used to write primitive data types to an output stream in Java?
a. FileWriter
b. DataOutputStream
c. PrintWriter
d. BufferedOutputStream
Correct Answer:
b. DataOutputStream
Detailed Solution:
The DataOutputStream class is used to write primitive data types (e.g., int, float, boolean) to
an output stream.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
import java.io.*;
class RandomAccessFileExample {
public static void main(String[] args) throws IOException {
RandomAccessFile file = new RandomAccessFile("test.dat", "rw");
file.writeInt(100);
file.seek(0);
System.out.println(file.readInt());
file.close();
}
}
a. 0
b. Runtime Error
c. Compilation Erro
d. 100
Correct Answer:
d. 100
Detailed Solution:
The program writes the integer 100 to the file test.dat. Using the seek(0) method, the file pointer
is reset to the beginning, and the integer is read back and printed.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
a. file.exists()
b. file.isFile()
c. file.fileExists()
d. file.isAvailable()
Correct Answer:
a. file.exists()
Detailed Solution:
The exists() method in the File class checks if a file or directory exists in the specified path.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
import java.io.*;
class SequenceInputStreamExample {
public static void main(String[] args) throws IOException {
ByteArrayInputStream input1 = new
ByteArrayInputStream("123".getBytes());
ByteArrayInputStream input2 = new
ByteArrayInputStream("ABC".getBytes());
SequenceInputStream sequence = new
SequenceInputStream(input1, input2);
int i;
while ((i = sequence.read()) != -1) {
System.out.print((char) i);
}
}
}
a. 123ABC
b. ABC123
c. Compilation Error
d. Runtime Error
Correct Answer:
a. 123ABC
Detailed Solution:
The SequenceInputStream combines input1 and input2 streams sequentially. It first reads the
content of input1 (123), followed by input2 (ABC), resulting in 123ABC.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
class Main {
a. 30
b. Compiler Error
c. Garbage value
d. 0
Correct Answer:
b. Compiler Error
Detailed Solution:
i is assigned a value twice. Final variables can be assigned values only one. Following is the compiler
error “Main.java:5: error: variable i might already have been assigned”.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
import java.io.*;
class Chararrayinput {
public static void main(String[] args) {
String obj = "abcdefgh";
int length = obj.length();
char c[] = new char[length];
obj.getChars(0, length, c, 0);
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 1, 4);
int i, j;
try {
while ((i = input1.read()) == (j = input2.read())) {
System.out.print((char) i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
a. abc
b. abcd
c. abcde
Correct Answer:
Detailed Solution:
No output is printed. CharArrayReader object input1 contains string “abcdefgh” whereas object input2
contains string “bcde”, when while((i=input1.read())==(j=input2.read())) is executed the starting
character of each object is compared since they are unequal control comes out of loop and nothing is
printed on the screen.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
a. 20
b. 100
c. 1000
d. 2
Correct Answer:
a. 20
Detailed Solution:
Here the class instance variable name(num) is same as calc() method local variable name(num). So for
referencing class instance variable from calc() method, this keyword is used. So in statement this.num =
num * 10, num represents local variable of the method whose value is 2 and this.num represents class
instance variable whose initial value is 100. Now in printNum() method, as it has no local variable whose
name is same as class instance variable, so we can directly use num to reference instance variable,
although this.num can be used.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
import java.io.*;
public class W7 {
public static void main(String[] args) {
try {
writer.write(9 + 97);
writer.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
c. j
d. 106
Correct Answer:
c. j
Detailed Solution:
The output of this program will be the character 'j' because the Unicode code point for 106
corresponds to 'j'.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
import java.io.File;
class FileSizeEample {
public static void main(String[] args) {
// Specify the file path
String filePath = "file.txt";
a. 42
b. 35
c. 7
d. 0
Correct Answer:
a. 42
Detailed Solution:
The length() method on the File object, which returns the size of the file in bytes.