Font size
WorksheetsJava & DA -FIN
Total questions: 111
Worksheet time: 2hrs 37mins
What will be the output of the following code snippet?
(a)
Identify the error in the code snippet below:
Missing (a) at the end of the statement.
What is the output of the following code?
(a)
What is the error in this code snippet?
The if condition uses (a) (=) instead of equality (==).
To exit a loop immediately, you can use the (a) statement.
What is the default value of an uninitialized boolean variable in Java?
Null
False
True
0
What is printed by the following code?
(a)
Which of the following is a valid way to declare an array in Java?
int [ ] arr;
int arr [ ];
Both A and B
Neither
What does this code print?
(a)
The main method must be declared as public static void main(String[] args) in Java.
True
False
What is wrong with this code segment?
What error does the following code contain?
What is the result of this code?
3
3.33
0
Error
Which Swing component is used for selecting multiple options?
JRadioButton
JCheckBox
JComboBox
JList
What layout manager arranges components in a grid of rows and columns?
FlowLayout
GridLayout
BorderLayout
GridBagLayout
This code should calculate the sum of two numbers but doesn’t compile. What’s wrong?
This code is supposed to display a frame with a button, but the button doesn’t appear. Why?
Missing frame.setVisible(true); to display the frame.
Missing button .add(new JButton)
Missing Variables
Missing Event
To handle a button click, you add an A_t__nL_____er to the button.
(a)
What is the output of the following code?
(a)
What is the output?
(a)
This code is supposed to print numbers from 1 to 5 but runs infinitely. What’s the error?
This code doesn’t compile. What’s wrong?
What is the Correct Code?
Write the Corrct code.
What is the error in this code?
Which package must you import to use Swing components?
java.awt.*
javax.swing.*
java.util.*
java.swing.*
Which method sets the text of a JLabel?
label.setLabel("text")
label.changeText("text")
label.setText("text")
label.setString("text")
JFrame is a top-level container used in Swing to host components.
True
False
The Swing component for single-line user text input is called JT (a) .
Which Swing component is commonly used to let the user choose one option from a set?
JCheckBox
JRadioButton
JButton
JTextField
A JCheckBox can be used for toggling options on and off.
True
False
Which container is commonly used to organize Swing components using a grid?
JPanel with GridLayout
JFrame with CardLayout
JDialog with BoxLayout
JPanel with FlowLayout
What is the output of the following code?
(a)
Which method is used to make a JFrame visible?
In Swing, components are added to containers using the add() method.
True
False
To group multiple JRadioButtons so that only one can be selected at a time, you use a (a) .
Given the code below, what label text is displayed when the button is clicked?
(a)
The method used to add a component to a container is (a) ()
What is printed by this code snippet?
(a)
Which method in Swing is used to set the background color of a component?
setColor()
setBackground()
changeBackground()
bgColor()
Identify the issue in this code for creating a JCheckBox:
Explain
Find the error in the following snippet for adding a component to a panel:
Write the Correct Code
Which Swing component is best suited for entering a single line of text?
JTextArea
JTextField
JLabel
JComboBox
What happens when you call setEnabled(false) on a JButton?
The button becomes invisible
The button cannot be clicked
The button’s text changes
The button moves to a new position
Which method retrieves the text from a JTextField?
getText()
setText()
getValue()
retrieveText()
What is the purpose of a ButtonGroup with JRadioButtons?
To align the buttons visually
To ensure only one button is selected at a time
To enable multiple selections
To change the button’s appearance
Which component displays non-editable text?
JTextField
JLabel
JCheckBox
JButton
Which component allows a user to toggle between two states?
JRadioButton
JLabel
JCheckBox
JButton
Which method changes the text displayed on a JButton?
changeText()
setLabel()
setText()
updateText()
What does a JRadioButton require to function as a mutually exclusive option?
JPanel
ButtonGroup
JFrame
JLabel
What does a JRadioButton require to function as a mutually exclusive option?
JPanel
ButtonGroup
JFrame
JLabel
Why doesn’t this ActionListener print anything when the button is clicked?
JTextField can display multiple lines of text.
True
False
JRadioButtons in a ButtonGroup allow only one selection.
True
False
JButton can trigger an event when clicked.
True
False
JCheckBox supports multiple selections by default.
True
False
JFrame is invisible until setVisible(true) is called.
True
False
What is the default value of elements in an integer array in Java?
1
0
Null
Undefined
In Java, an array's size can be changed after initialization.
True
False
To declare an array of 5 integers, you write: int[] numbers = new (a) ;
The length of an array named arr is accessed using arr. (a) .
Which loop is best suited for iterating through an array?
if
while
for
switch
How do you declare a 3x3 integer matrix in Java?
int matrix = new int[3,3];
int[][] matrix = new int[3][3];
int matrix[][] = int[3][3];
int matrix = int[3][3];
What is the Output?
(a)
In Java, int[][] nums = new int[2][3]; creates a matrix with 2 rows and 3 columns.
True
False
What is the correct way to assign 5 to the element in row 1 column 2 of a 2D
mat[1,2] = 5;
mat[2][1] = 5;
mat[1][2] = 5;
mat[1.2] = 5;
What is the result of the following code?
0
2
3
Compilation error
In Java, two-dimensional arrays can have rows with different column sizes.
True
False
How do you declare a single-dimensional array of integers in Java?
int array[] = new int;
int[] array = new int[5];
int array = new int[5];
int[] array = new int;
How do you declare a 2D array with 3 rows and 4 columns?
int[][] array = new int[3][4];
int[][] array = new int[4][3];
int[] array = new int[3][4];
int array[][] = new int[3, 4];
Which of the following correctly accesses the element in the second row, third column of a 2D array matrix?
matrix[1][2]
matrix[2][3]
matrix[2][1]
matrix[1][3]
What does the length property return for a single-dimensional array int[] arr = new int[5];?
The number of rows
The number of columns
The total number of elements (5)
The size of each element
You can initialize a single-dimensional array using int[] arr = {1, 2, 3};.
True
False
To access the number of rows in a 2D array matrix, use matrix. (a) .
What is the output of this code?
(a)
What does this code print?
(a)
What does this code print?
(a)
What does this code print?
(a)
What is the output?
(a)
What is the output?
10
20
4
1, 2, 3, 4
What is the output?
1
2
3
4
What is the output of this code?
2
4
6
8
Error
Which layout manager arranges components in a single row or column?
FlowLayout
GridLayout
BorderLayout
BoxLayout
What method is used to add an ActionListener to a JButton?
registerAction()
setActionListener()
attachListener()
addActionListener()
Which component is used to display a list of items for selection?
JCheckBox
JTextField
JList
JComboBox
What is the purpose of the setPreferredSize() method in a Swing component?
To set the minimum size of the component
To suggest a preferred size for the component
To automatically resize the component
To enforce a fixed size for the component
Which layout manager arranges components in a single row or column?
FlowLayout
BoxLayout
BorderLayout
GridLayout
What method is used to add an ActionListener to a JButton?
addActionListener()
attachListener()
setActionListener()
registerListener()
What layout manager arranges components in a single row?
FlowLayout
BorderLayout
GridLayout
BoxLayout
Which method is used to add an ActionListener to a JButton?
addActionListener()
setActionListener()
registerActionListener()
linkActionListener()
Which component is used to create a drop-down list in Swing?
JComboBox
JList
JTextField
JCheckBox
Which method is used to add a component to a JPanel?
append()
insert()
add()
addComponent()
Which of the following is the top-level container commonly used to create a standard application window?
JPanel
JFrame
JDialog
JApplet
What is the default layout manager for a JPanel?
BorderLayout
GridLayout
FlowLayout
BoxLayout
Which interface must be implemented to handle a button click event?
ItemListener
ActionListener
MouseListener
WindowListener
Which method is called to make a top-level container like a JFrame visible on the screen?
display()
show()
setVisible(true)
paint()
The (a) method is used to add a component (like a button or label) to a container.
In which package are most of the core Swing components located?
java.awt
java.util
javax.swing
java.swing
Which method is used to set the size of a JFrame?
setSize(width, height)
setDimensions(width, height)
setFrameSize(width, height)
resize(width, height)
To detect when a user selects an item from a dropdown list, you would add a _______________ or _______________ to a JComboBox.
(a)
When creating multiple similar components in a loop, which data structure is most appropriate to store references to them for later access?
String
Array or ArrayList
int
boolean
What would be the output of this code snippet?
One button labeled "Button 3"
Three buttons labeled "Button 0", "Button 1", "Button 2"
Three buttons all labeled "Button i"
No buttons are created
What will happen when this code runs?
4 buttons are added to the panel
5 buttons are added to the panel
6 buttons are added to the panel
A NullPointerException occurs
What is the value of i when this loop finishes executing?
9
10
11
undefined
What does this code accomplish?
Creates 8 text fields and adds them all to the panel
Creates 4 text fields and adds them to the panel
Creates text fields at even indexes only (0,2,4,6)
Creates text fields at odd indexes only (1,3,5,7)
What is the output of this code?
0 1 2 3 4
1 2 3 4 5
1 2 3 4
2 3 4 5
What does this loop calculate?
The average of the array elements
The product of the array elements
The sum of the array elements
The maximum value in the array
How many times will this loop execute?
4 times
5 times
6 times
Infinite loop
What is the value of arr[2] after this code executes?
0
2
4
6
Which loop is guaranteed to execute at least once?
for loop
while loop
do-while loop
Enhanced for loop
The following code reverses an array. Fill in the missing condition:
(a)
Complete the Code
The following method counts how many times a value appears in an array. Fill in the blank:
(a)
What does this code output?
(a)
