0% found this document useful (0 votes)
4 views5 pages

Java Collections Quiz Review

This document provides a quiz to review key concepts about Java's Collection Framework. It asks 6 multiple choice questions about Lists, Maps, Sets, hashing, and binary sorting trees. The answers section then identifies which multiple choice answer is correct for each question.

Uploaded by

Fuad
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)
4 views5 pages

Java Collections Quiz Review

This document provides a quiz to review key concepts about Java's Collection Framework. It asks 6 multiple choice questions about Lists, Maps, Sets, hashing, and binary sorting trees. The answers section then identifies which multiple choice answer is correct for each question.

Uploaded by

Fuad
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

Course: Easy-to-follow Java programming

The quiz questions


Answer the questions below to review what you have learned in Video 13. You will
find the right answers after the questions in the solution part.

1. Collection Framework is…

… a lot of classes for storing elements.

… a factory that creates picture frames.

… a tool set that is for creating new classes.

… a lot of classes and algorithms that are in connection with storing


elements.

2. What is true for List?

Lists are similar to arrays.

Size of List is fixed.

List can contain any values.

You can use [] for indexing elements just like in the case of arrays.

Lists are like phone books.

Lists contain elements in specified order.

List is subclass of Collection.

Duckademy IT courses – [Link]


3. What is true for Map?

Maps are similar to arrays.

Size of Map is fixed.

Keys and values of Maps have to be objects.

You can use [] for indexing elements just like in the case of arrays.

Maps are like phone books.

All Maps contain elements in the insert order.

Map is subclass of Collection.

4. What is true for Set?

Sets are similar to arrays but they do not store element order.

Size of Set is flexible.

Values of Sets have to be objects.

You can use [] for indexing elements just like in the case of arrays.

Sets are like phone books.

The order of elements is based on the actual implementation.

Set is subclass of Collection.

5. What is true for hashing?

Hashing is for specifying the position of the elements based on the


value.

Hashing is for accelerating retrieval of elements.

Hasing can be slow in corner cases.

Hashing is only for storing numbers.

Java uses bucket hash in the case of HashSets and HashMaps.

If two objects are equal, their hash codes have to be equal.

Duckademy IT courses – [Link]


6. What is true for binary sorting trees?

They contain elements in a form in which every element has 2 or 3


children.

Left elements of the binary sorting tree is less than the right elements.

Every element has exactly 2 children.

Binary trees can be unbalanced.

You can put elements in a binary sorting tree wherever you want.

A tree is a linear structure.

Duckademy IT courses – [Link]


----------------------------------------------------------------------------------------------------------------

The answers

1. Collection Framework is…

… a lot of classes for storing elements.

… a factory that creates picture frames.

… a tool set that is for creating new classes.

X … a lot of classes and algorithms that are in connection with


storing elements.

2. What is true for List?

X Lists are similar to arrays.

Size of List is fixed.

List can contain any values.

You can use [] for indexing elements just like in the case of arrays.

Lists are like phone books.

X Lists contain elements in specified order.

X List is subclass of Collection.

3. What is true for Map?

Maps are similar to arrays.

Size of Map is fixed.

X Keys and values of Maps have to be objects.

You can use [] for indexing elements just like in the case of arrays.

X Maps are like phone books.

All Maps contain elements in the insert order.

Map is subclass of Collection.

Duckademy IT courses – [Link]


4. What is true for Set?

X Sets are similar to arrays but they do not store element order.

Size of Set is flexible.

X Values of Sets have to be objects.

You can use [] for indexing elements just like in the case of arrays.

Sets are like phone books.

X The order of elements is based on the actual implementation.

X Set is subclass of Collection.

5. What is true for hashing?

X Hashing is for specifying the position of the elements based on


the value.

X Hashing is for accelerating retrieval of elements.

X Hasing can be slow in corner cases.

Hashing is only for storing numbers.

Java uses bucket hash in the case of HashSets and HashMaps.

X If two objects are equal, their hash codes have to be equal.

6. What is true for binary sorting trees?

They contain elements in a form in which every element has 2 or 3


children.

X Left elements of the binary sorting tree is less than the right
elements.

X Every element has exactly 2 children.

X Binary trees can be unbalanced.

You can put elements in a binary sorting tree wherever you want.

A tree is a linear structure.

Duckademy IT courses – [Link]

Common questions

Powered by AI

The Collection Framework in Java is a unified architecture for representing and manipulating collections. It is important because it provides a set of classes and algorithms that facilitate the operation of storing and managing groups of objects. This unified approach allows for consistent interface implementation, improving the efficiency of code reusability and manipulation .

Sets in Java are different from arrays in that they do not maintain a set order of elements. The order of elements in a Set is determined by its implementation, such as HashSet or TreeSet, which handle elements using hash codes or natural ordering/comparator. This flexibility and unordered nature allow Sets to efficiently manage unique elements without duplicates, contrasting with arrays that keep a strict order .

In Java Collections, the 'order of elements' is crucial for operations and use-cases. Lists maintain the order of insertion, making them suitable for situations where element sequence is important, like history lists or undo stacks. In contrast, Sets do not guarantee order, which is beneficial for maintaining collections of unique elements without worry of duplicates or sequence, such as in implementing feature toggles or flag sets .

Keys and values in a Map must be objects because the Map interface in Java is designed to store associations between object instances (key-value pairs), rather than primitive data types. This requirement affects a Map's functionality by enabling it to leverage methods inherent to objects, such as hash codes (for hashing), and the ability to implement custom objects with overridden equal checks, ensuring correctness and flexibility in accessing and storing data .

A binary sorting tree (BST) organizes elements such that each node has at most two children, classified typically as 'left' and 'right'. The left child node contains elements less than the parent node, and the right child, elements greater than the parent node. Performance is influenced by balance; a balanced tree provides logarithmic time complexity for operations like search, insert, and delete. In contrast, an unbalanced tree can degrade performance to linear time, similar to a linked list .

Maps are similar to phone books in that both store paired information—for Maps, these are key-value pairs—and phone books, names and phone numbers. However, unlike phone books, Maps are not limited to one-to-one mappings and can handle complex objects as keys or values, providing versatility. This analogy informs real-world applications like caching, configuration management, and associative arrays where quick retrieval and update of data is vital .

Hashing in Java collections works by computing a hash code from an object, which is then used to determine the position of elements (buckets) in hash structures like HashMap or HashSet. Hashing accelerates retrieval and storage operations by minimizing the need for linear searches. However, it can become inefficient in corner cases where many elements hash to the same bucket, leading to longer chains or lists that need to be traversed, slowing down access times .

Lists in Java differ from arrays primarily in their ability to change size; Lists are dynamic and can grow or shrink, whereas arrays have a fixed size once initialized. Key properties of Lists include their ability to contain elements in a specified order and being a subclass of Collection. Unlike arrays, Lists do not support array-like indexing using [], though they can be accessed via methods like get(int index).

The non-linear structure of a binary tree allows it to represent hierarchical data efficiently, such as file systems and organizational charts. This structure provides strengths, such as balanced trees offering O(log n) operations. However, weaknesses arise when binary trees become unbalanced, leading to a worst-case linear time complexity akin to a linked list, affecting performance in operations such as search, insert, and delete .

The requirement for 'keys and values to be objects' in Java Maps enhances type safety by allowing the use of generics that enforce compile-time type checks, reducing runtime errors. This requirement also affords design flexibility, as developers can define complex custom objects as keys or values, implementing methods such as equals and hashCode, which allow for nuanced control over how keys are compared and stored, crucial for advanced data structures and algorithms .

You might also like