Review Problems for Midterm I

The midterm will cover the following data structures: The midterm will cover the following Java topics:

Review problems

Note: review problems will help you refresh the material. They are not sample problems.

Clarification: "write an algorithm" means that you can write in pseudocode or in English (but make sure that all details are spelled out). "write a program" means that you have to write and test a Java program.

Problem 1

You are given a stack and a queue. Write an algorithm determine which of the two has more elements. At the end the stack and the queue have to be put back into the original state. Convert your algorithm into a Java program.

Now answer the same question for a stack and a linked list, a queue and an array, two stacks, etc.

Problem 2

Write a program to create an array of 10 integer elements and initialize it to random numbers, each number between 0 and 10. Then:

Problem 3

Write a program that has two linked lists: odds and evens. The program generates 20 random numbers between 0 and 20. If the number is even, it is added to the list evens. Otherwise it is added to the list odds (the order of elements doesn't matter). Print out both lists at the end of the program to check.

Problem 4

You are given a priority queue. Write a program to reverse the priority queue using an array. More specifically, do the following:

Problem 4

Draw the object diagram for this program. What will be printed in the while loop?

public class Review {
    public static void main(String [] args) {
	Node n1 = new Node(1);
	Node n2 = new Node(2);
	Node n3 = n1;
	Node n4 = n2;

	n1.setNext(n4);
	n2.setNext(n3);

	Node n = n1;

	while (n != null) {
	    System.out.println(n.getData());
	    n = n.getNext();
	}
    }
}

Problem 5

Answer the following questions about the program below:

public class ReviewLoop {
    public static void main(String [] args) {
	int [] arr = {1, 4, 5, 7, 3, 2, 1, 8, 6};
	
	for (int i = 0; i < arr.length - 1; i = i + 2) {
	    arr[i] = arr[i] + arr[i + 1] / 2;
	}

	for (int a: arr) {
	    System.out.println(a);
	}
    }
}

These are just some of the ideas for practice problems. You may create your own along the same lines. The more you practice, the better!

That's all!

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.