wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Quiz I

Total questions: 17

Worksheet time: 26mins

Name
Class
Date
1.

What type of Java Swing Layout is this?

a)

FlowLayout

b)

BoxLayout

c)

GridLayout

d)

BorderLayout

2.

What type of Java Swing Layout is this?

a)

FlowLayout

b)

CardLayout

c)

GridLayout

d)

BorderLayout

3.

Which method is used to handle clicks on JButton?

a)

add()

b)

addActionListener()

c)

setDefaultCloseOperation()

d)

show()

4.

What Java Swing component refers to a single-line text input field?

a)

JTextField

b)

JTextArea

c)

JPanel

d)

JLabel

5.

What Java Swing component performs an action after the user clicks it?

a)

JTextInput

b)

JButton

c)

JLabel

d)

JPanel

6.

Which of the following is a valid way to create a JButton in Java Swing?

a)

JButton button = new JButton("Click Me");

b)

JButton button = new Button("Click Me");

c)

Button button = new JButton("Click Me");

d)

JButton button = new Button("Click Me");

7.

Which method is used to add a component to a container in Java Swing?

a)

addComponent()

b)

append()

c)

add()

d)

put()

8.

Which method is used to make a JFrame visible?

a)

setVisible(true)

b)

showFrame()

c)

display()

d)

JFrame.setOn()

9.

Which class in Java Swing is used to create a basic window?

a)

JPanel

b)

JFrame

c)

JButton

d)

JTextField

10.

Which of the following statement is true?

a)

JTextArea is for numbers only

b)

JTextField supports multi-line input

c)

JTextArea supports multi-line input

d)

JTextField and JTextArea are used to display images

11.

Which of the following is a wrapper class in Java?

a)

Integer

b)

int

c)

System

d)

Scanner

12.

What will be the output of Math.round(4.6)?

a)

4

b)

5

c)

4.5

d)

Compilation Error

13.

What does the 'Matcher' class do in Java?

a)

Compiles regular expressions

b)

Finds occurrences of a pattern in a string

c)

Parses XML data

d)

Reads user input

14.

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() + " ");

}

a)

123 456

b)

abc 123 def 456

c)

a b c 1 2 3 d e f 4 5 6

d)

123def456

15.

Which of the following classes is used to define a regular expression pattern in Java?

a)

RegEx

b)

Scanner

c)

Pattern

d)

Matcher

16.

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?

4 lines
17.

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

4 lines