常见的java编程题_Java常见的程序题

1.

Test.java程序如下:,

1. public class Test

{

2.

public static void main(String [] abc) {

3. unsigned  int  x = 10;

//在Java中,不存在unsigned无符号数据类型

4.

for (int y=0; y<5; y++, x--)

5.

System.out.print(" " + x);

6. }

7. }

指出哪一行有错误? (Choose

one.)

A. 第2行

B.

第3行

C. 第4行

D.没有错误

相关:

在Java中,不存在Unsigned无符号数据类型,但可以轻而易举的完成Unsigned转换。

方案一:如果在Java中进行流(Stream)数据处理,可以用DataInputStream类对Stream中的数据以Unsigned读取。

Java在这方面提供了支持,可以用java.io.DataInputStream类对象来完成对流内数据的Unsigned读取,该类提供了如下方法:

(1)int readUnsignedByte() //从流中读取一个0~255(0xFF)的单字节数据,并以int数据类型的数据返回。返回的数据相当于C/C++语言中所谓的“BYTE”。

(2)int readUnsignedShort() //从流中读取一个0~65535(0xFFFF)的双字节数据,并以int数据类型的数据返回。返回的数据相当于C/C++语言中所谓的“WORD”,并且是以“低地址低字节”的方式返回的,所以程序员不需要额外的转换。

方案二:利用Java位运算符,完成Unsigned转换。

正常情况下,Java提供的数据类型是有符号signed类型的,可以通过位运算的方式得到它们相对应的无符号值,参见几个方法中的代码:

public int getUnsignedByte (byte

data){ //将data字节型数据转换为0~255 (0xFF 即BYTE)。

return data&0x0FF;

}

public int getUnsignedByte (short

data){ //将data字节型数据转换为0~65535 (0xFFFF 即 WORD)。

return data&0x0FFFF;

}

public long getUnsignedIntt (int

data){ //将int数据转换为0~4294967295 (0xFFFFFFFF即DWORD)。

return data&0x0FFFFFFFFl;

}

灵活的运用这些技法,根本不存“二进制在Java中得不到全面支持”的论断!

2.

TestCount.java程序如下:

1. interface Count {

2.

short counter = 0;

3.

void countUp();

4. }

5. public class TestCount

implements Count {

6.

7.

public static void main(String [] abc) {

8.

TestCount t = new TestCount();

9.

t.countUp();

10.

}

11.

public void countUp() {

12.

for (int x =

6; x>counter; x--,

++counter) { //在Count接口,counter是short型的

13.

System.out.print(" " + counter);

14.

}

15.

}

16. }

此程序的错误在于? (Choose

one.)

A. 第2行

B. 第7行

C. 第8行

D. 第12行

3.

Test3.java程序如下:

1. public class While

{ //while是关键字,关键字不能声明为类名或者方法名,变量名,

这是While是可以的,因为它是大写的

2.

public void loop() {

3.

int x= 0;

4.

while ( 1 ) {

5.

System.out.print("x plus one is " + (x + 1));

6.

}

7.

}

8. }

此程序编译时将会提示哪些行有语法错误?(Choose one.)

A. 第1行.

B. 第1、4行.

C. 第1、4、5行

D. 第4行.

4.

ChildClass.java程序如下:

1. class ParentClass

{

2.

public int doStuff(int x) {

3.

return x * 2;

4.

}

5. }

6.

7. public class ChildClass

extends ParentClass { //子类继承父类时,子类要重写父类的方法

8.

public static void main(String [] args ) {

9.

ChildClass cc = new ChildClass();

10.

long x = cc.doStuff(7);

11.

System.out.println("x = " + x);

12.

}

13.

14.

public

long doStuff(int x) {

//

ParentClass是int型的而这里是long型的

15.

return x * 3;

16.

}

17. }

进行编译时,哪一行将出错?(Choose

one.)

A. 第3行

B. 第10行

C.

第14行.

D. 第15行.

5.

TestObj.java程序如下:

1.public class TestObj

{

2.

public static void main (String [] args) {

3.

Object o = new Object() {

4.

public boolean equals(Object obj) {

5.

return true;

6.

}

7.

}

//相当于

Object o = new

Object(){};,少一个;

8.

System.out.println(o.equals("Fred"));

9.

}

10.}

如想正确编译,需要如何更正?(Choose

one.)

A. 无需要更正。编译时,没有错误

B. 删除第4、5、6行

C. 删除第8行

D.

添加“;”在第7行末尾

6.

TestPoly.java程序如下:

1. public class TestPoly

{

2.

public static void main(String [] args ){

3.

Parent p = new Child();

4.

}

5. }

6.

7. class Parent {

8.

public Parent() {

9.

super();

10.

System.out.println("instantiate a parent");

11.

}

12. }

13.

14. class Child extends Parent

{

15.

public

Child() {

16.

System.out.println("instantiate a child");

17.

super();

18.

}

19. }

指出此程序的错误之处?(Choose

one.)

A. 第3行将子对象赋值给一个父对象引用

B. 第9行,Parent类无父类,故不能调用super()方法

C.

第17行,super()方法调用应置于System.out.println(“instantiate

a child”)语句之前

D. 此程序可以正常运行,无错误

7.

MyThread.java程序如下:

1. public class MyThread

extends Thread {

2.

3.

public static void main(String [] args) {

4.

MyThread t = new MyThread();

5.

t.run();

6.

}

7.

8.

public void run() {

9.

for(int i=1;i<3;++i) {

10.

System.out.print(i + "..");

11.

}

12.

}

13. }

编译并运行,则?(Choose

one.)

A. 第4行错误,无法编译

B. 第5行错误,无法编译.

C. 可以正常编译,并输出:1..2..

D. 可以正常编译,并输出:1..2..3..

8.

AbstractTest.java程序如下:

1. public class AbstractTest

{

2. public static void main(String[] args) {

3. new B();

4. }

5. }

6. abstract class A

{

7. public A(){

8. System.out.println("hello");

9.

}

10. }

11. class B extends A

{

12. }

编译并运行,则?(Choose

one)

A. 可以正常编译,并输出”hello”

B. 第6行编译有错

C. 第7行编译有错

D. 第11行编译有错

9.

InterfaceTest.java程序如下:

1. public class InterfaceTest

{

2.

public static void main(String[] args) {

3.

new B() {

4.

public void x(){

5.

System.out.println("test");

6.

}

7.

}; //这里只是对x()方法进行了重写

//这么写,会打出“

test

”,因为调用了接口中的x()方法

`` new B()

{

public void x(){

System.out.println("test"); } }.x();

8.

}

9. }

10. interface A {

11. void x();

12. }

13. interface B extends A

{

14. }

编译并运行,则?(Choose

one.)

A. 正常编译,并输出”test”

B. 正常编译,不输出任何作息。

C. 第13行编译出错

D. 第3至7行编译出错

10.

SuperThis.java程序如下:

1. public class SuperThis

{

2.

public static void main(String[] args) {

3. new B();

4.

}

5. }

6. class A {

7.

public A(){

8. System.out.print ("A");

9. }

10. }

11. class B extends A

{

12. public B(Object obj){

13. System.out.print ("B");

14. }

15. public B(){

16. super();

17. this(new Object());

18. }

19. }

编译程序并运行,则?(Choose

one)

A. 正常编译,输出AB

B. 正常编译,无任何输出信息

C. 第16行编译错误,其应置于this()方法之后

D.

第17行编译错误,因为super()方法与this()方法不能同置于一个构造器内。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值