KEMBAR78
Week 09 Assignment 2025 | PDF | Computing | Computer Data
0% found this document useful (0 votes)
0 views5 pages

Week 09 Assignment 2025

Uploaded by

kishore0331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views5 pages

Week 09 Assignment 2025

Uploaded by

kishore0331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

NPTEL Online Certification Courses

Indian Institute of Technology Kharagpur

DATA STRUCTURES AND ALGORITHMS USING JAVA


Assignment 9
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10×1 = 10
______________________________________________________________________________

QUESTION 1:
Which of the following operations are possible in sets?

a. Insertion
b. Index-based access
c. Containment check
d. Ordering by insertion

Correct Answer: a and c

Detailed Solution:
b (sets don’t use indices), d (not all sets preserve order — only LinkedHashSet does).
______________________________________________________________________________

QUESTION 2:
Consider the following program.
import java.util.*;
public class HashSetDemo {
public static void main(String[] args) {
Set<String> s = new HashSet<>();
s.add("pen");
s.add("pencil");
s.add("pen");
System.out.println(s.size());
}
}

What will be the output of the above Java program?

a. 3
b. 2
c. 1
d. Compilation error

Correct Answer: b

Detailed Solution:
pen is added only once (no duplicates in Set)
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

______________________________________________________________________________

QUESTION 3:
Which of the following is not an interface in java.io?

a. Serializable
b. Externalizable
c. Closeable
d. DataInputStream

Correct Answer: d

Detailed Solution:
DataInputStream is a class, not an interface

______________________________________________________________________________

QUESTION 4:
Which of the following statements about Java streams is NOT true?

a. InputStream is a superclass for reading byte streams.


b. OutputStreamWriter is used to convert byte streams to character streams.
c. A stream can be reused after it is closed.
d. BufferedReader is used for efficient reading of characters.

Correct Answer: c

Detailed Solution:
Once closed, a stream cannot be reused.
______________________________________________________________________________

QUESTION 5:
Which of the following statements are valid?
a. DataInputStream in = new DataInputStream(System.in);
b. BufferedReader br = new BufferedReader(new FileReader("test.txt"));
c. Writer w = new Writer("file.txt");
d. OutputStream out = new FileOutputStream("log.txt");

Correct Answer: a, b, d

Detailed Solution:
c is invalid — Writer is abstract and cannot be instantiated.

______________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 6:
Consider the following program.
import java.io.*;
public class ByteStreamTest {
public static void main(String[] args) throws IOException {
byte[] data = {65, 66, 67};
ByteArrayInputStream bis = new ByteArrayInputStream(data);
int x;
while ((x = bis.read()) != -1) {
System.out.print((char)x + " ");
}
}
}

What will be the output of the above Java program?

a. ABC
b. 65 66 67
c. ABC
d. Compilation error

Correct Answer: a

Detailed Solution:
The byte array {65, 66, 67} represents the ASCII values for the characters 'A', 'B', and 'C'. The
ByteArrayInputStream reads each byte sequentially, and each byte is explicitly cast to a char
before being printed with a space. As a result, the output is "A B C", with spaces between each character.
______________________________________________________________________________

QUESTION 7:
What makes character streams different from byte streams?
a. Character streams handle Unicode
b. Byte streams handle ASCII only
c. Character streams use Reader/Writer classes
d. Byte streams are faster for binary files

Correct Answer: a, c, and d

Detailed Solution:
Option b is misleading because byte streams are not limited to ASCII. Byte streams handle raw 8-bit binary
data, which can represent any encoding, including ASCII, UTF-8, or even custom binary formats. While
ASCII is commonly used, byte streams themselves do not enforce or restrict the data to ASCII characters.
Character streams, on the other hand, are designed to handle 16-bit Unicode characters and perform
automatic encoding/decoding.
______________________________________________________________________________

QUESTION 8:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

Consider the following program.


import java.io.*;
public class CharStreamTest {
public static void main(String[] args) throws IOException {
StringReader sr = new StringReader("Hi!");
int x;
while ((x = sr.read()) != -1) {
System.out.print((char)x + " ");
}
}
}

What will be the output of the above Java program?

a. Hi!
b. 72 105 33
c. Hi!
d. Compilation error

Correct Answer: a

Detailed Solution:
The StringReader reads characters one at a time from the string "Hi!". Each character is cast from its
Unicode code point (int) to a char and printed with a space. 'H', 'i', and '!' are printed as characters, not their
integer values (72, 105, 33), and the System.out.print((char)x + " ") statement ensures a space between
each. Therefore, the output is "H i !" and the correct answer is a.

______________________________________________________________________________

QUESTION 9:
Consider the following program.
import java.io.*;
public class FileCheck {
public static void main(String[] args) throws IOException {
File f = new File("demo.txt");
System.out.println(f.exists() ? "Exists" : "Does not exist");
f.createNewFile();
System.out.println(f.exists() ? "Exists" : "Does not exist");
}
}

What will be the output the first time you run this code (assuming demo.txt does not exist
beforehand)?

a. Exists → Exists
b. Does not exist → Exists
c. Exists → Does not exist
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

d. Compilation error

Correct Answer: b

Detailed Solution:
Initially, demo.txt does not exist, so f.exists() returns false. After calling f.createNewFile(), the file is
created (assuming no exceptions), and f.exists() returns true. Therefore, the output is "Does not exist"
followed by "Exists".

______________________________________________________________________________

QUESTION 10:
Which of the following statements about Java file operations is true?

a. FileWriter can only append to a file.


b. FileReader is used to read binary files.
c. FileWriter automatically creates the file if it doesn't exist.
d. File.exists() deletes the file from disk.

Correct Answer: c

Detailed Solution:
Option c is true — if the file does not exist, FileWriter will create it. Option a is false because FileWriter
can overwrite unless created with the append flag. Option b is false because FileReader is for character
data, not binary. Option d is entirely incorrect — File.exists() just checks for existence, it does not delete
anything.

______________________________________________________________________________

************END************

You might also like