Comp Science Chap 2

Your page rank:

Total word count: 3090
Pages: 11

Calculate the Price

- -
275 words
Looking for Expert Opinion?
Let us have a look at your work and suggest how to improve it!
Get a Consultant

The character escape sequence to force the cursor to go to the next line is:

\n

The character escape sequence to force the cursor to advance forward to the next tab setting is:

\t

The character escape sequence to represent a single quote is:

\’

The character escape sequence to represent a double quote is:

\"

The character escape sequence to represent a backslash is:

\\

In Java, an argument is
* a logical sequence of statements proving the correctness of a program
* information that a method returns to its caller
* information provided to a method
* a verbal dispute

information provided to a method.

Before a variable is used, it must be
* defined
* declared
* imported
* evaluated
* assigned

declared

Write a literal representing the integer value zero.

0

Which of the following is NOT a legal identifier?
* outrageouslyAndShockinglyLongRunon
* _42
* __
* lovePotionNumber9
* 7thheaven

7thheaven

Which of the following IS a legal identifier?
* 5_And_10
* Five_&_Ten
* _______________
* LovePotion#9
* "Hello World"

_______________

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

monthlyPay

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

hasFever

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

mightBePrime

Declare an integer variable named degreesCelsius.

int degreesCelsius;

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

drivingAge=17;

Which of the following names in a program is equivalent to the name int?
* Int
* INT
* All of the above
* None of the above

None of the above

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);

Assume that message is a String variable. Write a statement to display its value on standard output.

System.out.println(message);

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 anything 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);

Assume that word is a String variable. Write a statement to display the message "Today’s Word-Of-The-Day is: " followed by the value of word. The message and the value of word should appear together, on a single line on standard output.

System.out.println("Today’s Word-Of-The-Day is: " + word);

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");

Given an integer variable i and a floating-point variable f, that have already been given values, write a statement that writes both of their values to standard output in the following format:

i=value -of-i f=value -of-f

Thus, if i’s value were 25 and f’s value were 12.34, the output would be:

i=25 f=12.34

But you don’t know what i’s value and f’s value are. They might be 187 and 24.06. If that’s what their values are, the output from your statement should be:

i=187 f=24.06

On the other hand, they might be 19 and 2.001. If that’s what their values are, the output from your statement should be:

i=19 f=2.001

Remember: you are GIVEN i and f– that means they are already declared and they already have values! Don’t change their values by assigning or initializing them! Just print them out the way we have shown above. Just write one statement to produce the output .

Remember: in your output you must be displaying both the name of the variable (like i) and its value .

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

Declare a short variable named patientsAge.

short patientsAge;

Declare two integer variables named profitStartOfQuarter and cashFlowEndOfYear.

int profitStartOfQuarter; int cashFlowEndOfYear;

Write a floating point literal corresponding to the value zero.

0

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

1.5

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

3.14159

Declare a float variable named price.

float price;

Declare a double variable named netWeight.

double netWeight;

Write a literal representing the false value.

false

Write a literal representing the true value .

true

Declare a variable isACustomer suitable for representing a true or false value .

boolean isACustomer;

Java represents characters using
* ASCII
* Unicode
* EBCDIC
* Morse Code
* Gray Code

Unicode

With its encoding Java can work with __________ characters.

65536

Write a character literal representing the (upper case) letter A.

‘A’

Write a character literal representing a comma.

‘,’

Write a character literal representing the digit 1.

‘1’

Write a literal representing the character whose unicode value is 65.

65

Write a literal representing the largest character value.

65535

Declare a character variable named c.

char c;

What’s the difference in UNICODE value between ‘E’ and ‘A’? (consult a table of UNICODE values):

4

What’s the difference in UNICODE value between ‘e’ and ‘a’? (consult a table of UNICODE values):

4

What’s the difference in UNICODE value between ‘3’ and ‘0’? (consult a table of UNICODE values):

3

What’s the difference in UNICODE value between ‘6’ and ‘0’? (consult a table of UNICODE values):

6

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 = 11; int companyRevenue = 5666777; int firstClassTicketPrice = 6000; int totalPopulation = 1222333;

Declare an integer variable cardsInHand and initialize it to 13.

int cardsInHand = 13;

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;

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

newRecord = oldRecord;

Declare a variable hasPassedTest, and initialize it to true.

boolean hasPassedTest = true;

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; i=jtemp; j=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 temp; temp=firstPlaceWinner; firstPlaceWinner=secondPlaceWinner; secondPlaceWinner=temp;

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

double tempValue; tempValue=bestValue; bestValue=secondBestValue; secondBestValue=tempValue;

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 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.09388641;

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

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

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

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.

price * totalNumber

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)

Which of the following is NOT an operator?

/
+
add
( )
*

add

Which of the following is NOT an operator?

%
!
+
;
*

;

Write a String constant consisting of exactly 5 exclamation marks.

"!!!!!"

Write a String constant consisting of exactly one character — any character will do.

"c"

Write a String constant that is the empty string .

""

Declare a String variable named mailingAddress.

String mailingAddress;

Write the declaration of a String variable named title.

String title;

Write the declaration of three String variables named win, place, and show.

String win; String place; String show;

Write the declaration of a String variable named foreground and initialize it to "black".

String foreground = "black";

Declare a String variable named oneSpace, and initialize it to a String consisting of a single space.

String oneSpace = " ";

Write the declaration of two String variable named background and selectionColor and initialize them to "white" and "blue" respectively.

String background = "white"; String selectionColor = "blue";

Declare a String variable named empty, and initialize it to the empty String.

String empty = "";

Assume that the String variable named foreground has already been declared. Assign it the value "red".

foreground = "red";

Assume that theString variable named text has already been declared . Assign to it the empty string .

text = "";

There are two String variables , s1 and s2, that have already been declared and initialized. Write some code that exchanges their values. Declare any other variables as necessary.

String temp = new String(); temp = s1; s1 = s2; s2 = temp;

Given a String variable address, write a String expression consisting of the string "http://" concatenated with the variable’s String value. So, if the variable refers to "www.turingscraft.com", the value of the expression would be "http://www.turingscraft.com".

"http://" + address

Write an expression that concatenates the String variable suffix onto the end of the String variable prefix.

prefix + suffix

Given a String variable word, write a String expression that parenthesizes the value of word. So, if word contains "sadly", the value of the expression would be the String "(sadly)"

"(" + word + ")"

Given three String variables that have been declared and given values, gold, silver, and bronze, write an expression whose value is the values of each these variables joined by a newline character. So if gold, silver, and bronze, had the values "Arakawa", "Cohen", and "Slutskaya", the expression, if it were printed would have the names "Arakawa", "Cohen", and "Slutskaya" each appearing on a separate line. (Do NOT print anything in this exercise: just write the expression.)

gold + "\n" + silver + "\n" + bronze

Given three String variables that have been declared and given values, firstName, middleName, and lastName, write an expression whose value is the values of each these variables joined by a single space. So if firstName, middleName, and lastName, had the values "Big", "Bill", and "Broonzy", the expression’s value would be "Big Bill Broonzy". Alternatively, if firstName, middleName, and lastName, had the values "Jerry", "Lee", and "Lewis", the expression’s value would be "Jerry Lee Lewis".

firstName + " " + middleName + " " + lastName

Given a String variable named sentence that has been initialized, write an expression whose value is the number of characters in the String referred to by sentence.

sentence.length()

Write an expression whose value is the number of characters in the following String:
"CRAZY!\n\\\t\\\\\\\\\\n. . . .\\ \\\r\007’\\’\"TOOMUCH!"

"CRAZY!\n\\\t\\\\\\\\\\n. . . .\\ \\\r\007’\\’\"TOOMUCH!".length()

Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is the first character of the value of name. So if the value of name were "Smith" the expression’s value would be ‘S’.

name.charAt(0)

Given the String variable str, write an expression that evaluates to the character at index 0 of str.

str.charAt(0)

Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is the second character of the value of name. So if the value of name were "Smith" the expression’s value would be ‘m’.

name.charAt(1)

Write an expression that whose value is the fifth character of the String name.

name.charAt(4)

Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is the last character of the value of name. So if the value of name were "Blair" the expression’s value would be ‘r’.

name.charAt(name.length() – 1)

Given the String variable address, write an expression that returns the position of the first occurrence of the String "Avenue" in address.

address.indexOf("Avenue")

Given a String variable named sentence that has been initialized, write an expression whose value is the the very last character in the String referred to by sentence.

sentence.charAt(sentence.length()-1)

Share This
Flashcard

More flashcards like this

NCLEX 10000 Integumentary Disorders

When assessing a client with partial-thickness burns over 60% of the body, which finding should the nurse report immediately? a) ...

Read more

NCLEX 300-NEURO

A client with amyotrophic lateral sclerosis (ALS) tells the nurse, "Sometimes I feel so frustrated. I can’t do anything without ...

Read more

NASM Flashcards

Which of the following is the process of getting oxygen from the environment to the tissues of the body? Diffusion ...

Read more

Unfinished tasks keep piling up?

Let us complete them for you. Quickly and professionally.

Check Price

Successful message
sending