一、选择题
(1)下列描述正确的是(B)
A.CallableStatement是PreparedStatement的父接口
B.PreparedStatement是CallableStatement的父接口
C.CallableStatement是Statement的父接口
D.PreparedStatement是Statement的父接口
PreparedStatement继承Statement,CallableStatement继承PreparedStatement
(2)下面的方法,当输入为2的时候返回值是多少()
public static int getValue(int i){
int result=0;
switch(i){
case 1:
result=result+i;
case 2:
result=result+2*i;
case 3:
result=result+3*i;
}
return result;
}
当i==2时,会进入到case 2;的分支语句中,result=0+2;由于没有break语句,会继续执行下面的代码
result=2+3*2=10;
return 10;
二、编程题
2.1 最近公共祖先
2.1.1 题目
2.1.2 题解
思路:比较a和b的值,每次都让a,b中较大的数除以2,直到两者相等,此时的节点就是最初的a和b的最近公共祖先
代码:
int getLCA(int a, int b) {
while(a!=b){
if(a>b){
a=a/2;
}else {
b=b/2;
}
}
return a;
}