Font size
WorksheetsJava Quiz I
Total questions: 17
Worksheet time: 26mins
What type of Java Swing Layout is this?
FlowLayout
BoxLayout
GridLayout
BorderLayout
What type of Java Swing Layout is this?
FlowLayout
CardLayout
GridLayout
BorderLayout
Which method is used to handle clicks on JButton?
add()
addActionListener()
setDefaultCloseOperation()
show()
What Java Swing component refers to a single-line text input field?
JTextField
JTextArea
JPanel
JLabel
What Java Swing component performs an action after the user clicks it?
JTextInput
JButton
JLabel
JPanel
Which of the following is a valid way to create a JButton in Java Swing?
JButton button = new JButton("Click Me");
JButton button = new Button("Click Me");
Button button = new JButton("Click Me");
JButton button = new Button("Click Me");
Which method is used to add a component to a container in Java Swing?
addComponent()
append()
add()
put()
Which method is used to make a JFrame visible?
setVisible(true)
showFrame()
display()
JFrame.setOn()
Which class in Java Swing is used to create a basic window?
JPanel
JFrame
JButton
JTextField
Which of the following statement is true?
JTextArea is for numbers only
JTextField supports multi-line input
JTextArea supports multi-line input
JTextField and JTextArea are used to display images
Which of the following is a wrapper class in Java?
Integer
int
System
Scanner
What will be the output of Math.round(4.6)?
4
5
4.5
Compilation Error
What does the 'Matcher' class do in Java?
Compiles regular expressions
Finds occurrences of a pattern in a string
Parses XML data
Reads user input
What will be the output of the following code?
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("abc123def456");
while (m.find()) {
System.out.print(m.group() + " ");
}
123 456
abc 123 def 456
a b c 1 2 3 d e f 4 5 6
123def456
Which of the following classes is used to define a regular expression pattern in Java?
RegEx
Scanner
Pattern
Matcher
Java provides wrapper classes for primitive data types, such as Integer, Double, and Character. Why do we need wrapper classes instead of just using primitive types?
The following code attempts to parse an integer using a wrapper class but throws a NumberFormatException. Explain what is wrong with the code below, and make an adjustment to fix it. (3 points for explanation + 2 points for fix)
String number = "12a";
Integer parsedNumber = Integer.parseInt(number);
