Chapter 5

25 July 2022
4.7 (114 reviews)
18 test answers

Unlock all answers in this set

Unlock answers (14)
question
Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
answer
total=0; k=50; while (k>0) { total+=k*k; k--; }
Explanation: The given code will compute the sum of the squares of the first 50 counting numbers and store the value in the total variable. The code will loop through each number from 1 to 50, compute the square of the number, and add it to the total. The final value in the total variable will be the sum of the squares of the first 50 counting numbers.
question
Given an int variable k that has already been declared, use a while loop to print a single line consisting of 88 asterisks. Use no variables other than k.
answer
k = 1; while (k <= 88) { System.out.print('*'); k++; } System.out.println();
question
Given an int variable n that has already been declared, write some code that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
answer
n = -1; while (n < 1 || n > 10) n = stdin.nextInt();
Explanation: Given an int variable n that has already been declared, write some code that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.The code would look something like this:while (true) { System.out.print(Enter a number between 1 and 10: ")
question
Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines, write the code necessary to read in every line and print them all out on a single line, separated by a space. Hint: Use input.nextLine() to read a line and use input.hasNextLine() to test if the input has a next line available to read.
answer
while (input.hasNextLine()) { System.out.print(input.nextLine()); if (input.hasNextLine()) System.out.print(" "); } System.out.println(); Correct
question
Given an int variable n that has already been declared and initialized to a positive value, use a while loop to print a single line consisting of n asterisks. Use no variables other than n.
answer
while (n > 0) { System.out.print('*'); n--; } System.out.println();
Explanation: The code to print a single line consisting of n asterisks is as follows:while (n > 0) { System.out.print(*")
question
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, each followed by a space, and that terminates when it reads an integer that is not positive. Declare any variables that are needed. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
answer
int x; x = stdin.nextInt(); while (x > 0) { if (x > 100) System.out.print(x + " "); x = stdin.nextInt(); }
question
Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". Any value that is the same as the immediately preceding value is considered a consecutive duplicate. In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. Note that the last 3 is not a consecutive duplicate because it was preceded by a 7. Write some code that uses a loop to read such a sequence of non-negative integers , terminated by a negative number. When the code finishes executing, the number of consecutive duplicates encountered is printed. In this case, 3 would be printed. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
answer
int n, prev, consecdups = 0; prev = stdin.nextInt(); n = stdin.nextInt(); while (n >= 0) { if (n == prev) consecdups++; prev = n; n = stdin.nextInt(); } System.out.println(consecdups);
question
Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of strings and two int variables, count and longest, write the code necessary to examine all the strings in the input source and determine how long the longest string (or strings are). That value should be assigned to longest. The number of strings that are of that length should be assigned to count. For a hint on this quiz, please check https://liveexample.pearsoncmg.com/supplement/REVELProjectHint.pdf.
answer
count = 0; longest = 0; while (input.hasNext()) { String s = input.next(); if (s.length() > longest) { longest = s.length(); count = 1; } else if (s.length() == longest) count++; }
Explanation: The Scanner class has a method called nextLine() that returns the next complete line of input from the input source. The Scanner class also has a method called hasNextLine() that returns true if there is another line of input and false if there is not.We can use a while loop to iterate through all the strings in the input source. For each string, we can use the length() method to determine how long it is. If it is longer than the longest string we have seen so far, we can update the value of longest. We can also keep track of how many strings are the same length as the longest string using a variable called count.
question
Given an int variable k that has already been declared, use a do...while loop to print a single line consisting of 53 asterisks. Use no variables other than k.
answer
k = 0; do { System.out.print('*'); k++; } while(k < 53); System.out.println();
Explanation: The code would look something like this:do { cout << *"
question
Given an int variable n that has already been declared and initialized to a positive value, use a do...while loop to print a single line consisting of n asterisks. Use no variables other than n.
answer
do { System.out.print('*'); } while(--n > 0); System.out.println();
question
Given int variables k and total that have already been declared, use a do...while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
answer
total = 0; k = 50; do { total += k*k; k--; } while (k > 0);
question
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total.
answer
total = 0; k = n; do { total += k * k * k; k--; } while(k > 0);
Explanation: The sum of the cubes of the first n whole numbers is given by the equation:sum = k(k+1)(k+2)where k is the number of terms in the sum. In this case, we want the sum of the first n cubes, so k = n. We can rewrite the equation as:sum = n(n+1)(n+2)We can compute the sum using a do...while loop as follows:int n = 1; // initialize n to 1int k = n; // initialize k to nint total = 0; // initialize total to 0do { total = total + k*(k+1)*(k+2); // compute sum of first n cubes k = k + 1; // increment k by 1}while (k <= n); // repeat while k is less than or equal to n
question
Given int variables k and total that have already been declared, use a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
answer
total = 0; for (k = 1; k <= 50; k++) total += k * k; Correct
Explanation: for (k = 1; k <= 50; k++) { total = total + k*k;}
question
Write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, separated by exactly one space.
answer
for (int k = 5; k < 175; k += 5) System.out.print(k + " ");
Explanation: ;To write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, we can use the following code:for(int i=5
question
Write a for loop that prints all the even integers from 80 through 20 inclusive, separated by exactly one space.
answer
for (int k = 80; k >= 20; k -= 2) System.out.print(k + " ");
Explanation: ;The for loop should print the integers in descending order.for (int i = 80
question
Write a for loop that prints in ascending order all the positive integers less than 200 that are divisible by both 2 and 3, separated by exactly one space.
answer
for (int k = 6; k <= 200; k += 6) System.out.print(k + " ");
Explanation: ;A for loop is a type of looping statement that allows you to run a set of statements repeatedly. The for loop has the following syntax:for (initialization
question
Assume that the int variables i and j have been declared, and that n has been declared and initialized. Using for loops (you may need more than one), write code that will cause a triangle of asterisks of size n to be output to the screen. For example, if n = 3 * ** ***
answer
for (i = 1; i <= n; i++) { for(j = 1; j <= i; j++) System.out.print('*'); System.out.println(); }
Explanation: i=1;for(i=1;i<=n;i++){ for(j=1;j<=i;j++) { printf(*")
question
Write a loop that displays all possible combinations of two letters where the letters are 'a', or 'b', or 'c', or 'd', or 'e'. The combinations should be displayed in ascending alphabetical order: aa ab ac ad ae ba bb ... ee
answer
for (char i = 'a'; i <= 'e'; i++) for (char j = 'a'; j <= 'e'; j++) { System.out.print(i); System.out.println(j); }
Explanation: There are 5^2 or 25 possible combinations of two letters. We can use two nested for loops to iterate through all possible combinations. The outer loop will iterate through the 5 letters ('a', 'b', 'c', 'd', 'e') and the inner loop will iterate through the 5 letters again. We will print the letter at the current index of the outer loop followed by the letter at the current index of the inner loop. This will give us all possible combinations of two letters in ascending alphabetical order.