前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >奇奇怪怪的面试题

奇奇怪怪的面试题

作者头像
九转成圣
发布2024-04-10 16:42:13
690
发布2024-04-10 16:42:13
举报
文章被收录于专栏:csdncsdn

下列存在死循环风险的是ABCD

有需要互关的小伙伴,关注一下,有关必回关,争取今年认证早日拿到博客专家

代码语言:javascript
复制
   public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("aa");
        list.add("bb");
        while (list.iterator().hasNext()){
            System.out.println(list.iterator().next());
        }

    }
代码语言:javascript
复制
   public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("aa");
        list.add("bb");
        while (list.iterator().hasNext()){
            System.out.println(list.iterator().next());
        }

    }
代码语言:javascript
复制
public static void main(String[] args) {
    int count = 0;
    while (count < 10) {
        if (count == 4) {
            continue;
        }
        System.out.println(count);
        count++;
    }
}
代码语言:javascript
复制
public class Tmp {
    private boolean flag = true;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public void fun() {
        while (flag) {
        }
        System.out.println("done");
    }

    public static void main(String[] args) throws InterruptedException {
        final Tmp flagTest = new Tmp();
        new Thread(() -> flagTest.fun()).start();
        Thread.sleep(200);
        flagTest.setFlag(false);
    }
}
代码语言:javascript
复制
public Category findRoot(long categoryId) {
    Category category = categoryMapper.findCategoryById(categoryId);
    if(category == null) {
        throw new BusinessException("分类不存在");
    }
    long parentId = category.getParentId();
    if(parentId == null || parentId == 0) {
        return category;
    }
    return findRoot(parentId);
}

环形链表

两个线程并发执行下列代码,其中直接使用线程安全类ConcurrentHashMap的put方法时不需要考虑多线程间互相覆盖的问题。

代码语言:javascript
复制
private static void extracted() {
    ConcurrentHashMap<String, Student> map = new ConcurrentHashMap<>();
    String key = "test";
    Student student = map.get(key);
    if (student == null) {
        student = new Student();
        map.put(key, student);
    }
}

下列代码可以编译通过。

代码语言:javascript
复制
public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("ab");
    printList(list);
}

public static void printList(List<Object> list) {
    Iterator it = list.iterator();
    while (it.hasNext()) {
        System.out.println(it.next());
    }
}

关于 hashCode 和 equals 的处理,下列哪些说法是正确的? AC

代码语言:javascript
复制
 A. 只要覆写equals,就必须覆写hashCode
 B. Set存储的对象可以不用覆写hashCode 和 equals这两种方法
 C. 如果自定义对象作为Map的键,那么必须覆写hashCode和equals
 D. hashCode相等时,equals一定相等

一般建议覆写equals就重写hashCode,

Set中存的对象为自定义对象时重写hashCode 和 equals这两种方法

如果自定义对象作为Map的键,那么最好覆写hashCode和equals

代码语言:javascript
复制
public class Tmp {
    public static void main(String[] args) {
        Student zs1 = new Student("zs");
        Student zs2 = new Student("zs");
        HashSet<Student> set = new HashSet<>();
        set.add(zs1);
        boolean addSuccess = set.add(zs2);
        if (addSuccess) {
            System.out.println("add 成功了");
        }
        Map<Student, Integer> map = new HashMap<>();
        map.put(zs1, 1);
        Integer put = map.put(zs2, 2);
        if (put == null) {
            System.out.println("put 成功了");
        }

    }


}

class Student {
    String name;

    public Student(String name) {
        this.name = name;
    }
}

下面选项中,请找出BigDecimal创建方法存在精度风险的选项?

代码语言:javascript
复制
A. BigDecimal a = new BigDecimal(0.1F)
B. BigDecimal b = new BigDecimal("0.1")
C. BigDecimal c = BigDecimal.valueOf(0.1)

会输出list 对

代码语言:javascript
复制
class A{
   public void hello(List list){
       System.out.println("list");
   }
   public void hello(ArrayList list){
       System.out.println("arrayList");
   }
   public static void main(String[] args) {
        A a = new A();
        List list = new ArrayList();
        a.hello(list);
        extracted();
    }
}

输出false false T

代码语言:javascript
复制
private static void main(String[] args) {
    double a = 1.0D - 0.9D;
    double b = 0.1D;
    Double x = Double.valueOf(a);
    Double y = Double.valueOf(b);
    System.out.println(a == b);
    System.out.println(x.equals(y));
}

输出 false 0 T

代码语言:javascript
复制
private static void main(String[] args) {
    BigDecimal b1 = new BigDecimal("1.0");
    BigDecimal b2 = new BigDecimal("1.00");
    System.out.println(b1.equals(b2));
    System.out.println(b1.compareTo(b2));
}

输出 0 1 2 3 4 和UnsupportedOperationException异常

代码语言:javascript
复制
private static void main(String[] args) {
    Integer[] arr = new Integer[5];
    for (int i = 0; i < 5; i++) {
        arr[i] = i;
    }
    List<Integer> list = Arrays.asList(arr);
    arr[0] = 6;
    for (Integer integer : list) {
        System.out.println(integer);
    }
    list.add(7);
}

输出false 和 true

用org.apache.commons.lang3包的StringUtils

代码语言:javascript
复制
private static void main(String[] args) {
    String str = " ";
    boolean empty = StringUtils.isEmpty(str);
    System.out.println("empty = " + empty);
    boolean blank = StringUtils.isBlank(str);
    System.out.println("blank = " + blank);
}

输出 NullPointerException

代码语言:javascript
复制
private static void extracted4() {
    Boolean a = null;
    Boolean b = true ? a : false;
    System.out.println("b = " + b);
}

输出true

代码语言:javascript
复制
private static void extracted5() {
    List<String> list1 = new ArrayList<>();
    list1.add("eee");
    list1.add("www");
    List<String> list2 = new LinkedList<>();
    list2.add("eee");
    list2.add("www");
    System.out.println(list1.equals(list2));
}

输出true

代码语言:javascript
复制
private static void extracted6() {
    Set<String> set1 = new HashSet<>();
    Set<String> set2 = new LinkedHashSet<>();
    set1.add("www");
    set2.add("www");
    System.out.println(set1.equals(set2));
}

输出true

代码语言:javascript
复制
private static void extracted7() {
    Map<Integer, String> m1 = new TreeMap<>();
    Map<Integer, String> m2 = new LinkedHashMap<>();
    m1.put(1, "www");
    m1.put(2, "eee");
    m2.put(2, "eee");
    m2.put(1, "www");
    System.out.println(m1.equals(m2));
}

输出false

代码语言:javascript
复制
private static void extracted8() {
    Integer integer = new Integer(3);
    System.out.println(integer.equals("3"));
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档