2.下面代碼的輸出是什么?一共在內存中生成了幾個String對象?為什么?
String s1 = “aaa”;
String s2 = “aaa”;
String s3 = new String(“aaa”);
String s4 = new String(“AAA”);
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1.equals(s3));
3.下列程序在1處是否會有異常,如果沒有,輸出是什么?是否會運行到2處,如果會,輸出是什么?為什么會有這樣的結果?
public class TestClass {
public void test1() {
List list = new ArrayList();
test2(list);
System.out.println(list.size()); // 1處
test3(list);
System.out.println(list.size()); // 2處
}
public void test2(List list) {
list = null;
}
public void test3(List list) {
list.add(“aaaa”);
}
}
4.請選出下面哪些描述是正確的。
public class ClassA {
public synchronized void a(){
}
public synchronized void b(){
}
}
2 instances of ClassA had been instantiated obj1 and obj2.
Which statements about thread are true?
1)One thread is calling obj1.a(), another thread can call obj1.b().? .
2)One thread is calling obj1.a(), another thread cannot call obj1.b().?
3)One thread is calling obj1.a(), another thread can call obj2.b().? .
4)One thread is calling obj1.a(), another thread cannot call obj2.b().?
5.下面的程序輸出是什么?為什么?
public class Parent {
public void test(ArrayList list) {
System.out.println("invoke parent's test method");
}
public static void main(String[] args) {
Child a = new Child();
ArrayList list = new ArrayList();
a.test(list);
}
}
class Child extends Parent {
public void test(List list) {
System.out.println("invoke child's test method");
}
}
6.下面的程序輸出是什么?為什么?
public class Parent {
public void test(List list) {
System.out.println("invoke parent's test method");
}
public static void main(String[] args) {
Child a = new Child();
ArrayList list = new ArrayList();
a.test(list);
}
}
class Child extends Parent {
public void test(List list) {
System.out.println("invoke child's test method");
}
}
7.仔細分析下面的程序,寫出程序的輸出結果。
public class Parent {
{
System.out.println("parent instance block");
}
public void test() {
System.out.println("parent test method");
}
static {
System.out.println("parent static block");
}
public Parent() {
System.out.println("parent constructor");
test();
}
public static void main(String[] args) {
new Child();
}
}
class Child extends Parent {
private static int staticValue = 20;
private int instanceValue = 20;
{
System.out.println("child instance block");
}
public void test() {
System.out.println("child test method");
System.out.println("static value is: " + staticValue);
System.out.println("instance value is: " + instanceValue);
}
static {
System.out.println("child static block");
}
public Child() {
System.out.println("child constructor");
}
}
8.下面程序的輸出是什么?
public class TestException {
public void test1() {
int result = test2();
System.out.println(result);
}
public int test2() {
try{
String s = null;
s.substring(0, 1);
return 1;
} catch(Exception e) {
return 2;
} finally {
return 3;
}
}
public static void main(String[] args) {
(new TestException()).test1();
}
}
9.請寫出數據庫查詢操作的偽代碼,程序不需要通過編譯,只要思路正確,關鍵步驟不丟失就可以了。注意異常的捕獲,IO流的關閉。可能用到的類或接口有(Connection,DriverManager, Statement, PreparedStatement, ResultSet, SQLException)。
posted on 2006-03-19 23:12
xnabx 閱讀(145)
評論(0) 編輯 收藏