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 + "]");
}

2.对数组进行升序排序,返回排序后数组的位置。
例如数组A:{8,5,7,6}
输出{3,0,2,1}

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]+" ");

    }
}

3.对密码有效性进行验证。密码要求:
a.长度超过8位
b.包括大写字母、小写字母、数字、其它符号,以上四种至少三种
c.不能有相同长度大于2的子串重复
输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG
示例1
输入
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出
OK
NG
NG
OK

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";
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值