笔试题02

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)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值