• 刷题日记【第三天】


    选择题模块

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

    在这里插入图片描述

    因为: 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    作对这道题的关键是要理解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是线程不安全的
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    解答:

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

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

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

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

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

    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”)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

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

    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;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

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

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

    在这里插入图片描述

    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() { } }
      
      • 1
      • 2
      • 3
      • 4

      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
      
      • 1
      • 2
      • 3
      • 4
      • 5

      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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    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;
        
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    代码随想录算法训练营第五十九天丨 单调栈02
    军用大数据 - Spark机器学习
    聊一聊前端面临的安全威胁与解决对策
    多商家AI智能名片商城系统(开源版)——构建高效数字化商业新生态
    中科创达C++ 一面(技术面、24min)
    第三代英特尔 至强 可扩展处理器(Ice Lake)和英特尔 深度学习加速助力阿里巴巴 Transformer 模型性能提升
    海域可视化监管:浅析海域动态远程视频智能监管平台的构建方案
    2023年腾讯云双十一活动攻略整理汇总
    HttpRunnerManager安装(三)-Linux下配置myql数据库&初始化数据
    测试工程师提升方向,提升产品思维提高测试效率......
  • 原文地址:https://blog.csdn.net/weixin_53939785/article/details/127401368