Java笔试题复盘

一 数据库题目

表结构:

Student表中有SCode,SName,SAge字段。
Course表中有ID,SCode,CCode,CScore字段。
如图所示
表结构
演示数据(便于理解)
示列数据

问题描述:
1.用一条sql语句,查询出没门课程都大于80分的学生编号,学生姓名。

select SCode,SName from appdb.Student where SCode NOT IN (select distinct SCode from appdb.Course where CScore <=80 );

distinct关键字是仅列出不同的值

2.将学生编号为S3的学生的课程C5的分数,更新为所有学生的平均分数。

update course set cscore = (select avg(cscore) from course) where scode = 'S3' and ccode = 'C5';

3.在Course表中,删除课程编号为"C1",且分数小于80分的数据。

delete from appdb.Course where CCode='C1' and CScore < 80;

4.将Course表中的数据,拷贝到Coursebak表中。

create table Coursebak like appdb.Course;
insert into Coursebak select * from appdb.Course;

二 编程题目

均在注释当中解析

package com.test;

public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        // 简单计算
        System.out.println(test.calSum(3));
        System.out.println(test.calSum(5));
        // 字符串比较
        test.equalCompare();
        // 继承与重写(覆盖)
        Father tFather = new Son();
        tFather.say(); // 结果 I am Son;
        // 打印乘法口诀。
        test.printMultiplicationFormulas();
        //找出字符串中abc出现的次数。
        String tStr = " kfjj  abkkk  dajca abccccabacaabcbcabcabccabckjlkjlkjcabc";
        int count = test.contStrBy(tStr,"abc");
        System.out.println("abc出现的次数:"+count+"次");
        // 计算硬币共有几个
        System.out.println(test.coinQuestion(5));
    }
    /**
     *  等号和equal的区别,new String() 和String的区别。 new String() 不是保存在常量池中
     */
    public void equalCompare(){
        String tOldName = "Hello";
        String tNewName = "He"+ new String("llo");
        System.out.println(tOldName == tNewName);
    }// 结果:false
    /**
     * 打印乘法口诀,嵌套循环
     */
    public void printMultiplicationFormulas(){
        // 如果重新排列,算法需要重新实现。按行去打印。
        for (int i = 1; i<10;i++){
            for (int j = i;j < 10;j++){
                // 省略代码。。。
                System.out.println(i + "*" + j + "="+(i*j));
            }
            System.out.println();
        }
    }

    /**
     * 简单计算--- 主要找到临界点
     * @param a
     * @return
     */
    public int calSum(int a){
        if (0 < a){
            return (calSum(a-2) + 3);
        }
        return 1;
    }//结果:calSum(3) = 7,calSum(5)=10;
    /**
     * 计算某个字符串中出现了几次子串,使用contains函数,如果过滤空格用,trim()方法,只过滤首尾空格。replaceAll替换也行。
     * @param totalStr
     * @param childStr
     * @return
     */
    public int contStrBy(String totalStr,String childStr){
        int num = 0;
        // 查看是否包含,包含的话继续查找
        while (totalStr.contains(childStr)){
            totalStr = totalStr.substring(totalStr.indexOf(childStr) + childStr.length());
            num = num + 1;
        }
        return num;
    }

    /**
     * 每次花一半 再花一个到第n天只剩一个问题,---倒推第一天的结果。
     * @return
     */
    public int coinQuestion(int day){
        int sum = 1;
        for (int i = day - 1;i > 0;i--){
            sum = 2*(sum + 1);
        }
        return sum;
    }// 结果:5天后只剩一个共有46个硬币
}

/**
 * 验证向上转型是否调用父类的方法,重写了方法就会实现对象的方法。
 */
class Father{
    public void say(){
        System.out.println("I am Father");
    }
}
class Son extends Father{
    public void show(){
        System.out.println("I Will Show");
    }
    public void say(){
        System.out.println("I Am Son");
    }
}
发布了14 篇原创文章 · 获赞 3 · 访问量 3493

猜你喜欢

转载自blog.csdn.net/yulianpeng/article/details/94409595
今日推荐