Question1:
Given the code fragment:
public class Test {
static int count = 0; //static会保留值
int i = 0;
public void changeCount() {
while (i < 5) {
i++;
count++;
}
}
public static void main(String[] args) {
Test check1 = new Test(); //5
Test check2 = new Test(); //10
check1.changeCount(); //5
check2.changeCount(); //10
System.out.print(check1.count + ":" + check2.count);
}
}
What is the result?
A.10 : 10
B.5 : 5
C.5 : 10
D.Compilation fails
Answer: A
标签:static关键字
Static的值只有一份 并且会保留永远执行最新
Question2:
public static void main(String[] args) {
String product = "Pen";
product.toLowerCase(); //仅小写
product.concat(" BOX".toLowerCase()); //连接 中间有空格
System.out.println(product.substring(4, 6));
}
What is the result?
A.box
B.nbo
C.bo
D.nb
E.An exception is thrown at runtime
Answer: E
标签:String关键字
System.out.println(product.substring(4, 6));
Product是指String product = "Pen";
所以截取不到
Question3:
Which three are advantages of the Java exception mechanism?
A.Improves the program structure because the error handling code is separated fromthe normal program function
B.Provides a set of standard exceptions that covers all the possible errors
C.Improves the program structure because the programmer can choose where to handle exceptions
D.Improves the program structure because exceptions must be handled in themethod in which they occurred
E.Allows the creation of new exceptions that are tailored to the particular program being created
Answer: ACE
标签:异常
Question4:
Given the code fragment:
public class Person {
String name;
int age = 25;
public Person(String name) {
this(); // line n1 //this()里面没有参数 而方法有参数
setName(name);
}
public Person(String name, int age) {
Person(name); // line n2 //不能直接调用方法 需要new
setAge(age);
}
// setter and getter methods go here
public String show() {
return name + "" + age + "";
}
public static void main(String[] args) {
Person p1 = new Person("Jesse");
Person p2 = new Person("Walter", 52);
System.out.println(p1.show());
System.out.println(p2.show());
}
}
What is the result?
A.Jesse 25Walter 52
B.Compilation fails only at line n1
C.Compilation fails only at line n2
D.Compilation fails at both line n1 and line n2
Answer: D
标签:this关键字
This()关键字里面的参数要和上边的参数对应
不能直接调用方法名 需要new
Question5:
Given:
class Mid {
public int findMid(int n1, int n2) {
return (n1 + n2) / 2;
}
}
class Calc extends Mid {
public static void main(String[] args) {
int n1 = 22, n2 = 2;
// insert code here
}
}
Which code fragments, when inserted at // in