一、JavaScript 编程题
按照下列要求,用JavaScript写一个年,月,日的联动菜单。
1、默认显示当前日期;
2、选择年才可以选择月,选择月才可以选择日,并且 2 月的天数要正确;
解答:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>年月日联动</title>
<style type="text/css">
</style>
</head>
<body>
<div id="container">
<form>
<select id="sYear">
<option> 请选择年</option>
</select>
<select id="sMonth" onclick="activateDay(this.value)">
<option> 请选择月</option>
</select>
<select id="sDay" disabled="disabled">
<option> 请先选择年月再选择日</option>
</select>
</form>
</div>
</body>
<script type="text/javascript">
window.onload = function(){
createOptionYear("年",$("sYear"),10); // 年选项操作。前后10年。
createOption("月",$("sMonth"),12); // 月选项操作。
}
// 函数:简化获取标签元素代码量。
function $(id){
return document.getElementById(id);
}
// 创建年选项的option。默认值当前年。
function createOptionYear(str,p,num){
var c;
var tim = new Date();
for(var i = tim.getFullYear() - num; i < tim.getFullYear() + num; i++){
if(i == tim.getFullYear()){
c = c + "<option selected>" + i + str + "</option>"; // 默认当前年。
}else{
c = c + "<option>" + i + str + "</option>";
}
}
p.innerHTML = c;
}
// 创建月日选项的option。无默认值。
function createOption(str,p,num){
var c;
for(var i = 1; i <= num; i++){
c = c + "<option>" + i + str + "</option>";
}
p.innerHTML = c; // 创建结果放入父标签中。
}
// 使日选择框可用。并创建option。
function activateDay(sMonth){
$("sDay").removeAttribute("disabled");
createOption("日",$("sDay"),outDays(parseInt(sMonth))); // 调用创建和判断函数。
}
// 判断闰年。返回相应2月天数。
function judgeLeapYear(year){
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0 )){
return 29;
}else{
return 28;
}
}
// 选择月天数。返回相应的月天数。
function outDays(smonth){
switch(smonth){
case 4:
return 30;
break;
case 6:
return 30;
break;
case 9:
return 30;
break;
case 11:
return 30;
break;
case 2:
return judgeLeapYear(parseInt($("sYear").value)); // 判断闰年,并返回天数。
break;
default:
return 31;
break;
}
}
</script>
</html>
二、MySQL 简答题
常见的几种约束有哪些?分别代表什么意思?如何使用?
解答:
约束类型 | 名称 | 含义 | 使用 |
---|---|---|---|
primary key | 主键 | not null + unique , 一个表有且仅有一个主键,但一个主键可以包含多个字段。保证实体完整性 | constraint 自定义主键名 primary key (列名)或者创建表时直接加入primary key。 |
foreign key | 外键 | 连接两个表,保证引用完整性 | alter table t_name add constraint 自定义外键名 foreign key (c_name) references 表名(列名) |
not null | 非空 | 必须填写, | 创建表时直接加入not null。 |
unique | 唯一 | 只有一个,保证实体完整性 | 创建表时直接加入unique。 |
三、Java 简答题
数组(Array)和列表(ArrayList)有什么区别?什么时候应该使用 Array 而不是 ArrayList?
解答:
项目 | Array | ArrayList |
---|---|---|
存储内容 | 同种类型 | 可不同种 |
包含数据类型 | 基本数据类型或对象类型 | 对象类型 |
长度 | 固定 | 可动态改变 |
方法和特性 | 较少 | 多 |
关系 | ArrayList可以算作Array的加强版 | - |
使用
Array:程序运行期间,数据存在且不变 。
ArrayList: 以数组形式保存,不操作,方便查找。
LinkedList:频繁的移动或删除。超大量数据。