Java笔试题

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: Java笔试题

随机从1到100000中间随机取出100个不同的质数,然后按从小到大的顺序排列

public class IsPrime { //工具类
    // 输入一个数,判断是否为质数,费时方法
    public static Integer isPrime(int num) {
        if (num == 0 || num == 1)
            return -1;
        for (int i = 2; i < num - 1; i++) {
            if (num % i == 0) return -1;
        }
        return num;
    }
}
 
 
//        一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数;否则称为合数。
        List<Integer> listrandom=new ArrayList(); //1-1000000数组
        List<Integer> res=new ArrayList(); //所有质数集合
        for(int i=1;i<=100000;i++){
            listrandom.add(i);
        }
        for (Integer integer : listrandom) {
            if((IsPrime.isPrime(integer)!=-1)){
                     res.add(IsPrime.isPrime(integer));
            }
        }
//        int a= (int) Math.floor(Math.random()*res.size());
 
        //100个不同的质数
        List<Integer> listRandom = new ArrayList<Integer>();
        //随机取出n条不重复的数据,这里我设置随机取100条数据
        for (int i = 100; i >=1; i--) {
            Random random = new Random();
            Math.random();
            //在数组大小之间产生一个随机数 j
            int j = random.nextInt(res.size()-1);
            //取得list 中下标为j 的数据存储到 listRandom 中
            listRandom.add(listrandom.get(j));
            //把已取到的数据移除,避免下次再次取到出现重复
            listrandom.remove(j);
        }
//        TreeSet 有序集合 从小到大排序
        List newList = new ArrayList(new TreeSet(listRandom));
        //循环取出 newList 中的数据
        for(Object l:newList) {
            System.out.println(l);
        }

有一个999级的台阶,可以选择每次上1级或者2级,请实现一个方法,计算有多少种上楼的方式。

  public int JumpFloor(int target) {
            if (target == 1){
                return 1;
            }else if(target == 2){
                return 2;
            } else {
                return JumpFloor(target-1)+JumpFloor(target-2);
            }
 
    }


用JSON格式描述下面表格的数据

学号

姓名

性别

年龄

1001

张三

20

1002

李四

22

1003

王五

18

// json数组
//        static String json = "[
//        {'学号':'1001','姓名':张三,'性别':男,'姓名':26,'年龄':20},
//        {'学号':'1002','姓名':李四,'性别':女,'姓名':26,'年龄':22},
//        {'学号':'1003','姓名':王五,'性别':男,'姓名':26,'年龄':18},
//        ]";

已知两张表完成如下SQL语句:

学生基本信息:
Student
 
ID NUMBER 学号
 
NAME VARCHAR2(10) 姓名
 
SEX VARCHAR2(2) 性别:男女
 
CLASS VARCHAR2(2) 班级:1,2,3,4
 
AGE NUMBER 年龄
 
SUBJECT VARCHAR2(10) 最高分课程
 
SCORE NUMBER 最高成绩
 
 
学生考试成绩:Score
 
ID NUMBER 学号
 
SUBJECT VARCHAR2(10) 课程
 
YEAR NUMBER 考试年份
 
SCORE NUMBER 成绩
 
 
 
 
/*
 Navicat Premium Data Transfer
 Source Server         : 0702
 Source Server Type    : MySQL
 Source Server Version : 50723
 Source Host           : localhost:3306
 Source Schema         : qqq
 Target Server Type    : MySQL
 Target Server Version : 50723
 File Encoding         : 65001
 Date: 13/02/2022 13:04:14
*/
 
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
 
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `ID` int(10) NOT NULL COMMENT '学号',
  `NAME` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
  `SEX` varchar(2) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别:男女',
  `CLASS` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '班级:1,2,3,4',
  `AGE` int(11) NULL DEFAULT NULL COMMENT '年龄',
  `SUBJECT` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '最高分课程',
  `SCORE` int(10) NULL DEFAULT NULL COMMENT '最高成绩',
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
 
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '张三', '男', '1', 18, '语文', 90);
INSERT INTO `student` VALUES (2, '李四', '男', '2', 18, '语文', 100);
INSERT INTO `student` VALUES (3, '王五', '男', '2', 18, '语文', 90);
INSERT INTO `student` VALUES (4, '丽丽', '女', '1', 18, '英语', 90);
 
SET FOREIGN_KEY_CHECKS = 1;
 
 
/*
 Navicat Premium Data Transfer
 Source Server         : 0702
 Source Server Type    : MySQL
 Source Server Version : 50723
 Source Host           : localhost:3306
 Source Schema         : qqq
 Target Server Type    : MySQL
 Target Server Version : 50723
 File Encoding         : 65001
 Date: 13/02/2022 13:04:46
*/
 
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
 
-- ----------------------------
-- Table structure for score
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score`  (
  `ID` int(10) NULL DEFAULT NULL COMMENT '学号',
  `SUBJECT` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '课程',
  `YEAR` int(255) NULL DEFAULT NULL COMMENT '考试年份',
  `SCORE` int(255) NULL DEFAULT NULL COMMENT '成绩'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
 
-- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES (1, '语文', 2021, 80);
INSERT INTO `score` VALUES (1, '数学', 2021, 90);
INSERT INTO `score` VALUES (1, '英语', 2021, 70);
INSERT INTO `score` VALUES (2, '语文', 2021, 100);
INSERT INTO `score` VALUES (2, '数学', 2021, 70);
INSERT INTO `score` VALUES (4, '英语', 2021, 23);
INSERT INTO `score` VALUES (3, '语文', 2021, 80);
INSERT INTO `score` VALUES (3, '数学', 2021, 12);
INSERT INTO `score` VALUES (3, '英语', 2021, 90);
INSERT INTO `score` VALUES (4, '语文', 2021, 90);
INSERT INTO `score` VALUES (4, '数学', 2021, 45);
INSERT INTO `score` VALUES (2, '语文', 2012, 50);
INSERT INTO `score` VALUES (2, '数学', 2012, 20);
INSERT INTO `score` VALUES (2, '英语', 2012, 30);
 
SET FOREIGN_KEY_CHECKS = 1;

- 列出2012年1班各科成绩的平均值。

SELECT
    AVG( sc.SCORE ) AS '平均成绩',
    sc.YEAR AS '年份',
    st.CLASS AS '班级',
    sc.SUBJECT AS '课程' 
FROM
    score sc
    LEFT JOIN student st ON sc.ID = st.ID 
WHERE
    sc.YEAR = 2021
    AND st.CLASS = 1 
GROUP BY
    sc.SUBJECT 

-- 列出2012年2班数学成绩不及格的学生人员列表。

SELECT
    st.`NAME` AS '姓名',
    sc.YEAR AS '年份',
    st.CLASS AS '班级',
    sc.SUBJECT AS '课程',
    sc.SCORE AS '成绩' 
FROM
    score sc
    LEFT JOIN student st ON sc.ID = st.ID 
WHERE
    sc.YEAR = 2012 
    AND st.CLASS = 2 
    AND sc.SUBJECT = '数学' 
    AND sc.SCORE < 60

-- 查出每位同学成绩所有成绩中分数最高的科目、成绩并更新到学生基本信息中最高分课程和最高成绩字段中。

UPDATE student AS A
INNER JOIN (
SELECT
    MAX( sc.SCORE ) AS maxscore,
    sc.ID AS maxid,
    sc.`SUBJECT` maxsub 
FROM
    score sc
    LEFT JOIN student st ON sc.ID = st.ID 
GROUP BY
    sc.ID 
    ) AS B ON A.ID = B.maxid 
    SET A.ID = B.maxid,
    A.SUBJECT = B.maxsub,
    A.score = B.maxscore

final, finally, finalize 的区别

  • final 用于申明属性,方法和类,表示属性不可变,方法不可以被改变,类不可以被继承。
  • finally 是异常处理语句结构中,表示总是执行的部分。
  • finallize   在垃圾回收器将内存中的对象进行清空之前,允许使用finalize()方法做清理工作

Error 和 Exception 的区别?

1,Exception是程序正常运行中,可以预料的意外情况,可以被捕获,进行相应的处理.

2.Error 是指正常情况下,不大可能出现的情况,绝大部分的Error 都会导致程序处于非正常的,不可恢复的状态, 不需要捕获。

MVC的各个部分都有那些技术来实现

  • model: 模型层 应用的业务逻辑(如:数据库的操作),通过JavaBean实现

    (hibernate、mybatis、ibatis)

  • view:视图层,用于与用户的交互,主要由jsp页面产生。
  • (jsp、FreeMarker、EL、)
  • controller:应用层 处理过程控制,一般是一个servlet。

java字符串反转

1、用stringBuffer或者stringBuilder自带的reverse方法

    public static String reverseTestOne(String s) {
        return new StringBuffer(s).reverse().toString();
    }

2.stringBuffer倒序拼接

 

 public static String reverseTestThree(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = s.length() - 1; i >= 0; i--) {
            sb.append(s.charAt(i));
        }
        return sb.toString();
    }

3、切割递归反转

  public static String reverseTestSix(String s) {
        if (s.length() <= 1) {
            return s;
        }
        return reverseTestSix(s.substring(1)) + s.substring(0, 1);
    }


4、利用栈的先进后出

   public static String reverseTestFour(String s) {
        StringBuffer sb = new StringBuffer();
        Stack stack = new Stack();
        for (int i = 0; i < s.length(); i++) {
            stack.push(s.charAt(i));
        }
        while (!stack.isEmpty()) {
            //stack会返回栈顶值,并且会把该值删除
            sb.append(stack.pop());
        }
        return sb.toString();
    }

3. SQL间答题

表结构:

1、表名: g_ cardapply

字段(字段

名/类型/长度):

g applynpo varchar 8: /申请单号(关键字)

g_ applydate bigint 8; /申请日期

g_ state varchar 2; //申请状态

2、表名: g_ _cardapplydetail

字段(字段名/类型/长度):

g_ _applyno varchar //申请单号(关键字)

g_ name varchar 30; //申请人姓名

g_ idcard varchar  18; //申请人身份证号

g_ state varchar 2; //申请状态

其中,两个表的关联字段为申请单号。

题目:

-- 1.查询身份证号码为440401430103082的申请日期

SELECT
  a.g_applydate,
  a.g_applyno 
FROM
  g_cardapply a
  LEFT JOIN g_cardapplydetail b ON a.g_applyno = b.g_applyno 
WHERE
  b.g_idcard = '440401430103082'

-- 2.查询同一个身份证号码有两条以上记录的身份证号码以及记录个数

SELECT
    COUNT( 1 ) 
FROM
    ( SELECT * FROM g_cardapplydetail AS s WHERE g_idcard = '440401430103082' ) AS a 
HAVING
    count( 1 ) >= 2

-- 3.将身份证号码为440401430103082的记录在两个表中的申请状态均改为07

​​​​​​​UPDATE g_cardapply a
LEFT JOIN g_cardapplydetail b ON a.g_applyno = b.g_applyno 
SET a.g_state = '1',
b.g_state = '1' 
WHERE
    b.g_idcard = '440401430103082'

-- 4、删除g_ _cardapplydetail 表中所有姓李的记

DELETE 
FROM
  g_cardapplydetail 
WHERE
  g_applyno = ( SELECT g_applyno FROM ( SELECT g_applyno FROM g_cardapplydetail WHERE g_name LIKE '李%' ) aa );

java的字符类型采用的编码方案是Unicode

构造函数:方法名与类名相同   主要作用是完成对垒的初始化工作 一般在创建新对象时,系统自动调用构造函数。


设三个变量x=1,y=2,z=3,则表达式y+=z--/++x的值为?

i++ 是先引用后增加 ,先在i所在的表达式中使用i的当前值,后让i加1

++i 是先增加后引用,让i先加1,然后在i所在的表达式中使用i的新值

他们其实都是i=i+1的意思,但是在程序中运行的时候的执行的顺序不一样。

i-- 和--i  也同理

上题y=2+(z--/++x)=2+(3/2)=3

字符串压缩。

利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaaa会变为a2b1c5a3。若“压缩”后的字符串没有变短,则返回原先的字符串。你可以假设字符串中只包含大小写英文字母(a至z)。

输入:"aabcccccaaa"

输出:"a2b1c5a3"

输入:"abbccd"

输出:"abbccd"

解释:"abbccd"压缩后为"a1b2c2d1",比原字符串长度更长。

class Solution {
    public String compressString(String S) {
        if (S.length() == 0) { // 空串处理
            return S;
        }
        StringBuffer ans = new StringBuffer();
        int cnt = 1;
        char ch = S.charAt(0);
        for (int i = 1; i < S.length(); ++i) {
            if (ch == S.charAt(i)) {
                cnt++;
            } else {
                ans.append(ch);
                ans.append(cnt);
                ch = S.charAt(i);
                cnt = 1;
            }
        }
        ans.append(ch);
        ans.append(cnt);
        return ans.length() >= S.length() ? S : ans.toString();
    }
}

基本表结构:

      student(sno,sname,sage,ssex)学生表
       course(cno,cname,tno) 课程表
       sc(sno,cno,score) 成绩表

     teacher(tno,tname) 教师表

101,查询课程1的成绩比课程2的成绩高的所有学生的学号
select a.sno from
(select sno,score from sc where cno=1) a,

(select sno,score from sc where cno=2) b
where a.score>b.score and a.sno=b.sno

102,查询平均成绩大于60分的同学的学号和平均成绩

select a.sno as "学号", avg(a.score) as "平均成绩"

from

(select sno,score from sc) a

group by sno having avg(a.score)>60

103,查询所有同学的学号、姓名、选课数、总成绩

select a.sno as 学号, b.sname as 姓名,

count(a.cno) as 选课数, sum(a.score) as 总成绩

from sc a, student b

where a.sno = b.sno
group by a.sno, b.sname

或者:

selectstudent.sno as 学号, student.sname as 姓名,

count(sc.cno) as 选课数, sum(score) as 总成绩

from student left Outer join sc on student.sno = sc.sno

group by student.sno, sname


104,查询姓“张”的老师的个数

selectcount(distinct(tname)) from teacher where tname like '%‘

或者:

select tname as "姓名", count(distinct(tname)) as "人数"

from teacher

where tname like'%'
group by tname

105,查询没学过“张三”老师课的同学的学号、姓名

select student.sno,student.sname from student

where sno not in (select distinct(sc.sno) from sc,course,teacher

where sc.cno=course.cno and teacher.tno=course.tno and teacher.tname='张三')

106,查询同时学过课程1和课程2的同学的学号、姓名
select sno, sname from student

where sno in (select sno from sc where sc.cno = 1)
and sno in (select sno from sc where sc.cno = 2)
或者:

selectc.sno, c.sname from

(select sno from sc where sc.cno = 1) a,

(select sno from sc where sc.cno = 2) b,

student c

where a.sno = b.sno and a.sno = c.sno

或者:

select student.sno,student.sname from student,sc where student.sno=sc.sno and sc.cno=1

and exists( select * from sc as sc_2 where sc_2.sno=sc.sno and sc_2.cno=2)

107,查询学过“李四”老师所教所有课程的所有同学的学号、姓名

select a.sno, a.sname from student a, sc b

where a.sno = b.sno and b.cno in

(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '李四')

或者:

select a.sno, a.sname from student a, sc b,

(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '李四') e

where a.sno = b.sno and b.cno = e.cno

108,查询课程编号1的成绩比课程编号2的成绩高的所有同学的学号、姓名

select a.sno, a.sname from student a,

(select sno, score from sc where cno = 1) b,

(select sno, score from sc where cno = 2) c

where b.score > c.score and b.sno = c.sno and a.sno = b.sno

109,查询所有课程成绩小于60分的同学的学号、姓名

select sno,sname from student

where sno not in (select distinct sno from sc where score > 60)

110,查询至少有一门课程与学号为1的同学所学课程相同的同学的学号和姓名

select distinct a.sno, a.sname

from student a, sc b

where a.sno <> 1 and a.sno=b.sno and
b.cno in (select cno from sc where sno = 1)

或者:

select s.sno,s.sname

from student s,

(select sc.sno

from sc

where sc.cno in (select sc1.cno from sc sc1 where sc1.sno=1)and sc.sno<>1

group by sc.sno)r1

where r1.sno=s.sno

基本表结构:

      student(sno,sname,sage,ssex)学生表
       course(cno,cname,tno) 课程表

       sc(sno,cno,score) 成绩表

       teacher(tno,tname) 教师表

111、把“sc”表中“王五”所教课的成绩都更改为此课程的平均成绩

update sc set score = (select avg(sc_2.score) from sc sc_2 wheresc_2.cno=sc.cno)

from course,teacher where course.cno=sc.cno and course.tno=teacher.tno andteacher.tname='王五'

112、查询和编号为2的同学学习的课程完全相同的其他同学学号和姓名

这一题分两步查:

1,

select sno
from sc
where sno <> 2

group by sno
having sum(cno) = (select sum(cno) from sc where sno = 2)

2,
select b.sno, b.sname

from sc a, student b

where b.sno <> 2 and a.sno = b.sno

group by b.sno, b.sname

having sum(cno) = (select sum(cno) from sc where sno = 2)


113、删除学习“王五”老师课的sc表记录

delete sc from course, teacher

where course.cno = sc.cno and course.tno = teacher.tno and tname = '王五'


114、向sc表中插入一些记录,这些记录要求符合以下条件:

将没有课程3成绩同学的该成绩补齐, 其成绩取所有学生的课程2的平均成绩

insert sc select sno, 3, (select avg(score) from sc where cno = 2)

from student

where sno not in (select sno from sc where cno = 3)

115、按平平均分从高到低显示所有学生的如下统计报表:

-- 学号,企业管理,马克思,UML,数据库,物理,课程数,平均分

select sno as 学号

,max(case when cno = 1 then score end) AS 企业管理

,max(case when cno = 2 then score end) AS 马克思

,max(case when cno = 3 then score end) AS UML

,max(case when cno = 4 then score end) AS 数据库

,max(case when cno = 5 then score end) AS 物理

,count(cno) AS 课程数

,avg(score) AS 平均分

FROM sc

GROUP by sno

ORDER by avg(score) DESC

116、查询各科成绩最高分和最低分:

以如下形式显示:课程号,最高分,最低分
select cno as 课程号, max(score) as 最高分, min(score) 最低分
from sc group by cno

select  course.cno as '课程号'

,MAX(score) as '最高分'

,MIN(score) as '最低分'

from sc,course

where sc.cno=course.cno

group by course.cno


117、按各科平均成绩从低到高和及格率的百分数从高到低顺序

SELECT t.cno AS 课程号,

max(course.cname)AS 课程名,

isnull(AVG(score),0) AS 平均成绩,

100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/count(1) AS 及格率

FROM sc t, course

where t.cno = course.cno

GROUP BY t.cno
ORDER BY 及格率 desc

118、查询如下课程平均成绩和及格率的百分数(用"1行"显示):

企业管理(001),马克思(002),UML (003),数据库(004)

select

avg(case when cno = 1 then score end) as 平均分1,

avg(case when cno = 2 then score end) as 平均分2,

avg(case when cno = 3 then score end) as 平均分3,

avg(case when cno = 4 then score end) as 平均分4,

100 * sum(case when cno = 1 and score > 60 then 1 else 0 end) / sum(casewhen cno = 1 then 1 else 0 end) as 及格率1,

100 * sum(case when cno = 2 and score > 60 then 1 else 0 end) / sum(casewhen cno = 2 then 1 else 0 end) as 及格率2,

100 * sum(case when cno = 3 and score > 60 then 1 else 0 end) / sum(casewhen cno = 3 then 1 else 0 end) as 及格率3,

100 * sum(case when cno = 4 and score > 60 then 1 else 0 end) / sum(casewhen cno = 4 then 1 else 0 end) as 及格率4

from sc


119、查询不同老师所教不同课程平均分, 从高到低显示

select max(c.tname) as 教师, max(b.cname) 课程, avg(a.score) 平均分

from sc a, course b, teacher c

where a.cno = b.cno and b.tno = c.tno

group by a.cno

order by 平均分 desc

或者:

select r.tname as '教师',r.rname as '课程' , AVG(score) as '平均分'

from sc,

(select  t.tname,c.cno as rcso,c.cname as rname

from teacher t ,course c

where t.tno=c.tno)r

where sc.cno=r.rcso

group by sc.cno,r.tname,r.rname

order by AVG(score) desc

120、查询如下课程成绩均在第3名到第6名之间的学生的成绩:

-- [学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩

select top 6 max(a.sno) 学号, max(b.sname) 姓名,

max(case when cno = 1 then score end) as 企业管理,

max(case when cno = 2 then score end) as 马克思,

max(case when cno = 3 then score end) as UML,

max(case when cno = 4 then score end) as 数据库,

avg(score) as 平均分

from sc a, student b

where a.sno not in


(select top 2 sno from sc where cno = 1 order by score desc)

 and a.sno not in (select top 2 sno from sc where cno = 2 order by scoredesc)

 and a.sno not in (select top 2 sno from sc where cno = 3 order by scoredesc)

 and a.sno not in (select top 2 sno from sc where cno = 4 order by scoredesc)

 and a.sno = b.sno

group by a.sno


相关文章
|
7月前
|
存储 安全 Java
冒死潜入某个外包公司获得的珍贵Java基础笔试题(附答案)
冒死潜入某个外包公司获得的珍贵Java基础笔试题(附答案)
181 0
|
4月前
|
存储 Java 编译器
刷完一千道java笔试题的常见题目分析
这篇文章是关于刷完一千道Java笔试题后的常见题目分析,涵盖了Java基础知识点,如标识符命名规则、抽象类与接口的区别、String类的equals方法、try-catch-finally块的执行逻辑、类与实例方法的区别、this与super关键字的用法、面向对象的基本概念、重写与重载的原则等,并建议结合JVM内存结构图加深理解。
刷完一千道java笔试题的常见题目分析
|
SQL 存储 数据管理
Java经典笔试题—day13
Java经典笔试题—day13
123 0
|
机器学习/深度学习 SQL 关系型数据库
Java经典笔试题—day14
Java经典笔试题—day14
Java经典笔试题—day12
Java经典笔试题—day12
|
算法 Java 数据库
Java经典笔试题—day11
Java经典笔试题—day11
|
存储 安全 Java
Java经典笔试题—day10
Java经典笔试题—day10
187 0
Java经典笔试题—day09
Java经典笔试题—day09
104 0
|
存储 机器学习/深度学习 Java
Java经典笔试题—day08
Java经典笔试题—day08
112 0
|
存储 Java 数据安全/隐私保护
Java经典笔试题—day07
Java经典笔试题—day07