AP-Style MC Practice QuestionsAP 风格选择题练习
Unit 1: Using Objects and Methods第 1 单元:使用对象与方法CSA
Multiple Choice)Choose the best of the four options. Assume all referenced classes and methods are imported / available unless otherwise stated. "Consider the following code segment" implies the code compiles and runs without exception unless the question states otherwise.从四个选项中选择最合适的一个。除非另有说明,假定所有引用的类(class)与方法(method)均已导入且可用。"Consider the following code segment"("考虑以下代码段")默认代码可以正常编译并运行(不抛出异常),除非题目另有说明。
Consider the following code segment.考虑以下代码段(code segment)。
int x = 17;
int y = 4;
System.out.println(x / y + x % y);
What is printed as a result of executing the code segment?运行此代码段后输出的结果是什么?
4567Which of the following expressions returns a random even integer between 2 and 10 inclusive (i.e., one of 2, 4, 6, 8, or 10)?下列哪个表达式返回 2 到 10(含端点,inclusive)之间的一个随机偶数(即 2、4、6、8 或 10 之一)?
(int)(Math.random() * 5) * 2 + 2(int)(Math.random() * 9) + 22 * ((int)(Math.random() * 6) + 1)(int)(Math.random() * 5) * 2Consider the following declarations.考虑以下变量声明(declarations)。
String a = "Java";
String b = "Ja" + "va";
String c = new String("Java");
Which row in the table correctly evaluates the listed expressions?下面哪一行正确给出了所列表达式的求值结果?
a == b is为 true; a == c is为 true; a.equals(c) is为 truea == b is为 true; a == c is为 false; a.equals(c) is为 truea == b is为 false; a == c is为 false; a.equals(c) is为 truea == b is为 false; a == c is为 false; a.equals(c) is为 falseConsider the following code segment.考虑以下代码段。
int a = 10;
int b = 4;
double r = (double) a / b + a / b;
System.out.println(r);
What is printed?输出是什么?
4.04.55.02.5Consider the following code segment.考虑以下代码段。
String s = "ABCDEFG";
String t = s.substring(2);
String u = t.substring(t.indexOf("E"));
String v = u.substring(0, u.length() - 1);
System.out.println(v + u.substring(u.length() - 1));
What is printed?输出是什么?
EFEFGEFGFGFGConsider the following code segment.考虑以下代码段。
String a = "go";
String b = a;
a = a + a;
b += a;
System.out.println(b);
What is printed?输出是什么?
gogogogogogogogogogogogogogoConsider the following expression.考虑以下表达式。
(Integer.MAX_VALUE + 1) / 2
What is the value of this expression in Java?在 Java 中,此表达式的值是多少?(提示:考虑整数溢出 integer overflow。)
1073741824-1073741824Integer.MAX_VALUErun-time error)。Consider the following code segment.考虑以下代码段。
int x = 7;
double y = 2.0;
double z = (int) (x / y * 2);
System.out.println(z);
What is printed?输出是什么?
6.07.014.0compile-time error)。Consider the following code segment.考虑以下代码段。
String s = "BAOBAB";
int i = s.indexOf("OB");
String a = s.substring(i);
String b = s.substring(i, i + a.length() - 2);
System.out.println(a.length() + b.length());
What is printed?输出是什么?
4567Consider the following code segment.考虑以下代码段。
int x = -17;
int y = 5;
System.out.println((x / y) + " " + (x % y));
What is printed?输出是什么?(注意 Java 中负数的整除与取模 modulus 的方向。)
-4 -3-3 -2-4 3-3 2Consider the following code segment.考虑以下代码段。
String a = "AB";
String b = "CD";
a = a + b;
b = a.substring(0, a.length() - b.length());
a = a.substring(b.length());
System.out.println(a + b);
What is printed?输出是什么?
ABCDCDABABABCDCDConsider the following code segment.考虑以下代码段。
int x = (int) Math.pow(-3, 3)
+ (int) Math.pow(-3, -1)
+ (int) Math.pow(0, 0);
System.out.println(x);
What is printed?输出是什么?(注意:(int) 强制类型转换会向零截断 truncate toward zero。)
-28-27-26-25Consider the following code segment.考虑以下代码段。
String s = "" + 1 + 2 + 3;
String t = 1 + 2 + 3 + "";
System.out.println(s.length() + t.length());
What is printed?输出是什么?(注意 + 操作符的左结合 left-associative 求值顺序。)
2346Consider the following code segment.考虑以下代码段。
Integer a = 5;
Integer b = 7;
int c = a + b;
System.out.println(c);
What is printed (or what happens)?输出是什么(或者会发生什么)?
1257Integer + Integer is not defined.编译错误(compile-time error):Integer + Integer 未定义。Integer cannot be auto-unboxed.运行时错误(run-time error):Integer 无法自动拆箱(auto-unboxing)。Consider the following code segment.考虑以下代码段。
String a = "apple";
String b = "Apple";
String c = "apricot";
int p = a.compareTo(b);
int q = a.compareTo(c);
System.out.println((p > 0) + " " + (q > 0));
What is printed?输出是什么?(提示:compareTo 比较的是字典序 lexicographic order,且区分大小写 case-sensitive。)
true truetrue falsefalse truefalse false