Module 1: Data structures

Knowledge check: Arrays

Practice Assignment

1. True or False: Arrays in Java can hold elements of the same data type within the same array.

  • True

  • False

2. When the following code is executed, what value is stored in the x variable?

  • Index of the last element
  • number of elements currently in the array
  • The size of the array

  • First Index of the array

3. You’re a software developer working on a logistics application to monitor delivery efficiency. Your team lead has tasked you with creating an array to store the delivery times (in hours) for the first ten deliveries each day as part of a pilot program for performance analysis. Correctly declaring this array is crucial to avoid compilation errors and ensure smooth application operation. After reviewing the project requirements, you need to determine the proper way to declare this array in Java.

Which of the following correctly declares an array of integers with a size of 10?

  • int[] arr = new int(10);
  • int[] arr = new int[10];

  • A: int[] arr = 10;
  • String arr[10] = new int[10];

4. Complete the code in the blank below to print the second element.

  • 3
  • 1
  • 2
  • 0

5. When the code below is executed, what will be the output?

  • 1
  • 2
  • 3
  • ArrayIndexOutOfBoundException

Knowledge check: ArrayLists

Practice Assignment

6. You’re creating a messaging system where user messages are stored in an ArrayList for later retrieval and analysis. Consider the following Java code snippet, which attempts to print a specific message from the list, and identify the error:

Select the option that best describes the problem.

  • ArrayList should be imported before use.
  • The ArrayList should be initialized with a capacity.
  • ArrayList cannot store String objects.
  • The index in arrayList.get(3) is out of bounds.

7. A software development team is deciding whether to use an array or an ArrayList for storing user data in a new application. The decision hinges on the flexibility required for managing the data. Which of the following is a key difference between arrays and ArrayLists in Java that could influence their choice?

  • ArrayLists can store primitive data types directly, whereas arrays cannot.
  • Arrays do not need to be declared before use, but ArrayLists do.
  • ArrayLists are part of core Java syntax, whereas arrays are a class in the java.util package.
  • Arrays have a fixed size, whereas ArrayLists can grow or shrink dynamically.

8. An inventory management system is designed to track products by their unique ID numbers. The following code is intended to insert the product ID numbers 10, 20, and 30 into an ArrayList. However, there is a logical error in the code. Identify the line where the error occurs.

  • Line 4

  • Line 5
  • Line 3
  • Line 2

9. You’re developing a task management app where users can update their daily tasks. Suppose you need to replace an existing task in the user's task list with a new one at a specific position. Which method would you use to replace the task in an ArrayList?

  • get(int index)
  • put(int index, E element)
  • set(int index, E element)

  • add(E element)

10. A contact management app needs to store and display contacts exactly as they are entered by the user, even if some contacts have the same name.

True or False: The ArrayList data structure would be suitable for this task because it allows duplicates and maintains the insertion order of the elements.

  • True

  • False

Knowledge Check: Linked list

Practice Assignment

11. True or False: A doubly linked list allows for the traversal of the list in both forward and backward directions.

  • True

  • False

12. In the following code snippet, how would you complete the Node class to create a doubly linked list?

  • Add Node prev; to reference the previous node.

  • Add int prevData; to store the previous node’s data.
  • Add Node tail; to reference the last node.
  • Add Node head; to reference the first node.

13. In a linked list, what is the primary function of the next reference in a node?

  • It stores the data contained in the node.
  • It points to the head of the list.
  • It points to the previous node in the list.
  • It points to the next node in the sequence.

14. When comparing singly linked lists to doubly linked lists, which of the following statements is correct?

  • Doubly linked lists require more memory than singly linked lists.

  • Singly linked lists use more memory than doubly linked lists.
  • Singly linked lists are better suited for scenarios where backward traversal is needed.
  • Both singly and doubly linked lists have the same memory usage.

15. What is wrong with the following code for a singly linked list node?

  • The prev reference is not needed for a singly linked list.

  • The next reference should point to the previous node.
  • There is nothing wrong; the code is correct.
  • The data field is incorrectly named.

Knowledge check: Stacks and queues

Practice Assignment

16. Which of the following is a disadvantage of implementing a queue using a linked list compared to an array?

  • A queue with a linked list cannot be resized dynamically.
  • Linked list implementation of a queue requires more memory.

  • The queue with a linked list must be initialized with a maximum size.
  • list implementation of a queue is slower than array-based implementation.

17. True or false: A stack is a Last-in First-out (LIFO) data structure.

  • True

  • False

18. Which of the following operations are performed on a stack? Select all that apply.

  • peek()
  • sort()
  • push()
  • enqueue()

19. You are required to employ the undo and redo functionality in your text editor app. Which of the following approaches will you take? Select all that apply.

  • Array-based queue
  • Linked list-based queue
  • Array-based stack
  • Linked list-based stack

20. Consider the following declaration of Stack class:

Module Quiz: Data structures

Practice Assignment

21. Consider the following array: int[] arr = {11,22,33,44,55};

True or false: The statement arr[2] = 100;overwrites the element at index 2.

  • False
  • True

22. Consider the following code segment:

What will happen when this code is executed?

  • This would cause a Type Mismatch Exception
  • This will add the number 100in newList.
  • This would cause a compile-time error

  • This will add a string “100” in newList.

23. Which method is used to add an element to an ArrayList in Java?

  • push()
  • add()

  • pop()
  • insert()

24. Given the following class declaration for a doubly linked list:

Which of the following correctly defines the constructor?

25. Consider the following Java code:

What will be the output when this code is executed?

  • [10, 30]
  • [20,30]
  • [10, 20]
  • [10, 20, 30]

26. Which of the following terms are commonly used to describe Stack operations? Select all that apply.

  • remove
  • peek

  • insert
  • push

27. What will be the output when the following Java code is executed?

  • C:10 20
  • 10
  • Null
  • 10 20 30

28. How can you add a new element to an existing array in Java?

  • Create a new array with a size of the current array’s length + 1, copy the existing elements, and add the new element at the last index

  • Add a new element at the 0th index, and the other elements will automatically shift forward
  • Resize the array and add the new element
  • Append the new element to the existing array

29. Given the array declaration int[] numbers = new int[]{1,2,3,4,5};, which of the following is the correct way to find the length of the array?

  • numbers.length

  • Arrays.length(numbers)
  • D:length(numbers)
  • numbers.length()

30. In an array-based queue implementation, what happens when the rear pointer reaches the end of the array?

  • The rear pointer wraps around to the front if there is available space

  • A new array is created, and elements are copied over
  • An element is removed from the front to make room for a new element at the rear.
  • The size of the array automatically doubles

Leave a Reply