奇奇怪怪的面试题

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

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

   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());
        }

    }
   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());
        }

    }
public static void main(String[] args) {
    int count = 0;
    while (count < 10) {
        if (count == 4) {
            continue;
        }
        System.out.println(count);
        count++;
    }
}
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);
    }
}
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方法时不需要考虑多线程间互相覆盖的问题。

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);
    }
}

下列代码可以编译通过。

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

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

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

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

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

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创建方法存在精度风险的选项?

A. BigDecimal a = new BigDecimal(0.1F)
B. BigDecimal b = new BigDecimal("0.1")
C. BigDecimal c = BigDecimal.valueOf(0.1)

会输出list 对

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

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

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异常

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

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

private static void extracted4() {
    Boolean a = null;
    Boolean b = true ? a : false;
    System.out.println("b = " + b);
}

输出true

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

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

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

private static void extracted8() {
    Integer integer = new Integer(3);
    System.out.println(integer.equals("3"));
}

Java高频面试之总纲篇

Java高频面试之集合篇

Java高频面试之异常篇

Java高频面试之并发篇
Java高频面试之SSM篇

Java高频面试之Mysql篇

Java高频面试之Redis篇

Java高频面试之消息队列与分布式篇

50道SQL面试题

奇奇怪怪的面试题

五花八门的内存溢出

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值