单选题下列程序的运行结果是(  )。public class test{private String [] data = {"10","10.5"};public void fun(){ double s = 0; for(int i = 0; i try{ s = s + Integer.parseInt(data[i]); } catch(Exception e){ System.out.print("error1:" + data[i]); } }}public static v

题目
单选题
下列程序的运行结果是(  )。public class test{private String [] data = {"10","10.5"};public void fun(){ double s = 0; for(int i = 0; i try{ s = s + Integer.parseInt(data[i]); } catch(Exception e){ System.out.print("error1:" + data[i]); } }}public static void main(String[] args){ try{ test d = new test(); d.fun(); } catch(Exception e){ System.out.println("error2"); }} }
A

error1:10.5

B

error2

C

error1:10.5error2

D

以上都不对


相似考题
参考答案和解析
正确答案: D
解析:
Integer.parseInt(参数)函数作用是将字符类型数据转换为整型数据。当参数为一些不能转换为整型的字符时,会抛出异常。10.5是不可转换的,抛出异常,输出 error1:10.5。因为data数组溢出,所以抛出异常,输出error2。
更多“单选题下列程序的运行结果是(  )。public class test{private String [] data = {"10","10.5"};public void fun(){ double s = 0; for(int i = 0; i try{ s = s + Integer.parseInt(data[i]); } catch(Exception e){ System.out.print("error1:" + data[i]); } }}public static v”相关问题
  • 第1题:

    下列程序的执行结果是 ( ) public class Test { public int aMethod() { satic int i=0; i++; System.out.println(i); } public static void.main(String args[]) { Test test=new Test(); test.aMethod(); }

    A.编译错误

    B.0

    C.1

    D.运行成功,但不输出


    正确答案:A

  • 第2题:

    请完成Java程序:本题是一个冒泡排序程序的实例。冒泡排序的含义是将相邻的两个数作比较,如果是升序排列的话,如果前边的数大,则将两个数交换。从第一个数开始两两比较一次,就可以将最大的数移动到最后。

    注意:请勿修改main()主方法和其他已有语句内容,仅在横线处填入适当语句。

    import java.io.*;

    public class simple

    {

    public static int[]Data=new int[10];

    public static void main(String[] args)

    int i;

    int Index;

    Index=0;

    InputStreamReader ir;

    BufferedReader in;

    ir=new InputStreamReader(System.in);

    in=new BufferedReader(ir);

    try

    {

    do

    {

    System.out.println("Please input the number"+

    Index+"you want to sort(Exit for 0):");

    String s=in.readLine();

    Data[Index]=Integer.parseInt(s);

    Index++;

    }

    while(Data[Index-1]!=0);

    }

    catch(IOException e)

    {

    System.out.println(e.getMessage());

    }

    System.out.print("Before bubble sorting:");

    for(i=0; i<Index-1; i++)

    System.out.print(" "+Data[i]+" ");

    System.out.println(" ");

    BubbleSort(Index-1);

    System.out.print("After Bubble Sorting:");

    for(i=0; i<Index-1;i++)

    System.out.print(" "+Data[i]+" ");

    System.out.println(" ");

    }

    public static void BubbleSort(int Index)

    {

    int i, j, k;

    boolean Change;

    int Temp;

    for(j=Index; j>1;j--)

    {

    Change=false;

    for(i=0; i<j-1;i++)

    {

    if(Data[i+1]<Data[i])

    {

    Temp=Data[i+1];

    Data[i+1]=Data[i];

    ______;

    ______;

    }

    }

    if(Change)

    {

    System.out.print("Current Sorting Result:");

    for(k=0; k<Index; k++)

    System.out.print(" "+Data[k]+" ");

    System.out.println(" ");

    }

    }

    }

    }


    正确答案:Data[i]=Temp Change=true
    Data[i]=Temp Change=true 解析:本题考查知识点:基本排序算法。冒泡排序是最常用的交换排序方式,其核心算法就是比较和交换。交换的最简单方法是用一个中间变量作交换中介。先把其中一个值存入临时变量,然后将另一个值赋给第一个值,最后将临时变量赋给另一个值(第1个空)。另外,为加快速度,如果排序已经完成了,就可以直接返回,而不必等待循环结束(第2个空,将标志变量设为真)。

  • 第3题:

    下列类的定义中,有( )处语法错误。 class Base { public: Base(){} Base(int i) { data=i; } private: int data; }; class Derive : public Base { public: Derive() : Base(0) {} Derive (int x) { d=x; } void setvalue(int i) { data=i; } private: d; };

    A.1

    B.2

    C.3

    D.4


    正确答案:B
    解析:本题考核派生类的定义和成员的访问权限。第一处错误:在派生类的构造函数Derive(int x)中没有调用基类的构造函数对基类对象初始化;第二处错误:数据data是基类Base的私有成员,派生类Derive不能访问,所以在函数setvalue中对data的赋值是错误的。

  • 第4题:

    下面程序代码运行结果为( )。 import java.awt.*; public class Test { public static void main (String args[]) { String s1="a+b+c"; String s2="+"; int i=s1.lastIndexOf (s2); System.out.println(i); } }

    A.0

    B.1

    C.2

    D.3


    正确答案:D
    解析:lastIndexOf方法返回一个整数值,指出String对象内子字符串的最后一次出现的开始位置下标。如果没有找到子字符串,则返回-1。本题中子串“+”在字符串“a+b+c”中出现了两次,最后一次出现的起始位置为3。

  • 第5题:

    下列程序的运行结果是( )。 public class test{ private String[]data={¨10","10.5"); public void fun{ double s=0: for(int i=0;i<3;j++){ try{ s=s+Integer.parseInt(data[i]); catch(Exception e){ System.out.print("errorl:"+data[i]); } } } public static void main(string[]args){ try{ test d=new test: fun: }catch(Exception e){ System.OUt.println("error2") } } }

    A.errorl:10.5

    B.error2

    C.errorl:10.5 error2

    D.以上都不对


    正确答案:C
    C。【解析】try-catch块是可以嵌套分层的,并且通过异常对象的数据类型来进行匹配,以找到正确的catchblock异常错误处理代码。以下是通过异常对象的数据类型来进行匹配找到正确的catchblock的过程。①首先在抛出异常的try-catch块中查找catchblock,按顺序先与第一个catchblock块匹配,如果抛出的异常对象的数据类型与catchblock中传入的异常对象的临时变量(就是catch语句后面参数)的数据类型完全相同,或是它的子类型对象,则匹配成功,进入到catchblock中执行,否则到第2步;②如果有两个或更多的catchblock,则继续查找匹配第二个、第三个,直至最后一个catchblock,如匹配成功,则进入到对应的catchblock中执行,否则到第3步;③返回到上一级的try-catch块中,按规则继续查找对应的catchblock。如果找到,进入到对应的catchblock中执行,否则到第4步;④再到上上级的try-catch块中,如此不断递归,直到匹配到顶级的try-catch块中的最后一个catchblock,如果找到,进入到对应的catchblock中执行;否则程序将会执行terminate退出。所以本题选C。

  • 第6题:

    下列程序的运行结果是( )。 public class test{ private String[]data={¨10","10.5"}, public void fun( double S=0; for(int i=0;i<3;i++){ try{ s=s+Integer.parseInt(data[i]); catch(Exception e){ System.out.print(“errorl:”+data[i])。 } } } public static void main(string[]args){ try{ test d=new test; fun; }catch(Exception e){ System.out.println("error2") } } }

    A.errorl:10.5

    B.error2

    C.errorl:10.5 error2

    D.以上都不对


    正确答案:C
    C。【解析】try-catch块是可以嵌套分层的,并且通过异常对象的数据类型进行匹配,以找到正确的catchblock异常错误处理代码。以下是通过异常对象的数据类型进行匹配找到正确的catchblock的过程。①首先在抛出异常的try-catch块中查找catchblock,按顺序先与第一个catchblock块匹配,如果抛出的异常对象的数据类型与catchblock中传入的异常对象的临时变量(就是catch语句后面参数)的数据类型完全相同,或是它的子类型对象,则匹配成功,进入catchblock中执行,否则到第②步;②如果有两个或更多的catchblock,则继续查找匹配第二个、第三个,乃至最后一个catchblock,如匹配成功,则进入对应的catchblock中执行,否则到第③步;③返回到上一级的try-catch块中,按规则继续查找对应的catchblock。如果找到,进入对应的catchblock中执行,否则到第4步;④再到上上级的try-catch块中,如此不断递归,直到匹配到顶级的try-catch块中的最后一个catchblock,如果找到,进入到对应的catchblock中执行;否则程序将会执行terminate退出。所以选C。

  • 第7题:

    下列类的定义中,有( )处语法错误。 class Base { publiC: Base(){} Base(int i) { data=i; } ptivate: int data; }; class DeriVe:public Base { public: Derive():Base(0){} Derive(int x) { d=x; } void setvalue(int i) { data=i; } private: d; };

    A.1

    B.2

    C.3

    D.4


    正确答案:B

  • 第8题:

    以下程序的输出结果为:public class example {public static void main(String args[]) {int s=0;for (int i=0;i<5;i++) {for (int j=10;j>3*i;j--)s += i*j;}System.out.println(s);}}

    A. 127

    B.136

    C. 147

    D.153


    正确答案:C

  • 第9题:

    public class TestApp{   public static void main(String[] args){   try{   int i = 0;   int j = 1 / i;   System.out.println(“1”);   } catch(Exception e){   System.out.print(“3”);   } finally{   System.out.print(“4”);   }   }   }   上述程序运行后的输出是哪项?() 

    • A、 4
    • B、 34
    • C、 43
    • D、 14

    正确答案:B

  • 第10题:

    public class TestApp{  public static void main(String[] args){       try{  int i = 0;           int j = 1 / i;           String myname=null;            if(myname.length()>2)              System.out.print(“1”);        }catch(NullPointerException e){  System.out.print(“2”);       }  catch(Exception e){  System.out.print(“3”);       }   } }  上述程序运行后的输出是哪项?()

    • A、 3
    • B、 2
    • C、 231
    • D、 32

    正确答案:A

  • 第11题:

    单选题
    Public class Holt extends Thread{   Private String sThreadName;   Public static void main(String argv[]) {  Holt  h=new Holt(); h.go(); Holt(){};  Holt(String s){ sThreadName=s;  Public String getThreadName() {  return sThreadName;} }  Public void go(){  Hot first=new Hot("first"); first.start();  Hot second=new Hot("second"); second.start();  }  Public void start() {  For(int i=0;i<2;i++) {  System.out.print(getThreadName()+i); Try{  Thread.sleep(100); }catch(Exception e){  System.out.print(e.getMessage()) ;  } } }  }  当编译运行上面代码时,将会出现()
    A

    编译时错误

    B

    输出first0,second0,first0,second1

    C

    输出first0,first1,second10,second1

    D

    运行时错误


    正确答案: B
    解析: 暂无解析

  • 第12题:

    单选题
    下列程序的运行结果是(  )。public class ArrayTest{public static void main(String[] args){ int data[][]={{1,2,3,4,5},{11,22,33,44,55},{111,222,333,444,555}}; for(int i=0;i if(i%2==0)System.out.print(data[i][4]);}}}
    A

    5555

    B

    555

    C

    555

    D

    55


    正确答案: C
    解析: 该程序的功能是遇到行下标数为偶数时,输出这一行的列下标为4的数,即这一行的第五个数。而程序定义的二维数组一共有三行五列。第0行的时候,可以被2整除,所以输出第5个数“5”,第2行的时候,也可以被2整除,所以输出这一行的第5个数“555”。由于最后输出方式是“print”,没有换行,所以最终显示结果是“5555”。

  • 第13题:

    本题程序中实现了一个“生产者一消费者问题”。生产者产生一个随机数存入DataPool类中,消费者从中取出数据。DataPool类一次只能存放一个数据。请更正题中带下划线的部分。

    注意:不改变程序的结构,不得增行或删行。

    class DataPool

    {

    private int data;

    private boolean isFull;

    public DataPool()

    {

    isFull=false;

    }

    public synchronized void putData(int d)

    {

    if(isFull= =true)

    {

    try

    {

    this.notify();

    }

    catch(InterruptedException e)

    {}

    }

    data=d;

    isFull=true;

    System.out.println("生产了一个数据:"+data);

    this.notify();

    }

    public synchronized int getData()

    {

    if(isFull= =false)

    {

    try

    {

    this.wait();

    }

    catch(InterruptedException e)

    {}

    }

    isFull=false;

    System.out.println("消费了一个数据"+data);

    this.wait();

    return this.data;

    }

    boolean getIsFull()

    {

    return isFull;

    }

    }

    class Producer extends Thread

    {

    DataPool pool;

    public Producer(DataPool pool)

    {

    this.pool=pool;

    }

    public void run()

    {

    for(int i=0; i<10; i++)

    {

    int data=(int) (Math.random()*1000);

    try

    {//用于生产数据

    sleep(data);

    }

    catch(InterruptedException e)

    {}

    pool.putData(data);

    }

    }

    }

    class Consumer implements Runnable

    {

    DataPool pool;

    public Consumer(DataPool pool)

    {

    this.pool=pool;

    }

    public void run()

    {

    for(int i=0; i<10; i++)

    {

    int data=pool.getData();

    try

    {//用于处理数据

    sleep((int) (Math.random()*1000));

    }

    catch(InterruptedException e)

    {}

    }

    }

    }

    public class advance

    }

    public static void main(String[] args)

    {

    Data Pool pool=new Data Pool();

    Producer pro=new Producer(pool);

    Runnable con=new Consumer(pool);

    Thread conTh=new Thread(con);

    &n


    正确答案:this.wait() this.notify() thread.sleep((int)(Math.random()*1000))
    this.wait() this.notify() thread.sleep((int)(Math.random()*1000)) 解析:本题考查知识点:多线程同步与互斥、线程的概念和实现方法。解题思路:Data Pool是一个用来存放数据的缓冲池,其中可以存放一个血型数据,变量isFull作为标志量,标志该缓冲池中是否有数据。Put Data()方法负责向Data Pool中存放数据,本方法调用成功,缓冲池中就存入了数据,getData()方法负责从DataPool中获得数据,本方法调用成功,缓冲池就为空。Producer类负责产生随机数据,然后将数据存放到DataPool中。Consumer类负责才能够从DataPool中取出数据。Producer和Consumer共享同一个Data Pool对象。当某个线程进入synchronized块后,共享的数据并不一定能满足该线程的需要,这样线程就需要等待其他线程修改共享数据,直到满足需要以后才继续执行,但是当前线程必须释放锁以使得其他线程可以运行。wait()和notify()方法是实现线程之间通信的两个方法。wait()用来释放线程拥有的锁,使线程进入等待队列;notify()用来唤醒等待队列中的线程,并把它加入到申请锁的队列。本题中生产者在DataPool有数据的情况下需要进入等待消费者取出数据,所以需要调用wait()方法,因此第1个下划线的地方应该改为this.wait()。消费者线程在取出数据以后,必须通知生产者线程DataPool中已经没有数据了,因此第2个下划线的地方改为this.notify()。第3个下划线的地方,考查常用的线程类的使用。Runnable接口的目的是使任何类都可以为线程提供线程体,但它本身并不是线程,所以并不能直接调用Thread类的方法,需要改为 thread.sleep。

  • 第14题:

    下列代码的执行结果是public class Test{ public int aMethod(){ static int i=0; i++; System.out.println(i); } public static void main(String args[]){ Test test= new Test(); test. aMethod(); }}

    A.编译错误

    B.0

    C.1

    D.运行成功,但不输出


    正确答案:A
    解析:static不能修饰局部变量。

  • 第15题:

    下列程序段运行的结果为 public class Test{ static void print(String s,int i){ System.out.println("String:"+s+",int:"+i); } static void print(int i, String s){ System.out.println("int:"+i+",String:"+s); } public static void main(String [] args){ print(99,"Int first"); } }

    A.String:Stringfirst,int:11

    B.int:11,String:Int first

    C.String:String first,int:99

    D.int:99,String:int first


    正确答案:D
    解析:本题考查考生阅读程序的能力。JavaApplication都是以main()方法作为入口,首先执行的是print(99,“Intfirst”),根据构造方法的参数类型选择调用方法,这里调用的是print(inti,Strings)方法,因此输出的是int:99,String:Intfirst。

  • 第16题:

    有如下程序: public class Sun { public static void main(String args[ ]) { int s=0; int i=1; while(i<=100) { s=s+i; } System.out.println(s); } } 运行后的结果是( )。

    A.5050

    B.5051

    C.死循环,直到溢出

    D.无穷大的数


    正确答案:C
    解析:本题考查对while循环语句的理解。在while语句中的条件再循环中必须有加的可能,循环才能正常结束。如条件恒为真,循环永无终止地进行下去,出现死循环。此程序由于i的值始终小于0,因此,这是一个死循环,s的值为1+1+1+…直到系统溢出。如在第4行后面加上语句i=i+1,循环就会正常结束。故本题答案是C。

  • 第17题:

    下面程序的输出结果是( )。 public class Sun { public static void main(String args[]) { int[] a={1,2,3,4); int j=1,s=0; for(int i=3;i>=0;i--) { s=s+a[i]*j; j=j*10; } System.out.println(s); } }

    A.1234

    B.21

    C.43

    D.4321


    正确答案:A
    解析:本题考查对for循环语句和数组的理解。for语句中步长为-1即每循环一次,循环变量i的值减1,数组进行反运算,即a[i]取值4、3、2、1。故本题答案是A。

  • 第18题:

    3下列程序段运行的结果为( )。 public class Test{ static void print(String s,int i){ System.out.pdntlnC String: "+s+",int:"+i); } static void print(iht i,String s){ System.out.prinflnCint:"+i+",gtring:"+s); } public static void main(String[] args){ print(99,"Int first"); } }

    A.String:String first,int: 11

    B.int: 11,String:Int first

    C.String:String first,int:99

    D. int:99,Stfing:Int first


    正确答案:D

  • 第19题:

    下列程序运行后的输出结果是( )。 public class Sun { public static void main(String args[]) { int i,j,s; s=0; for(i=1;i<=3;i++) for(j=0;j<=i-1;j++) s=s+1; System.out.println(s); } }

    A.6

    B.5

    C.4

    D.3


    正确答案:A
    解析:本题考查对for循环语句理解。第1重for循环共有3次,第2重for循环共有2次,所以总共执行6次循环,每次循环s的值加1,因此s=6。故本题答案是A。

  • 第20题:

    以下程序的运行结果为?

    class ValHold{

    public int i = 10;

    }

    public class ObParm{

    public static void main(String argv[]){

    ObParm o = new ObParm();

    o.amethod();

    }

    public void amethod(){

    int i = 99;

    ValHold v = new ValHold();

    v.i=30;

    another(v,i);

    System.out.print( v.i );

    }

    public void another(ValHold v, int i){

    i=0;

    v.i = 20;

    ValHold vh = new ValHold();

    v = vh;

    System.out.print(v.i);

    System.out.print(i);

    }

    }

    A.10030

    B. 20030

    C. 209930

    D. 10020


    正确答案:D

  • 第21题:

    Public class Holt extends Thread{   Private String sThreadName;   Public static void main(String argv[]) {  Holt  h=new Holt(); h.go(); Holt(){};  Holt(String s){ sThreadName=s;  Public String getThreadName() {  return sThreadName;} }  Public void go(){  Hot first=new Hot("first"); first.start();  Hot second=new Hot("second"); second.start();  }  Public void start() {  For(int i=0;i<2;i++) {  System.out.print(getThreadName()+i); Try{  Thread.sleep(100); }catch(Exception e){  System.out.print(e.getMessage()) ;  } } }  }  当编译运行上面代码时,将会出现() 

    • A、编译时错误
    • B、输出first0,second0,first0,second1
    • C、输出first0,first1,second10,second1
    • D、运行时错误

    正确答案:C

  • 第22题:

    单选题
    public class TestApp{  public static void main(String[] args){        try{  int i = 0;           int j = 1 / i;  System.out.println(“1”);        }catch(Exception e){              System.out.print(“3”);       }finally{  System.out.print(“4”);       }   } }  上述程序运行后的输出是哪项?()
    A

     4

    B

     34

    C

     43

    D

     14


    正确答案: B
    解析: 暂无解析

  • 第23题:

    单选题
    public class TestApp{  public static void main(String[] args){       try{  int i = 0;           int j = 1 / i;           String myname=null;            if(myname.length()>2)              System.out.print(“1”);        }catch(NullPointerException e){  System.out.print(“2”);       }  catch(Exception e){  System.out.print(“3”);       }   } }  上述程序运行后的输出是哪项?()
    A

     3

    B

     2

    C

     231

    D

     32


    正确答案: A
    解析: 暂无解析