Module 4 Self Test Questions
What is the string literal that consists of a single character A?
"A"
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
Which of the following are valid specifiers for the printf statement?
%4c %10b %6d %10.2e
Write a character literal representing a comma.
','
Write a character literal representing the digit 1.
'1'
Which of the following is the correct expression of character 4?
'4'
What is a literal for character A?
'A'
Write a literal representing the character whose unicode value is 65.
(char)65
The statement System.out.printf("%10s", 123456) outputs ___________. (Note: * represents a space)
****123456
'3' - '2' + 'm' / 'n' is ______.
1
What is the output of System.out.print ("ABCD" .indexOf("BC"))?
1
The statement System.out.printf("%3.1e", 1234.56) outputs ___________.
1.2e+03
The statement System.out.printf("%5d", 123456) outputs ___________.
123456
What is Math.floor(4.6)?
4.0
What is Math.ceil(3.6)?
4.0, Explanation: Note that ceil returns a double value
What is Math.rint(3.6)?
4.0, Explanation: Note that rint returns a double value
What is Math.round(5.4)?
5
What is the output of System.out.print ("ABCDABC". lastIndexOf("BC"))?
5
What is Math.ceil(4.1)?
5.0
What is Math.rint(4.5)?
5.0
What is Math.rint(5.4)?
5.0
What is Math.rint(5.5)?
6.0
An int variable can hold __________.
A. 'x' B. 120 Explanation: Choice (A) is also correct, because a character can be implicitly cast into an int variable. The Unicode value of character is assignment to the int variable. In this case, the code is 120 (see Appendix B).
Note that the Unicode for character A is 65. The expression 'A' + 1 evaluates to ________.
A. 66
To check if a string s contains the suffix "Java", you may write
A. if (s.endsWith("Java")) ... if (s.substring(s.length() - 4).equals("Java")) ... if (s.charAt(s.length() - 4) == 'J' && s.charAt(s.length() - 3) == 'a' && s.charAt(s.length() - 2) == 'v' && s.charAt(s.length() - 1) == 'a') ...
To check if a string s contains the prefix "Java", you may write
A. if (s.startsWith("Java")) ... B. if (s.indexOf("Java") == 0) ... C. if (s.substring(0, 4).equals("Java")) ... D. if (s.charAt(0) == 'J' && s.charAt(1) == 'a' && s.charAt(2) == 'v' && s.charAt(3) == 'a') ...
Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.
A. true
What is the output of System.out.print ("ABC". substring(0, 2))?
AB
stands for American Standard Code for Information Interchange, an 8-bit encoding scheme for representing all uppercase and lowercase letters, digits, punctuation marks, and control characters.
ASCII
To obtain the sine of 35 degrees, use _______.
B. Math.sin(Math.toRadians(35))
Suppose i is an int type variable. Which of the following statements display the character whose Unicode is stored in variable i?
B. System.out.println((char)i);
What is Math.round(3.6)?
C. 4
Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to ________.
C. A1
The expression "Java " + 1 + 2 + 3 evaluates to ________.
C. Java 123
Suppose x is a char variable with a value 'b'. What is the output of the statement System.out.println(++x)?
C. c Explanation: The ++ and -- operators can be applied to a char variable. ++x is preincrement. x is 'b' before ++x. After ++x, x becomes c.
Analyze the following code: int i = 3434; double d = 3434;System.out.printf("%5.1f %5.1f", i, d);
C. i is an integer, but the format specifier %5.1f specifies a format for double value. The code has an error.
What is the output of System.out.print ("ABCD". substring(2))?
CD
Write an expression that tests whether a character ch is a letter or a digit.
Character.isLetterOrDigit(ch)
"AbA".compareToIgnoreCase("abC") returns ___________.
D. -2 Explanation: Ignoring case, you compare aba with abc. The first two characters in the two strings are the same. The different between the last two characters is -2. The correct answer is D.
The __________ method parses a string s to a double value.
Double.parseDouble(s);
The statement System.out.printf("%3.1f", 1234.56) outputs ___________.
E. 1234.6
is to map a character to its binary representation.
Encoding
refers to the backslash character \.
Escape Character
is a special notation to represent special characters. This special notation consists of a backslash (\) followed by a character or a combination of digits.
Escape Sequence
specifies how an item should be formatted.
Format Specifier
is a method that can only be invoked from a specific object.
Instance Method
The __________ method parses a string s to an int value.
Integer.parseInt(s);
To obtain the arc sine of 0.5, use _______.
Math.asin(0.5)
Write an expression that returns the logarithm of 290.
Math.log10(290)
The Math class provides a static method, max, that accepts two int arguments and returns the value of the larger one. Two int variables, population1 and population2, have already been declared and initialized. Write an expression (not a statement!) whose value is the larger of population1 and population2 by calling max.
Math.max(population1,population2)
Write an expression that returns the sine of angle degree 36.5 using Math.sin and Math.toRadians.
Math.sin(Math.toRadians(36.5))
If a right triangle has sides of length a, b and c and if c is the largest, then it is called the hypotenuse and its length is the square root of the sum of the squares of the lengths of the shorter sides (a and b).Assume that variables a and b have been declared as doubles and that a and b contain the lengths of the shorter sides of a right triangle: write an expression for the length of the hypotenuse.
Math.sqrt(a * a + b * b)
Will System.out.println((char)4) display 4?
No
Suppose s is a string with the value "java". What will be assigned to x if you execute the following code? char x = s.charAt(4);
Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.
is a method that can be invoked without using an object.
Static Method
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 = s1; s1 = s2; s2 = temp;
What is the return value of "SELECT".substring(0, 5)?
The correct answer is B Explanation: Note that the sustring is from index 0 to 4, which is 5 - 1. The correct answer is B.
Assume x is "ABC" and y is "ABD", what is x.equals(y)?
false
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 of 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
returns true if the specified character is a digit.
isDigit(ch)
returns true if the specified character is a letter.
isLetter(ch)
returns true if the specified character is a letter or a digit.
isLetterOrDigit(ch)
returns true if the specified character is a lowercase letter.
isLowerCase(ch)
returns true if the specified character is an uppercase letter.
isUpperCase(ch)
Write an expression that evaluates to true if the value of variable lastName is greater than the string "Dexter ".
lastName.compareTo("Dexter") > 0
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)
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".
name.substring(name.length() - 1)
Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999—2002 as well as 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.
recalled = modelName.equals("Extravagant") && 1999 <= modelYear && modelYear <= 2002 || modelName.equals("Guzzler") && 2004 <= modelYear && modelYear <= 2007;
Write an expression that returns the first character in string s.
s.charAt(0)
Write an expression that evaluates to true if and only if the string variable s equals the string "end".
s.equals("end")
Write an expression that returns the length of a string s.
s.length()
Write an expression that returns a new string with all letters in uppercase from string s.
s.toUpperCase()
Write an expression that evaluates to true if the value of the string variable s1 is greater than the value of string variable s2.
s1.compareTo(s2) > 0
Assume that the String variable named text has already been declared. Assign to it the empty string.
text = "";
returns the lowercase letter for the specified character.
toLowerCase(ch)
returns the uppercase of the specified character.
toUpperCase(ch)
is an encoding scheme using two bytes. It includes ASCII code.
Unicode
refers to the characters ' ', \t, \f, \r, or \n.
Whitespace
Write an expression that returns a lowercase letter for a character ch.
Write an expression that returns a lowercase letter for a character ch.