单选题public class Threads4 {  public static void main (String[] args) {  new Threads4().go();  }  public void go() {  Runnable r = new Runnable() { public void run() {  System.out.print(”foo”);  }  };  Thread t = new Thread(r);  t.start();  t.start();  }  }

题目
单选题
public class Threads4 {  public static void main (String[] args) {  new Threads4().go();  }  public void go() {  Runnable r = new Runnable() { public void run() {  System.out.print(”foo”);  }  };  Thread t = new Thread(r);  t.start();  t.start();  }  }  What is the result?()
A

 Compilation fails.

B

 An exception is thrown at runtime.

C

 The code executes normally and prints „foo”.

D

 The code executes normally, but nothing is printed.

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

阅读下面程序 public class Test implements Runnable { public static void main(String[] args) { ______ t.start(); } public void run() { System.out.println("Hello!"); } } 程序中下画线处应填入的正确选项是

A.Test t=new Test();

B.Thread t=new Thread();

C.Thread t=new Thread(new Test());

D.Test t=new Thread();


正确答案:C

第2题:

class MyThread extends Thread {  public void run() { System.out.println(“AAA”); }  public void run(Runnable r) { System.out.println(“BBB”); }  public static void main(String[] args) {  new Thread(new MyThread()).start();  }  }   What is the result?()  

  • A、 AAA
  • B、 BBB
  • C、 Compilation fails.
  • D、 The code runs with no output.

正确答案:A

第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题:

Which two code fragments will execute the method doStuff() in a separate thread?()

  • A、 new Thread() { public void run() { doStuff(); } }
  • B、 new Thread() { public void start() { doStuff(); } }
  • C、 new Thread() { public void start() { doStuff(); } } .run();
  • D、 new Thread() { public void run() { doStuff(); } } .start();
  • E、 new Thread(new Runnable() { public void run() { doStuff(); } } ).run();
  • F、 new Thread(new Runnable() { public void run() { doStuff(); } }).start();

正确答案:D,F

第5题:

public class Foo implements Runnable (  public void run (Thread t) {  system.out.printIn(“Running.”);  }  public static void main (String[] args)  {  new thread (new Foo()).start();  } )   What is the result?()      

  • A、 An exception is thrown.
  • B、 The program exists without printing anything.
  • C、 An error at line 1 causes compilation to fail.
  • D、 An error at line 2 causes the compilation to fail.
  • E、 “Running” is printed and the program exits.

正确答案:D

第6题:

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

第7题:

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

第8题:

阅读下面程序 public class Test implements Runnable{ public static void main(String[]args){ _______________________________________; t. start(); } public void mR(){ System. out. println("Hello!"); }} 在程序下画线处填入正确选项是

A.Test t=flew Test()

B.Thread t=new Thread();

C.Thread t=new Thread(new Test());

D.Test t=new Thread();


正确答案:C
解析:根据t. start()可知t应该是一个Thread类,排除A)。Thread类与Test类之间没有继承关系,所以排除D)。B)没有指定创建线程的对象,因此t. start()语句不能使Test类的run方法运行。所以选C)。

第9题:

Runnable r = new Runnable() {  public void run() {  System.out.print(”Cat”);  }  };  Threadt=new Thread(r) {  public void run() {  System.out.print(”Dog”);  }  };  t.start();  What is the result?() 

  • A、 Cat
  • B、 Dog
  • C、 Compilation fails.
  • D、 The code runs with no output.
  • E、 An exception is thrown at runtime.

正确答案:B

第10题:

现有:   class Thread2 implements Runnable {   void run() {   System.out.print("go ");   }   public static void main(String [] args) {   Thread2 t2 = new Thread2();   Thread t = new Thread(t2);   t.start();   }   }   结果为:()

  • A、 go
  • B、 编译失败
  • C、 代码运行,无输出结果
  • D、 运行时异常被抛出

正确答案:B

更多相关问题