1.
public class Test {
public static void changeStr(String str){
str="welcome";
}
public static void main(String[] args) {
String str="1234";
changeStr(str);
System.out.println(str);
}
}
此题结果为:1234;
比较简单分析下内存就行了.
2.
public class ForTest
{
static boolean foo(char c)
{
System.out.println(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
这道题考查的for循环的执行顺序.
for(int i = 0; i < 10; i ++)
{}
首先先执行int i = 0;注意这个只是初始化一次,
就是在第一次的时候.接着执行i < 10;
然后执行方法体{}里面的内容,最后才执行i++;
第二次及以后就直接执行i <10;然后方法体;最后
i ++;如此顺序直到结束为止.
3.
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; }
此题考查的是继承重写问题.
当一个子类重写父类的方法时,重写的方法的访问权限
必须大于等于父类的访问权限.
在此题中父类中的方法访问权限为protected,子类只能是
protected或public.这时A是符合题意的.
由于选项C的形参和父类的不一样,没有重写的效果,所以
在子类出现也是没问题的.
所以此题选:AC
4.
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();
}
}
5.
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.