您的位置:首页 > 职场人生

每天10个面试题(day05)

2016-09-07 10:54 232 查看
1、下面有关servlet中init,service,destroy方法描述错误的是?

A. init()方法是servlet生命的起点。一旦加载了某个servlet,服务器将立即调用它的init()方法

B. service()方法处理客户机发出的所有请求

C. destroy()方法标志servlet生命周期的结束

D. servlet在多线程下使用了同步机制,因此,在并发编程下servlet是线程安全的

解:D,

Servlet在多线程下并不是线程安全的,假如Servlet类中定义了全局变量,当多线程并发就会对该变量不断的修改,而引起错误。所以为了防止这个问题,应该将全局变量设置为存在方法中的局部变量。由于方法中的局部变量是在栈中,彼此各自都拥有独立的运行空间而不会互相干扰,因此才做到线程安全。

2、结构型模式中最体现扩展性的模式是()

A. 装饰模式

B. 合成模式

C. 桥接模式

D. 适配器模式

解:A, 装饰模式动态给类增加了一些功能,最典型的装饰模式就是java中的I/O流。

3、Which two statements are true if this class is compiled and run?

public class NameList
{
private List names = new ArrayList();
public synchronized void add(String name)
{
names.add(name);
}
public synchronized void printAll()     {
for (int i = 0; i < names.size(); i++)
{
System.out.print(names.get(i) + ””);
}
}

public static void main(String[]args)
{
final NameList sl = new NameList();
for (int i = 0; i < 2; i++)
{
new Thread()
{
public void run()
{
sl.add(“A”);
sl.add(“B”);
sl.add(“C”);
sl.printAll();
}
} .start();
}
}
}


A. An exception may be thrown at runtime.

B. The code may run with no output, without exiting.

C. The code may run with no output, exiting normally(正常地).

D. The code may rum with output “A B A B C C “, then exit.

E. The code may rum with output “A B C A B C A B C “, then exit.

F. The code may ruin with output “A A A B C A B C C “, then exit.

G. The code may ruin with output “A B C A A B C A B C “, then exit.

解:A,

第一次println的字符个数肯定大于等于3,小于等于6;第二次println的字符个数肯定等于6;所以输出的字符中,后6位肯定是第二次输出的,前面剩下的就是第一次输出的。而且第一次的输出结果肯定是第二次输出结果的前缀。所以选E、G。

这里有个规律:线程内顺序执行,线程间交叉执行。

4、针对以下代码,哪些说法是正确的:()

class CompareReference{
public static void main(String [] args){
float f=42.0f;
float f1[]=new float[2];
float f2[]=new float[2];
float[] f3=f1;
long x=42;
f1[0]=42.0f;
}
}


A. f1==f2

B. x==f1[0]

C. f1==f3

D. f2==f1[1]

解:BC, C毋庸置疑,两个引用指向了同一个内存;B正确因为基本类型之间的比较,会自动将低精度类型转化为高精度类型。

拓展:

当操作元中存在double类型的操作元时,所有操作元自动转换为double类型,表达式的值为double类型。

否则 float –> float 表达式返回 float

否则 long –> long 表达式返回 long

否则 所有操作元自动转换为int类型,表达式的值为int类型。

复习一下8种基本数据类型:



5、执行下列代码的输出结果是( )

public class Demo{
 public static void main(String args[]){
   int num = 10;
   System.out.println(test(num));
}
public static int test(int b){
   try
   {
    b += 10;
    return b;
   }
   catch(RuntimeException e)
   {
   }
   catch(Exception e2)
   {
   }
   finally
   {
    b += 10;
    return b;
   }
  }
}


A. 10

B. 20

C. 30

D. 40

解:C, 这个是个try{}catch{}finally{}的知识点,本程序的流程:

进入try{} 执行
b += 10;
,接着读到
return b;
,程序会判断是否有finally{},有,因为finally{}必须要执行,所以
return b;
无效。

进入finally{} 执行
b += 10;
,接着读到
return b;
程序退出。

3.

6、以下JAVA程序的输出是什么()

public class HelloSogou{
public static synchronized void main(String[] a){
Thread t=new Thread(){
public void run(){Sogou();}
};
t.run();
System.out.print("Hello");
}
static synchronized void Sogou(){
System.out.print("Sogou");
}
}


A. HelloSogou

B. SogouHello

C. Hello

D. 结果不确定

解:B,这里有个陷阱t调用的run(),并没有启动新线程,新线程需要用start()方法

7、检查程序,是否存在问题,如果存在指出问题所在,如果不存在,说明输出结果。

package algorithms.com.guan.javajicu;
public class Example {
String str = new String("good");
char[] ch = {'a','b','c'};
public static void main(String[] args) {
Example ex = new Example();
ex.change(ex.str, ex.ch);
System.out.print(ex.str +"and");
System.out.print(ex.ch);
}

public void change(String str, char ch[]){
str= "test ok";
ch[0]= 'g';
}
}


A. test okandabc

B. test okandgbc

C. goodandabc

D. goodandgbc

8、运行结果是什么?

public void getCustomerInfo() {
try {
// do something that may cause an Exception
} catch (java.io.FileNotFoundException ex) {
System.out.print("FileNotFoundException!");
} catch (java.io.IOException ex) {
System.out.print("IOException!");
} catch (java.lang.Exception ex) {
System.out.print("Exception!");
}
}


A. IOException!

B. IOException!Exception!

C. FileNotFoundException!IOException!

D. FileNotFoundException!IOException!Exception!

解:A,

题目说抛出一个异常,但是没说具体是什么异常,那么就要分情况了:

1.如果抛出一个FileNotFoundException(或其子类),那么最终结果就打印FileNotFoundException

2.如果抛出一个IOException,或者IOException的子类(不包含FileNotFoundException及其子类),那么最终结果就打印IOException

3.如果抛出一个Exception(不包含IOException及其子类),那么最终结果就打印Exception.

以上,所以3个皆有可能.但是,不管是哪一种情况,只会输出其中之一。

拓展:

Exception>IOException>FileNotFoundException 继承结构

9、执行以下程序后的输出结果是()

public class Test {
public static void main(String[] args) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operator(a, b);
System.out.println(a + "," + b);
}
public static void operator(StringBuffer x, StringBuffer y) {
x.append(y); y = x;
}
}


A. A,A

B. A,B

C. B,B

D. AB,B

10、下面哪个不是标准Statement类?

A. Statement

B. PreparedStatement

C. CallableStatement

D. BatchedStatement

解:D,

Statement在JDBC中相当于SQL语句的载体

A,Statement是最基本的用法,采用字符串拼接的方式,存在注入漏洞

B,PreparedStatement对Statement中的SQL语句进行预编译,同时检查合法性,效率高

C,CallableStatement接口扩展 PreparedStatement,用来调用存储过程,它提供了对输出和输入/输出参数的支持。CallableStatement 接口还具有对 PreparedStatement 接口提供的输入参数的支持。

D,不是标准的Statement类
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: