牛客网-Java专项练习01

牛客网-Java专项练习记录01

答案设置为白色,框选即可显示

1.通过HttpServletRequest.getParameter获取的参数
A.总是采用UTF-8编码
B.总是采用lS08859-1编码
C.由客户端浏览器和Web容器配置共同决定编码
D.由服务器所在的操作系统决定编码

答案C
解析:浏览器是根据jsp页面开头声明的编码方式对request参数进行编码。在Tomcat中默认是lS08859-1编码,使用request.setCharacterEncoding(“UTF-8”)指定编码为UTF-8,或者修改tomcat的配置文件server.xml中的编码。


2.将下列(A、B、C、D)哪个代码替换下列程序中的【代码】不会导致编译错误?

interface Com{
int M=200;
int f();
}
class ImpCom implements Com{
【代码】
}

A.public int f(){return 100+M;}
B.int f(){return 100;}
C.public double f(){return 2.6;}
D.public abstract int f();

答案A
解析:实现类必须实现接口所有方法。在接口中方法默认为public,在java中一个类如果没有权限的修饰符,默认是friendly(同一个包内的其它类才可访问),而子类的访问权限不可比父类小。所以,在实现类中一定要写public。

3.What will happen when you attempt to compile and run the following code?

public class Test{
static{
   int x=5;
}
static int x,y;
public static void main(String args[]){
   x--;
   myMethod( );
   System.out.println(x+y+ ++x);
}
public static void myMethod( ){
  y=x++ + ++x;
 }
}

A.compiletime error
B.prints:1
C.prints:2
D.prints:3
E.prints:7
F.prints:8

答案D
解析
1.静态代码块中的x是局部变量,执行完后被销毁。
2.而后又声明了静态成员变量x,y默认为0。
3.执行x–操作,x=-1。
4.调用MyMethod()方法,y=x++ + ++x可以看成是y=(x++)+(++x)=-1+1=0,此时x=1
5.最后x+y + ++x=1+0+1+2=3

4.下列选项中是正确的方法声明的是?()
A.protected abstract void f1();
B.public final void f1() {}
C.static final void fq(){}
D.private void f1() {}

答案ABCD
解析
A.抽象方法只可被public 和 protected修饰
B.final可以修饰类、方法、变量,分别表示:该类不可继承、该方法不能重写、该变量是常量
C.static final 表示该方法是静态的不可重写的方法
D.private 修饰方法表示私有方法,只允许本类可以访问


5.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 run with output “A B A B C C “, then exit.
E.The code may run with output “A B C A B C A B C “, then exit.
F.The code may run with output “A A A B C A B C C “, then exit.
G.The code may run with output “A B C A A B C A B C “, then exit.

答案EG
解析
假设for循环中新建的2个线程是A和B,若B线程在A线程完全结束后再进行操作。则A线程输出A B C,此时B线程开始执行,又输出A B C A B C。这说明选项E的情况可能出现。
而若线程B在线程A插入A B C三个元素后抢占了时间片,并插入了B线程的A元素。此时A线程夺回时间片再输出时,则输出A B C A,B线程再输出A B C A B C。这种情况下,选项G也可能出现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值