Tag: java

Questions Related to java

  1. You must have a reference to an instance of the enclosing class in order to instantiate it.

  2. It does not have access to nonstatic members of the enclosing class.

  3. It's variables and methods must be static.

  4. It must extend the enclosing class.


Correct Option: B
Explanation:

To answer this question, you need to have an understanding of nested classes in Java.

A static nested class is a class that is defined inside another class, and it is marked as static. Here is an explanation of each option:

A. You must have a reference to an instance of the enclosing class in order to instantiate it. This statement is false. Unlike an inner class, a static nested class does not require an instance of the enclosing class to be instantiated. You can create an instance of a static nested class without having an instance of the enclosing class.

B. It does not have access to nonstatic members of the enclosing class. This statement is true. Since a static nested class is static, it does not have access to nonstatic members (variables or methods) of the enclosing class. It can only access static members of the enclosing class.

C. Its variables and methods must be static. This statement is false. Although the static nested class is static, it can have both static and nonstatic variables and methods. It is not required for all of its members to be static.

D. It must extend the enclosing class. This statement is false. A static nested class does not have to extend the enclosing class. It is a separate class and can have its own inheritance hierarchy.

Based on the explanations above, the correct statement about a static nested class is:

The Answer is: B. It does not have access to nonstatic members of the enclosing class.

  1. Runnable r = new Runnable() { };

  2. Runnable r = new Runnable(public void run() { });

  3. Runnable r = new Runnable { public void run(){}};

  4. System.out.println(new Runnable() {public void run() { }});


Correct Option: D
  1. init();

  2. start();

  3. run();

  4. resume();


Correct Option: B
Explanation:

The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Which cannot directly cause a thread to stop executing?

  1. Calling the SetPriority() method on a Thread object.

  2. Calling the wait() method on an object.

  3. Calling notify() method on an object.

  4. Calling read() method on an InputStream object.


Correct Option: C
Explanation:

notify() - wakes up a single thread that is waiting on this object's monitor.

  1. wait()

  2. notify()

  3. notifyall()

  4. exits synchronized code


Correct Option: A
  1. void run()

  2. public void run()

  3. public void start()

  4. void run(int priority)


Correct Option: B

Which will contain the body of the thread?

  1. run();

  2. start();

  3. stop();

  4. main();


Correct Option: A

AI Explanation

To answer this question, we need to understand the concepts related to threads.

In computer programming, a thread is a sequence of instructions that can be executed independently of the main program. Each thread has its own stack and execution context, allowing multiple threads to run concurrently.

In the given options, the correct answer is A) run().

The run() method is the entry point for a thread when it is executed. It contains the body of the thread and defines the code that will be executed by the thread. The run() method should be overridden in a class that extends the Thread class or implements the Runnable interface.

Option B) start() is used to start a thread by invoking its run() method. It does not contain the body of the thread.

Option C) stop() is used to stop a thread, but it is considered unsafe and deprecated in most programming languages.

Option D) main() is the entry point for the main program, not for a thread. It is executed by the main thread.

Therefore, the correct answer is A) run(). This option is correct because it contains the body of the thread.

Please select the most appropriate option.

Which method registers a thread in a thread scheduler?

  1. run();

  2. construct();

  3. start();

  4. register();


Correct Option: C

AI Explanation

To answer this question, you need to understand how threads are registered in a thread scheduler.

Option A) run() - This option is incorrect because the run() method is responsible for the code that will be executed when the thread is started, but it does not register the thread in the thread scheduler.

Option B) construct() - This option is incorrect because there is no specific method named construct() for registering a thread in a thread scheduler.

Option C) start() - This option is correct because the start() method is used to register a thread in a thread scheduler. When the start() method is called, the thread is added to the thread scheduler's queue and will be executed by the scheduler when its turn comes.

Option D) register() - This option is incorrect because there is no specific method named register() for registering a thread in a thread scheduler.

The correct answer is C) start(). This option is correct because the start() method is used to register a thread in a thread scheduler.

Which of the following will not directly cause a thread to stop?

  1. notify()

  2. wait()

  3. InputStream access

  4. sleep()


Correct Option: A