Day16
Q1
下面有关 JAVA 异常类的描述,说法正确的有()
- 异常的继承结构:基类为 Throwable,Error 和 Exception 。实现 Throwable, RuntimeException 和 IOException 等继承 Exception
- 非 RuntimeException 一般是外部错误(不考虑Error的情况下),其可以在当前类被 try{}catch 语句块所捕获
- Error 类体系描述了 Java 运行系统中的内部错误以及资源耗尽的情形,Error 不需要捕捉
- RuntimeException 体系包括错误的类型转换、数组越界访问和试图访问空指针等等,必须 被 try{}catch 语句块所捕获
题解
- Exception(异常) :是程序本身可以处理的异常
- Error(错误): 是程序无法处理的错误
- 检查异常(编译器要求必须处置的异常) : 除Error,RuntimeException及其子类以外,其他的Exception及其子类都属于可查异常**[要么用try-catch语句捕获它,要么用throws子句声明抛出它]**
- 非检查异常(编译器不要求处置的异常): 包括运行时异常(RuntimeException与其子类)和错误(Error)
Q2
以下哪项不属于java类加载过程?
- 生成java.lang.Class对象
- int类型对象成员变量赋予默认值
- 执行static块代码
- 类方法解析
题解
类的加载包括:加载,验证,准备,解析,初始化
类加载的时候会赋予初值的是类变量,而非对象成员
Q3
表达式(short)10/10.2*2运算后结果是什么类型?
- short
- int
- double
- float
题解
java 黙认浮点类型为double
float数据类型有一个后缀为" f “或” F "
long类型有一个后缀,为" l " 或者" L "
Q4
以下代码段执行后的输出结果为
public class Test {
public static void main(String args[]) {
int x = -5;
int y = -12;
System.out.println(y % x);
}
}
- -2
题解
使用%取余/模时,结果与被除数同符号
-7 % 3 = -7 % -3 = -1
7 % 3 = 7 % -3 = 1
使用rem(y, x)取余时,结果与被除数同符号rem(7, 3) = rem(7, -3) = 1
rem(-7, 3) = rem(-7, -3) = -1
使用mod(y, x)取模时,结果与除数同符号mod(7, 3) = mod(-7, -3) = -1
mod(7, 3) = mod(-7, 3) = 1
Day17
Q1
以下各类中哪几个是线程安全的?( )
- ArrayList
- Vector
- Hashtable
- Stack
题解
- 线程同步:喂,SHE——Vector、Stack、Hashtable、enumerate
- vector:就比arraylist多了个同步化机制(线程安全)
- statck:堆栈类,先进后出
- hashtable:就比hashmap多了个线程安全
- enumeration:枚举,相当于迭代器
Q2
下列代码输出结果为( )
class Animal{
public void move(){
System.out.println("动物可以移动");
}
}
class Dog extends Animal{
public void move(){
System.out.println("狗可以跑和走");
}
public void bark(){
System.out.println("狗可以吠叫");
}
}
public class TestDog{
public static void main(String args[]){
Animal a = new Animal();
Animal b = new Dog();
a.move();
b.move();
b.bark();
}
}
- 编译错误
题解
编译错误:The method bark() is undefined for the type Animal。Animal中没有定义bark()方法。
编译看左边,运行看右边。也就是是编译的时候会把它当成左边的类型,运行的时候看右边类型的方法体。
Q3
java Thread中,run方法和start方法的区别,下面说法错误的是?
- 通过调用Thread类的start()方法来启动一个线程,这时此线程是处于就绪状态,并没有运行。
- 他们都可以实现了多线程运行。
- run方法是thread的一个普通方法调用。
- 调用start方法后,一旦得到cpu时间片,就开始执行run()方法。
题解
start方法
- 用 start方法来启动线程,是真正实现了多线程
- 调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态
- 一旦得到cpu时间片,就开始执行run()方法
- 此时无需等待run()方法执行完毕,即可继续执行下面的代码
run方法
- run()方法只是类的一个普通方法
- 直接调用Run方法,程序中依然只有主线程这一个线程
- 程序执行路径只有一条,要顺序执行,要等待run方法体执行完毕后才可继续执行下面的代码
Q4
有这样一段程序:
public class Test{
public static void main(String [] args){
List list=new ArrayList();
list.add("a");
list.add("b");
list.add("a");
Set set=new HashSet();
set.add("a");
set.add("b");
set.add("a");
System.out.println(list.size()+","+set.size());
}
}
请问运行主要的程序会打印出的是什么()
- 2,2
- 2,3
- 3,2
- 3,3
题解
list有序可重复,set无序不可重复
Q5
以下代码,运行结果是什么?
public class Test {
private static int j = 0;
private static Boolean methodB(int k) {
j += k;
return true;
}
public static void methodA(int i) {
boolean b;
b = i < 10 | methodB(4);
b = i < 10 || methodB(8);
}
public static void main(String args[]) {
methodA(0);
System.out.println(j);
}
}
- The program prints”0”
- The program prints”4”
- The program prints”8”
- The program prints”12”
- The code does not complete.
题解
condition 1 | condition 2、condition 1 || condition 2
- "|"是按位或:先判断条件1,不管条件1是否为true,都会执行条件2
- "||"是逻辑或:先判断条件1,条件1是否为true,决定是否执行条件2
Q6
Test.main()函数执行后的输出是( )
class Test {
public static void main(String[] args) {
System.out.println(new B().getValue());
}
static class A {
protected int value;
public A (int v) {
setValue(v);
}
public void setValue(int value) {
this.value= value;
}
public int getValue() {
try {