5.5 pearson 2/21/17 – Flashcards

Unlock all answers in this set

Unlock answers
question
the body of a do..while loop is always executed at least
answer
once
question
Given an int variable k that has already been declared, use a do...while loop to print a single line consisting of 53 astericks. Use no variables other than k.
answer
k = 0; do{ System.out.print("*"); k++; }while (k < 53);
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 astericks. Use no variables other than n.
answer
do{ System.out.print("*"); n--; } while (n > 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
k = 0 ; total = 0; while (k <= n){ total = total + (k*k*k); k++; } but, this is not a do while
question
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 less than 20 and the sum is initially 0. Which is the preferred loop construct to use?
answer
do while loop
question
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?
answer
while loop
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
k = 1; total = 0; while ( k <= 50 ){ total = total + ( k * k ); k = k + 1; }
question
You need to write a loop that will repeat exactly 125 times. Which is the preferred loop construct to use?
answer
for loop
question
java In the Happy Valley School System, children are classified by age as follows: less than 2, ineligible 2, toddler 3-5, early childhood 6-7, young reader 8-10, elementary 11 and 12, middle 13, impossible 14-16, high school 17-18, scholar greater than 18, ineligible Given an int variable age, write a switch statement that prints out, on a line by itself, the appropriate label from the above list based on age.
answer
Java In the Happy Valley School System, children are classified by age as follows: less than 2, ineligible 2, toddler 3-5, early childhood 6-7, young reader 8-10, elementary 11 and 12, middle 13, impossible 14-16, high school 17-18, scholar greater than 18, ineligible Given an int variable age, write a switch statement that prints out, on a line by itself, the appropriate label from the above list based on age. Posted in: Java, Learn To Code | May 28, 201420925, Java LANGUAGE: JAVA CHALLENGE: In the Happy Valley School System, children are classified by age as follows: less than 2, ineligible 2, toddler 3-5, early childhood 6-7, young reader 8-10, elementary 11 and 12, middle 13, impossible 14-16, high school 17-18, scholar greater than 18, ineligible Given an int variable age, write a switch statement that prints out, on a line by itself, the appropriate label from the above list based on age. SOLUTION: ? switch (age){ case 0: case 1: System.out.println("ineligible"); break; case 2: System.out.println("toddler"); break; case 3: case 4: case 5: System.out.println("early childhood"); break; case 6: case 7: System.out.println("young reader"); break; case 8: case 9: case 10: System.out.println("elementary"); break; case 11: case 12: System.out.println("middle"); break; case 13: System.out.println("impossible"); break; case 14: case 15: case 16: System.out.println("high school"); break; case 17: case 18: System.out.println("scholar"); break; default: System.out.println("ineligible"); }
question
java HTTP is the protocol that governs communications between web servers and web clients (i.e. browsers). Part of the protocol includes a status code returned by the server to tell the browser the status of its most recent page request. Some of the codes and their meanings are listed below: 200, OK (fulfilled) 403, forbidden 404, not found 500, server error Given an int variable status, write a switch statement that prints out the appropriate label from the above list based on status.
answer
switch( status ){ case 200: cout << "OK (fulfilled)"; break; case 403: cout << "forbidden"; break; case 404: cout << "not found"; break; case 500: cout << "server error"; break; }
question
java Given an int variable named yesCount and another int variable named noCount and an int variable named response write the necessary code to read a value into response and then carry out the following: if the value typed in is a 1 or a 2 then increment yesCount and print out "YES WAS RECORDED" if the value typed in is a 3 or a 4 then increment noCount and print out "NO WAS RECORDED" If the input is invalid just print the message "INVALID" and do nothing else. ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.
answer
response = stdin.nextInt(); if (response == 1 || response == 2){ yesCount++; System.out.print("YES WAS RECORDED"); }else if (response == 3 || response == 4){ noCount++; System.out.print("NO WAS RECORDED"); }else System.out.print("INVALID");
question
Strings can be used in a switch statement's controlling expression and in its
answer
case labels
Get an explanation on any task
Get unstuck with the help of our AI assistant in seconds
New