张晨的个人博客

1024活动程序员来找茬,程序员常见BUG

张晨的个人博客2020-10-26综合技术 1978 0A+A-

1024是程序员节,为伙伴们准备了一个找bug活动,以下是题目分享:


第一题

if (object == null) {
    object.doSomething();
} else {
    object.doSomethingElse();
}

答案空对象,会报空指针异常


第二题

Connection conn = DriverManager.getConnection(...);
...
conn.close();

答案:应使用try catch 捕获可能出现的异常,并最后关闭连接

Connection conn = DriverManager.getConnection(...);
try {
    ...
} finally {
    conn.close();
}


第三题

double d = 1.1;
BigDecimal bd= new BigDecimal(d);

答案:1.1是一个double类型,精度不完全准确,传入方法的值也不完全是等于1.1,可以用BigDecimal.valueOf方法消除浮点精度错误

bd2=1.100000000000000088817841970012523233890533447265625


第四题

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
for (Integer integer : list) {
    if(integer == 3){
        list.remove(integer);
    }
}

答案调用list.remove()方法导致modCountexpectedModCount的值不一致程序就抛出了ConcurrentModificationException异常

解决方法用Iteratorremove方法


第五题

一个mysql数据库的连接地址:

emr_jdbc_url=jdbc:mysql://192.168.1.100:1521/emdata_emr_prod_1?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull

答案mysql默认端口是3306(不要假设这台服务器把mysql的端口修改为1521的情况。。。)


第六题

Mysql 更新语句:

update ip_patient_info set gender=1 
where id in (select id from ip_patient_info where gender= 6);

答案:报错Error : You can't specify target table 'ip_patient_info' for update in FROM clause不能先select一个表的记录,在按此条件进行更新和删除同一个表的记录

 

第七题

-- 表:
CREATE TABLE `activity1024` (
  `id` int(10) NOT NULL,
  `birthdate` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
-- SQL语句
INSERT INTO   activity1024  VALUES(1,'1949-10-01')

答案timestamp 的值范围是从19702037年,此sql语句插入的结果是0000-00-00 00:00:00

 

第八题

一个nginx.conf配置

user  www;
worker_processes  1;
events {
    worker_connections  1024;}
http {
    include       mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80 ;
        root         /data/test;
        location / {
            index index.html index.htm;
        }       
    }}

答案:在http配置中缺少server_name server_name  127.0.0.1;


文章关键词
程序员
1024
bug
发表评论