0% found this document useful (0 votes)
61 views

Assignment Solution 9

The document contains an assignment for a Java programming course with 10 multiple-choice questions focused on Java Swing components and their functionalities. Each question includes the correct answer and a detailed solution explaining the reasoning behind it. The topics covered include components like JComboBox, JButton, JLabel, and layout managers such as FlowLayout and BoxLayout.

Uploaded by

Haf hafeefa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Assignment Solution 9

The document contains an assignment for a Java programming course with 10 multiple-choice questions focused on Java Swing components and their functionalities. Each question includes the correct answer and a detailed solution explaining the reasoning behind it. The topics covered include components like JComboBox, JButton, JLabel, and layout managers such as FlowLayout and BoxLayout.

Uploaded by

Haf hafeefa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

NPTEL Online Certification Courses

Indian Institute of Technology Kharagpur


NOC25-CS57 (JAN-2025 25S)

PROGRAMMING IN JAVA
Assignment X
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10

QUESTION 1:

Which Swing component is best suited for displaying a drop-down list of selectable options?

a. JButton

b. JComboBox

c. JTextField

d. JPanel

Correct Answer:

b. JComboBox

Detailed Solution:

JComboBox is a Swing component that provides a drop-down list from which users can select one
option.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 2:

What will be the output of the following Java code?

import javax.swing.*;
import java.awt.*;

public class SwingExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Example");
frame.setLayout(new FlowLayout());
frame.add(new JButton("Button 1"));
frame.add(new JButton("Button 2"));
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

a. A frame with two buttons labeled “Button 1” and “Button 2”.

b. A frame with only one button labeled “Button 2”.

c. Compilation Error.

d. Runtime Error.

Correct Answer:

a. A frame with two buttons labeled “Button 1” and “Button 2”.

Detailed Solution:

The FlowLayout layout manager arranges components in a row. Here, both buttons are added and
displayed in the frame.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 3:

Which of the following is true about the JLabel component in Java Swing?

a. It is used only for displaying text.

b. It can display text and icons.

c. It cannot be added to a JPanel.

d. It generates mouse events by default.

Correct Answer:

b. It can display text and icons.

Detailed Solution:

JLabel can display both text and images/icons. It is commonly used for non-interactive purposes in
Swing GUIs.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 4:

Which method is used to handle mouse click events in Java Swing?

a. mouseClicked()

b. keyPressed()

c. actionPerformed()

d. componentShown()

Correct Answer:

a. mouseClicked()

Detailed Solution:

The mouseClicked() method in the MouseListener interface is used to handle mouse click
events in Java Swing.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 5:

What should replace // INSERT CODE HERE to create a JFrame with a JButton labeled
"Click Me"?

import javax.swing.*;

public class FrameExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Demo Frame");
JButton button = new JButton("Click Me");
// INSERT CODE HERE
frame.setSize(300, 200);
frame.setVisible(true);
}
}

a. frame.add(button);

b. frame.insert(button);

c. frame.append(button);

d. frame.push(button);

Correct Answer:

a. frame.add(button);

Detailed Solution:

The add() method is used to add components like buttons, text fields, or panels to a JFrame.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 6:

Identify and correct the error in the following program:

import javax.swing.*;
import java.awt.*;

public class PanelExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Panel Example");
JPanel panel = new JPanel();
JButton button = new JButton("Submit");
panel.setFlowLayout(); // ERROR
panel.add(button);
frame.add(panel);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
What should the erroneous line (panel.setFlowLayout();) be replaced with?

a. panel.setLayout(new GridLayout());

b. panel.addFlowLayout();

c. panel.appendLayout(new FlowLayout());

d. panel.setLayout(new FlowLayout());

Correct Answer:

d. panel.setLayout(new FlowLayout());

Detailed Solution:

The setLayout() method is used to define the layout manager for a panel. FlowLayout is the default
layout for JPanel.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 7:

What will the following Java program output?

import javax.swing.*;

public class LabelExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Label Demo");
JLabel label = new JLabel("Welcome to Swing");
frame.setSize(250, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

a. A frame with the label "Welcome to Swing".

b. A frame with no visible label.

c. Compilation Error.

d. Runtime Error.

Correct Answer:

b. A frame with no visible label.

Detailed Solution:

The JLabel has to be added to the frame using add(). The frame is visible, but the label is not
displayed as it has not been added.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 8:

What does the following code do?

import javax.swing.*;

public class NPTEL extends JFrame {


JButton button;

public NPTEL() {
button = new JButton("Programming in Java");
add(button);
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) {


new NPTEL();
}
}

a. Creates a JFrame with a JButton labeled "Programming in Java"

b. Compiles with errors

c. Displays a message dialog

d. Creates a JPanel with a JButton labeled "Programming in Java"

Correct Answer:

a. Creates a JFrame with a JButton labeled "Programming in Java"

Detailed Solution:

The code extends JFrame and uses the JButton class object to create a button with the name of
“Programming in Java”.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 9:

What happens when the button in this Java code snippet is clicked?

import javax.swing.*;
import java.awt.event.*;
public class NPTEL {
public static void main(String[] args) {
JFrame frame = new JFrame("NPTEL Java Course");
JButton button = new JButton("Click Me");
button.setBounds(50, 100, 100, 40);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Welcome to the course");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}

a. The program exits

b. A message dialog with the text "Welcome to the course" is displayed

c. The button label changes to "Welcome to the course"

d. Nothing happens

Correct Answer:

b. A message dialog with the text "Welcome to the course" is displayed

Detailed Solution:

The code creates a button with label “Click Me” and in the frame titled “NPTEL Java Course”. A action
listener is defined that opens a new message dialog with the text “Welcome to the course” when the
button is clicked.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 10:

The container displays a number of components in a column, with extra space going between the first
two components.

Which of the following layout manager(s) most naturally suited for the described layout?

a. BoxLayout

b. FlowLayout

c. BorderLayout

d. GridLayout

Correct Answer:

a. BoxLayout

Detailed Solution:

BoxLayout lays out components in either a column or a row. You can specify extra space using an
invisible component.

You might also like