初级试题错题库
1.下图子类会自动调用父类构造器无参的构造方法
class Example1{
public Example1(){
System.out.print(1);
}
}
class Example2 extends Example1{
public Example2(){
System.out.print(2);
}
}
class Example3 extends Example2{
public Example3(){
System.out.print(3);
}
}
public class Example{
public static void main(String[] args){
new Example3();
}
}
A 1
B. 3
C. 123
D. 321
2.
如果程序需要在匿名内部类中使用局部变量,那么这个局部变量必须使用_outter.this.属性名+static???final______修饰符。
3.以在内部类的类体里面调用外部类的方法,但是创建出来的内部类对象并不能调用外部类的方法
11. 下面程序运行的结果是( a)。(选择一项) public class Test{ class Inner{ void test(){ sample(); } } public void sample(){ System.out.println("Sample"); } public Test() { (new Inner()).test(); } public static void main(String[] args){ new Test(); } } A 输出Sample B. 输出null C. 编译错误 D. 运行错误
4.try后可以不跟catch,而直接跟fianlly。
比如:有时候为了释放某些资源(例如锁),但必须保证异常往上层抛出,可以采用try{} finally{//释放锁操作}
13. 对于Java中异常处理,以下描述错误的是( d )。(选择一项) A 捕获异常是一种积极的异常处理方式,通过 try 、catch 等关键字来实现 B. catch 之后的( )用于接收异常对象,因此需要指定异常类型和变量名称,比如 catch( Exception e ) C. 在JDK 1.7 中,允许在 catch 中捕获多个类型异常, 如catch( NullPointerException e1 | ClassCastException e2 ) D. try 必须 跟 catch 连用,而 finally 是可以没有的
5.j==i是2个对象的引用在比较!!
17. 下面程序运行的结果是( d )。(选择一项) public class Test{ public static void main(String[] args){ Integer i = 10; Integer j = 10; System.out.print(i==j); System.out.print(i.equals(j)); } } A false false B. true true C. true false D. false true
6.datainputstream只读取基本数据类型+它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体。
17. DataInputStream 数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型和各种引用数据类型,提供了相应的读取方法。( × ) 18. 线程是进程的中的一个实体,通常一个进程有若干个线程,但同一进程中的多个线程不能并发执行。( × )
7.字节字符数组和字符串间的转换(数组只是长度不能更改,值还是可以对其下手的!!)
1. 不使用Java系统函数,自己动手实现一个字符串翻转的方法。(5分)
public class Test {
public static String reverse(String s) {
char ch[] = s.toCharArray();
int start = 0, end = ch.length - 1;
char temp;
while (start < end) {
temp = ch[start];
ch[start] = ch[end];
ch[end] = temp;
start++;
end--;
}
String s1 = new String(ch);
return s1;
}
}
public class test01 { public static void main(String[] args) { String str="生产者生产的是哇哈哈矿泉水"; StringBuffer ele01=new StringBuffer(str); /* int ele02=ele01.length(); for (int i = 0; i < ele02 ;i++) { char ele03=ele01.charAt(ele02-i-1); ele01.setCharAt(i,ele03); } System.out.println(ele01); }*/ } }
8利用map的key是唯一的特性进行解题
给定一个字符串,必如"yekmaakkccekymbvb",求字符串中有多少种字符以及每个字符的个数;把结果写入D盘名为test的文本文件中;读出刚才写入test文本文件的内容。(8分)
public class Test { public static void main(String[] args) { String str = "yekmaakkccekymbvb"; Map<String, Integer> map = count(str); write(map); read(); } /** * 求字符串中有多少种字符以及每个字符的个数(3分) */ public static Map<String, Integer> count(String s) { Map<String, Integer> map = new LinkedHashMap<String, Integer>(); for (int i = 0; i < s.length(); i++) { String key = s.substring(i, i + 1); Integer value = map.get(key); if (value == null) { map.put(key, 1); } else { map.put(key, value + 1); } } return map; } /** * 把结果写入D盘名为test的文本文件中(3分) */ public static void write(Map<String, Integer> map) { PrintWriter pw = null; try { pw = new PrintWriter("d:/test.txt"); pw.println("size:" + map.size()); Set<Entry<String, Integer>> entrySet = map.entrySet(); for (Entry entry : entrySet) { pw.println(entry.getKey() + ":" + entry.getValue()); } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { pw.close(); } } /** * 读出刚才写入test文本文件的内容(2分) */ public static void read() { BufferedReader br = null; try { br = new BufferedReader(new FileReader("d:/test.txt")); String str = null; do { str = br.readLine(); System.out.println(str); } while (str != null); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { } } }