1.阅读如下代码。 请问,对语句行 test.hello(). 描述正确的有(A)
package NowCoder;
class Test {
public static void hello() {
System.out.println("hello");
}
}
public class MyApplication {
public static void main(String[] args) {
Test test=null;
test.hello();
}
}
A 能编译通过,并正确运行
B 因为使用了未初始化的变量,所以不能编译通过
C 以错误的方式访问了静态方法
D 能编译通过,但因变量为null,不能正常运行
因为Test类的hello方法是静态的,所以是属于类的,当实例化该类的时候,静态会被优先加载而且只加载一次,所以不受实例化new Test();影响,只要是使用到了Test类,都会加载静态hello方法!
另外,在其他类的静态方法中也是可以调用公开的静态方法,此题hello方法是使用public修饰的所以在MyApplication中调用hello也是可以的。
总结:即使Test test=null;这里也会加载静态方法,所以test数据中包含Test类的初始化数据。
2.在使用super和this关键字时,以下描述正确的是(A)
A 在子类构造方法中使用super()显示调用父类的构造方法,super()必须写在子类构造方法的第一 行,否则编译不通过
B super()和this()不一定要放在构造方法内第一行
C this()和super()可以同时出现在一个构造函数中
D this()和super()可以在static环境中使用,包括static方法和static语句块
B.调用super()必须写在子类构造方法的第一行,否则编译不通过。每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错。
C.this()和super()不可以同时出现在一个构造函数中,因为在构造的时候只需要调用父类的super()作为初始化父类一次,如果super(…)和this(…)同时出现的话,那么就会出现初始化父类两次的不安全操作,因为当super(…)和this(…)同时出现的时候,在调用完了super(…)之后还会执行this(…),而this(…)中又会自动调用super(),这就造成了调用两次super()的结果
D.this()和super()都指的是对象,所以,均不可以在static环境中使用。包括:static变量,static方法,static语句块。
3.如下代码的输出结果是:编译失败
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);
}
}
Java中静态变量只能在类主体中定义,不能在方法中定义。 静态变量属于类所有而不属于方法。
编程题1
有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?
import java.util.*;
public class Main{
public static int drink(int n){
int total = 0;
while(n>2){
//有n个空瓶则可以换n/3瓶饮料
total = total + n/3;
//则空瓶数量就是n/3+n%3
n = n/3 + n%3;
}
//当空瓶数量为2时,可以向老板借一瓶
if(n == 2){
total = total + 1;
}
return total;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n;
while(in.hasNext()){
n = in.nextInt();
System.out.println(drink(n));
}
}
}
编程题2
有一组数,对于其中任意两个数组,若前面一个大于后面一个数字,则这两个数字组成一个逆序对。请设计 一个高效的算法,计算给定数组中的逆序对个数。
给定一个int数组A和它的大小n,请返回A中的逆序对个数。保证n小于等于5000
public class AntiOrder {
public static int count(int a[],int n){
if (a == null|| n ==0){
return 0;
}
return mergeSortRecursion(a,0,n-1);
}
public static int mergeSortRecursion(int[] a,int left,int right){
if (left == right){
return 0;
}
int mid = (left+right)/2;
//总逆序对 = 左边的逆序对+右边的逆序对+合并后的逆序对
return mergeSortRecursion(a,left,mid)+mergeSortRecursion(a,mid+1,right)+
merge(a,left,mid,right);
}
public static int merge(int[] a,int left,int mid,int right){
int[] temp = new int[right-left+1];
int i = left;
int j = mid+1;
int inverseNum = 0;//用来保存逆序对
int index = 0;
while (i<=mid && j<=right){
if (a[i]<= a[j]){
temp[index++] = a[i++];
}else{
//a[i]>a[j]则 a[i].a[i+1]......a[mid]>a[j]
inverseNum += (mid-i+1);
temp[index++] = a[j++];
}
}
while (i<=mid){
temp[index++] = a[i++];
}
while (j<=right){
temp[index++] = a[j++];
}
for (int k=0;k<temp.length;k++){
a[left++] = temp[k];
}
return inverseNum;
}
public static void main(String[] args) {
int[] a = {1,2,3,4,5,0};
System.out.println(count(a,6));
}
}