【Java笔试题】第1套

it2025-03-17  11

答案为笔者个人总结,如有错误欢迎指正!

一、选择题(可多选,请把正确选项写到括号内)

下列说法正确的是?(A、B、C) A、Java程序的main方法必须写在类里面B、Java程序中可以有多个main方法C、Java程序中类名必须与文件名一样D、Java程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来 变量命名规范说法正确的是?(B) A、变量由字母、下划线、数字、$符号随意组成B、变量不能以数字作为开头C、A和a在Java中是同一个变量D、不同类型的变量,可以起相同的名字 下列javaDoc注释正确的是?(C) A、/*我爱北京天安门*/B、//我爱北京天安门*/C、/**我爱北京天安门*/D、/*我爱北京天安门*/ String s = new String(“xyz”) ;请问以上语句创建了几个String Object?(B) A、1个B、2个C、3个D、4个 Java中的final关键字有哪些用法?(A、B、C) A、修饰类表示该类不能被继承B、修饰方法表示方法不能被重写C、修饰变量表示变量只能一次赋值以后值不能被修改(常量)D、修饰接口表示接口不能被实现 List,Set,Map是否继承自Collection接口?(C) A、是B、不是C、部分是D、没有继承关系 在Java中,如何跳出当前的多重嵌套循环?(B) A、没有此命令B、breakC、returnD、goto 如何判断两个Long型是否相等(假设L1、L2分别是两个Long型)?(B、C) A、L1 == L2B、L1.equals(L2)C 、L1.longValue() == L2.longValue()D、L1 == L2.longValue() 以下代码运行打印输出结果是?(Bpublic void m1() { String a = null; try { a = a + "1"; System.out.println(1); return; } catch (Exception e) { a = "2"; System.out.println(2); e.printStackTrace(); } finally { a = "3"; System.out.println(3); } System.out.println(4); return; } A、1B、13C、123D、1234 在Java中,分布式锁主要常见实现包含哪些?(C、D) A、synchronized关键字B、基于Lock实现C、基于Redis缓存实现D、基于ZooKeeper实现

二、编程题

请编写Java单例模式的实现代码? 一种实现方法即可 //1.懒汉式,线程不安全 public class Singleton { private static Singleton instance; private Singleton() { } public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } //2.懒汉式,线程安全 public class Singleton { private static Singleton instance; private Singleton() { } public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } //3.饿汉式 public class Singleton { private static Singleton instance = new Singleton(); private Singleton() { } public static Singleton getInstance() { return instance; } } //4.双检锁/双重校验锁 public class Singleton { private volatile static Singleton singleton; private Singleton() { } public static Singleton getSingleton() { if (singleton == null) { synchronized (Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; } } //5.登记式/静态内部类 public class Singleton { private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } private Singleton() { } public static final Singleton getInstance() { return SingletonHolder.INSTANCE; } } //6.枚举 public enum Singleton { INSTANCE; public void whateverMethod() { } }

摘自菜鸟教程


请在service中实现一个方法,用于比较和指定日期:2019-06-18 00:15:00 的结果;如果比指定日期早,则返回true,否则返回false //1.使用LocalDateTime.now().isBefore(dateTime)或者LocalDateTime.now().isAfter(dateTime)比较 public class Test { private static final String PATTERN = "yyyy-MM-dd HH:mm:ss"; public static void main(String[] args) { System.out.println(testDate("2019-06-18 00:15:00")); } public static boolean testDate(String time) { LocalDateTime dateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern(PATTERN)); return LocalDateTime.now().isBefore(dateTime); } } //2.使用Duration.between(LocalDateTime.now(), dateTime).toMillis()比较 public class Test { private static final String PATTERN = "yyyy-MM-dd HH:mm:ss"; public static void main(String[] args) { System.out.println(testDate("2019-06-18 00:15:00")); } public static boolean testDate(String time) { LocalDateTime dateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern(PATTERN)); return Duration.between(LocalDateTime.now(), dateTime).toMillis() > 0; } } //3.使用new Date().before(dateTime)或者new Date().after(dateTime)比较 public class Test { private static final String PATTERN = "yyyy-MM-dd HH:mm:ss"; public static void main(String[] args) throws ParseException { System.out.println(testDate("2019-06-18 00:15:00")); } public static boolean testDate(String time) throws ParseException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN); Date dateTime = simpleDateFormat.parse(time); return new Date().before(dateTime); } } //4.使用new Date().compareTo(dateTime)比较 public class Test { private static final String PATTERN = "yyyy-MM-dd HH:mm:ss"; public static void main(String[] args) throws ParseException { System.out.println(testDate("2019-06-18 00:15:00")); } public static boolean testDate(String time) throws ParseException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN); Date dateTime = simpleDateFormat.parse(time); return new Date().compareTo(dateTime) < 0; } } //5.使用System.currentTimeMillis() - dateTime.getTime()比较 public class Test { private static final String PATTERN = "yyyy-MM-dd HH:mm:ss"; public static void main(String[] args) throws ParseException { System.out.println(testDate("2019-06-18 00:15:00")); } public static boolean testDate(String time) throws ParseException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN); Date dateTime = simpleDateFormat.parse(time); return System.currentTimeMillis() - dateTime.getTime() < 0; } }
最新回复(0)