天天看点

java基础9-18考试总结

选择题

1.给定Java代码,如下:

byte b1 = 5;

byte b2 = 2;

byte a = _____;

要使用这段代码能够编译成功,横线处可以填入(A)

A.(byte)(b1-b2) B. (byte) b1-b2 C. b1-b2 D. (byte) b1/b2

2.执行如下程序代码后,c的值是(C)

int a=0;

int b=0;

int c=0;

do{

–c;

a=a-1;

}while(a>0);

A.0 B. 1 C. -1 D. 死循环

3.设有定义 int i = 6,则执行以下语句后,i的值为(C)

i += i - 1;

A.10 B.12 C.11 D.100

4.以下程序的运行结果是(C)。

public class Increment {

public static void main(String[] args) {

int c;

c = 2;

System.out.println©;

System.out.println(c++);

System.out.println©;

}

}

A、2 B、 2 C、 2 D、3

2 3 2 4

2 3 3 4

5.执行如下代码,运行结果为(A)。

public class Test{

public static void main (String args[]) {

for(int i=1; i<3; i++) {

System.out.print(i);

}

System.out.print(i);

}

}

A.编译时报错 B. 正确运行,输出012

C.正确运行,输出123 D. 正确运行,输出0123

6.下面代码的输出结果为(D)。

public class Test{

public static void main (String args[]) {

int a =5;

System.out.println((a%2 == 1) ? (a+1) /2 : a/2);

}

}

A 1 B 2 C 2.5 D 3

7.已知表达式int m[ ] ={1,2,3,4,5,6};下面哪个表达式的值与数组下标量总数相等?(B)

A m.length() B m.length C m.length()+1 D m.length+1

8.当编译并运行下面程序的时候会出现的结果(B)

public class Myar{

public static void main(String args[]){

 int[] i =new int [5];

 System.out.println(i[5]);

}

}

A 编译错误

B 运行错误

C 输出0

D 输出’null’

编程题

1.已知一个整型数组中,仅有一对数字是重复的,请编写Java代码,找出这个数字。

例如:数组{3,2,15,4,222,13,2,7,8}

输出:2

public static void main(String[] args) {
    int a[] = {3, 2, 15, 4, 222, 13, 2, 7, 8};
    int i = 0;
    int b = 0;
    int j = 0;
    while (i < a.length) {
        for (j = i + 1; j < a.length; j++) {
            if (a[i] == a[j]) {
                b = a[i];
                j=1;
                break;
            }
        }
        if(j == 1) break;
        i++;
    }
    System.out.println("args = [" + b + "]");
}      
public static void main(String[] args) {
    int[] a = {8,5,7,6};
    int[] b = new int[a.length];
    int[] c = Arrays.copyOf(a,a.length);
    Arrays.sort(a);
    for(int i = 0; i < c.length; i++){
        for(int j = 0; j < a.length; j++){
            if (c[i] == a[j]) {
                b[i] = j;
                break;
            }
        }
    }
    for (int i = 0; i < b.length; i++){
        System.out.print(b[i]+" ");

    }
}      
public class Test01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.nextLine();
            if (s.length()>8){
                //大写字母
                boolean type_A=false;
                //小写字母
                boolean type_a=false;
                //数字
                boolean type_n=false;
                //其他字符
                boolean type_o=false;
                //遍历判断
                for (int i=0;i<s.length();i++){
                    char c = s.charAt(i);
                    if (c<='Z'&& c>='A'){
                        type_A=true;
                    } else
                    if (c<='z'&& c>='a'){
                        type_a=true;
                    } else
                    if (Character.isDigit(c)){
                        type_n=true;
                    } else {
                        type_o=true;
                    }
                }
                int count=0;
                if (type_A){
                    count++;
                }
                if (type_a){
                    count++;
                }
                if (type_n){
                    count++;
                }
                if (type_o){
                    count++;
                }

                if (count>=3){
                    String compare = compare(s);
                    if ("OK".equals(compare)){
                        System.out.println("OK");
                    }else {
                        System.out.println("NG");
                    }
                }else {
                    System.out.println("NG");
                }
            }else {
                System.out.println("NG");
            }
        }
    }

    public static String compare(String s){
        int len=s.length();
        for (int i=0;i<len;i++){
            for (int j=i+1;j<len;j++){
                int cou =0;
                int a=i;
                int b=j;
                while (a<len && b<len && (s.charAt(a)==s.charAt(b))){
                    cou++;
                    a++;
                    b++;
                }
                if (cou>2){
                    return "NG";
                }
            }
        }
        return "OK";
    }
}