- public class a {
int il=9;
static int i2=9;
final int i3=10;
}
请 指出static 和 final所代表的含义
答案:static是指该变量为类变量,在内存中只有一份,所有该类对象共享该变量;
Final是指该变量为常量,不能被修改。
- 类 a的成员变量如果没有初始化缺省是什么值?
Class bbb{}
public class a{
boolean a1;
int a2;
String a3;
bbb a4;
int[] a5;
}
答案:
Class bbb{}
public class a{
boolean a1;//false;
int a2;//0
String a3;//null
bbb a4;//null
int[] a5;//null
}
- 下面的程序有问题?该如何纠正?
public class MyClass{
public static void main(String arguments[]){
amethod(arguments);
}
public void amethod(String[] arguments){
System.out.println(arguments);
System.out.println(arguments[1]);
}
}
答案:在静态方法中不能调用非静态方法和属性。
两种改法:要么把amethod()声明为static,要么在main方法中构造MyClass对象,由MyClass对象调用amethod()方法。
- 类成员的默认访问和public,protected,private访问指示符代表的含义?
答案:public指该变量可以被所有其他对象访问;protected指该变量只能被继承类访问;private指该变量只能被本类对象访问。
- 下面代码有什么问题?
abstract class MineBase{
abstract void amethod();
static int I ;
}
public class Mine extends MineBase{
public static void main(String argv[]){
int[] ar=new int[5];
for(i=0;i<ar.length;i++)
System.out.println(ar[i]);
}
}
答案:Mine是MineBase的子类,要么实现MineBase的抽象方法amethod(),要么把自己声明为abstract。
- 布局管理器(Layout Manager)是做什么用的?举几个SDK提供的布局管理器?
答案:用来管理UI界面对象布局的。
常见的有FlowLayout、GridLayout等。
- 下面哪些是正确的?
1)The ActionListener interface has no corresponding Adapter class
2) The processing of an event listener requires a try/catch block
3) If multiple listeners are added to a component only events for the last listener added will be processed
4) If multiple listeners are added to acomponent the events will be processed for all but with no guarantee in the order
答案:3
- 下面哪些是正确的?
1) A class may extend only one other class and implement only one interface
2) interfaces are the Java approach to addressing its lack of multiple inheritance but require implementing classes to create the functionality of the Interfaces.
3) All of the variables in an interface are implicitly static and final
4) All of the methods in an interface are implicitly abstract
答案:2、3、4
- 根据前面给出的类,下面四个哪些编译没有错误
interface Iface{}
Class Cface implements Iface{}
Class Base{};
Public class ObRef extends Base{
ObRef ob=new ObRef();
Base b=new Base();
Object o1=new Object();
Iface o2=new Cface();
}
1)o1=o2;
2)b=ob;
3)ob=b;
4)o1=b;
答案:1、2、4
- 下面哪些是正确的?
1) At the root of the collection hierarchy is a class called Collection
2) The collection interface contains a method called enumerator
3) The interator method returns an instance of the Vector class
4) The set interface is designed for unique elements
答案:4
- 下面的程序合理吗?(其中Items[i]是String类型的数组)?为什么?如何改进?
public String statement(){
String =””;
for(int i=0;i<500;i++)
s+=Item[i];
return s;
}
答案:String的连接操作(+=)会产生很多对象,性能不好
public String statement(){
StringBuffer buffer = new StringBuffer();
for(int i=0;i<500;i++)
buffer.append(Item[i].toString());
return buffer.toString();
}
- 通常情况下,下面这段程序怎样改进才更合理?为什么?
try{
Statement sm = con.createStatement();
ResultSet rs=sm.execQuery(“SQL字符串”);
int i=rs.getInt(1);
//其它语句
rs.close();
sm.close();
}catch(SQLException e){
处理程序
}
答案:数据库的释放应该在finnaly块中
Statement sm = null;
ResultSet rs = null;
try{
m = con.createStatement();
rs=sm.execQuery(“SQL字符串”);
int i=rs.getInt(1);
//其它语句
}catch(SQLException e){
处理程序
}finally{
try{
if(rs != null && !rs.isClosed()){
rs.close();
}
}catch(Exception ex){
rs = null;
}
try{
if(sm != null){
sm.close();
}
}catch(Exception ex){
sm = null;
}
}
- 假如一个JAVA程序 bbb.java 用到了aaa.jar中的某个类,所需的其他类都正确定义在类路径中。aaa.jar 放在目录 c:aaa中,假如在环境变量CLASSPATH加入目录 c:aaa(继比如Classpath=其他目录或者文件c:aaa)bbb.java成功编译为bbb.class
那么java bbb.class 会运行成功吗?如果不能运行话,什么原因?
答案:能够正确运行。
- 如果编译和运行下面的代码,哪一个会发生
class Base{
private void amethod (int iBase){
System.out.println(“Base.amethod”);
}
}
class Over extends Base{
public static void main(String vrgv[]){
Over o=new Over();
int iBase=0;
o.amethod(iBase);
}
public void amethod(int iOver){
System.out.println(“Over.amethod”);
}
}
1)Compile time error complaining that Base amethod is private
2)Runtime error complaining that Base amethod is private
3)Output of “Base.amethod”;
4)Output of “Over.amethod”;
答案:4
- JAVA 是具有对象回收(垃圾回收)功能 的,简要说明为什么还有“内存泄漏”现象?
答案:垃圾回收是指对于那些没有被引用的对象,JVM会自动回收。
但是,如果很多对象依然被其他对象引用,那么JVM就不会回收,这就造成“内存泄漏”现象
- Hibernate的三种状态
Hibernate中的对象有三种状态: 瞬时状态(Transient),持久状态(Persistent),脱管状态(Detached)
l 瞬时状态(Transient):由new命令开辟内存空间的Java对象,也就是平时所熟悉的普通Java对象。如:Student stu = new Student();。瞬时对象特点:(1)不和Session实例关联;(2)在数据库中没有和瞬时对象关联的记录。
l 持久状态(Persistent):持久的实例在数据库中有对应的记录,并拥有一个持久化标识(identifier)。持久对象总是与Session和Transaction相关联,在一个Session中,对持久对象的改变不会马上对数据库进行变更,而必须在Transaction终止,也就是执行commit()之后,才在数据库中真正运行SQL进行变更,持久对象的状态才会与数据库进行同步。在同步之前的持久对象称为脏(dirty)对象。持久化对象的特点:(1) 和Session实例关联(2) 在数据库中有和持久对象关联的记录。
l 脱管状态(Detached):与持久对象关联的Session被关闭后,对象就变为脱管对象。对脱管对象的引用依然有效,对象可继续被修改。脱管对象特点:(1) 本质上和瞬时对象相同;(2) 只是比爱瞬时对象多了一个数据库记录标识值id.。
持久对象转为脱管对象:
l 当执行close()或clear(),evict()之后,持久对象会变为脱管对象。
瞬时对象转为持久对象:
l 通过Session的save()和saveOrUpdate()方法把一个瞬时对象与数据库相关联,这个瞬时对象就成为持久化对象。
l 使用fine(),get(),load()和iterater()待方法查询到的数据对象,将成为持久化对象。
脱管对象转为持久对象:
通过Session的update(),saveOrUpdate()和lock()等方法,把脱管对象变为持久对象。
- static和final的区别
答案:static所修饰的对象全局对象,static所修饰的属性是类属性。static对象和属性在内存中只存在一份。
final修饰的对象是不可更改的对象,final修饰的属性是常量,也不可更改。
- 下面代码有错误吗?为什么?
public class Test{
public static void main(string []args){
amethod();
}
public void amethod(){
System.out.prinln(“this amethod “);
}
}
答案:在静态方法中不能调用非静态方法和属性。
两种改法:要么把amethod()声明为static,要么在main方法中构造Test对象,由Test对象调用amethod()方法。
- 下面这段代码合理吗?如不合理应怎样改正?
public String setout(){
String s=””;
for(int i=0;i<500;i++){
s+=i;
}
return i;
}
答案:字符串连接操作(+=)会产生很多中间对象,性能不好。
public String setout(){
StringBuffer buffer=new StringBuffer();
for(int i=0;i<500;i++){
buffer.append(i);
}
return buffer.toString();
}
- 递归:0,1,1,2,3,5,8,13,21……第30位是什么数值,要求用递归方法。
答案:
public class Fibonacci{
int fib(int n){
if (n == 0) return 0;
else if (n == 1) return 1;
else return fib(n-1) + fib(n-2);
}
public static void main(String[] args){
int[] fi=new int[30];
Fibonacci f = new Fibonacci();
for(int i=0;i<30;i++){
fi[i]=f.fib(i+1);
System.out.print(fi[i]+“t“);
}
}
}
- 除了用session,application,cookie在页面之间传值还有哪些方法?
答案:request.setAttribute();以及URL方式
- 什么是SQL注入?举例说明
答案:SQL资料隐码攻击(SQL injection)又称为隐码攻击、SQL注入等,是发生于应用程式之资料库层的安全漏洞。简而言之,是在输入的资料字串之中夹带SQL指令,在设计不良的程式当中忽略了检查,那麽这些夹带进去的指令就会被资料库伺服器误认为是正常的SQL指令而执行,因此招致到破坏。
例子
某个网站的登入验证的SQL查询代码为
strSQL = “SELECT * FROM users WHERE (name = ‘” + userName + “‘) and (pw = ‘”+ passWord +”‘);”
恶意填入userName = “‘ OR ‘1’=’1”;与passWord = “‘ OR ‘1’=’1”;时,将导致原本的SQL字串被填为
strSQL = “SELECT * FROM users WHERE (name = ” OR ‘1’=’1′) and (pw = ” OR ‘1’=’1′);”
也就是实际上运行的SQL命令会变成下面这样的
strSQL = “SELECT * FROM users;”
因此达到无帐号密码,亦可登入网站。所以SQL隐码攻击被俗称为骇客的填空游戏。
- 写一个getWeek(aDate)方法,根据时间返回星期一星期二。。。。
答案:public String getWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
switch (dayOfWeek) {
case 1:
return “星期日”;
case 2:
return “星期一”;
case 3:
return “星期二”;
case 4:
return “星期三”;
case 5:
return “星期四”;
case 6:
return “星期五”;
case 7:
return “星期六”;
}
return “”;
}
- Hibernate的缓存是在何时清除的?
答案: Hibernate提供了两级缓存,第一级是Session的缓存。由于Session对象的生命周期通常对应一个数据库事务或者一个应用事务,因此它的缓存是事务范围的缓存。第一级缓存是必需的,不允许而且事实上也无法比卸除。在第一级缓存中,持久化类的每个实例都具有唯一的OID。
第二级缓存是一个可插拔的的缓存插件,它是由SessionFactory负责管理。由于SessionFactory对象的生命周期和应用程序的整个过程对应,因此第二级缓存是进程范围或者集群范围的缓存。这个缓存中存放的对象的松散数据。第二级对象有可能出现并发问题,因此需要采用适当的并发访问策略,该策略为被缓存的数据提供了事务隔离级别。缓存适配器用于把具体的缓存实现软件与Hibernate集成。第二级缓存是可选的,可以在每个类或每个集合的粒度上配置第二级缓存。
什么样的数据适合存放到第二级缓存中?
1、很少被修改的数据.
2、不是很重要的数据,允许出现偶尔并发的数据.
3、不会被并发访问的数据.
不适合存放到第二级缓存的数据?
1、经常被修改的数据.
2、财务数据,绝对不允许出现并发.
3、与其他应用共享的数据.
无论何时,当你给save()、update()或 saveOrUpdate()方法传递一个对象时,或使用load()、 get()、list()、iterate() 或scroll()方法获得一个对象时, 该对象都将被加入到Session的内部缓存中。
当随后flush()方法被调用时,对象的状态会和数据库取得同步。如果你不希望此同步操作发生,或者你正处理大量对象、需要对有效管理内存时,你可以调用evict() 方法,从一级缓存中去掉这些对象及其集合。
//a huge result set
ScrollableResult cats = sess.createQuery(“from Cat as cat”).scroll();
while ( cats.next() ) {
Cat cat = (Cat) cats.get(0);
doSomethingWithACat(cat);
sess.evict(cat);
}
Session还提供了一个contains()方法,用来判断某个实例是否处于当前session的缓存中。如若要把所有的对象从session缓存中彻底清除,则需要调用Session.clear()。
对于二级缓存来说,在SessionFactory中定义了许多方法, 清除缓存中实例、整个类、集合实例或者整个集合。
sessionFactory.evict(Cat.class, catId); //evict a particular Cat
sessionFactory.evict(Cat.class); //evict all Cats
sessionFactory.evictCollection(“Cat.kittens”, catId); //evict a particular collection of kittens
sessionFactory.evictCollection(“Cat.kittens”); //evict all kitten collections
CacheMode参数用于控制具体的Session如何与二级缓存进行交互。
CacheMode.NORMAL – 从二级缓存中读、写数据。
CacheMode.GET – 从二级缓存中读取数据,仅在数据更新时对二级缓存写数据。
CacheMode.PUT – 仅向二级缓存写数据,但不从二级缓存中读数据。
CacheMode.REFRESH – 仅向二级缓存写数据,但不从二级缓存中读数据。通过 hibernate.cache.use_minimal_puts的设置,强制二级缓存从数据库中读取数据,刷新缓存内容。
- Hibernate中的inverse设为false是什么意思?
答案:代表由自己来维护数据关系
- 简述Struts工作流程
答案:首先actionservlet是strtus最核心的组件,当web容器启动时,actionservlet就被初始化,并加载struts配置文件,从而得知哪个action与哪个actionform是关联的,并且知道哪个请求路径与哪个action关联。当一个.do请求到来是,首先会被actionservlet拦截,并根据配置信息分析该请求路径对应的actionbean和formbean,如前面加法器请求页面提交时,是请求http://localhost:8080/zf2/add.do,因此actionservelt根据struts配置信息查找path属性为/add的actionbean,于是找到addaction,并得知该action的name属性为addform,于是找到addform的实例(如果不存在,则创建)之后,将请求中包含的值填充到formbean中,然后在action中实例化模型层的对象对请求数据进行处理,并将返回的结果,发送到视图层。(使用actionforward)
- 写一个存储过程,一个表test有三列ID,name,password,当输入的用户名和密码和数据库相同时返回1,否则返回0
答案:
- public class StaticTest{
static int x=10;
static {
x+=5;
}
public static void main(String args[]){
System.out.println(“x=”+x);
}
static {
x/=3;
}
}
输出等于5 为什么?
答案:static int x=10; ——x=10;
static {x+=5;} ——x=15;
static {x/=3;} ——x=5;
- int [] arr=new int[25]
A.arr[24]==0 .B.arr[0]==null .C.arr[25]==0. D.arr[24]==null;
答案:A。int数组初始化时,所有元素全部初始化为0 。
- JSP和servlet的生命周期和二者的关系。
答案:生命周期分为4个阶段:1.加载。2.初始化。3.调用。4.销毁。
JSP最终会被编译为Servlet执行。一般情况下,JSP用以展示数据,作为MVC中View层出现,Servlet用以控制业务流程,作为MVC中的Control层出现。