Analyze the following code: import javax.swing.*; public class Test extends JFrame { private JButton jbtOK = new JButton("OK"); public static void main(String[] args) { // Create a frame and set its properties JFrame frame = new Test(); frame.setTitle("Logic Error"); frame.setSize(200, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public Test() { jbtOK.setToolTipText("This is a button"); add(new JButton("OK")); } }

Analyze the following code:
import javax.swing.*;
public class Test extends JFrame  {
  private JButton jbtOK = new JButton("OK");
 
  public static void main(String[] args) {
    // Create a frame and set its properties
    JFrame frame = new Test();
    frame.setTitle("Logic Error");
    frame.setSize(200, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
  public Test() {
    jbtOK.setToolTipText("This is a button");
    add(new JButton("OK"));
  }
}



A. The tool tip text is displayed when you move the mouse on the button.
B. The tool tip text will be displayed if you replace add(new JButton("OK")) with add(jbtOK).
C. The tool tip text will be displayed if you swap the two lines in the Test constructor.
D. The tool tip text will be displayed if you replace add(new JButton("OK")) with add(jbtOK = new JButton("OK")).

The correct answer is B

Explanation: The tool tip text is not displayed, because Line 16 sets a tool tip text on jbtOK, but Line 17 adds a new button (different from jbtOK) to the content pane of the frame. Only (b) is correct. (c) is same as the original code. (d) creates a new button and assigns to jbtOK, the previous button created in Line 4 is unreferenced now.


Java

Learn More Multiple Choice Question :