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.

Which constructs an anonymous inner class instance?

  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.

  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
  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