`
snowyvalley
  • 浏览: 143085 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Java基础测试题

    博客分类:
  • Java
阅读更多

1 下面的语句哪一行在编译时没有警告和编译错误

a)      float f=1.3;

b)      char c="a";

c)      byte b=257;

d)      boolean b=null;

e)      int i=10;

2 下面的代码编译后会出现什么问题

public class MyClass {

    public static void main(String arguments[]) {

         amethod(arguments);

}

    public void amethod(String[] arguments) {

         System.out.println(arguments);

         System.out.println(arguments[1]);

    }

}

a)      错误,不能静态引用amethod方法

b)      错误,main方法不正确

c)      错误,数组必须包含参数

d)      Amethod必须声明为String类型

3 下面的哪一组代码会出现编译错误

a)      import java.awt.*;

package Mypackage;

class Myclass {}

b)      package MyPackage;

import java.awt.*;

class MyClass{}

c)      /*This is a comment */

<o:p> </o:p>

package MyPackage;

import java.awt.*;

class MyClass{}

4) byte类型的大小是

a) -128 ~  127

b) -28-1 ~28

c) -255 ~ 256

d)不同的操作系统Java虚拟机分配不同的大小

5) 下面的代码在输入下面的命令行后会输出什么内容

命令行:java myprog good morning

代码:

public class myprog{

    public static void main(String argv[])

    {

         System.out.println(argv[2]);

    }

}

a) myprog

b) good

c) morning

d) Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2"

6)下面哪个不是Java的关键字或者保留字

a) if

b) then

c) goto

d) while

e) case

7) 下面哪些是合法的标志符 (多选)

a) 2variable

b) variable2

c) _whatavariable

d) _3_

e) $anothervar

e) #myvar

8) 试图编译运行下面的代码会发生什么情况

public class MyClass{

 static int i;

 public static void main(String argv[]){

 System.out.println(i);

 }

}

a)错误,变量i没有被初始化

b) null

c) 1

d) 0

9)试图编译运行下面的代码会发生什么情况

public class Q {

 public static void main(String argv[]){

 int anar[]=new int[]{1,2,3};

 System.out.println(anar[1]);

 }

}

a) 1

b) Error anar 被引用前没有初始化

3) 2

4) Error: 数组大小没有定义

10)试图编译运行下面的代码会发生什么情况

public class Q {

 public static void main(String argv[]){

 int anar[]=new int[5];

 System.out.println(anar[0]);

 }

}

a) Error: anar is referenced before it is initialized

b) null

c) 0

d) 5

11)试图编译运行下面的代码会发生什么情况

abstract class MineBase {

 abstract void amethod();

 static int i;

}

public class Mine extends MineBase {

 public static void main(String argv[]){

 int[] ar=new int[5];

 for(i=0;i < ar.length;i++)

 System.out.println(ar[i]);

 }

}

a) 50的序列会被输出

b) Error: ar 在使用前要初始化

c) Error Mine 必须被声明为 abstract

d) Error 数组越界

12)试图编译运行下面的代码会输出什么样的结果

int i=1;

 switch (i) {

 case 0:

 System.out.println("zero");

 break;

 case 1:

 System.out.println("one");

 case 2:

 System.out.println("two");

 default:

 System.out.println("default");

 }

a) one

b) one, default

c) one, two, default

d) default

13)试图编译运行下面的代码会输出什么样的结果

int i=9;

switch (i) {

 default:

 System.out.println("default");

 case 0:

 System.out.println("zero");

 break;

 case 1:

 System.out.println("one");

 case 2:

 System.out.println("two");

}

a) default

b) default, zero

c) error default没有定义

d) 无输出

14)下面的哪些组代码没有编译错误(多选)

a)

int i=0;

if(i) {

 System.out.println("Hello");

 }

b)

boolean b=true;

boolean b2=true;

if(b==b2) {

 System.out.println("So true");

 }

c)

int i=1;

int j=2;

if(i==1|| j==2)

 System.out.println("OK");

d)

int i=1;

int j=2;

if(i==1 &| j==2)

<o:p> </o:p>

 System.out.println("OK");

15 如果在当前目录下不存在Hello.txt 文件,试图编译和运行下面代码会输出什么

import java.io.*;

public class Mine {

    public static void main(String argv[]){

         Mine m=new Mine();

         System.out.println(m.amethod());

    }

    public int amethod() {

         try {

             FileInputStream dis=new FileInputStream("Hello.txt");

         }catch (FileNotFoundException fne) {

             System.out.println("No such file found");

             return -1;

         }catch(IOException ioe) {

         } finally{

             System.out.println("Doing finally");

         }

<o:p> </o:p>

         return 0;

    }

<o:p> </o:p>

}

a) No such file found

b No such file found ,-1

c) No such file found, Doing finally, -1

d) 0

16)在下面的注释处插入哪些部分代码是合法的(多选)

class Base{

 public void amethod(int i) { }

}

<o:p> </o:p>

public class Scope extends Base{

 public static void main(String argv[]){

 }

 //在这里定义一个方法

}

a) void amethod(int i) throws Exception {}

b) void amethod(long i)throws Exception {}

c) void amethod(long i){}

d) public void amethod(int i) throws Exception {}

17)下面哪行代码输出-4.0

a) System.out.println(Math.floor(-4.7));

b) System.out.println(Math.round(-4.7));

c) System.out.println(Math.ceil(-4.7));

d) System.out.println(Math.min(-4.7));

18)如果运行下面的代码会输出什么内容

String s=new String("Bicycle");

int iBegin=1;

char iEnd=3;

System.out.println(s.substring(iBegin,iEnd));

1) Bic

2) ic

3) icy

4) error:没有匹配的方法 substring(int,char)

19)给出下面的代码在注释部分放置什么样的代码会输出“Equal

public class EqTest{

          public static void main(String argv[]){

                    EqTest e=new EqTest();

        }

<o:p> </o:p>

         EqTest(){

                    String s="Java";

                    String s2="java";

                    //place test here {

                             System.out.println("Equal");

                             }else

                             {

                             System.out.println("Not equal");

                    }

          }

}

a) if(s==s2)

b) if(s.equals(s2)

c) if(s.equalsIgnoreCase(s2))

d)if(s.noCaseMatch(s2))

20)给出下面的代码,怎样调用Base的构造方法会输出"base constructor"

class Base{

    Base(int i){

         System.out.println("base constructor");

    }

    Base(){

    }

}

<o:p> </o:p>

public class Sup extends Base{

    public static void main(String argv[]){

         Sup s= new Sup();

         //One

    }

    Sup()

    {

         //Two

    }

<o:p> </o:p>

    public void derived()

    {

         //Three

    }

}

a) //One 后放置Base(10);

b) //One 后放置 super(10);

c) //Two 后放置 super(10);

d) //Three 后放置super(10);

参考答案:

1-5 e a a a d

6-10 b bcde d c c

11-15 c c b bc c

16-20 bc c b c d

分享到:
评论
1 楼 java2000_net 2009-04-08  
JAVA自测题栏目正式上线,来测测你的Java功力到底如何!
http://www.java2000.net/selftest/

其它推荐栏目:
JAVA的API文档速查: http://api.java2000.net
JAVA各种面试题答案:http://www.java2000.net/f146
JAVA各种jar下载:   http://www.java2000.net/download/jar.jsp

相关推荐

Global site tag (gtag.js) - Google Analytics