Introduction to Java Programming, AP Version, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors from the Instructor's Resource Website.

Chapter 4 Mathematical Functions, Characters, and Strings


Section 4.2 Common Mathematical Functions
4.1  What is Math.round(3.6)?
A. 3.0
B. 3
C. 4
D. 4.0

4.2  What is Math.rint(3.6)?
A. 3.0
B. 3
C. 4.0
D. 5.0

4.3  What is Math.rint(3.5)?
A. 3.0
B. 3
C. 4
D. 4.0
E. 5.0

4.4  What is Math.ceil(3.6)?
A. 3.0
B. 3
C. 4.0
D. 5.0

4.5  What is Math.floor(3.6)?
A. 3.0
B. 3
C. 4
D. 5.0

4.6  To obtain the sine of 35 degrees, use _______.
A. Math.sin(35)
B. Math.sin(Math.toRadians(35))
C. Math.sin(Math.toDegrees(35))
D. Math.sin(Math.toRadian(35))
E. Math.sin(Math.toDegree(35))

4.7  To obtain the arc sine of 0.5, use _______.
A. Math.asin(0.5)
B. Math.asin(Math.toDegrees(0.5))
C. Math.sin(Math.toRadians(0.5))
D. Math.sin(0.5)

4.8  Math.asin(0.5) returns _______.
A. 30
B. Math.toRadians(30)
C. Math.PI / 4
D. Math.PI / 2

4.9  Math.sin(Math.PI) returns _______.
A. 0.0
B. 1.0
C. 0.5
D. 0.4

4.10  Math.cos(Math.PI) returns _______.
A. 0.0
B. 1.0
C. -1.0
D. 0.5

Section 4.3 Character Data Type and Operations
4.11  Which of the following is the correct expression of character 4?
A. 4
B. "4"
C. '\0004'
D. '4'

4.12  A Java character is stored in __________.
A. one byte
B. two bytes
C. three bytes
D. four bytes

4.13  Suppose x is a char variable with a value 'b'. What is the output of the statement System.out.println(++x)?
A. a
B. b
C. c
D. d

4.14  Which of the following statement prints smith\exam1\test.txt?
A. System.out.println("smith\exam1\test.txt");
B. System.out.println("smith\\exam1\\test.txt");
C. System.out.println("smith\"exam1\"test.txt");
D. System.out.println("smith"\exam1"\test.txt");

4.15  Suppose i is an int type variable. Which of the following statements display the character whose Unicode is stored in variable i?
A. System.out.println(i);
B. System.out.println((char)i);
C. System.out.println((int)i);
D. System.out.println(i + " ");

4.16  The Unicode of 'a' is 97. What is the Unicode for 'c'?
A. 96
B. 97
C. 98
D. 99

4.17  Will System.out.println((char)4) display 4?
A. Yes
B. No

4.18  What is the output of System.out.println('z' - 'a')?
A. 25
B. 26
C. a
D. z

4.19  An int variable can hold __________.
A. 'x'
B. 120
C. 120.0
D. "x"
E. "120"

4.20  Which of the following assignment statements is correct?
A. char c = 'd';
B. char c = 100;
C. char c = "d";
D. char c = "100";

4.21  '3' - '2' + 'm' / 'n' is ______.
A. 0
B. 1
C. 2
D. 3

4.22  To check whether a char variable ch is an uppercase letter, you write ___________.
A. (ch >= 'A' && ch >= 'Z')
B. (ch >= 'A' && ch <= 'Z')
C. (ch >= 'A' || ch <= 'Z')
D. ('A' <= ch <= 'Z')

4.23  Which of the following is not a correct method in the Character class?
A. isLetterOrDigit(char)
B. isLetter(char)
C. isDigit()
D. toLowerCase(char)
E. toUpperCase()

4.24  Suppose Character x = new Character('a'), __________________ returns true.
A. x.equals(new Character('a'))
B. x.compareToIgnoreCase('A')
C. x.equalsIgnoreCase('A')
D. x.equals('a')
E. x.equals("a")

Section 4.4 The String Type
4.25  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);
A. 'a'
B. 'v'
C. Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.

4.26  Suppose s1 and s2 are two strings. Which of the following statements or expressions is incorrect?
A. String s3 = s1 - s2;
B. boolean b = s1.compareTo(s2);
C. char c = s1[0];
D. char c = s1.charAt(s1.length());

4.27  Suppose s1 and s2 are two strings. What is the result of the following code?

    s1.equals(s2) == s2.equals(s1)
A. true
B. false

4.28  "abc".compareTo("aba") returns ___________.
A. 1
B. 2
C. -1
D. -2
E. 0

4.29  "AbA".compareToIgnoreCase("abC") returns ___________.
A. 1
B. 2
C. -1
D. -2
E. 0

4.30  ____________________ returns true.
A. "peter".compareToIgnoreCase("Peter")
B. "peter".compareToIgnoreCase("peter")
C. "peter".equalsIgnoreCase("Peter")
D. "peter".equalsIgnoreCase("peter")
E. "peter".equals("peter")

4.31  What is the return value of "SELECT".substring(0, 5)?
A. "SELECT"
B. "SELEC"
C. "SELE"
D. "ELECT"

4.32  What is the return value of "SELECT".substring(4, 4)?
A. an empty string
B. C
C. T
D. E

4.33  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') ...

4.34  To check if a string s contains the suffix "Java", you may write
A. if (s.endsWith("Java")) ...
B. if (s.lastIndexOf("Java") >= 0) ...
C. if (s.substring(s.length() - 4).equals("Java")) ...
D. if (s.substring(s.length() - 5).equals("Java")) ...
E. 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') ...

4.35  Which of the following is the correct statement to return JAVA?
A. toUpperCase("Java")
B. "Java".toUpperCase("Java")
C. "Java".toUpperCase()
D. String.toUpperCase("Java")

4.36  The expression "Java " + 1 + 2 + 3 evaluates to ________.
A. Java123
B. Java6
C. Java 123
D. java 123
E. Illegal expression

4.37  Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to ________.
A. 66
B. B
C. A1
D. Illegal expression

4.38  Note that the Unicode for character A is 65. The expression 'A' + 1 evaluates to ________.
A. 66
B. B
C. A1
D. Illegal expression

4.39  The __________ method parses a string s to an int value.
A. integer.parseInt(s);
B. Integer.parseInt(s);
C. integer.parseInteger(s);
D. Integer.parseInteger(s);

4.40  The __________ method parses a string s to a double value.
A. double.parseDouble(s);
B. Double.parsedouble(s);
C. double.parse(s);
D. Double.parseDouble(s);

Section 4.6 Formatting Console Output
4.41  Which of the following are valid specifiers for the printf statement?
A. %4c
B. %10b
C. %6d
D. %8.2d
E. %10.2e

4.42  The statement System.out.printf("%3.1f", 1234.56) outputs ___________.
A. 123.4
B. 123.5
C. 1234.5
D. 1234.56
E. 1234.6

4.43  The statement System.out.printf("%3.1e", 1234.56) outputs ___________.
A. 0.1e+04
B. 0.123456e+04
C. 0.123e+04
D. 1.2e+03
E. 1.23+03

4.44  The statement System.out.printf("%5d", 123456) outputs ___________.
A. 12345
B. 23456
C. 123456
D. 12345.6

4.45  The statement System.out.printf("%10s", 123456) outputs ___________. (Note: * represents a space)
A. 123456****
B. 23456*****
C. 12345*****
D. ****123456

4.46  Analyze the following code:

int i = 3434double d = 3434;
System.out.printf("%5.1f %5.1f", i, d);
A. The code compiles and runs fine to display 3434.0 3434.0.
B. The code compiles and runs fine to display 3434 3434.0.
C. i is an integer, but the format specifier %5.1f specifies a format for double value. The code has an error.