java初学者每日一练,Java每日一练(2017/7/7)

单选题)有以下程序片段,下列哪个选项不能插入到行1。()。

1.

2.public  class  A{

3.//do sth

4. }

Apublic classMainClass{ }

Bpackage mine;

CclassANotherClass{ }

Dimportjava.util.*;

2

(单选题)设有定义int a=3,b=4,c=5;则以下的表达式中,值为false的是()

Aa < b&& b < c

Ba <= b

Ca < ( b + c )

D! ( a < b )3

(单选题)Given the following code:

publicclassTest {

privatestaticintj =0;

privatestaticBoolean methodB(intk) {

j += k;

returntrue;

}

publicstaticvoidmethodA(inti) {

booleanb;

b = i <10| methodB(4);

b = i <10|| methodB(8);

}

publicstaticvoidmain(String args[]){

methodA(0);

System.out.println(j);

}

}

What isthe result?

AThe programprints”0”

BThe programprints”4”

CThe programprints”8”

DThe programprints”12”

EThe code doesnot complete.4

(单选题)下列命令中,用来运行Java程序的是()

Ajava

Bjavadoc

Cjar

Djavac5

(不定项选择题)Considerthe following code:

String s=null;

Which code fragments cause an object of type NullPointerException to be thrown?

Aif((s!=null)&(s.length()>0))

Bif((s!=null)&&(s.length()>0))

Cif((s==null)|(s.length()==0))

Dif((s==null)||(s.length()==0))6

(不定项选择题)实现或继承了Collection接口的是()

AMap

BList

CVector

DIterator

ESet

44ab2adf5136657a277e82d63f8a30d6.gif

1

(单选题)有一个源代码,只包含importjava.util.*;这一个import语句,下面叙述正确的是?()

A只能写在源代码的第一句

B可以访问java/util目录下及其子目录下的所有类

C能访问java/util目录下的所有类,不能访问java/util子目录下的所有类

D编译错误

正确答案是:C

解析

导入java.util.*不能读取其子目录的类,因为如果java.util里面有个a类,java.util.regex里面也有个a类,我们若是要调用a类的方法或属性时,应该使用哪个a类呢。所以也应该选C2

(单选题)多个线程可同时操作一个数据,为了保证该数据的准确性,可将操作该数据的部分改为()

A同步

B异步

C只读

D只写

正确答案是:A

解析:

多个线程可同时操作一个数据,为了保证该数据的准确性,可将操作该数据的部分改为同步。

同步方法和同步代码块的区别是什么?

同步方法 ( 粗粒度锁 ):

1. 修饰一般方法 : public synchronized void method (){...}, 获取的是当前调用                对象 this 上的锁

2. 修饰静态方法 : public static synchronized void method (){...}, 获取当前类的              字节码对象上的锁

同步代码块 ( 细粒度锁 ):

synchronized ( obj ) {...}, 同步代码块可以指定获取哪个对象上的锁 , obj 任意

3

(单选题)A派生出子类B,B派生出子类C,并且在java源代码中有如下声明:

A a0=new A();

A a1=newB();

A a2=newC();

以下哪个说法是正确的?

A第1行,第2行和第3行的声明都是正确的

B第1,2,3行都能通过编译,但第2,3行运行时出错

C第1,2行能通过编译,但第3行编译出错

D只有第1行能通过编译

正确答案是:A

解析:

多态的三种表现形式:继承重写,重载,向上转型。A a=new B()属于向上转型,该语句创建了一个B的对象,并将A的对象变量(引用)指向B的对象,即父类引用指向子类对象,是可以的

4

(单选题)选项中哪一行代码可以替换//add code here而不产生编译错误

public abstract class MyClass {

public intconstInt = 5;

//add codehere

public voidmethod() {

}

}

Apublic abstractvoid method(int a);

BconsInt=constInt+5;

Cpublic intmethod();

Dpublic abstractvoid anotherMethod(){}

正确答案是:A

解析:

B.类中不能直接进行运算,可以写在代码块和静态代码块中

C.和method()重复了

D.abstract不能有{}5

(单选题)在基本JAVA类型中,如果不明确指定,整数型的默认是什么类型?带小数的默认是什么类型?

Aint float

Bint double

Clong float

Dlong double

正确答案是:B

解析:

Java中,如果对整数不指定类型,默认时int类型,对小数不指定类型,默认是double类型

如果要指定长整型,最好写为long a = 100000000L,如果要指定为单精度最好写为float a= 12.34F6

(单选题)Whichlines of the following will produce an error?

1. byte a1 = 2,a2 = 4, a3;

2. short s = 16;

3. a2 = s;

4. a3 = a1 * a2;

ALine 3 and Line4

BLine 1 only

CLine 3 only

DLine 4 only

正确答案是:A

解析:

1、 Java中整数默认 类型为int型,浮点默认为double型;

2、对于byte型运算时,会自动转化为int 型;

3、对于byte\short\char同级别运算时,需要强制转换;

4、高级型向低级需要强制类型转换;

高级向低级必须强制类型转换byte

short,byte,char是同级别的,所有逻辑操作都会自动转换成int型再进行相关运算

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值