1、请写出如何计算105的阶乘的核心算法?
public static int calculateNum(int num)
{
if(num<=1) return 1;
else return num*calculateNum(num-1);
}
calculateNum(105);
2、int[] arr={6,12,33,87,90,97,108,561}请写出如何查找97的核心算法?
public static int getIndex(int[] arr, int value) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i; //字符串时,换为 equals
}
}
return -1;//如果未找到返回-1
}
getIndex(arr,97);
3、什么是不可变量,请写一个不可变类的代码
被final修饰时 不可变量不能重新赋值
public final class String{
Private final char value[];
…
}
常见的不可变类:String Integer Long等类型
4、使用工厂模式根据输入参数int类型,如果:
输入参数为0,返回hello
输入参数为-1,返回world
输入参数为-2,返回success
interface IFactory{ String getResult}
class A implenments IFactory
{
private String getResult()
{
return "hello";
}
}
class B implenments IFactory
{
private String getResult()
{
return "hello";
}
}
class C implenments IFactory
{
private String getResult()
{
return "hello";
}
}
class Factory
{
public IFactory D(int num){
if(num==0) return new A();
if(num==-1) return new B();
if(num==-2) return new C();
return null;
}
}
使用该工厂:
public static void main(String[] args) {
Factory factory=new Factory();
IFactory E=factory.D(0);
E.getResult();
}
5、select count(*)与select count(1)的区别
假如表沒有主键(Primary key), 那么count(1)比count(*)快,如果有主键的话,那主键作为count的条件时候count(主键)最快。
如果你的表只有一个字段的话那count(*)就是最快的
6、设一个java类叫Student,结构如下:
Class Student{
int age;
String name;
}
设一个List<Student>,请根据age从大到小对这个list进行排序
stuList.sort(Comparator.comparing(Student::getAge).reversed());
7、设一个List<int>里面有这样的数据{1,1,3,3,4,5,6,2,8,1,5},我要把所有重复的数字去掉得到一个新的List,请写出代码
List<Integer> list=new ArrayList<>();
list.add(1);
list.add(1);
list.add(3);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(2);
list.add(8);
list.add(1);
list.add(5);
//1、利用list contains方法
// List<Integer> tempList=new ArrayList<>();
// for(Integer i:list)
// {
// if(!tempList.contains(i))
// {
// tempList.add(i);
// }
// }
// 2、利用set值不可重复特性
HashSet<Integer> set =new HashSet<>(list);
List<Integer> tempList=new ArrayList<>(set);
//3、利用java8 stream新特性
//List<Integer> tempList = //list.stream().distinct().collect(Collectors.toList());
8、有一个HashMap<Student>对象,Student结构为String name和int age,每个字段有一对相应的getX,setX,请用至少2种方式写出如何把这个Map对象里所有的student的信息取得?
public void getHashMapInfo()
{
HashMap<Student,String> hm=new HashMap<Student,String>();
hm.put(new Student("张三",21),"上海");
hm.put(new Student("李四",23),"北京");
hm.put(new Student("王五",28),"杭州");
hm.put(new Student("赵六",32),"哈尔滨");
//1、keySet取出
// Set<Student> keySet=hm.keySet();
// for(Iterator<Student> it=keySet.iterator();it.hasNext();)
// {
// Student s=it.next();
// System.out.println(s.getName()+" "+s.getAge()+" "+hm.get(s));
// }
//2、EntrySet
Set<Map.Entry<Student,String>> entrySet=hm.entrySet();
for(Iterator<Map.Entry<Student,String>> iter=entrySet.iterator();iter.hasNext();)
{
Map.Entry<Student,String> en=iter.next();
Student s=en.getKey();
String address=en.getValue();
System.out.println(s.getName()+" "+s.getAge()+" "+address);
}
}
9、请使用lambda表达式打印catch(Exception e)里Exception里的消息及错误追溯信息
public void ExceptionTest()
{
List<Integer> integers = Arrays.asList(1,2,3,4,5,0);
integers.forEach(i -> {
try {
System.out.println(1 / i);
}
catch (ArithmeticException e)
{
System.err.println("Arithmetic Exception occured : " + e.getMessage());
e.printStackTrace();
}
});
}
10、请用lambda表达式申明一个Runnable线程类的启动
new Thread(()->{}).start();
11、Spring的事务有7种传播性设置,请问以下几种传播性的区别
PROPAGATION_REQUIRED与PROPAGATION_SUPPORTS
一、支持当前事务,如果当前没有事务,就新建一个事务
二、支持当前事务,如果当前没有事务,就以非事务方式执行
12、JDBC中Statement与PreparedStatement的区别
使用PreparedStatment对象,数据库系统会对sql语句进行预编译处理,查询运行速度更快,阻止常见SQL注入攻击
13、java 32位虚拟机和64位虚拟机最大可支持的内存分别为多少?
理论上说上32位的JM堆内存可以到达2^32 ,即4GB ,但实际上会比这个小很多。不同操作系统之间不同,如Windows系统大约1.5GB , Solaris大约3GB。64 位JVM允许指定最大的堆内存,理论上可以达到2^64 ,这是一个非常大的数字,实际上你可以指定堆内存大小到100GB。甚至有的JVM ,如Azul ,堆内存到1000G都是可能的。
14、如何在linux下查看名为“mongodb”的进程
db.currentOP()
注:
部分题目用到的帮助类
public class Student{
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Integer getAge() {
return Age;
}
public void setAge(Integer age) {
Age = age;
}
private String Name;
private Integer Age;
public Student(String name,Integer age)
{
Name=name;
Age=age;
}
}