java笔试 - 套题(5 答案在尾)

试题

1,SQL

Table Name: Students
        Name                        Null?                                 Type
------------------------  ------------------------  ---------------------------------------------------
Student_ID                 NOT NULL                         NUMBER (10)
Student_Name           NOT NULL                         VARCHAR (25)
Gender                                                                   VARCHAR (6)
Birthday                                                                   DATE
ID_Card_No               NOT NULL                         VARCHAR (20)
Nationality                                                             VARCHAR (25)
Native_Place                                                         VARCHAR (25)
Major                         NOT NULL                           VARCHAR (50)
Class_Grade                                                            VARCHAR (20)
Enrollment_Date                                                      DATE

Table Name: Student_Scores
Name                                  Null?                                 Type
------------------------  ------------------------  ---------------------------------------------------
Score_ID                         NOT NULL                            NUMBER (12)
Student_ID                      NOT NULL                         NUMBER (10)
Course_Name                 NOT NULL                           VARCHAR (50)
Teacher_Name                                                            VARCHAR (25)
Score                              NOT NULL                         NUMBER (3 , 2)
Exam_Date                                                                   DATE
Pass_Or_Fail                                                                VARCHAR (16)
Comment                                                                       VARCHAR (200)

Question 1): Write a SQL script to search all the students’ name whose score of each course is above 80.

Question 2): Write a SQL script to update the column Pass_Or_Fail in table Student_Scores, with value “Pass” (if score is >= 60) or with value “Fail” (if score is < 60), and the students’ major must be “Software Engineering”.

2,Java

Question 1)

public class Starter extends Thread {
  private int x = 2;
        public static void main(String[] args) throws Exception {
        new Starter().makeItSo();
}
        public Starter(){
                x = 4;
                start();
        }
        public void makeItSo() throws Exception {
                join();     
           x = x - 1;
    System.out.println(x);       }

         public void run() { x *= 2; }

}

A. 3                         B. 5                        C. 7                        D. 9

Question 2)

public void go() {
String o = "";
 z:
 for (int x = 0; x < 3; x++) {
       for (int y = 0; y < 2; y++) {
              if(x==1) break;
              if(x==2 && y==1) break z;
              o = o + x + y;
       } }
 System.out.println(o);
 }

What is the result when the go() method is invoked?
A. 00                        B. 0001                        C. 000120

D. 00012021                        E. Compilation fails.                   F. An exception is thrown at runtime.

3,Answer

Question 1)

Write a Java function to print all the node values of a binary tree. The tree supports the following methods:
public class TreeNode {
 public TreeNode leftChild();   // return the left child of a node.      
public TreeNode rightChild();  // return the right child of a node.      
public void printValue();       // print the value of the node. }

public static void printTree(TreeNode t) {

}

Question 2)

Write a program with singleton mode

Question 3)

 There is a list store millions of phone number, 
             ArrayList<String> phoneNumbers = new ArrayList<String>()
              phoneNumbers.add("0668-8976555");
              phoneNumbers.add("0668-8976666");
              ...    
          Please write a Java function to remove duplicate phone number in this list and the list request sorted by phone number. (At least two ways, performance first)

答案

1,问题1:编写SQL脚本,搜索每门课成绩在80分以上的所有学生的名字。

sql1:
SELECT Student_Name FROM Students WHERE Student_ID IN(
SELECT Student_ID FROM Student_Scores GROUP BY Student_ID HAVING MIN(Score)>80)

sql2:
SELECT Student_Name FROM Students WHERE Student_ID IN(
SELECT Student_ID FROM Student_Scores GROUP BY Student_ID HAVING Student_ID NOT IN(SELECT Student_ID FROM Student_Scores WHERE Score <80))

sql3:
SELECT Student_Name FROM Students WHERE Student_ID IN(
SELECT Student_ID FROM Student_Scores GROUP BY Student_ID HAVING COUNT(Score)=SUM(CASE WHEN Score>80 THEN 1 ELSE 0 END))

问题2:编写SQL脚本更新Student_Scores表中的Pass_Or_Fail列,值为Pass(如果分数为>= 60)或Fail(如果分数< 60),学生的专业必须为“软件工程”。

UPDATE Student_Scores SET Pass_Or_Fail = 
(CASE WHEN Score>=60 THEN 'Pass' ELSE 'Fail' END) 
WHERE  Course_Name  = '软件工程'

2,问题1:C

  问题2:C

3,问题1:编写一个Java函数来打印二叉树的所有节点值。树支持以下几种方法

public class TreeNode {
 public TreeNode leftChild();   // 返回节点的左子节点.      
public TreeNode rightChild();  // 返回节点的右子节点。     
public void printValue();       // 打印节点的值. }

public static void printTree(TreeNode t) {

https://www.csdn.net/tags/NtTaIgzsNzcyLWJsb2cO0O0O.html

}

问题2:用单例模式写一个程序

饥汉模式:

 * 优点:当类被加载的时候,已经创建好了一个静态的对象,因此,是线程安全的; 
 * 缺点:这个对象还没有被使用就被创建出来了。
public class HungrySingleton {
	// 私有化静态成员变量,已初始化
	private static HungrySingleton test = new HungrySingleton();
 
	// 私有化构造方法
	private HungrySingleton() {
	}
 
	// 提供一个公共的接口供外界获得单例对象
    // 不需要同步(类加载时已经初始化,不存在多线程的问题)
    // 始终只有一个对象
	public static HungrySingleton getInstance() {
		return test;
	}
}


懒汉模式:

 *  优点:按需加载对象,只有对象被使用的时候才会被创建
 *  缺点:这种写法不是线程安全的,例如当第一个线程执行判断语句if(test = null)时,
 *  第二个线程执行判断语句if(test = null),接着第一个线程执行语句test = new Test(), 
 *  第二个线程也执行语句test = new Test(),在这种多线程环境下,可能会创建出来两个对象。
public class LazySingleton {
	// 私有化静态成员变量,防止外界修改,没有实例化
	private static LazySingleton test = null;
 
	// 私有化构造方法,防止外界调用,保证对象是单例对象
	private LazySingleton() {
	}
 
	// 提供一个公共的接口供外界获得单例对象
	// 当多个线程都在调用此方法时,必须保证只有一个单例对象生成,
	// 这里采用对同步代码块加上
	// 因为成员变量是静态的,该方法也必须是静态方法
	public static LazySingleton getInstance() {
		if (test == null) {
			// 静态方法,使用当前类本身充当进程锁
			synchronized (LazySingleton.class) {
				test = new LazySingleton();
			}
		}
		return test;
	}
}

问题3:有一个列表存储数百万个电话号码,
             ArrayList<String> phoneNumbers = new ArrayList<String>()
              phoneNumbers.add("0668-8976555");
              phoneNumbers.add("0668-8976666");
              ...    
          请写一个Java函数来删除这个列表中重复的电话号码和按电话号码排序的列表请求. (至少有两种方式,性能第一)

ArrayList arrayListnew = new ArrayList<>();
        for(String s:phoneNumbers){
            if(!arrayListnew.contains(s)){
                arrayListnew.add(s);
            }
        }
        Object collect = arrayListnew.stream().sorted().collect(Collectors.toList());
        System.out.println(collect);

人无完人,接受指点与反驳,森!!仔


版权声明:本文为zs1972551648原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。