Assignment Solution 9
Assignment Solution 9
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:
import javax.swing.*;
import java.awt.*;
c. Compilation Error.
d. Runtime Error.
Correct Answer:
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?
Correct Answer:
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:
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.*;
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:
import javax.swing.*;
import java.awt.*;
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:
import javax.swing.*;
c. Compilation Error.
d. Runtime Error.
Correct Answer:
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:
import javax.swing.*;
public NPTEL() {
button = new JButton("Programming in Java");
add(button);
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
Correct Answer:
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);
}
}
d. Nothing happens
Correct Answer:
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.