Java认证考试综合练习

单选题class Order3 implements Runnable {  public static void main(String [] args) {  new Thread(new Order3()).start();  for(int x = 0; x 〈 10; x++) System.out.print("m"); }  public void run() {  for(int x = 0; x 〈 10; x++) {  //insert code here  System.out.p

题目
单选题
class Order3 implements Runnable {  public static void main(String [] args) {  new Thread(new Order3()).start();  for(int x = 0; x 〈 10; x++) System.out.print("m"); }  public void run() {  for(int x = 0; x 〈 10; x++) {  //insert code here  System.out.print("r");  }  }  }  和:  当代码被编译并照此运行时产生 "before" 的输出, 当下列内容插入到代码第8行时产生"after"输出  if (x 〉 3 && x 〈 7) Thread.yield();  对比“before”的输出结果和“after”的输出结果,下面哪一项是正确的?()
A

输出字符的总数可能改变。

B

当添加额外的代码时,编译将失败。

C

在“after”输出结果中,字符“m”较早出现的可能性较小。

D

在“after”输出结果中,字符“m”较早出现的可能性较大。

参考答案和解析
正确答案: B
解析: 暂无解析
如果没有搜索结果,请直接 联系老师 获取答案。
相似问题和答案

第1题:

在下面程序的下画线处应填入的选项是 public class Test______{ public static void main(String args[]) { Test t=new Test(); Thread tt=new Thread(t); tt.start(); } public void run() { for(int i=0;i<5;i++) System.out.println("i="+i); } }

A.implements Runnable

B.extends Thread

C.implements Thread

D.extends Runnable


正确答案:A
解析:创建线程有两种方法:实现java.lang.Runnahle接口和继承Thread类并重写run()方法。从创建线程实例的语句Thread tt=new Thread(t);可以看出本程序将Test类的实例t作为参数传递给Thread类的构造方法,因此是通过实现Runnable接口创建线程。

第2题:

interface A{

int x = 0;

}

class B{

int x =1;

}

class C extends B implements A {

public void pX(){

System.out.println(x);

}

public static void main(String[] args) {

new C().pX();

}

}


正确答案:

 

错误。在编译时会发生错误(错误描述不同的JVM 有不同的信息,意思就是未明确的

x 调用,两个x 都匹配(就象在同时import java.util 和java.sql 两个包时直接声明Date 一样)。

对于父类的变量,可以用super.x 来明确,而接口的属性默认隐含为 public static final.所以可

以通过A.x 来明确。

第3题:

通过实现Rmmable接口创建线程,请在下面横线处填写代码完成此程序。

public class ThreadTest

{

public static void main(String args [])

{

Thread testObj1 = new Thread (new Hello ());

Thread testObj2 = new Thread (new Hello ());

testObj 2.start ( );

}

}

class Hello implements Runnable

{

int j;

public void run()

{

System.out.println("Hello" + j ++);

}

}


正确答案:testObj 1.start();
testObj 1.start();

第4题:

public class Threads5 {  public static void main (String[] args) {  new Thread(new Runnable() {  public void run() {  System.out.print(”bar”);  } }).start();  }  }  What is the result?() 

  • A、 Compilation fails.
  • B、 An exception is thrown at runtime.
  • C、 The code executes normally and prints “bar”.
  • D、 The code executes normally, but nothing prints.

正确答案:C

第5题:

public class Threads3 implements Runnable {  public void run() {  System.out.print(”running”);  }  public static void main(String[] args) {  Thread t = new Thread(new Threads3());  t.run();  t.run();  t.start();  }  }  What is the result?() 

  • A、 Compilation fails.
  • B、 An exception is thrown at runtime.
  • C、 The code executes and prints “running”.
  • D、 The code executes and prints “runningrunning”.
  • E、 The code executes and prints “runningrunningrunning”.

正确答案:E

第6题:

下列程序的运行结果是______。 class A implements Runnable { int a; iht i = 2; A(int x) { a = x; } public void run() { while(i > 0) { System.out.println("线程" + a); i--; } } } public class Testl3 { public static void main(String[] args) { Thread a1 = new Thread(new A(1)); Thread a2 = new Thread(new A(2)); a1.start(); a2.start(); } }

A.线程1 线程1 线程2 线程2

B.线程1 线程2

C.线程1 线程2 线程1 线程2

D.线程1 线程1 线程1 线程1


正确答案:A
解析:类A实现了Runnable接口,并且通过类的构造函数A(int x)传递整型参数给对象作为线程的编号。在run()线程体内,实现打印两个字符串。在main()方法中,用A类的2个对象创建了a1和a2两个线程,当a1和a2开始执行时,从A类的run()方法开始执行。

第7题:

( 30 )在程序的下划线处应填入的选项是

public class Test _________{

public static void main(String args[]){

Test t = new Test();

Thread tt = new Thread(t);

tt.start();

}

public void run(){

for(int i=0;i<5;i++){

system.out.println( " i= " +i);

}

}

}

A ) implements Runnable

B ) extends Thread

C ) implements Thread

D ) extends Runnable


正确答案:A

第8题:

下列程序执行后的结果为______。 public class exl2 { public static void main(string[] args) { int n=4; int x=0; do{ System.out.print(n); }while (x++<n--); } }

A.12

B.432

C.43

D.4


正确答案:B

第9题:

现有:  class Passer f  static final int X=5;  public  static void main (String  []  args)  {      new  Passer().go (x);      System. out .print (x);      }  void go (int x)  {     System. out .print(x++);     }     结果是什么?()     

  • A、55
  • B、56
  • C、65
  • D、66

正确答案:A

第10题:

现有:  class ThreadExcept implements Runnable  {  public void run()  {  throw new RuntimeException("exception ");  }  public static void main(Stri_ng  []  args)  {  new  Thread (new  ThreadExcept()).start();  try  {  int x=Integer.parselnt (args [0]);  Thread. sleep (x);  System.out.print("main");  } catch (Exception e)  {  }  }  }  和命令行:  java ThreadExcept l000     哪一个是结果?()    

  • A、main
  • B、编译失败
  • C、main java.lang.RuntimeException: exception
  • D、代码运行,但没有输出

正确答案:C

更多相关问题