1.long sum(String n,String m)throws Exception,求n和m的和.
答:
long sum(String n, String m) throws Exception {
long ln = 0, lm = 0;
try {
ln = Long.parseLong(n);
} catch (NumberFormatException ex) {
throw new Exception("n转换异常");
}
try {
lm = Long.parseLong(m);
} catch (NumberFormatException ex) {
throw new Exception("m转换异常");
}
return ln + lm;
}
2.设计模式:什么是设计模式,描述使用过的设计模式.
答:设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。如:懒汉模式,饿汉模式,观察者模式。。。
3.写一个方法,实现字符串的反转,如:输入abcd,输出dcba.
答:
public String convert(String str) {
String newstr;
char[] oldchars = str.toCharArray();
char[] newchars = new char[oldchars.length];
for (int i = oldchars.length - 1; i >= 0; i--) {
newchars[oldchars.length - i - 1] = oldchars[i];
}
newstr = new String(newchars);
return newstr;
}
4.比较两个列表中的对象,列表中保存的都是Student对象,根据Student的名字作为比较关键字;和旧的列表相比,找出全部新增,修改(年龄不同),删除的列表;
public class Student{
String name;
int age;
}
public void compare(List
public void compare(List
List newStudents = new ArrayList();
List modifyStudents = new ArrayList();
List deleteStudents = new ArrayList();
boolean isFindStudent = false;
for (int i = 0; i < oldList.size(); i++) {
Student oldStudent = oldList.get(i);
for (int j = newList.size() - 1; j >= 0; j--) {
Student newStudent = newList.get(j);
isFindStudent = false;
if (https://www.wendangku.net/doc/9815727155.html,.equals(https://www.wendangku.net/doc/9815727155.html,) &&
oldStudent.age != newStudent.age) {
//名字相同年龄不同的为修改的
modifyStudents.add(oldStudent);
newList.remove(j);
isFindStudent = true;
} else if (https://www.wendangku.net/doc/9815727155.html,.equals(https://www.wendangku.net/doc/9815727155.html,) &&
oldStudent.age == newStudent.age) {
//删除新列表中没有发生变化的
newList.remove(j);
isFindStudent = true;
}
}
//新列表没找到,是删除的
if (!isFindStudent)
deleteStudents.add(oldStudent);
}
//剩下的是新增的
for (Student newStudent : newList) {
newStudents.add(newStudent);
}
}
5. 接口和内部类、抽象类的特征?
答:接口:在一个类里,只有申明没有实现。
内部类:是在一个类的内部定义的一个类;
抽象类:是以abstract 定义的,里面至少有一个抽象方法6.编译和执行下例代码会输出什么?
class Test {
int i;
String s;
public void method(){
int i=10;
system.out.println(i);
public Test(){
system.out.println(s);
}
}
答:编译不过。方法method()缺少},且未对system作出声明。
7、编译和执行下例代码会输出什么?
public class ko3_6{
static{
System.out.println(“Hello”);
}
}
答:可编译,类加载时输出Hello。
8、编译和执行下例代码会输出什么?
public class ko5_8{
public static void main(String args[ ]) {
int x=1,sum=0;
while(x<=10) {
sum+=x;
x++;
}
System.out.println("sum="+sum);
}
}
答:会输出“sum=55”。
9.编译和执行下例代码会输出什么?public class ko6_9{
public static void main(String args[ ]) { int sum=0;
int ko[ ][ ]={{1,2,3},{4,5,6},{7,8,9}};
for(int n=0;n<3;n++)
for(int m=0;m<3;m++)
sum+=ko[n][m];
System.out.println("sum="+sum);
}
}
答:会输出“sum=45”。
10.编译和执行下例代码会输出什么?public class ko8_1{
public static void main(String args[ ]) {
try{
int x[ ]=new int[-5];
System.out.println("此行将无法被执行!");
}catch(NegativeArraySizeException e) {
System.out.println("exception: " + e.getMessage());
}
}
}
答:初始化数组时抛出异常,执行catch语句,输出exception: null 11.执行下例代码会输出什么?
public class ko10_1 extends Thread{
int n;
ko10_1(){
Thread td=new Thread(this);
td.start();
}
public void run(){
for (n=0;n<6;n++){
try{
System.out.print(n);
Thread.sleep(500);
}catch(InterruptedException e) {
System.out.println("Exception");
}
}
}
public static void main(String args[ ]){
new ko10_1();
}
}
答:每隔半秒打印一次,分别是012345