1、java基础数据类型:
5、写出下面程序输出结果
6、找出下面程序中的错误
解答:
(1). 普通索引
普通索引(由关键字KEY或INDEX定义的索引)的唯一任务是加快对数据的访问速度。因此,应该只为那些最经常出现在查询条件(WHERE column = …)或排序条件(ORDER BY column)中的数据列创建索引。只要有可能,就应该选择一个数据最整齐、最紧凑的数据列(如一个整数类型的数据列)来创建索引。
(2). 唯一索引
普通索引允许被索引的数据列包含重复的值。比如说,因为人有可能同名,所以同一个姓名在同一个”员工个人资料”数据表里可能出现两次或更多次。
如果能确定某个数据列将只包含彼此各不相同的值,在为这个数据列创建索引的时候就应该用关键字UNIQUE把它定义为一个唯一索引。这么做的好处:一是简 化了MySQL对这个索引的管理工作,这个索引也因此而变得更有效率;二是MySQL会在有新记录插入数据表时,自动检查新记录的这个字段的值是否已经在 某个记录的这个字段里出现过了;如果是,MySQL将拒绝插入那条新记录。也就是说,唯一索引可以保证数据记录的唯一性。事实上,在许多场合,人们创建唯 一索引的目的往往不是为了提高访问速度,而只是为了避免数据出现重复。
(3). 主索引
在前面已经反复多次强调过:必须为主键字段创建一个索引,这个索引就是所谓的”主索引”。主索引与唯一索引的唯一区别是:前者在定义时使用的关键字是 PRIMARY而不是UNIQUE。
(4). 外键索引
如果为某个外键字段定义了一个外键约束条件,MySQL就会定义一个内部索引来帮助自己以最有效率的方式去管理和使用外键约束条件。
(5). 复合索引
2、Linux 64位操作系统下,int、long、float分别占多少个字节。
3、写出下面代码的输出结果:
String s1 = "123";
String s2 = "123";
String s3 = new String("123");
String s4 = s3.intern();
System.out.println(String.format("%s,%s,%s,%s",s1 == s2, s1 == s3, s1 == s4, s3 == s4));
4、有m(m > 0)个人需要住宾馆,每间房间可以住n个人,完成函数(只用+、-、*、/四则运算)
/**
*
* @param m
* @param n
* @return
*/
public int room(int m, int n){
return _____;
}
5、写出下面程序输出结果
static{
show();
}
private static final int m = 10;
private static final String s1 = "123";
private static final String s2 = "456";
public static void show(){
System.out.println(String.format("%s,%s,%s", m,s1,s2));
}
6、找出下面程序中的错误
public class TestC{
public void openFile(File filename){
}
private static class Child extends TestC{
@Override
public void openFile(File filename) throws FileNotFoundException{
}
}
}
7、当tootal趋近与无穷时,输出下面程序运行结果
public static void random(int total){ int count = 0; for(int i = 0; i < total; i++){ Random random = new Random(); double x = random.nextDouble(); double y = random.nextDouble(); if(x * x + y * y < 1) count ++; } System.out.println((double)count/total); }
8、为了使索引键的值在基本表中唯一,在建立索引语句中应使用的保留字是
A、UNIQUE B、FOREIGN C、DISTINCT D、KEY
9、关系型数据库一般采用的索引算法是
10、写出7中设计模式名称,并用代码实现单例模式
11、从一堆球m个中取出n个,m和n都很大,实现下面代码
Ball [] find(Ball [] balls, int n)
解答:
1、基本数据类型:布尔型:boolean
整数型:byte,short,int,long
字符型:char
浮点型:float,double
2、int 4字节,long 8字节,float 4字节
3、true,false,true,false
public String intern()返回字符串对象的规范化表示形式。
当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(用 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并返回此 String 对象的引用。
当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(用 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并返回此 String 对象的引用。
可参考如下内容:http://www.cnblogs.com/zdwillie/archive/2013/10/23/3384766.html
4、(int)(m + n - 1)/n
5、10,123,456
6、重写时,子类不能比父类抛出更多的异常
7、0.78
8、A
MySQL索引类型包括:
普通索引(由关键字KEY或INDEX定义的索引)的唯一任务是加快对数据的访问速度。因此,应该只为那些最经常出现在查询条件(WHERE column = …)或排序条件(ORDER BY column)中的数据列创建索引。只要有可能,就应该选择一个数据最整齐、最紧凑的数据列(如一个整数类型的数据列)来创建索引。
(2). 唯一索引
普通索引允许被索引的数据列包含重复的值。比如说,因为人有可能同名,所以同一个姓名在同一个”员工个人资料”数据表里可能出现两次或更多次。
如果能确定某个数据列将只包含彼此各不相同的值,在为这个数据列创建索引的时候就应该用关键字UNIQUE把它定义为一个唯一索引。这么做的好处:一是简 化了MySQL对这个索引的管理工作,这个索引也因此而变得更有效率;二是MySQL会在有新记录插入数据表时,自动检查新记录的这个字段的值是否已经在 某个记录的这个字段里出现过了;如果是,MySQL将拒绝插入那条新记录。也就是说,唯一索引可以保证数据记录的唯一性。事实上,在许多场合,人们创建唯 一索引的目的往往不是为了提高访问速度,而只是为了避免数据出现重复。
(3). 主索引
在前面已经反复多次强调过:必须为主键字段创建一个索引,这个索引就是所谓的”主索引”。主索引与唯一索引的唯一区别是:前者在定义时使用的关键字是 PRIMARY而不是UNIQUE。
(4). 外键索引
如果为某个外键字段定义了一个外键约束条件,MySQL就会定义一个内部索引来帮助自己以最有效率的方式去管理和使用外键约束条件。
(5). 复合索引
索引可以覆盖多个数据列,如像INDEX(columnA, columnB)索引。这种索引的特点是MySQL可以有选择地使用一个这样的索引。如果查询操作只需要用到columnA数据列上的一个索引,就可以使 用复合索引INDEX(columnA, columnB)。不过,这种用法仅适用于在复合索引中排列在前的数据列组合。比如说,INDEX(A, B, C)可以当做A或(A, B)的索引来使用,但不能当做B、C或(B, C)的索引来使用。
参考:http://www.cnblogs.com/kupig/archive/2011/10/19/2217228.html
其他关键字表示的意义:
FOREIGN :表示外键索引
DISTINCT:去重
9、B+、B- Tree
10、设计模式分为三种类型,共23种。
-
创建型模式:单例模式、抽象工厂模式、建造者模式、工厂模式、原型模式。
-
结构型模式:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。
-
行为型模式:模版方法模式、命令模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式、状态模式、策略模式、职责链模式、访问者模式
Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在。
第一种:饱汉模式
public class SingleTon {
private SingleTon(){}
//实例化放在静态代码块里可提高程序的执行效率,但也可能更占用空间
private final static SingleTon instance =new SingleTon();
public static SingleTon getInstance(){
return instance;
}
}
//第二种:饥汉模式
class SingleTon {
private SingleTon(){}
private static SingleTon instance = null;//newSingleTon();
public static synchronized SingleTon getInstance(){
if(instance == null)
instance = new SingleTon();
return instance;
}
}
11、自己编写了一点
public Ball [] pickUp(Ball [] balls, int n){
LinkedList<Ball> results = new LinkedList<Ball>();
int size = 0;
int length = balls.length;
Random random = new Random();
int t = 0;
int index = 0;
Ball temp = null;
do{
t = length - size - 1;
index = random.nextInt(t);
results.add(balls[index]);
temp = balls[t];
balls[t] = balls[index];
balls[index] = temp;
size ++;
}while(size < (n - 1));
return (Ball[]) results.toArray();
}
public Ball [] pickUp2(Ball [] balls, int n){
int size = 0;
Random random = new Random();
HashMap<Integer, Ball> map = new HashMap<Integer, Ball>();
int index = 0;
do{
index = random.nextInt(n);
if(!map.containsKey(index)){
map.put(index, balls[index]);
size ++;
}
}while(size < (n - 1));
return (Ball[]) map.values().toArray();
}