该代码可能会破坏输出“ ABCAABCABC”,然后退出。
来源:https://www.nowcoder.com/questionTerminal/25deb8d21e7d442e86c90302d6e03133
在每个线程中都是顺序执行的,所以sl.printAll();必须在前三句执行之后执行,也就是输出的内容必有(连续或非连续的)ABC。
而线程之间是穿插执行的,所以一个线程执行 sl.printAll();之前可能有另一个线程执行了前三句的前几句。
E答案相当于线程1顺序执行完然后线程2顺序执行完。
G答案则是线程1执行完前三句add之后线程2插一脚执行了一句add然后线程1再执行 sl.printAll();输出ABCA。接着线程2顺序执行完输出ABCABC
输出加起来即为ABCAABCABC。
true、false、null、sizeof、goto、synchronized 哪些是Java关键字?
正确答案: E F 你的答案: C D E F (错误)
true
false
null
sizeof
goto
synchronized
来源:https://www.nowcoder.com/questionTerminal/dc5651a872ff44a187afd7034a1708ba
子类要调用继承自父类的方法,必须使用super关键字。
正确答案: B 你的答案: B (正确)
正确
错误
来源:https://www.nowcoder.com/questionTerminal/a1aafec6367449079de0ffa6ed9e64b5
1、子类构造函数调用父类构造函数用super
2、子类重写父类方法后,若想调用父类中被重写的方法,用super
3、未被重写的方法可以直接调用。
下面程序的运行结果()
Object obj=new Object();
List aList=new ArrayList();
List bList=new LinkedList();
long t1=System.currentTimeMillis();
for(int i=0;i<50000;i++){
aList.add(0,obj);
}
long t2=System.currentTimeMillis()-t1;
t1=System.currentTimeMillis();
for(int i=0;i<50000;i++){
bList.add(0,obj);
}
long t3=System.currentTimeMillis()-t1;
正确答案: D 你的答案: A (错误)
t2
t2=t3
不确定
t2>t3
来源:https://www.nowcoder.com/questionTerminal/22a15ba9381845e9b225e9d393d5c028
ArrayList内部是动态数组实现,在增加空间时会复制全部数据到新的容量大一些的数组中。
而LinkedList内部为双向链表,可以按需分配空间,扩展容量简单,因此LinkedList用时少。
下列代码执行结果为()
public static void main(String args[])throws InterruptedException{
Thread t=new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.print("2");
}
});
t.start();
t.join();
System.out.print("1");
}
正确答案: A 你的答案: A (正确)
21
12
可能为12,也可能为21
以上答案都不对
来源:https://www.nowcoder.com/questionTerminal/9b791ef55f5d479aa91172a2a763913e
因为子线程的休眠时间太长,因此主线程很有可能在子线程之前结束也就是输出结果是12,但是子线程用了join函数,因此主线程必须等待子线程执行完毕才结束因此输出结果只能是21
如何获取ServletContext设置的参数值?
正确答案: B 你的答案: A (错误)
context.getParameter()
context.getInitParameter()
context.getAttribute()
context.getRequestDispatcher()
来源:https://www.nowcoder.com/questionTerminal/5b73597e04f4443f9ae21973be6b10f2
getParameter()是获取POST/GET传递的参数值;
getInitParameter获取Tomcat的server.xml中设置Context的初始化参数
getAttribute()是获取对象容器中的数据值;
getRequestDispatcher是请求转发。
What is displayed when the following is executed;
double
d1=-
0.5
;
System.out.println(
"Ceil d1="
+Math.ceil(d1));
System.out.println(
"floor d1="
+Math.floor(d1));
正确答案: A 你的答案: B (错误)
Ceil d1=-0.0
floor d1=-1.0
Ceil d1=0.0
floor d1=-1.0
Ceil d1=-0.0
floor d1=-0.0
Ceil d1=0.0
floor d1=0.0
Ceil d1=0
floor d1=-1
来源:https://www.nowcoder.com/questionTerminal/d44fe32902d44b178685d76a9c56ec27
ceil:大于等于 x,并且与它最接近的整数。
floor:小于等于 x,且与 x 最接近的整数。
至于为什么是-0.0,而不是0.0,因为源码注释:“If the argument value is less than zero but greater than -1.0, then the result is negative zero ”
,翻译过来就是:如果参数是-1.0到0.0之间的数,结果是-0.0。
说明输出结果。
package test;
import java.util.Date;
public class SuperTest extends Date{
private static final long serialVersionUID = 1L;
private void test(){
System.out.println(super.getClass().getName());
}
public static void main(String[]args){
new SuperTest().test();
}
}
正确答案: C 你的答案: A (错误)
SuperTest
SuperTest.class
test.SuperTest
test.SuperTest.class
来源:https://www.nowcoder.com/questionTerminal/35a08d98faba40e6a8946fcb88c6c091
TestSuper和Date的getClass都没有重写,他们都是调用Object的getClass,而Object的getClass作用是返回的是运行时的类的名字。这个运行时的类就是当前类,所以
super.getClass().getName()
返回的是test.SuperTest,与Date类无关
要返回Date类的名字需要写super.getClass().getSuperclass()
完整路径名要加上包名
下列说法正确的是()?
正确答案: A B 你的答案: A B (正确)
对于局部内部类,只有在方法的局部变量被标记为final或局部变量是effctively final的,内部类才能使用它们
成员内部类位于外部类内部,可以直接调用外部类的所有方法(静态方法和非静态方法)
由于匿名内部类只能用在方法内部,所以匿名内部类的用法与局部内部类是一致的
静态内部类可以直接访问外部类的非静态成员
来源:https://www.nowcoder.com/questionTerminal/e081f2dcd1864be0ab6ddc7be153e0d6
假设如下代码中,若t1线程在t2线程启动之前已经完成启动。代码的输出是()
public static void main(String[]args)throws Exception {
final Object obj = new Object();
Thread t1 = new Thread() {
public void run() {
synchronized (obj) {
try {
obj.wait();
System.out.println("Thread 1 wake up.");
} catch (InterruptedException e) {
}
}
}
};
t1.start();
Thread.sleep(1000);//We assume thread 1 must start up within 1 sec.
Thread t2 = new Thread() {
public void run() {
synchronized (obj) {
obj.notifyAll();
System.out.println("Thread 2 sent notify.");
}
}
};
t2.start();
}
正确答案: B 你的答案: C (错误)
Thread 1 wake up
Thread 2 sent notify.
Thread 2 sent notify.
Thread 1 wake up
A、B皆有可能
程序无输出卡死
来源:https://www.nowcoder.com/questionTerminal/a804e21b00d4464faf4e9bbe3a7e2df4
notify()就是对对象锁的唤醒操作。
但有一点需要注意的是notify()调用后,并不是马上就释放对象锁的,而是在相应的synchronized(){}语句块执行结束,自动释放锁后,
JVM会在wait()对象锁的线程中随机选取一线程,赋予其对象锁,唤醒线程,继续执行。
这样就提供了在线程间同步、唤醒的操作。
Java 提供的事件处理模型是一种人机交互模型。它有三个基本要素
正确答案: A B D 你的答案: A B C D (错误)
事件源
事件对象
事件过程
事件监听器
来源:https://www.nowcoder.com/questionTerminal/4edff4d82fa341ad9d8301cacc878b48
Java 提供的事件处理模型是一种人机交互模型。它有三个基本要素:
1) 事件源(Event Source):即事件发生的场所,就是指各个组件,如按钮等,点击按钮其实就是组件上发生的一个事件;
2) 事件(Event):事件封装了组件上发生的事情,比如按钮单击、按钮松开等等;
3) 事件监听器(Event Listener):负责监听事件源上发生的特定类型的事件,当事件到来时还必须负责处理相应的事件;