NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
NOC25-CS57 (JAN-2025 25S)
PROGRAMMING IN JAVA
Assignment 7
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
What will be the output of the following Java program?
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:
This is Programming in Java online course.
a. Hello, World!
b. This is Programming in Java online course.
c. IOException
d. null
Correct Answer:
b. This is Programming in Java online course.
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:
What will the following code print?
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:
Complete the following snippet with the required code.
File file = new File("file.txt");
if (__________________) // Fill in the blanks
{
System.out.println("File exists.");
} else
{
System.out.println("File does not exist.");
}
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:
What will the following Java program output?
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:
What is the output of the following Java program?
class Main {
public static void main(String args[]) {
final int i;
i = 20;
i = 30;
System.out.println(i);
}
}
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:
What will be the output of the following Java program?
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
d. none of the mentioned
Correct Answer:
d. none of the mentioned
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:
What will be the output of the following Java program?
public class Calculator {
int num = 100;
public void calc(int num) {
this.num = num * 10;
}
public void printNum() {
System.out.println(num);
}
public static void main(String[] args) {
Calculator obj = new Calculator();
obj.calc(2);
obj.printNum();
}
}
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:
What will be the output of the following code?
import java.io.*;
public class W7 {
public static void main(String[] args) {
try {
PrintWriter writer = new PrintWriter(System.out);
writer.write(9 + 97);
writer.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
a. It will give compile-time error
b. It will give run-time error
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:
What will be the output of the following code.
file.txt contain "This is Programming in Java online course." (without quotes)
import java.io.File;
class FileSizeEample {
public static void main(String[] args) {
// Specify the file path
String filePath = "file.txt";
// Create a File object
File file = new File(filePath);
// Get the size of the file
long fileSize = file.length();
// Print the size of the file
System.out.println(fileSize);
}
}
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.