wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java & DA -FIN

Total questions: 111

Worksheet time: 2hrs 37mins

Name
Class
Date
1.

What will be the output of the following code snippet?

(a)  

2.

Identify the error in the code snippet below:
Missing (a)   at the end of the statement.

3.

What is the output of the following code?

(a)  

4.

What is the error in this code snippet?
The if condition uses (a)   (=) instead of equality (==).

5.

To exit a loop immediately, you can use the (a)   statement.

6.

What is the default value of an uninitialized boolean variable in Java?

a)

Null

b)

False

c)

True

d)

0

7.

What is printed by the following code?

(a)  

8.

Which of the following is a valid way to declare an array in Java?

a)

int [ ] arr;

b)

int arr [ ];

c)

Both A and B

d)

Neither

9.

What does this code print?

(a)  

10.

The main method must be declared as public static void main(String[] args) in Java.

a)

True

b)

False

11.

What is wrong with this code segment?

4 lines
12.

What error does the following code contain?

4 lines
13.

What is the result of this code?

a)

3

b)

3.33

c)

0

d)

Error

14.

Which Swing component is used for selecting multiple options?

a)

JRadioButton

b)

JCheckBox

c)

JComboBox

d)

JList

15.

What layout manager arranges components in a grid of rows and columns?

a)

FlowLayout

b)

GridLayout

c)

BorderLayout

d)

GridBagLayout

16.

This code should calculate the sum of two numbers but doesn’t compile. What’s wrong?

4 lines
17.

This code is supposed to display a frame with a button, but the button doesn’t appear. Why?

a)

Missing frame.setVisible(true); to display the frame.

b)

Missing button .add(new JButton)

c)

Missing Variables

d)

Missing Event

18.

To handle a button click, you add an A_t__nL_____er to the button.

(a)  

19.

What is the output of the following code?

(a)  

20.

What is the output?

(a)  

21.

This code is supposed to print numbers from 1 to 5 but runs infinitely. What’s the error?

4 lines
22.

This code doesn’t compile. What’s wrong?
What is the Correct Code?
Write the Corrct code.

4 lines
23.

What is the error in this code?

4 lines
24.

Which package must you import to use Swing components?

a)

java.awt.*

b)

javax.swing.*

c)

java.util.*

d)

java.swing.*

25.

Which method sets the text of a JLabel?

a)

label.setLabel("text")

b)

label.changeText("text")

c)

label.setText("text")

d)

label.setString("text")

26.

JFrame is a top-level container used in Swing to host components.

a)

True

b)

False

27.

The Swing component for single-line user text input is called JT (a)   .

28.

Which Swing component is commonly used to let the user choose one option from a set?

a)

JCheckBox

b)

JRadioButton

c)

JButton

d)

JTextField

29.

A JCheckBox can be used for toggling options on and off.

a)

True

b)

False

30.

Which container is commonly used to organize Swing components using a grid?

a)

JPanel with GridLayout

b)

JFrame with CardLayout

c)

JDialog with BoxLayout

d)

JPanel with FlowLayout

31.

What is the output of the following code?

(a)  

32.

Which method is used to make a JFrame visible?

b)

frame.setVisible(true)

c)

frame.display()

d)

frame.enable()

33.

In Swing, components are added to containers using the add() method.

a)

True

b)

False

34.

To group multiple JRadioButtons so that only one can be selected at a time, you use a (a)   .

35.

Given the code below, what label text is displayed when the button is clicked?

(a)  

36.

The method used to add a component to a container is (a)   ()

37.

What is printed by this code snippet?

(a)  

38.

Which method in Swing is used to set the background color of a component?

a)

setColor()

b)

setBackground()

c)

changeBackground()

d)

bgColor()

39.

Identify the issue in this code for creating a JCheckBox:
Explain

4 lines
40.

Find the error in the following snippet for adding a component to a panel:
Write the Correct Code

4 lines
41.

Which Swing component is best suited for entering a single line of text?

a)

JTextArea

b)

JTextField

c)

JLabel

d)

JComboBox

42.

What happens when you call setEnabled(false) on a JButton?

a)

The button becomes invisible

b)

The button cannot be clicked

c)

The button’s text changes

d)

The button moves to a new position

43.

Which method retrieves the text from a JTextField?

a)

getText()

b)

setText()

c)

getValue()

d)

retrieveText()

44.

What is the purpose of a ButtonGroup with JRadioButtons?

a)

To align the buttons visually

b)

To ensure only one button is selected at a time

c)

To enable multiple selections

d)

To change the button’s appearance

45.

Which component displays non-editable text?

a)

JTextField

b)

JLabel

c)

JCheckBox

d)

JButton

46.

Which component allows a user to toggle between two states?

a)

JRadioButton

b)

JLabel

c)

JCheckBox

d)

JButton

47.

Which method changes the text displayed on a JButton?

a)

changeText()

b)

setLabel()

c)

setText()

d)

updateText()

48.

What does a JRadioButton require to function as a mutually exclusive option?

a)

JPanel

b)

ButtonGroup

c)

JFrame

d)

JLabel

49.

What does a JRadioButton require to function as a mutually exclusive option?

a)

JPanel

b)

ButtonGroup

c)

JFrame

d)

JLabel

50.

Why doesn’t this ActionListener print anything when the button is clicked?

4 lines
51.

JTextField can display multiple lines of text.

a)

True

b)

False

52.

JRadioButtons in a ButtonGroup allow only one selection.

a)

True

b)

False

53.

JButton can trigger an event when clicked.

a)

True

b)

False

54.

JCheckBox supports multiple selections by default.

a)

True

b)

False

55.

JFrame is invisible until setVisible(true) is called.

a)

True

b)

False

56.

What is the default value of elements in an integer array in Java?

a)

1

b)

0

c)

Null

d)

Undefined

57.

In Java, an array's size can be changed after initialization.

a)

True

b)

False

58.

To declare an array of 5 integers, you write: int[] numbers = new (a)   ;

59.

The length of an array named arr is accessed using arr. (a)   .

60.

Which loop is best suited for iterating through an array?

a)

if

b)

while

c)

for

d)

switch

61.

How do you declare a 3x3 integer matrix in Java?

a)

int matrix = new int[3,3];

b)

int[][] matrix = new int[3][3];

c)

int matrix[][] = int[3][3];

d)

int matrix = int[3][3];

62.

What is the Output?

(a)  

63.

In Java, int[][] nums = new int[2][3]; creates a matrix with 2 rows and 3 columns.

a)

True

b)

False

64.

What is the correct way to assign 5 to the element in row 1 column 2 of a 2D

a)

mat[1,2] = 5;

b)

mat[2][1] = 5;

c)

mat[1][2] = 5;

d)

mat[1.2] = 5;

65.

What is the result of the following code?

a)

0

b)

2

c)

3

d)

Compilation error

66.

In Java, two-dimensional arrays can have rows with different column sizes.

a)

True

b)

False

67.

How do you declare a single-dimensional array of integers in Java?

a)

int array[] = new int;

b)

int[] array = new int[5];

c)

int array = new int[5];

d)

int[] array = new int;

68.

How do you declare a 2D array with 3 rows and 4 columns?

a)

int[][] array = new int[3][4];

b)

int[][] array = new int[4][3];

c)

int[] array = new int[3][4];

d)

int array[][] = new int[3, 4];

69.

Which of the following correctly accesses the element in the second row, third column of a 2D array matrix?

a)

matrix[1][2]

b)

matrix[2][3]

c)

matrix[2][1]

d)

matrix[1][3]

70.

What does the length property return for a single-dimensional array int[] arr = new int[5];?

a)

The number of rows

b)

The number of columns

c)

The total number of elements (5)

d)

The size of each element

71.

You can initialize a single-dimensional array using int[] arr = {1, 2, 3};.

a)

True

b)

False

72.

To access the number of rows in a 2D array matrix, use matrix. (a)   .

73.

What is the output of this code?

(a)  

74.

What does this code print?

(a)  

75.

What does this code print?

(a)  

76.

What does this code print?

(a)  

77.

What is the output?

(a)  

78.

What is the output?

a)

10

b)

20

c)

4

d)

1, 2, 3, 4

79.

What is the output?

a)

1

b)

2

c)

3

d)

4

80.

What is the output of this code?

a)

2

b)

4

c)

6

d)

8

e)

Error

81.

Which layout manager arranges components in a single row or column?

a)

FlowLayout

b)

GridLayout

c)

BorderLayout

d)

BoxLayout

82.

What method is used to add an ActionListener to a JButton?

a)

registerAction()

b)

setActionListener()

c)

attachListener()

d)

addActionListener()

83.

Which component is used to display a list of items for selection?

a)

JCheckBox

b)

JTextField

c)

JList

d)

JComboBox

84.

What is the purpose of the setPreferredSize() method in a Swing component?

a)

To set the minimum size of the component

b)

To suggest a preferred size for the component

c)

To automatically resize the component

d)

To enforce a fixed size for the component

85.

Which layout manager arranges components in a single row or column?

a)

FlowLayout

b)

BoxLayout

c)

BorderLayout

d)

GridLayout

86.

What method is used to add an ActionListener to a JButton?

a)

addActionListener()

b)

attachListener()

c)

setActionListener()

d)

registerListener()

87.

What layout manager arranges components in a single row?

a)

FlowLayout

b)

BorderLayout

c)

GridLayout

d)

BoxLayout

88.

Which method is used to add an ActionListener to a JButton?

a)

addActionListener()

b)

setActionListener()

c)

registerActionListener()

d)

linkActionListener()

89.

Which component is used to create a drop-down list in Swing?

a)

JComboBox

b)

JList

c)

JTextField

d)

JCheckBox

90.

Which method is used to add a component to a JPanel?

a)

append()

b)

insert()

c)

add()

d)

addComponent()

91.

Which of the following is the top-level container commonly used to create a standard application window?

a)

JPanel

b)

JFrame

c)

JDialog

d)

JApplet

92.

What is the default layout manager for a JPanel?

a)

BorderLayout

b)

GridLayout

c)

FlowLayout

d)

BoxLayout

93.

Which interface must be implemented to handle a button click event?

a)

ItemListener

b)

ActionListener

c)

MouseListener

d)

WindowListener

94.

Which method is called to make a top-level container like a JFrame visible on the screen?

a)

display()

b)

show()

c)

setVisible(true)

d)

paint()

95.

The (a)   method is used to add a component (like a button or label) to a container.

96.

In which package are most of the core Swing components located?

a)

java.awt

b)

java.util

c)

javax.swing

d)

java.swing

97.

Which method is used to set the size of a JFrame?

a)

setSize(width, height)

b)

setDimensions(width, height)

c)

setFrameSize(width, height)

d)

resize(width, height)

98.

To detect when a user selects an item from a dropdown list, you would add a _______________ or _______________ to a JComboBox.

(a)  

99.

When creating multiple similar components in a loop, which data structure is most appropriate to store references to them for later access?

a)

String

b)

Array or ArrayList

c)

int

d)

boolean

100.

What would be the output of this code snippet?

a)

One button labeled "Button 3"

b)

Three buttons labeled "Button 0", "Button 1", "Button 2"

c)

Three buttons all labeled "Button i"

d)

No buttons are created

101.

What will happen when this code runs?

a)

4 buttons are added to the panel

b)

5 buttons are added to the panel

c)

6 buttons are added to the panel

d)

A NullPointerException occurs

102.

What is the value of i when this loop finishes executing?

a)

9

b)

10

c)

11

d)

undefined

103.

What does this code accomplish?

a)

Creates 8 text fields and adds them all to the panel

b)

Creates 4 text fields and adds them to the panel

c)

Creates text fields at even indexes only (0,2,4,6)

d)

Creates text fields at odd indexes only (1,3,5,7)

104.

What is the output of this code?

a)

0 1 2 3 4

b)

1 2 3 4 5

c)

1 2 3 4

d)

2 3 4 5

105.

What does this loop calculate?

a)

The average of the array elements

b)

The product of the array elements

c)

The sum of the array elements

d)

The maximum value in the array

106.

How many times will this loop execute?

a)

4 times

b)

5 times

c)

6 times

d)

Infinite loop

107.

What is the value of arr[2] after this code executes?

a)

0

b)

2

c)

4

d)

6

108.

Which loop is guaranteed to execute at least once?

a)

for loop

b)

while loop

c)

do-while loop

d)

Enhanced for loop

109.

The following code reverses an array. Fill in the missing condition:

(a)  

110.

Complete the Code
The following method counts how many times a value appears in an array. Fill in the blank:

(a)  

111.

What does this code output?

(a)