Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Quiz 5 Worksheet - Object Oriented Programming I | CMSC 131, Quizzes of Computer Science

Material Type: Quiz; Class: OBJECT-ORIENTED PROG I; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Quizzes

Pre 2010

Uploaded on 02/13/2009

koofers-user-rx2
koofers-user-rx2 🇺🇸

10 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download Quiz 5 Worksheet - Object Oriented Programming I | CMSC 131 and more Quizzes Computer Science in PDF only on Docsity!

CMSC 131 Quiz 5 Worksheet

The next quiz will be on Wednesday , Oct 27 (note the change from our normal quiz day!) during your lab session (either at 10 am or 11 am). The following list provides more information about the quiz:

  • You will have 25 minutes to complete the quiz.
  • It will be a written quiz (not using any computer).
  • It will be closed-book, closed-notes, and no calculator is allowed.
  • Answers must be neat and legible. We recommend that you use pencil and eraser.
  • The quiz will be based on the exercises you will find below. The quiz will ask you to write a class for a particular application.

The following exercises cover the material to be covered in Quiz #5. Solutions to these exercises will not be provided, but you are welcome to discuss your solutions with TAs and instructors during office hours.

When asked for a “piece of code” you do not need to provide an entire class definition or even an entire method. Just present the Java statements and any variable declarations as needed to make your solution clear.

Problem 1

Write a piece of code that determines whether two arrays of doubles have the same elements, but perhaps in a different order. For example, given the following arrays:

int[] a = {1.0, 4.5, 8.9, 10.0}; int[] b = {4.5, 1.0, 10.0, 8.9}; int[] c = {1.0, 4.5};

we can see that a and b are the only arrays that have the same elements.

Problem 2

Write a piece of code that determines whether the elements of an array are contained in a second array. For example, given the following two arrays:

int[] a = {10, 20}; int[] b = {5, 7, 20, 3, 10};

we can see that all the elements of a are present in b.

Problem 3

Write a piece of code that appends the contents of one array to the end of another. A new array (with a length corresponding to sum of the lengths of the originals arrays) should be created.

Problem 4

Write a piece of code that removes the first element of an array. A new array with a size that is one less the size of the original array should be created, and elements from the original array should be copied over. You may assume that the array contains at least one element to start.

Problem 5

Write a piece of code that determines whether the elements of an integer array represent an increasing sequence. For example, 2, 10, 56 represent an increasing sequence. The values 40, 2, 45 represent a non-increasing sequence

Problem 6

Write a piece of code that reverses the elements of an array in place. That is, you must perform the operation without the use of an additional array. For example, if we start with an array with elements 10, 4, 6, 8 after completing the reverse process the elements will be organized as 8, 6, 4, 10.

Problem 7

Write a piece of code that determines whether an array of chars is a palindrome. A palindrome is a word that reads the same backwards as forwards, for example the following are palindromes:

{ ‘r’, ‘a’, ‘d’, ‘a’, ‘r’ } and { ‘o’, ‘t’, ‘t’, ‘o’ }

Whereas the following is not a palindrome:

{ ‘a’, ‘b’, ‘a’, ‘b’ }

To make things simple, you may assume that the character array contains no spaces or punctuation symbols, just letters.

Problem 8

Write a piece of code that shifts the elements of an array by a particular number of positions towards the right wrapping around in the process. The following represents an example of a shift operation:

10, 20, 4, 8, 7 shifted two positions to the right 8, 7, 10, 20, 4

Hint : The % operator can prove very useful.

Problem 9

Write a piece of code similar to the one in Problem 8 which shifts elements to the left instead.

Problem 10

Write a piece of code that, given an integer array and a range of values [low…high], creates a new array which contains the elements of the original array that lie within the range. For example, if the original array is 10, 20, 40, 8, 7, 90 and the range was [8…20] the new array will contain the elements 10, 20, 8.

Problem 11

Write a piece of code that inserts an element into a particular index position of an array. The elements that follow the newly inserted element must be shifted to the right to make room. The last element of the array simply “falls off” the end. For example, suppose the original array is

34, 21, 6, 45, 6, 7

And you are asked to insert the element 99 at position 2, the result will be:

34, 21, 99, 6, 45, 6