• 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/78

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;

78 Cards in this Set

  • Front
  • Back

Given an integer variable count, write a statement that displays the value of count on the screen.Do not display anything else on the screen -- just the value of count.

System.out.println(count);

Two variables , num and cost have been declared and given values : num is an integer and cost is a double . Write a single statement that outputs num and cost to standard output . Print both values (num first, then cost), separated by a space on a single line that is terminated with a newline character . Do not output any thing else.

System.out.println(num + " " + cost + "\n");

Given a floating point variable fraction, write a statement that displays the value of fraction on the screen.Do not display anything else on the screen-- just the value of fraction.

System.out.println(fraction);

Which of the following is NOT a legal identifier?

7thheaven (begins with digit)

Which of the following IS a legal identifier?

______

An identifier that cannot be used as a variable name is a

reserved word

Before a variable is used it must be

declared

Declare an integer variable named degreesCelsius.

int degreesCelsius;

Declare two integer variables named profitStartOfQuarter and cashFlowEndOfYear.

int profitStartOfQuarter;


int cashFlowEndOfYear;

Declare a float variable named price.

float price;

Declare a double variable named netWeight.

double netWeight;

Declare two double variables , one named length with a value of 3.5 and the other named width with a value of 1.55.

double length = 3.5;


double width = 1.55;

Declare an integer variable cardsInHand and initialize it to 13.

int cardsInHand = 13;

Declare a variable populationChange, suitable for holding numbers like -593142 and 8930522.

int populationChange;

Declare a variable x, suitable for storing values like 3.14159 and 6.02E23.

float x;

Declare a variable temperature and initialize it to 98.6.

double temperature = 98.6

Declare a variable precise and initialize it to the value 1.09388641.

double precise = 1.0938864;

Write a single statement that will print the message "first is " followed by the value of first, and then a space, followed by "second = ", followed by the value of second. Print everything on one line and go to a new line after printing. Assume that first has already been declared as a double and that second has been declared as an int . Assume also that the variables have already been given values .

System.out.println("first is " + first + " second = " + second + "\n");

System.out.println(" i = " + i + " f = " + f);

Given an int variable datum that has already been declared , write a few statements that read an integer value from standard input into this variable .

Scanner input = new Scanner(System.in);


datum = input.nextInt();

Write a statement that reads an integer value from standard input into val. Assume that val has already been declared as an int variable . Assume also that stdin is a variable that references a Scanner object associated with standard input.

val = stdin.nextInt();

Write a statement that reads a floating point value from standard input into temperature. Assume that temperature. has already been declared as an double variable . Assume also that stdin is a variable that references a Scanner object associated with standard input.

temperature = stdin.nextDouble();

A location in memory used for storing data and given a name in a computer program is called a _____ because _____

A variable because the data in the location can be changed.

Of the following variable names, which is the best one for keeping track of whether a patient has a fever or not?

hasFever

Of the following variable names, which is the best one for keeping track of whether an integer might be prime or not?

mightBePrime

Which is the best identifier for a variable to represent the amount of money your boss pays you each month?

monthlyPay

Given an integer variable drivingAge that has already been declared , write a statement that assigns the value 17 to drivingAge.

drivingAge = 17;

Given two integer variables oldRecord and newRecord, write a statement that gives newRecord the same value that oldRecord has.

newRecord = oldRecord;

Given two integer variables matricAge and gradAge, write a statement that gives gradAge a value that is 4 more than the value of matricAge.

gradAge = matricAge + 4;

Given two int variables , i and j, which have been declared and initialized , and two other int variables , itemp and jtemp, which have been declared , write some code that swaps the values in i and j by copying their values to itemp and jtemp respectively, and then copying itemp and jtemp to j and i respectively.

itemp = i;


jtemp = j;


j = jtemp;


i = itemp;

Given three already declared int variables , i, j, and temp, write some code that swaps the values in i and j. Use temp to hold the value of i and then assign j's value to i. The original value of i, which was saved in temp, can now be assigned to j.

temp = i;


i = j;


j = temp;

Given two int variables , firstPlaceWinner and secondPlaceWinner, write some code that swaps their values . Declare any additional variables as necessary.

int swap_me = firstPlaceWinner;


firstPlaceWinner = secondPlaceWinner;


secondPlaceWinner = swap_me;

Given two double variables , bestValue and secondBestValue, write some code that swaps their values . Declare any additional variables as necessary.

double tempNum;


tempNum = bestValue;


bestValue = secondBestValue;


secondBestValue = tempNum;

Four integer variables , pos1, pos2, pos3, pos4 have been declared and initialized . Write the code necessary to "left rotate" their values : for each variable to get the value of the successive variable , with pos4 getting pos1's value .

int temp;


temp = pos1;


pos1 = pos2;


pos2 = pos3;


pos3 = pos4;


pos4 = temp;

Declare an integer constant , MONTHS_IN_YEAR, whose value is 12.

final int MONTHS_IN_YEAR = 12;

Declare a constant MONTHS_IN_DECADE, whose value is the value of the constant MONTHS_IN_YEAR (already declared ) multiplied by 10.

final int MONTHS_IN_DECADE = MONTHS_IN_YEAR * 10;

Declare a short variable named patientsAge.

short patientsAge;

Declare a long integer variable named grossNationalProduct.

long grossNationalProduct;

Declare and initialize the following variables :monthOfYear, initialized to the value 11


companyRevenue, initialized to the value 5666777


firstClassTicketPrice, initialized to the value 6000


totalPopulation, initialized to the value 1222333

int monthOfYear;


int companyRevenue;


short firstClassTicketPrice;


int totalPopulation;




monthOfYear = 11;


companyRevenue = 5666777;


firstClassTicketPrice = 6000;


totalPopulation = 1222333;

Write an expression that computes the sum of two variables verbalScore and mathScore (already declared and assigned values ).

verbalScore + mathScore

Given the variables taxablePurchases and taxFreePurchases (already declared and assigned values ), write an expression corresponding to the total amount purchased.

taxablePurchases + taxFreePurchases

Write an expression that computes the difference of the variables endingTime and startingTime.

endingTime - startingTime

Given the variables fullAdmissionPrice and discountAmount (already declared and assigned values ), write an expression corresponding to the price of a discount admission.

fullAdmissionPrice - discountAmount

Given the variable pricePerCase, write an expression corresponding to the price of a dozen cases.

pricePerCase * 12

Given the variables costOfBusRental and maxBusRiders of type int , write an expression corresponding to the cost per rider (assuming the bus is full). (Do not worry about any fractional part of the expression -- let integer arithmetic, with truncation, act here.)

costOfBusRental / maxBusRiders

Write an expression that computes the remainder of the variable principal when divided by the variable divisor. (Assume both are type int .)

principal % divisor

A wall has been built with two pieces of sheetrock, a smaller one and a larger one. The length of the smaller one is stored in the variable small. Similarly, the length of the larger one is stored in the variable large. Write a single expression whose value is the length of this wall.

small + large

Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the number of single dollars that would have to be paid.

price / 100

Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the amount of change (in cents) that would have to be paid.

price % 100

Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Write a statement that prints the value of price in the form "X dollars and Y cents" on a line by itself. So, if the value of price was 4321, your code would print "43 dollars and 21 cents". If the value was 501 it would print "5 dollars and 1 cents". If the value was 99 your code would print "0 dollars and 99 cents".

System.out.println(price/100 + " dollars and " + price%100 + " cents ");

Assume that an int variable x that has already been declared , and initialized to a non-negative value .Write an expression whose value is the last (rightmost) digit of x.

x % 10

You are given two variables , already declared and assigned values , one of type double , named price, containing the price of an order, and the other of type int , named totalNumber, containing the number of orders. Write an expression that calculates the total price for all orders.

totalNumber * price

Write an expression that computes the sum of two double variables total1 and total2, which have been already declared and assigned values

total1 + total2

Write an expression that computes the difference of two double variables salesSummer and salesSpring, which have been already declared and assigned values .

salesSummer - salesSpring

You are given two double variables , already declared and assigned values , named totalWeight, containing the weight of a shipment, and weightOfBox, containing the weight of the box in which a product is shipped. Write an expression that calculates the net weight of the product .

totalWeight - weightOfBox

Write a literal representing the integer value zero.

0

Write a literal representing the long integer value twelve billion.

12000000000L

Write a hexadecimal integer literal representing the value fifteen.

0xf

Write a floating point literal corresponding to the value zero.

0f

Write a literal corresponding to the floating point value one-and-a-half.

1.5f

Write a literal corresponding to the value of the first 6 digits of PI ("three point one four one five nine").

3.14159d

Write a long integer literal that has the value thirty-seven

37L

You are given two variables , already declared and assigned values , one of type double , named totalWeight, containing the weight of a shipment, and the other of type int , named quantity, containing the number of items in the shipment. Write an expression that calculates the weight of one item.

totalWeight / quantity

Write an expression that computes the average of the values 12 and 40.

(12 + 40) / 2

Write an expression that computes the integer average of the int variables exam1 and exam2 (both declared and assigned values ).

(exam1 + exam2) / 2

The dimensions (width and length) of room1 have been read into two variables : width1 and length1. The dimensions of room2 have been read into two other variables : width2 and length2. Write a single expression whose value is the total area of the two rooms.

(width1 * length1) + (width2 * length2)

Each of the walls of a room with square dimensions has been built with two pieces of sheetrock, a smaller one and a larger one. The length of all the smaller ones is the same and is stored in the variable small. Similarly, the length of all the larger ones is the same and is stored in the variable large. Write a single expression whose value is the total area of this room. DO NOT any method invocations.

(small + large) * (small + large)

Given two integer variables distance and speed, write an expression that divides distance by speed using floating point arithmetic, i.e. a fractional result should be produced.

(double) distance / speed

Calculate the average (as a double ) of the values contained in the integer variables num1, num2, num3 and assign that average to the double variable avg.




Assume the variables num1, num2, and num3 have been declared and assigned values , and the variable avg declared .

avg = (double) (num1 + num2 + num3) / 3;

Assume that children is an integer variable containing the number of children in a community and that families is an integer variable containing the number of families in the same community. Write an expression whose value is the average number of children per family.

(double) children / families

Three classes of school children are selling tickets to the school play. The number of tickets sold by these classes, and the number of children in each of the classes have been read into these variables :tickets1, tickets2, tickets3 and class1, class2, class3. Write an expression for the average number of tickets sold per school child.

(double) (tickets1 + tickets2+ tickets3) / (class1 + class2 + class3)

In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Assume that n is an integer variable whose value is some positive integer N. Assume also that hn is a variable whose value is the Nth harmonic number. Write an expression whose value is the (N+1)th harmonic number.

hn + 1.0 / (n + 1)

Given an integer variable bridgePlayers, write a statement that increases the value of that variable by 4.

bridgePlayers += 4;

Given an integer variable profits, write a statement that increases the value of that variable by a factor of 10.

profits = profits * 10;

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

strawsOnCamel++;

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

timer--;

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

19 is printed, v ends up with 20

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

s is 20 and t is 40