Quiz on Java Array Declarations and Manipulations - Prof. Erliang Zeng, Quizzes of Javascript programming

A java programming quiz focusing on array declarations, initialization, and manipulations. It includes multiple-choice questions and a bonus question with a method to switch the contents of two arrays. Students are expected to finish the quiz in 10 minutes.

Typology: Quizzes

Pre 2010

Uploaded on 03/10/2009

koofers-user-2o9
koofers-user-2o9 🇺🇸

4.5

(2)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Quiz 10
Name: Panther ID:
Please use 10 minutes to finish this quiz.
1. [2 pts] Which of the following are valid declarations?
a. int []primes = {2, 3, 4, 5, 7};
b. int[] scores = new int[5];
c. int[] primes = /* new */ {2, 3, 4, 5, 7};
d. float elapsedTimes[] = {11.47, 12.04, 11.72};
e. char[] grades = {‘a’, ‘b’, ‘c’, ‘d’, ‘f’};
a, b, c, and e
2. [4 pts] Please use new and initializer list to create the following array: “COP”,
“2250”, “Section 5” respectively.
String [] names = new String[3];
names[0] = “COP”;
names[1] = “2250”;
names[2] = “Section 5”;
or
String [] names = {“COP”, “2250”, “Section 5”};
// you could replace any String literal with new String constructor
3. [4 pts] Write a method called averageArray that accepts an array of double values
and returns the average of the values stored in the array.
public static double averageDouble(double [] nums)
{
if (nums.length == 0) return 0;
double result = 0;
for(int i = 0; i < nums.length; i++)
result += nums[i];
return result/nums.length;
}
pf2

Partial preview of the text

Download Quiz on Java Array Declarations and Manipulations - Prof. Erliang Zeng and more Quizzes Javascript programming in PDF only on Docsity!

Quiz 10

Name: Panther ID: Please use 10 minutes to finish this quiz.

  1. [2 pts] Which of the following are valid declarations? a. int []primes = {2, 3, 4, 5, 7}; b. int[] scores = new int[5]; c. int[] primes = /* new */ {2, 3, 4, 5, 7}; d. float elapsedTimes[] = {11.47, 12.04, 11.72}; e. char[] grades = {‘a’, ‘b’, ‘c’, ‘d’, ‘f’}; a, b, c, and e
  2. [4 pts] Please use new and initializer list to create the following array: “COP”, “2250”, “Section 5” respectively. String [] names = new String[3]; names[0] = “COP”; names[1] = “2250”; names[2] = “Section 5”; or String [] names = {“COP”, “2250”, “Section 5”}; // you could replace any String literal with new String constructor
  3. [4 pts] Write a method called averageArray that accepts an array of double values and returns the average of the values stored in the array. public static double averageDouble(double [] nums) { if (nums.length == 0) return 0; double result = 0; for(int i = 0; i < nums.length; i++) result += nums[i]; return result/nums.length; }

Bonus [5pts] Write a method called switchThem that accepts two integer arrays as parameters and switches the contents of the arrays. Take into account that the arrays may be of different sizes. public static void switchThem(int [] nums1, int [] nums2) { int length = 0; if(nums1.length > nums2.length) length = nums2.length; else length = nums1.length; for(int i=0, temp=0; i<length; i++) { temp = nums1[i]; nums1[i] = nums2[i]; nums2[i] = temp; } }