Java认证考试实例疑难辨析(5)

5.

【知识点】

(1)Thread类的start()方法

start()方法用于启动线程的执行,也就是让run()方法开始运行。

start()方法只允许执行一次,也就是说一个线程只允许启动一次,即使执行完毕也不允许重新启动,否则会抛出IllegalThreadStateException。

【例题】

       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.
【Answer】 B

【例题辨析】

A. 可以在Eclipse开发环境中尝试录入本程序,如果编辑窗口中没有错误提示出现,通常不存在编译错误;

B. 同样,在Eclipse开发环境中尝试录入本程序,可以看到如下信息:

fooException in thread "main"java.lang.IllegalThreadStateException

       atjava.lang.Thread.start(Unknown Source)

       atThreads4.go(Threads4.java:17)

       atThreads4.main(Threads4.java:5)

上述信息说明运行时抛出了异常,这是因为Thread类的start()方法只允许执行一次,也就是说一个线程只允许启动一次,即使执行完毕也不允许重新启动;

C. 同上

D. 同上
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,根据提供的引用内容,没有提供具体的Java在线考试系统的编程实例。但是,我可以为您提供一个基本的Java在线考试系统的编程框架,供您参考: ```java import java.util.ArrayList; import java.util.List; import java.util.Scanner; class Question { private String question; private List<String> options; private int correctOption; public Question(String question, List<String> options, int correctOption) { this.question = question; this.options = options; this.correctOption = correctOption; } public String getQuestion() { return question; } public List<String> getOptions() { return options; } public int getCorrectOption() { return correctOption; } } class Exam { private List<Question> questions; public Exam(List<Question> questions) { this.questions = questions; } public void startExam() { int score = 0; Scanner scanner = new Scanner(System.in); for (int i = 0; i < questions.size(); i++) { Question question = questions.get(i); System.out.println("Question " + (i + 1) + ": " + question.getQuestion()); List<String> options = question.getOptions(); for (int j = 0; j < options.size(); j++) { System.out.println((j + 1) + ". " + options.get(j)); } System.out.print("Enter your answer: "); int answer = scanner.nextInt(); if (answer == question.getCorrectOption()) { score++; } } System.out.println("Your score: " + score + "/" + questions.size()); } } public class OnlineExamSystem { public static void main(String[] args) { List<Question> questions = new ArrayList<>(); questions.add(new Question("What is the capital of France?", List.of("London", "Paris", "Berlin", "Madrid"), 2)); questions.add(new Question("What is the largest planet in our solar system?", List.of("Mars", "Jupiter", "Earth", "Saturn"), 2)); questions.add(new Question("What is the chemical symbol for gold?", List.of("Au", "Ag", "Fe", "Cu"), 1)); Exam exam = new Exam(questions); exam.startExam(); } } ``` 这是一个简单的Java在线考试系统的编程示例。它包括了问题(Question)和考试(Exam),并在主(OnlineExamSystem)中创建了一些问题并开始考试。您可以根据自己的需求进行扩展和修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值