• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/45

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

45 Cards in this Set

  • Front
  • Back

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.

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.

Write a statement that increases the value of the int variable total by the value of the int variable amount. That is, add the value of amount to total and assign the result to total.

total += amount;

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.

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 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 and do not modify the value of n.

Given an int variable n that has already been declared , write some code that repeatedly reads a value inton 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.

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.

Write a loop that reads positive integers from standard input, printing out those values that are even, 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.

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates , it prints out, on a line by itself, the sum of all the even integers read. Declare any variables that are needed.




ASSUME the availability of a variable , stdin, that references a Scanner object associated with standard input.

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates , it prints out, separated by a space and on a single line, the sum of all the even integers read and the sum of all the odd integers read. Declare any variables that are needed.




ASSUME the availability of a variable , stdin, that references a Scanner object associated with standard input.

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates , it prints out, on a line by itself and separated by spaces, the sum of all the even integers read, the sum of all the odd integers read, a count of the number of even integers read, and a count of the number of odd integers read, all separated by at least one space. Declare any variables that are needed.




ASSUME the availability of a variable , stdin, that references a Scanner object associated with standard input.

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of integers and an int variable named total, write the code necessary to add all the integers in the input source and place their sum into total.

Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines of text and an int variable named count, write the code necessary to count the lines of text in the input source and place that value into count.

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 it out on its own line.

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. There should NOT be a trailing space at the end of the line.

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.



Write a loop that reads strings from standard input where the string is either "land", "air", or "water". The loop terminates when "xxxxx" (five x characters ) is read in. Other strings are ignored. After the loop, your code should print out 3 lines: the first consisting of the string "land:" followed by the number of "land" strings read in, the second consisting of the string "air:" followed by the number of "air" strings read in, and the third consisting of the string "water:" followed by the number of "water" strings read in. Each of these should be printed on a separate line.




ASSUME the availability of a variable , stdin, that references a Scanner object associated with standard input.



Consider this data sequence: "fish bird reptile reptile bird bird bird mammal fish". Let's define a SINGLETON to be a data element that is not repeated immediately before or after itself in the sequence. So, here there are four SINGLETONs (the first appearance of "fish", the first appearance of "bird", "mammal", and the second appearance of "fish"). Write some code that uses a loop to read a sequence of words, terminated by the "xxxxx". The code assigns to the variable n the number of SINGLETONs that were read. (For example in the above data sequence it would assign 4 to n. Assume that n has already been declared . but not initialized . Assume that there will be at least one word before the terminating "xxxxx".




ASSUME the availability of a variable , stdin, that references a Scanner object associated with standard input.



Given a String variable response that has already been declared , write some code that repeatedly reads a value from standard input intoresponse until at last a Y or y or N or n has been entered.




ASSUME the availability of a variable , stdin, that references a Scanner object associated with standard input.

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.



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.



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.



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.



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.



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 for 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.



Assume the int variables i and result have been declared but not initialized .




Write a for loop header, i.e. something of the formfor ( . . . )




for the following loop body :




result = result * i;




When the loop terminates , result should hold the product of the odd numbers between 10 and 20.

result = 1;


for (i = 11; i < 20; i = i + 2)

Assume the int variables i,lo, hi, and result have been declared and that lo and hi have been initialized . Assume further that result has been initialized to the value 0.




Write a for loop that adds the integers from lo up through hi (inclusive), and stores the result in result.




Your code should not change the values of lo and hi. Also, do not declare any additional variables -- use only i,lo, hi, and result.

for(i = lo; i <= hi; i++)


result = result + i;

Given an int variable k that has already been declared , use a for loop to print a single line consisting of 97 asterisks. Use no variables other than k.

for(k=1;k<=97;k++)


{


System.out.print("*");


}

Given an int variable n that has already been declared and initialized to a positive value , and another int variable j that has already been declared , use a for loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j.

for (j = 0; j < n; j++)


{


System.out.print("*");


}

Write a for loop that prints the integers 0 through 39, separated by spaces.

for (int n = 0; n < 40; n++)


System.out.print(n + " ");

Write a for loop that prints the odd integers 11 through 121 inclusive, separated by spaces.

for (int n = 11; n <= 121; n = n + 2)


System.out.print(n + " ");

Write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, separated by spaces.

for (int n = 5; n < 175; n = n + 5)


System.out.print(n + " ");

Write a for loop that prints the integers 50 through 1, separated by spaces. Use no variables other than count.

for (int count = 50; count >= 1; count--)


System.out.print(count + " ");

Write a for loop that prints all the even integers from 80 through 20 inclusive, separated by spaces.

for (int n = 80; n >= 20; n = n - 2)


System.out.print(n + " ");

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 spaces.



You need to write a loop that will repeat exactly 125 times. Which is the preferred loop construct to use?

For Loop

You need to write a loop that will keep reading and adding integers to a sum, until the sum reaches or exceeds 21. The numbers are all less than 20 and the sum is initially 0. Which is the preferred loop construct to use?

Do While Loop

You need to write a loop that reads integers and adds them to a sum as long as they are positive. Once 0 or a negative value is read in your loop terminates. Which is the preferred loop construct to use?

While Loop

You have a variable, n, with a non-negative value, and need to write a loop that will keep print n blank lines. What loop construct should you use?

For Loop

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.

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



Assume the input data is structured as follows: first there is a non-negative integer specifying the number of employee timesheets to be read in. This is followed by data for each of the employees. The first number for each employee is an integer that specifies their pay per hour in cents. Following this are 5 integers , the number of hours they worked on each of the days of the workweek. Given this data, and given that an int variable total has been declared , write a loop and any necessary code that reads the data and stores the total payroll of all employees intotal. Note that you will have to add up the numbers worked by each employee and multiply that by that particular employee's pay rate to get the employee's pay for the week-- and sum those values into total.




ASSUME the availability of a variable , stdin, that references a Scanner object associated with standard input.



Write a statement that terminates the current loop when the value of the int variables x. andy.are equal

if(x == y){


break;


}

Suppose that the code below is the body of some loop. Use a continue statement to make sure that nothing is written tostandard out when y is 0.

int x = stdin.nextInt();


int y = stdin.nextInt();


if (y == 0) continue;


System.out.println(x / y);