THE CP TEST MAN
double quiz score;
declare a variable that will hold a quiz score
double [ ] scores;
declare an array that will hold quiz scores
System.out.println(gpa[0]); System.out.println(gpa[6]);
output the first and last element in the gpa array.
System.out.println(gpa[3]);
output the gpa for the fourth student.
System.out.println(gpa.length);
output the length of the gpa array.
System.out.println(scores[2]);
output the third quiz score from the array
for(int i = 0; i < scores.length; i++) System.out.println(scores[i]);
use a for loop to output all of the quiz scores in the array.
int count = 0; for (int i = 0; i < gpa.length( ); i++) System.out.println(gpa[ i ]); count++;
using a for loop. Output all values in the gpa array.
scores[0] = 7.2; scores[1] = 5.0; scores[2] = 9.0;
Assign the first three quiz scores
gpa[0] = 2.0; gpa[1] = 1.0; gpa[2] = 7.0; gpa[3] = 9.0;
Assign the gpa for the first three students.
int j = 1; while (j < 10) { System.out.print(j); j += 2; }
Consider the following code segment. for (int j = 1; j < 10; j += 2) { System.out.print(j); } Which of the following code segments will produce the same output as the code segment above?
Code segment 2 will print the sum of only the odd integers from 1 through 30, inclusive because it starts k at one, increments k by twos, and terminates when k exceeds 30.
Consider the following code segments. Code segment 2 is a revision of code segment 1 in which the loop increment has been changed. Code Segment 1 int sum = 0; for (int k = 1; k <= 30; k++) { sum += k; } System.out.println("The sum is: " + sum); Code Segment 2 int sum = 0; for (int k = 1; k <= 30; k = k + 2) { sum += k; } System.out.println("The sum is: " + sum); Code segment 1 prints the sum of the integers from 1 through 30, inclusive. Which of the following best explains how the output changes from code segment 1 to code segment 2 ?
The code segments print the same values but in a different order, because code segment I iterates from 1 to 5 and code segment II iterates from 5 to 1.
Consider the following two code segments. Code segment II is a revision of code segment I in which the loop header has been changed. I. for (int k = 1; k <= 5; k++) { System.out.print(k); } II. for (int k = 5; k >= 1; k--) { System.out.print(k); } Which of the following best explains how the output changes from code segment I to code segment II?
int low = prices[ 0 ]; for (int i = 0; i < prices.length; i++) if (low > prices[ i ]{ low = prices [ i ]; } }
Create a variable that will hold the lowest value in the array. Then use a for loop to search for the lowest value in the array. Output lowest value only once.
double[ ] scores = new double [5];
Declare and create an array that will hold five quiz scores
double[ ] gpa = new double [ 7 ];
Declare and create an array that will hold the gpa for 7 students.
double[ ] items = new double [5]
Declare and create an array that will hold the prices of five items.
System.out.println(scores.length);
Output the length array for quiz scores. (DO NOT USE "( )" WITH LENGTH ON ARRAY).
int letter = 0; for (int i = 0; i < text.length( ); i++) if(text.CharAt(i) == 'a') letter++;
Write a for loop that will count the number of characters 'a' in any string. String text = " "; Output number of 'a' characters only once.
for ( int i = 0; i < score.length; i++) System.out.println(scores [ i ]);
Write a for loop that will output every value in the array.
for(int i = 0; i < text.length( ); i++) System.out.println(text.charAt(i));
Write a for loop that will output the string below one letter at a time. String text = "Friday";
for(int i = 10; i < 21; i++) System.out.println(i);
Write a for loop that will output values 10-20.
int num = 21; while(num<=31){ System.out.println(num); num += 2; }
Write a while loop that will output ODD VALUES 21-31
4
Write will be the ending value of val? int val = 2; for(int i = 1; i < 3; i++){ val*= i; }
7, 12, 17
What values will be output by the for loop below? for (int i = 5; i <= 15; i += 5){ System.out.println(i + 2); }
int num = 0; for(int i = 0; i < word.length( ); i++) if(word.charAt(i) == '#'){ num++; } System.out.println(num);
Using a for loop, search the string below for character '#'. Output the number of '#' found. String word = "Ha##y Decemb#r";
int scount = 0; for(int i = 0; i < word.length( ); i++) if(word.charAt(i) == 's'){ scount = i; } System.out.println(scount);
Using a for loop, write an algorithm that will find the index of character 's', from variable word. Then output the index. Output only once. String word = "Thursday";
for(int i = 3; i < 6; i++) System.out.println(word.charAt(i));
Using a for loop, write an algorithm that will output characters 4-6 from variable word. String word = "Halloween";