MyProgrammingLab 4.4

25 July 2022
4.7 (114 reviews)
62 test answers

Unlock all answers in this set

Unlock answers (58)
question
Write a String constant consisting of exactly 5 exclamation marks.
answer
"!!!!!"
question
Write a String constant consisting of exactly one character-- any character will do.
answer
"f"
question
Write a String constant that is the empty string.
answer
""
question
Declare a String variable named mailingAddress.
answer
String mailingAddress;
question
Write the declaration of a String variable named title.
answer
String title;
question
Write the declaration of three String variables named win, place, and show.
answer
String win = new String(); String place = new String(); String show = new String();
question
Write the declaration of a String variable named foreground and initialize it to "black".
answer
String foreground = new String("black");
question
Declare a String variable named oneSpace, and initialize it to a String consisting of a single space.
answer
String oneSpace = " ";
question
Write the declaration of two String variable named background and selectionColor and initialize them to "white" and "blue" respectively.
answer
String background = new String ( "white"); String selectionColor = new String ( "blue" );
question
Declare a String variable named empty, and initialize it to the empty String.
answer
String empty = "";
question
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.
answer
System.out.println("Today's Word-Of-The-Day is: " + word);
question
Assume that message is a String variable. Write a statement to display its value on standard output.
answer
System.out.println(message);
question
Assume that the String variable named foreground has already been declared. Assign it the value "red".
answer
foreground = "red";
question
Assume that theString variable named text has already been declared. Assign to it the empty string.
answer
text = "";
question
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.
answer
String temp = s1; s1 = s2; s2 = temp;
question
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".
answer
firstName + " " + middleName + " " + lastName
question
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".
answer
"http://" + address
question
Write an expression that concatenates the String variable suffix onto the end of the String variable prefix .
answer
prefix + suffix
question
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)"
answer
"(" + word + ")"
question
Given four int variables that have been declared and given values, octet1, octet2, octet3, and octet4, write a String expression whose value is the String equivalent of each these variables joined by a single period (.) So if octet1, octet2, octet3, and octet4, had the values 129, 42, 26, and 212 the expression's value would be "129.42.26.212". Alternatively, if octet1, octet2, octet3, and octet4, had the values 192, 168, 1and 44 the expression's value would be "192.168.1.44".
answer
octet1 + "." + octet2 + "." + octet3 + "." + octet4
question
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.)
answer
gold + "n" + silver + "n" + bronze
question
Given three int variables that have been declared and given values, areaCode, exchange, and lastFour, write a String expression whose value is the String equivalent of each these variables joined by a single hyphen (-) So if areaCode, exchange, and lastFour, had the values 800, 555, and 1212, the expression's value would be "800-555-1212". Alternatively, if areaCode, exchange, and lastFour, had the values 212, 867 and 5309 the expression's value would be "212-867-5309".
answer
areaCode + "-" + exchange + "-" + lastFour
question
Write a statement that reads a word from standard input into firstWord. Assume that firstWord. has already been declared as a String variable. Assume also that stdin is a variable that references a Scanner object associated with standard input.
answer
firstWord = stdin.next();
question
Assume that name and age have been declared suitably for storing names (like "Abdullah", "Alexandra" and "Zoe") and ages respectively. Assume also that stdin is a variable that references a Scanner object associated with standard input. Write some code that reads in a name and an age and then prints the message "The age of NAME is AGE" on a line by itself, where NAME and AGE are replaced by the values read in for the variables name and age. For example, if your code read in "Rohit" and 70 then it would print out "The age of Rohit is 70" on a line by itself. There should NOT be a period in the output.
answer
name = stdin.next(); age = stdin.nextInt(); System.out.print("The age of " + name + " is " + age);
question
Three business partners are forming a company whose name will be of the form "Name1, Name2 and Name3". However, they can't agree whose name should be first, second or last. Help them out by writing code that reads in their three names and prints each possible combination exactly once, on a line by itself (that is, each possible combination is terminated with a newline character). Assume that name1, name2 and name3 have already been declared and use them in your code. Assume also that stdin is a variable that references a Scanner object associated with standard input. For example, if your code read in "Larry", "Curly" and "Moe" it would print out "Larry, Curly and Moe", "Curly, Larry and Moe", etc., each on a separate line.
answer
name1 = stdin.next(); name2 = stdin.next(); name3 = stdin.next(); System.out.println(name1 + ", " + name2 + " and " + name3); System.out.println(name1 + ", " + name3 + " and " + name2); System.out.println(name2 + ", " + name1 + " and " + name3); System.out.println(name2 + ", " + name3 + " and " + name1); System.out.println(name3 + ", " + name1 + " and " + name2); System.out.println(name3 + ", " + name2 + " and " + name1);
question
Assume that name has been declared suitably for storing names (like "Amy", "Fritz" and "Moustafa"). Assume also that stdin is a variable that references a Scanner object associated with standard input. Write some code that reads a value into name then prints the message "Greetings, NAMEVALUE!!!" on a line by itself where NAMEVALUE is replaced the value that was read into name. For example, if your code read in "Hassan" it would print out "Greetings, Hassan!!!" on a line by itself.
answer
name = stdin.next(); System.out.print("Greetings, " + name + "!!!");
question
Assume that name has been declared suitably for storing names (like "Misha", "Emily" and "Sofia"). Assume also that stdin is a variable that references a Scanner object associated with standard input Write some code that reads a value into name then prints the message "Greetings, NAME" on a line by itself, where NAME is replaced the value that was read into name. For example, if your code read in "Rachel" it would print out "Greetings, Rachel" on a line by itself.
answer
name = stdin.next(); System.out.println("Greetings, " + name);
question
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.
answer
sentence.length()
question
Write an expression whose value is the number of characters in the following String: "CRAZY!nt\\n. . . . r 07''"TOOMUCH!" Suggestion: copy/paste the above String into your code-- it's too crazy to try to copy it by typing.
answer
"CRAZY!nt\\n. . . . r 07''"TOOMUCH!".length()
question
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'.
answer
name.charAt(0)
question
Given the String variable str, write an expression that evaluates to the character at index 0 of str.
answer
str.charAt(0)
question
Given the String variable name, write an expression that evaluates to the third character of name.
answer
name.charAt(2)
question
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'.
answer
name.charAt(1)
question
Write an expression that whose value is the fifth character of the String name.
answer
name.charAt(4)
question
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'.
answer
name.charAt(name.length() - 1)
question
Write an expression that evaluates to true if and only if the string variable s equals the string "end".
answer
(s.equals("end"))
question
Write an expression that evaluates to true if the value of the string variable s1 is greater than the value of string variable s2.
answer
(s1.compareTo(s2) >0)
question
Write an expression that evaluates to true if the value of variable lastName is greater than the string Dexter.
answer
(lastName.compareTo("Dexter") >0)
question
Write an expression that evaluates to true if and only if the string variable s does not equal the String literal end.
answer
!s.equals("end")
question
Given the String variables name1 and name2, write a fragment of code that assigns the larger of the two to the variable first (assume that all three are already declared and that name1 and name2 have been assigned values). (NOTE: "larger" here means alphabetically larger, not "longer". Thus, "mouse" is larger than "elephant" because "mouse" comes later in the dictionary than "elephant"!)
answer
if (name1.compareTo(name2)>0) first = name1; else first = name2;
question
Given the string variables name1, name2, and name3, write a fragment of code that assigns the largest value to the variable max (assume all three have already been declared and have been assigned values).
answer
if (name1.compareTo(name2)>0) max = name1; else max = name2; if (name3.compareTo(max)>0) max = name3;
question
Assume that s is a String . Write an expression whose value is true if and only if the value of s would come between "mortgage" and "mortuary" in the dictionary.
answer
(s.compareTo("mortgage")>0 && s.compareTo("mortuary")<0)
question
Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002. Given a variable modelYear and a String modelName write a statement that prints the message "RECALL" to standard output if the values of modelYear and modelName match the recall details.
answer
if (modelYear >= 1999 && modelYear <= 2002) { if (modelName == "Extravagant") System.out.print("RECALL"); }
question
Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002 as well all vehicles in its Guzzler line from model years 2004-2007. Given a variable modelYear and a String modelName write a statement that prints the message "RECALL" to standard output if the values of modelYear and modelName match the recall details.
answer
if (modelName == "Extravagant") { if (modelYear >= 1999 && modelYear <= 2002) System.out.println("RECALL"); } if (modelName == "Guzzler") { if (modelYear >= 2004 && modelYear <= 2007) System.out.println("RECALL"); }
question
Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002. A boolean variable named recalled has been declared. Given a variable modelYear and a String modelName write a statement that assigns true to recalled if the values of modelYear and modelName match the recall details and assigns false otherwise.
answer
if (modelName == "Extravagant" && modelYear >=1999 && modelYear <=2002) recalled = true; else recalled = false;
question
Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002 as well all vehicles in its Guzzler line from model years 2004-2007. A boolean variable named recalled has been declared. Given a variable modelYear and a String modelName write a statement that assigns true to recalled if the values of modelYear and modelName match the recall details and assigns false otherwise.
answer
if ((modelYear>=1999 && modelYear<=2002 && modelName=="Extravagant") || (modelYear>=2004 && modelYear<=2007 && modelName=="Guzzler")) { recalled=true; } else recalled=false;
question
Given the String variable address, write an expression that returns the position of the first occurrence of the String "Avenue" in address.
answer
address.indexOf("Avenue")
question
Assume that word is a variable of type String that has been assigned a value. Write an expression whose value is a String consisting of the first three characters of the value of word. So if the value of word were "dystopia" the expression's value would be "dys".
answer
word.substring(0,3)
question
Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is a String containing the first character of the value of name. So if the value of name were "Smith" the expression's value would be "S".
answer
name.substring(0,1)
question
Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is a String containing the second character of the value of name. So if the value of name were "Smith" the expression's value would be "m".
answer
name.substring(1,2)
question
Write an expression that results in a String consisting of the third through tenth characters of the String s.
answer
s.substring(2,10)
question
Assume that word is a variable of type String that has been assigned a value. Write an expression whose value is a String consisting of the last three characters of the value of word. So if the value of word were "biggest" the expression's value would be "est".
answer
word.substring(word.length() - 3,word.length())
question
Given three String variables that have been declared and given values, firstName, middleName, and lastName, write an expression whose value is the initials of the three names: the first letter of each, joined together. So if firstName, middleName, and lastName, had the values "John", "Fitzgerald", and "Kennedy", the expression's value would be JFK". Alternatively, if firstName, middleName, and lastName, had the values "Franklin", "Delano", and "Roosevelt", the expression's value would be "FDR".
answer
"" + firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0)
question
Assume that sentence is a variable of type String that has been assigned a value. Assume furthermore that this value is a String consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared, firstWord, also of type String. Write the statements needed so that the first word of the value of sentence is assigned to firstWord. So, if the value of sentence were "Broccoli is delicious." your code would assign the value "Broccoli" to firstWord.
answer
firstWord = sentence.substring(0,sentence.indexOf(" "));
question
Assume that given, middle and family are three variables of type String that have been assigned values. Write an expression whose value is a String consisting of the first character of given followed by a period followed by the first character of middle followed by a period followed by the first character of family followed by a period: in other words, the initials of the name. So if the values of these three variables were "John" "Fitzgerald" "Kennedy", then the expression's value would be "J.F.K.".
answer
given.charAt(0) + "." + middle.charAt(0) + "." + family.charAt(0) + "."
question
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.
answer
sentence.charAt(sentence.length() - 1)
question
Assume that word is a variable of type String that has been assigned a value. Assume furthermore that this value always contains the letters "dr" followed by at least two other letters. For example: "undramatic", "dreck", "android", "no-drip". Assume that there is another variable declared, drWord, also of type String. Write the statements needed so that the 4-character substring word of the value of word starting with "dr" is assigned to drWord. So, if the value of word were "George slew the dragon" your code would assign the value "drag" to drWord.
answer
drWord = word.substring(word.indexOf("dr"),4);
question
Write a sequence of statements that finds the first comma in the String line, and assigns to the variable clause the portion of line up to, but not including the comma. You may assume that an int variable pos, as well as the variables line and clause, have already been declared.
answer
pos = line.indexOf(','); clause = line.substring(0,pos);
question
Assume that sentence is a variable of type String that has been assigned a value. Assume furthermore that this value is a String consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared, secondWord, also of type String. Write the statements needed so that the second word of the value of sentence is assigned to secondWord. So, if the value of sentence were "Broccoli is delicious." your code would assign the value "is" to secondWord.
answer
int start = sentence.indexOf(" ") + 1; int end = sentence.indexOf(" ", start+1); secondWord = sentence.substring(start,end);
question
Given a String variable named sentence that has been initialized, write an expression whose value is the index of the very last character in the String referred to by sentence.
answer
sentence.length() - 1
question
Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is a String containing the last character of the value of name. So if the value of name were "Smith" the expression's value would be "h".
answer
name.substring(name.length() - 1,name.length())
question
A String variable, fullName, contains a name in one of two formats: last name, first name (comma followed by a blank), or first name last name (single blank) Extract the first name into the String variable firstName and the last name into the String variable lastName. Assume the variables have been declared and fullName already initialized. You may also declare any other necessary variables.
answer
if (fullName.indexOf(", ") != -1) { lastName = fullName.substring(0, fullName.indexOf(", ")); firstName = fullName.substring(fullName.indexOf(", ") + 2, fullName.length()); } else { firstName = fullName.substring(0, fullName.indexOf(" ")); lastName = fullName.substring(fullName.indexOf(" ") + 1, fullName.length()); }