Week-09 Assignment Solution
Week-09 Assignment Solution
QUESTION 1:
Which of the following statements about the Set interface in Java is FALSE?
Correct Answer: c
Detailed Solution:
Option A: True. The Set interface is part of the java.util package.
Option C: False. A Set represents an unordered collection of elements, meaning the order in which elements
are stored is not guaranteed.
Option D: True. A Set can store a null element in some implementations, such as HashSet.
____________________________________________________________________________
QUESTION 2:
Which of the following declarations is/are Incorrect?
Correct Answer: d
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Set s4 = HashSet();
● This declaration is incorrect. It attempts to create a HashSet without using the "new" keyword,
which is necessary for creating a new instance of an object in Java. The correct way to create a
HashSet and assign it to the reference variable s4 would be: Set s4 = new HashSet();
Refer week#9 slide #5
QUESTION 3:
What is the default initial capacity of a HashSet in Java, and why might you specify a
different capacity in the constructor?
Correct Answer: d
Detailed Solution:
The default initial capacity of a HashSet in Java is 16. If you expect your HashSet to contain more
than 16 elements, it’s better to specify a larger capacity in the constructor. This helps to avoid
rehashing, which occurs when the HashSet's size exceeds its capacity, requiring the table to grow
and redistribute elements. Rehashing is an expensive operation, so specifying an appropriate initial
capacity can improve performance.
QUESTION 4:
Which of the following is not a way to iterate over a HashSet in Java?
Correct Answer: d
Detailed Solution:
You can iterate over a HashSet in Java using a simple for-each loop, the iterator() method, iterator()
combined with forEachRemaining(), or using forEach() with a lambda expression. However, you
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
cannot use a traditional for loop with an index because HashSet does not maintain an ordered
collection or index-based access, as it is based on hashing.
QUESTION 5:
What is the key difference between a HashSet and a LinkedHashSet in Java?
a. HashSet is faster than LinkedHashSet for all operations
b. HashSet maintains insertion order, while LinkedHashSet does not
c. LinkedHashSet maintains the order of elements based on insertion, while HashSet does
not
d. LinkedHashSet allows duplicate elements, while HashSet does not
Correct Answer: c
Detailed Solution:
The key difference between a HashSet and a LinkedHashSet is that LinkedHashSet maintains the order of
elements in which they were inserted, using a doubly-linked list. In contrast, a HashSet does not guarantee
any specific iteration order. Both sets do not allow duplicate elements, but the iteration order is predictable
in LinkedHashSet and unpredictable in HashSet.
QUESTION 6:
Which of the following statements about the TreeSet class in Java is true?
Correct Answer: c
Detailed Solution:
TreeSet is a part of Java's collections framework that stores elements in ascending sorted order
by default. It implements the NavigableSet interface, which extends SortedSet and Set. While
TreeSet does not define its own methods, it provides fast access and retrieval times by utilizing a
tree structure for storage. Unlike LinkedHashSet, which maintains insertion order, TreeSet
automatically sorts elements.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
What is the primary function of Byte Stream classes in Java?
Correct Answer: b
Detailed Solution:
Byte Stream classes in Java are specifically designed to handle input and output operations with
raw bytes. They read bytes from an input stream and write bytes to an output stream, making them
suitable for binary data. In contrast, character stream classes handle input and output of characters
and are used for working with text.
QUESTION 8:
Which Writer stream class in Java is typically used to format and write primitive data types
and objects as text output?
a. OutputStreamWriter
b. PrintWriter
c. FileWriter
d. DataOutputStream
Correct Answer: b
Detailed Solution:
PrintWriter is a Writer stream class in Java that provides methods to write formatted text,
including primitive data types (e.g., int, float) and objects, making it ideal for text output. It can
write characters, arrays, and strings efficiently. OutputStreamWriter converts byte streams to
character streams, and FileWriter is a basic class for writing characters to a file.
DataOutputStream is for writing primitive data in a binary format, not text.
QUESTION 9:
Which of the following java.io classes allows both reading from and writing to a file at any
specific location?
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
a. File
b. FileInputStream
c. FileOutputStream
d. RandomAccessFile
Correct Answer: d
Detailed Solution:
RandomAccessFile is a class in the java.io package that allows reading from and writing to a file at
specific locations within the file. Unlike FileInputStream and FileOutputStream, which are limited to
sequential reading and writing, RandomAccessFile supports "random access," meaning you can jump to
different parts of the file and modify data at specific positions. The File class, on the other hand,
represents file path names but does not handle file content directly.
QUESTION 10:
Which of the following is true about graphical input and output in Java?
a. Java uses ByteStream for handling graphical input and output
b. The java.awt and javax.swing packages are used for graphical input and output
c. Graphical input and output in Java are handled using the Scanner class
d. The System.out class is used for graphical output in Java
Correct Answer: b
Detailed Solution:
In Java, graphical input and output are managed using the java.awt and javax.swing packages.
These packages provide classes like JFrame, JButton, JPanel, and others to create graphical user
interfaces (GUIs). These are used to display windows, capture user interactions, and manage
graphical components. Other options like ByteStream or Scanner are not related to graphical I/O.
************END************