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.

Which of the following will directly stop the execution of a Thread?

  1. wait()

  2. notify()

  3. notifyall()

  4. exits synchronized code


Correct Option: A

AI Explanation

To answer this question, we need to understand the concepts of thread synchronization and the methods wait(), notify(), and notifyAll().

Option A) wait() - This option is correct. The wait() method is used to suspend the execution of a thread and releases the lock it holds. It allows other threads to acquire the lock and continue their execution. The thread will remain in a suspended state until another thread calls the notify() or notifyAll() method.

Option B) notify() - This option is incorrect. The notify() method is used to wake up a single thread that is waiting on the same object. It does not stop the execution of a thread, but rather notifies one waiting thread to resume execution.

Option C) notifyAll() - This option is incorrect. The notifyAll() method is used to wake up all threads that are waiting on the same object. It also does not stop the execution of a thread but rather notifies all waiting threads to resume execution.

Option D) exits synchronized code - This option is incorrect. Exiting synchronized code does not directly stop the execution of a thread. It simply releases the lock it holds and allows other threads to acquire the lock and continue their execution.

Therefore, the correct answer is option A) wait(). This option directly stops the execution of a thread by suspending it until another thread calls the notify() or notifyAll() method.

Please let me know if you need any further clarification.

  1. void run()

  2. public void run()

  3. public void start()

  4. void run(int priority)


Correct Option: B
  1. run();

  2. start();

  3. stop();

  4. main();


Correct Option: A
  1. run();

  2. construct();

  3. start();

  4. register();


Correct Option: C
  1. notify()

  2. wait()

  3. InputStream access

  4. sleep()


Correct Option: A