刷题日记【第三天】

选择题模块

  1. 下面程序 编译运行后,输出的结果为(A
public class Test {
    public static void main(String args[]) {
        int x, y;
        x = 5 >> 2;
        y = x >>> 2;
        System.out.println(y);
    }
}

在这里插入图片描述

因为: 5>>2 就是5 向右移两位(除于两次2),>>> 表示 无符号右移, 高位用0填充 就是 0001 右移两位就为 0000 ,所以答案选A

  1. 以下代码的运行结果为:(C
public class foo {
    public static void main(String sgf[]) {
        StringBuffer a=new StringBuffer("A");
        StringBuffer b=new StringBuffer("B");
        operate(a,b);
        System.out.println(a+"."+b);
    }
    static void operate(StringBuffer x,StringBuffer y) {
        x.append(y);
        y=x;
    }
}

在这里插入图片描述

作对这道题的关键是要理解Java的值传递,关于值传递引用传递的解释如下:
值传递:是指在调用函数时将实际参数复制一份传递到函数中,这样在函数中如果对参数进行修改,将不会影响到实际参数。
引用传递:是指在调用函数时将实际参数的地址直接传递到函数中,那么在函数中对参数所进行的修改,将影响到实际参数。
这道题中a、b(是引用的副本,Java中没有引用传递)传入operate()函数中,但最后a指向的对象的值发生了变化,而b指向的对象的值没有发生变化,这是因
为x.append(y)改变了a指向的对象的值;而y=x并没有改变b指向对象的值;指向将y指向了x所指向的对象(之前y和b指向同一个对象)。

在这里插入图片描述

  1. 下列那个说法是正确的(D
A ConcurrentHashMap使用synchronized关键字保证线程安全
B HashMap实现了Collction接口
C Array.asList方法返回java.util.ArrayList对象
D SimpleDateFormat是线程不安全的

解答:

A:HashMap线程不安全,而ConcurrentHashMap就线程安全

ConcurrentHashMap运用各类CAS操作,将扩容操作的并发性能实现最大化,在扩容过程中,就算有线程调用get查询方法,也可以安全的查询数据,若有线程进行put操作,还会协助扩容,利用sizeCtl标记位和各种volatile变量进行CAS操作达到多线程之间的通信、协助,在迁移过程中只锁一个Node节点,即保证了线程安全,又提高了并发性能。
B:

C:Arrays的asList方法使用的ArrayList类是一个内部定义的类,而不是java.util.ArrayList类。

public static <T> List<T> asList(T... a) {  
    return new ArrayList<T>(a);  
} 

  1. 下列有关JAVA异常处理的叙述中正确的是(A B C D
A finally是为确保一段代码不管是否捕获异常都会被执行的一段代码
B throws是用来声明一个成员方法可能抛出的各种非运行异常情况
C final用于可以声明属性和方法,分别表示属性的不可变及方法的不可继承
D throw是用来明确地抛出一个异常情况

此题为基本概念,牢记就行。

  1. 下面哪段程序能够正确的实现了GBK编码字节流到UTF-8编码字节流的转换:(B
A dst=String.frombytes(src,”GBK”).getbytes(“UTF-8”)
B dst=new String (src,”GBK”).getbytes(“UTF-8”)
C dst=new String (”GBK”, src,) getbytes()
D dst=String.encode(String.decode(src,”GBK”)), “UTF-8”)

new一个GBK编码的string对象使用UTF-8格式获取字节流

  1. 访问修饰符作用范围由大到小排列是 public>protected>default>private
  2. 在Java中,HashMap中是用哪些方法来解决哈希冲突的 链地址法
  3. 阅读程序,选择正确的输出结果(C
class HelloA{
    public HelloA()
    {
        System.out.println("I’m A class ");
    }
    static
    {
        System.out.println("static A");
    }
}
public class HelloB extends HelloA{
    public HelloB()
    {
        System.out.println("I’m B class");
    }
    static{
        System.out.println("static B");
    }
    public static void main (String[] args){
        new HelloB();
    }
}

//A. static A I’m A class static B I’m B class
//B. I’m A class I’m B class static A static B
//C. static A static B I’m A class I’m B class
//D. I’m A class static A I’m B class static B

该题的代码执行顺序是: 父类静态代码块->子类静态代码块->父类方法->子类方法

  1. 执行下列代码的输出结果是(30)
public class Demo{
    public static void main(String args[]){
        int num = 10;
        System.out.println(test(num));
    }
    public static int test(int b){
        try
        {
            b += 10;
            return b;
        }
        catch(RuntimeException e)
        {
        }
        catch(Exception e2)
        {
        }
        finally
        {
            b += 10;
            return b;
        }
    }
}

注意: 如果finally块中有return语句的话,它将覆盖掉函数中其他return语句,所以返回30。

  1. 下列代码的输出结果是(false
boolean b=true?false:true==true?false:true;
System.out.println(b);

在这里插入图片描述

  1. 在Java中下面Class的声明哪些是错误的(A B C)?

    A public abstract final class Test { abstract void method(); }
    B public abstract class Test { abstract final void method(); }
    C public abstract class Test { abstract void method() { } }
    D public class Test { final void method() { } }
    

    A.B: abstract 和 final 不能同时出现

    C:抽象方法不能具体实现 (不能有大括号)

  2. 下面哪些赋值语句是正确的(A B D

    A long test=012
    B float f=-412
    C int other =(int)true
    D double d=0x12345678
    E byte b=128
    

    A .八进制 D.十六进制√

    C.布尔类型不能与数据类型转换 E. byte取值范围【-128,127】×

编程题模块

1. 统计回文

在这里插入图片描述
思路分析
在这里插入图片描述
代码实现

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.nextLine();
        String str2 = scanner.nextLine();
        int count = 0;
        for (int i = 0; i <= str1.length(); i++) {
            StringBuilder sb = new StringBuilder(str1);
            sb.insert(i, str2);
            if (isHW(sb.toString())) {
                count++;
            }
        }
        System.out.println(count);
    }

    public static boolean isHW(String s) {
        int i = 0;
        int j = s.length() - 1;
        while (i < j) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}

2. 连续最大和

在这里插入图片描述
思路分析
在这里插入图片描述
代码实现

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = scan.nextInt();
        }

        int i = 0;
        int maxSum1 = 0;
        int maxSum2 = 0;
        while(i < n) {
            if(array[i] >= 0) {
                //只要连续大于0就++
                while(i < n && array[i] >= 0) {
                    maxSum1 += array[i];
                    i++;
                }
            }else {
                if(maxSum1 > maxSum2) {
                    maxSum2 = maxSum1;
                }
                maxSum1 = 0;
                //数组元素小于0就跳过
                i++;
            }
        }
        if(maxSum1 > maxSum2) {
            maxSum2 = maxSum1;
        }
        System.out.println(maxSum2);
    }
}

3. 把字符串转换成整数

在这里插入图片描述
思路分析
在这里插入图片描述

代码实现

public class Solution {
    public int StrToInt(String str) {
        //偷懒代码
        // Integer res = 0;
        // try {
        //     res = new Integer(str);
        // } catch (NumberFormatException e) {
        // } finally {
        //     return res;
        // }
        char[] ch  =  str.toCharArray();
        int len = str.length();
        int ans = 0, m = 0;
        if (len == 0) {
            return 0;
        } else {
            for (int i = 0 ; i <= len-1 ; i++) {
                //如果非数字和符号,直接return 0
                if ((ch[i] < '0' || ch[i] > '9') && ch[i] != '+' && ch[i] != '-') {
                    return 0;
                }
                //如果是数字,就从低位到高位累加,每次扩大10倍
                if (ch[i] >= '0' && ch[i] <= '9') {
                    ans = ans * 10 + ch[i] - '0';
                }

            }
        }
        if (ch[0] == '-') {
            return -ans;   //判断符号位
        }
        return ans;
    
    }
}

4. 不要二

在这里插入图片描述

思路分析
在这里插入图片描述

在这里插入图片描述
代码实现
**

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int w= in.nextInt();
        int h= in.nextInt();
        int count = 0;
        int[][] arr = new int[w][h];
        for(int i=0;i<w;i++){
            for(int  j=0;j<h;j++){
                if(arr[i][j] == 0){
                    count++;
                    if(i+2<w){
                        arr[i+2][j] = 1;
                    }
                    if(j+2<h){
                        arr[i][j+2] = 1;
                    }
                }
            }
        }
        System.out.println(count);
    }
}
  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孙宇航_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值