543
京东网上商城
华为JAVA面试题及详解
今晚强哥说我要多写代码,基础很重要,为了长远起见,我就开始写一些面试题的代码。
第一题
package com.zzk.test; public class Test { public static void main(String[] args) { String str="1234"; changeStr(str); System.out.println(str); } public static void changeStr(String str) { str="change yes or not?"; } }
1234
不会改变
分析:
这个主要涉及到String类型变量的不变性,private final char value[];String类中保存值得成员是这样定义的,所以在类存堆中new出一个String对象后该对象不能再改变。
主函数中的str与changestr(String str)中的str是两个不同的引用,当调用函数时,把主函数中str引用指向的对象赋值给changestr(String str)中的str,即是changestr(String str)中str与主函数中的str指向同一个String对象,当函数中给str赋值是,由于String类型变量的不变性所以不是去改变"1234" (String str = "1234";)这个对象而是去创建另一个新的对象“welcome”(str = "welcome";),而changestr(String
str)中的str这个引用指向它,而不是主函数中的str指向它,所以会出现这样的情况。
对于String类型变量的赋值,遵循下面的规则,java中有String pool(我估计是由于String对象的不变性,所以java中有String pool)。
java中有两种赋值方式,一种采用字面值方式的赋值,String s = "aaa"; 查找String pool中是否存在“aaa”这个对象,如果不存在,则在String pool 中创建一个"aaa"对象,然后将String pool中这个对象的地址返回给s这个引用,这样s会指向String pool 中"aaa"这个对象。如果String pool中已经有这个“aaa”对象时java虚拟机不会创建新的对象,而是将这个对象的地址直接返回。
另一种方式是String s = new String("aaa"); 首先在String pool 中查找有没有这个对象,如果已经有了,则不再String pool中创建了,而是在堆中创建该对象,然后返回堆中该对象的地址。如果没有,则在String pool中创建该对象,并且在堆中也创建该对象,最后返回堆中该对象的地址给s。
改变可以这样改
package com.zzk.test; public class Test { public static void main(String[] args) { String str="1234"; str=changeStr(str); System.out.println(str); } public static String changeStr(String str) { str="change?"; return str; } }
输出:
change?
第二题
package com.zzk.test; public class Test { static boolean foo(char c) { System.out.print(c); return true; } public static void main(String[] args) { int i=0; for(foo('A');foo('B')&&(i<2);foo('C')) { i++; foo('D'); } } }
输出:
猜测:ABDCBDCB
对了
分析:
第三题
1. class A { 2. protected int method1(int a, int b) { return 0; } 3. } Which two are valid in a class that extends class A? (Choose two) A. public int method1(int a, int b) { return 0; } B. private int method1(int a, int b) { return 0; } C. private int method1(int a, long b) { return 0; } D. public short method1(int a, int b) { return 0; } E. static protected int method1(int a, int b) { return 0; }
答案A C
当一个子类重写父类的方法时,重写的方法的访问权限
必须大于等于父类的访问权限.
在此题中父类中的方法访问权限为protected,子类只能是
protected或public.这时A是符合题意的.
由于选项C的形参和父类的不一样,没有重写的效果,所以
在子类出现也是没问题的.
第四题
1. public class Outer{ 2. public void someOuterMethod() { 3. // Line 3 4. } 5. public class Inner{} 6. public static void main( String[]argv ) { 7. Outer o = new Outer(); 8. // Line 8 9. } 10. } Which instantiates an instance of Inner? A. new Inner(); // At line 3 B. new Inner(); // At line 8 C. new o.Inner(); // At line 8 D. new Outer.Inner(); // At line 8//new Outer().new Inner()
选A
内部类的实例化可以在普通方法里,也可以在static方法里实例化.
package com.test.a;public class Outer
{
Inner i = new Outer.Inner();
public void method()
{
new Inner();
}
public class Inner
{
}
public static void main(String[] args)
{
Outer o = new Outer();
Inner i = o.new Inner();
}
static void a()
{
Outer o = new Outer();
Inner i = o.new Inner();
}
}
第五题
Which method is used by a servlet to place its session ID in a URL that is written to the servlet’s response output stream?(译:那个方法是servlet用于将其session ID入在一个URL中,该URL写入servlet的响应输出流)
A. The encodeURL method of the HttpServletRequest interface.
B. The encodeURL method of the HttpServletResponse interface.
C. The rewriteURL method of the HttpServletRequest interface.
D. The rewriteURL method of the HttpServletResponse interface.
答案:B
请看J2EE API关于此方法的说明:
Encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. Because the rules for making this determination can differ from those used to decide whether to encode a normal link, this method is separated from the encodeURL method. All URLs sent to the HttpServletResponse.sendRedirect method should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies.
第六题
Which two are equivalent? (Choose two)
A. <%= YoshiBean.size%>
B. <%= YoshiBean.getSize()%>
C. <%= YoshiBean.getProperty("size")%>
D. <jsp:getProperty id="YoshiBean" param="size"/>
E. <jsp:getProperty name="YoshiBean" param="size"/>
F. <jsp:getProperty id="YoshiBean" property="size"/>
G. <jsp:getProperty name="YoshiBean" property="size"/>
此题考查的是JavaBean在jsp中的取值方式.
其中C和G效果是一样的.
概念题
1. 描述Struts体系结构?对应各个部分的开发工作主要包括哪些?

2.
XML包括哪些解释技术,区别是什么?
包括:DOM(Document Object Modal)文档对象模型,SAX(Simple API for XML)。DOM是一次性将整个文档读入内存操作,如果是文档比较小,读入内存,可以极大提高操作的速度,但如果文档比较大,那么这个就吃力了。所以此时SAX应用而生,它不是一次性的将整个文档读入内存,这对于处理大型文档就比较就力了
3. JSP有哪些内置对象和动作?它们的作用分别是什么?
表结构: 1、 表名:g_cardapply 字段(字段名/类型/长度): g_applyno varchar 8;//申请单号(关键字) g_applydate bigint 8;//申请日期 g_state varchar 2;//申请状态 2、 表名:g_cardapplydetail 字段(字段名/类型/长度): g_applyno varchar 8;//申请单号(关键字) g_name varchar 30;//申请人姓名 g_idcard varchar 18;//申请人身份证号 g_state varchar 2;//申请状态 其中,两个表的关联字段为申请单号。 题目: 1、 查询身份证号码为440401430103082的申请日期 Select g_cardapply.g_ applydate from g_cardapply, g_cardapplydetail where g_cardapplydetail.g_idcard=’’ and g_cardapply.g_applyno=g_cardapplydetail.g_applyno 2、 查询同一个身份证号码有两条以上记录的身份证号码及记录个数 3、 将身份证号码为440401430103082的记录在两个表中的申请状态均改为07 Update g_cardapply. g_state=’07’, g_cardapplydetail .g_state 4、 删除g_cardapplydetail表中所有姓李的记录
create database mianshi use mianshi; create table g_cardapply( g_applyno varchar(8), g_applydate bigint, g_state varchar(20) ) go create table g_cardapplydetail( g_applyno varchar(8), g_name varchar(30), g_idcard varchar(18), g_state varchar(20) ) 1、select a1.g_applydate from g_cardapply as a1 inner join g_cardapplydetail a2 on a1.g_applyno=a2.g_applyno where a2.g_idcard="123" ; 2、select g_idcard,count(g_idcard) from g_cardapplydetail group by g_idcard having count(g_idcard)>=2; 3、update g_cardapply set g_state=603 from g_cardapply as g_d inner join g_cardapplydetail as g_c on g_d.g_applyno=g_c.g_applyno and g_idcard='123';更新第一个表的g_state update g_cardapplydetail set g_state=603 where g_idcard='123';
最后更新:2017-04-02 06:52:09