1、A 派生出子类 B , B 派生出子类 C ,并且在 java 源代码有如下声明:问以下哪个说法是正确的?()
- A a0=new A();
- A a1=new B();
- A a2=new C();
第1行,第2行和第3行的声明都是正确的
2、看输出()
public class SystemUtil{
public static boolean isAdmin(String userId){
return userId.toLowerCase()=="admin";
}
public static void main(String[] args){
System.out.println(isAdmin("Admin"));
}
}
false “==”比较的是地址
· toLowerCase()方法不会在原字符串上更改,而是生成一个新串进行转化
3、阅读如下代码。 请问,对语句行 test.hello(). 描述正确的有()
class Test {
public static void hello() {
System.out.println("hello");
}
}
public class MyApplication {
public static void main(String[] args) {
Test test=null;
test.hello();
}
}
能编译通过,并正确运行
· 若把方法改为非静态的,
· 静态方法是类方法 与实例无关 因此在调用的时候 不需要传递对象的地址即this引用 而非静态方法是实例方法 每个实例方法都有一个默认的参数this引用 这个this将来引用的就是实例对象 因为你对象是null,也就是this是空 就报了空指针异常了
· 即:静态方法没有this 因此不会报错
而非静态方法有一个默认的this要接受对象的地址 而对象现在是空的 所以就报空指针异常了
4、在使用super和this关键字时,以下描述正确的是()
在子类构造方法中使用super()显示调用父类的构造方法,super()必须写在子类构造方法的第一行,否则编译不通过
5、如下代码的输出结果是什么?
public class Test {
public int aMethod(){
static int i = 0;
i++;
return i;
}
public static void main(String args[]){
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
编译失败
· static修饰的变量为类变量,不能放在方法内部。
6、下列叙述正确的是()?
声明抽象方法不可写出大括号
7、汽水瓶
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(canGet(n));
}
public static int canGet(int n){
int count = 0;
if(n<1||n>100){
return 0;
}
while(n>2){
count += n/3;
n = (n/3)+(n%3) ;
}
if(n == 2){
count++;
}
return count;
}
}
· n%3剩下的瓶子要加进n/3后新的n值中
8、数组中的逆序对
方法一:
简单粗暴,对脑子友好
public class Test {
public static void main(String[] args) {
int[] A = new int[]{1,2,3,4,5,6,7,0};
System.out.println(count(A,A.length));
}
public static int count(int[] a , int n ){
int count = 0;
for (int i = 0; i < n ; i++) {
for (int j = (i+1); j < n ; j++) {
if(a[i]>a[j]){
count++;
}
}
}
return count;
}
}
方法二:
分治思想,采用归并排序的思路来处理,如下图,先分后治:先把数组分隔成子数组,先统计出子数组内部的逆序对的数目,然后再统计出两个相邻子数组之间的逆序对的数目。在统计逆序对的过程中,还需要对数组进行排序,其实这个排序过程就是归并排序的思路 .
逆序对的总数=左边数组中的逆序对的数量+右边数组中逆序对的数量+左右结合成新的顺序数组时中出现的逆序对的数量;
优秀博客链接:http://www.cnblogs.com/tongkey/p/7815179.html
加油鸭~