Java Code Test 2

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

k = 0 ; total = 0; while (k <= n){ total = total + (k*k*k); k++; }

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.

strawsOnCamel++;

Given an integer variable strawsOnCamel, write a statement that uses the auto-increment operator to increase the value of that variable by 1.

s is 20 and t is 40

Consider this code: "int s = 20; int t = s++ + --s;". What are the values of s and t?

19 is printed, v ends up with 20

Consider this code: "int v = 20; --v; System.out.println(v++);". What value is printed, what value is v left with?

for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }

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.

k = 1; total = 0; do { total = total + (k*k); k++; } while (k <= 50);

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.

total += amount;

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.

k = n; total = 0; do { total = total + (k*k*k); k--; } while (k >= 1);

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.

total = 0; for (k = n; k >= 0; k--) total = total + (k*k*k);

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.

for (int n = 80; n >= 20; n-=2) { System.out.print(n + " "); }

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

for (int n = 1; n< 200; n++) { if (n % 2 == 0) { if (n % 3 == 0) 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.

while ( n > 0) { System.out.print("*"); n--; }

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.

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

Assume the int variables i and result have been declared but not initialized . Write a for loop header. 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.

do { n = stdin.nextInt(); } while (n>10 || n < 1);

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

do { response = stdin.next(); } while (!response.equals("Y") && !response.equals("y") && !response.equals("N") && !response.equals("n"));

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.

k = 1; do { System.out.print("*"); k++; } while (k <= 53);

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.

for(k = 0; k<97; k++) { System.out.print("*"); }

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.

k = 1; while ( k <= 88 ) { System.out.print("*"); k++; }

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.

for (j = 0; j < n; j++) { 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.

int x = stdin.nextInt(); int y = stdin.nextInt(); if (y == 0) continue; System.out.println(x / y);

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

int numberOfTimesheets; int centsPerHour = 0; int hoursWorked; total = 0; numberOfTimesheets = stdin.nextInt(); for(int i = 1; i <= numberOfTimesheets; i++) { hoursWorked = 0; centsPerHour = stdin.nextInt(); for (int ii = 1; ii <= 5; ii++) { hoursWorked = hoursWorked + stdin.nextInt(); } total = total + (hoursWorked * centsPerHour); }

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.

do { System.out.print("*"); n--; } while (n > 0);

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.

timer--;

Given an integer variable timer, write a statement that uses the auto-decrement operator to decrease the value of that variable by 1.

total = 0; for (k=1; k <= 50; k++) total = total + (k*k);

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.

k = 1 ; total = 0 ; while ( k <= 50 ) { total = total + ( k * k ) ; k = k + 1 ; }

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.

for (int n = 5; n < 175; n = n + 5) { 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 = 0; n < 40; n++) { System.out.print(n + " "); }

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

for (int count= 50; count >= 1; count--) { System.out.print(count + " "); }

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

for( int n = 11; n <= 121 ; n+=2) { System.out.print(n + " "); }

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

char[] letters = {'a', 'b', 'c', 'd', 'e'}; for(int i=0; i < 5; i++){ for(int ii=0; ii < 5 ; ii++){ System.out.print(letters[i]); System.out.println(letters[ii]); } }

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:

int number = 1; int oddTotal, evenTotal, oddCount, evenCount; oddTotal = evenTotal = oddCount = evenCount = 0; while (stdin.hasNextInt() && number > 0) { number = stdin.nextInt(); if (number % 2 == 0 && number > 0) { evenTotal += number; evenCount += 1; } if (number % 2 != 0 && number > 0) { oddTotal += number; oddCount += 1; } } System.out.println(evenTotal + " " + oddTotal + " " + evenCount + " " + oddCount);

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.

int number = 1; int total = 0; while (stdin.hasNextInt() && number > 0) { number = stdin.nextInt(); if (number % 2 == 0 && number > 0) total = total + number; } System.out.println(total);

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.

int number = 1; int oddTotal = 0; int evenTotal = 0; while (stdin.hasNextInt() && number > 0) { number = stdin.nextInt(); if (number % 2 == 0 && number > 0) evenTotal = evenTotal + number; if (number % 2 != 0 && number > 0) oddTotal = oddTotal + number; } System.out.println(evenTotal + " " + oddTotal);

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.

int number = 1; while (stdin.hasNextInt() && number > 0) { number = stdin.nextInt(); if (number % 2 == 0 && number > 0) System.out.print(number + " "); }

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.

int number = 0; while (stdin.hasNextInt() && number >= 0) { number = stdin.nextInt(); if (number > 100) System.out.print(number + " "); }

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.

if (x == y) break;

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


संबंधित स्टडी सेट्स

Psych/Mental Health Exit HESI - Saunders

View Set

NUTR 3362 Ch.1: The Basics of Nutrition

View Set

Organizational Behavior Chapter 6 Finalized

View Set

Business Law Final Exam Study Guide

View Set

Chapter 21: Peripheral Vascular System and Lymphatic System

View Set