WorksheetsMock Test Java - 3rd Quarter
Total questions: 50
Worksheet time: 25mins
Which GUI design principle should be followed for a good user experience?
Use as many colors as possible
Keep layouts simple and consistent
Place buttons randomly on the screen
Use small, unreadable fonts
You are asked to create a registration form in Java Swing. Which components should you use?
JLabel, JTextField, JButton
JPanel, JFrame, JLabel
JTextArea, JList, JButton
JTable, JMenuBar, JPasswordField
How can you validate user input in a JTextField before submission?
Use ActionListener and getText()
Use JLabel to store the input
Use JPanel for validation
Use JMenuBar
What happens if you call setVisible(false) on a JFrame?
It hides the window but keeps the program running
It closes the program
It disposes of the JFrame
It restarts the JFrame
What is the difference between JTextField and JTextArea?
JTextField is single-line, JTextArea supports multiple lines
JTextField is editable, JTextArea is not
JTextField stores numbers only
JTextArea is only used in JPanel
You need to create a menu bar with menu items. What should you use?
JToolBar
JMenuBar
JList
JPanel
You want to group multiple GUI components together inside a JFrame. What should you use?
JToolBar
JMenuBar
JTextArea
JPanel
You need a password input field where text is hidden. What component should you use?
JTextField
JPasswordField
JLabel
JButton
You need to create a button that displays a message when clicked. Which component should you use?
JTextField
JPasswordField
JLabel
JButton
You are asked to create a login form with username and password fields, and a submit button. Which of the following is the best approach?
Use JTextField for username, JPasswordField for password, and JButton for submit
Use JLabel for username, JLabel for password, and JButton for submit
Use JTextField for both username and password
Use JCheckBox for password visibility
You want to display a text message inside a JFrame. Which component should you use?
JTextField
JButton
JLabel
JCheckBox
Which method is required to detect a button click in Java Swing?
actionPerformed()
keyPressed()
onClick()
keyTyped()
You are building a JFrame application where the user selects options from a drop-down list. Which component should you use?
JComboBox
JButton
JLabel
JCheckBox
Which method should be used to center a JFrame on the screen?
setLocationRelativeTo(null);
setBoundsToCenter();
setFrameCenter();
setDefaultLocation();
What is the purpose of the getText() and setText() methods in Swing components like JTextField or JLabel?
getText() is used to set the text of a component, and setText() is used to get the text
getText() retrieves the text entered by the user, and setText() sets the text to be displayed
getText() clears the text in a component, and setText() initializes it
getText() converts text to uppercase, and setText() sets the text format
What JDBC method is used to execute an INSERT, UPDATE, or DELETE statement?
execute()
executeUpdate()
executeQuery()
executeStatement()
Which of the following JDBC interfaces helps retrieve the result of a query?
Statement
ResultSet
Connection
DriverManager
Which JDBC method is used to execute a SELECT query?
execute()
executeUpdate()
executeQuery()
executeStatement()
What is the correct SQL statement to delete only one record where id=10?
DELETE employees WHERE id = 10;
DELETE FROM employees WHERE id = 10;
REMOVE FROM employees WHERE id = 10;
DELETE RECORD FROM employees WHERE id = 10;
How do you update a student's age to 23 where id=5?
CHANGE students SET age = 23 WHERE id = 5;
UPDATE students SET age = 23 WHERE id = 5;
MODIFY students SET age = 23 WHERE id = 5;
SET age = 23 ON students WHERE id = 5;
Which of the following needs to be imported for Java JDBC connectivity JFrame project from netbeans?
javax.sql.*;
Java.sql.*;
Javac.sql.*;
Java.sql;
Which SQL command is used to INSERT a new record in a table?
ADD INTO
INSERT INTO
UPDATE
MODIFY
What is the correct SQL query to insert a new student record (John, Doe, 22) into a students table with columns (first_name, last_name, age)?
INSERT students VALUES ('John', 'Doe', 22);
INSERT INTO students (first_name, last_name, age) VALUES ('John', 'Doe', 22);
ADD INTO students VALUES ('John', 'Doe', 22);
SAVE ('John', 'Doe', 22) INTO students;
What is the correct SQL statement to retrieve all records from a table named students?
GET ALL FROM students;
SELECT * FROM students;
FETCH * FROM students;
SHOW students;
What is it called when a program is written to respond to the button clicks, menu selections, and other actions of the user in whatever order the user does them?
Event-driven
Action-driven
User-driven
Mouse-driven
What is the correct order of establishing a JDBC connection?
Register driver → Create Statement → Establish Connection → Execute Query
Register driver → Establish Connection → Create Statement → Execute Query
Establish Connection → Register driver → Execute Query → Create Statement
Create Statement → Establish Connection → Register driver → Execute Query
What is ResultSet in JDBC?
A table stored in the database
A function that executes queries
An object that stores the result of an SQL query
A connection object
Which of the following sets the frame to 300 pixels wide by 200 high?
frm.setSize(300, 200);
frm.paint(300, 200);
frm.setSize(200, 300);
frm.setVisible(300, 200);
What is the purpose of the DriverManager class in JDBC?
Executes SQL queries
Manages database connections
Represents a result set
Handles database transactions
Explain the role of the Connection interface in JDBC.
Manages database connections
Represents a result set
Provides information about the database schema
Executes SQL queries
When using ResultSet, which method would you use to retrieve a specific column value of the current row?
getString()
getColumn()
getData()
getRowData()
Describe the function of the Statement interface in JDBC.
Manages database connections
Represents a result set
Executes SQL queries
Provides information about the database schema
What is the significance of the ResultSet interface in JDBC?
Executes SQL queries
Manages database connections
Represents a result set
Provides information about the database schema
How can you handle exceptions in JDBC?
Using try-catch blocks
Using finally blocks only
Ignoring exceptions
Exceptions are not possible in JDBC
In NetBeans IDE, which of the following is the correct way to execute an SQL query using Statement?
Connection conn = DriverManager.getConnection(...); Statement stmt = conn.createStatement(); stmt.execute("SELECT * FROM users");
Statement stmt = new Statement(); stmt.execute("SELECT * FROM users");
Connection conn = new Connection(); conn.execute("SELECT * FROM users");
Statement stmt = new Statement(); stmt.runQuery("SELECT * FROM users");
In NetBeans, where can you find and add external libraries, such as the JDBC driver for MySQL to your Java project?
In the "Libraries" section of the "Projects" window
In the "Run" menu
In the "Properties" of the database
In the "Edit Configuration" window
In NetBeans, when running a Java application with SQL queries, where do you view any errors or output generated by your program?
In the "Output" window
In the "Projects" window
In the "Database Explorer" tab
In the "Services" tab
What happens if you execute an INSERT INTO SQL statement without using executeUpdate() in a Java program within NetBeans?
The INSERT will fail silently
The query will be ignored
The query will throw a compile-time error
The query will not affect the database
Which method would you use to execute a SELECT query using PreparedStatement?
preparedStatement.executeUpdate();
preparedStatement.runQuery();
preparedStatement.executeQuery();
preparedStatement.execute();
Which method is used to change the text the label displays?
changeText
setText
changeLabel
setLabel
What is the purpose of using Class.forName() in a Java JDBC program?
It loads the JDBC driver class into memory for database connectivity
It establishes a connection to the database
It compiles the SQL query
It retrieves the ResultSet from the database
Which of the following is the correct way to load the MySQL JDBC driver using Class.forName()?
Class.forName("com.mysql.jdbc.Driver");
Class.forName("jdbc:mysql.Driver");
Class.load("com.mysql.jdbc.Driver");
DriverManager.load("com.mysql.jdbc.Driver");
Which of the following is used to retrieve data from a SQL database?
INSERT
UPDATE
DELETE
SELECT
Which of the following is used to establish a connection to a database using JDBC?
Connection.createStatement()
Connection.prepareStatement()
DriverManager.getConnection()
Statement.executeQuery()
How do you set a parameter in a PreparedStatement for a String value?
preparedStatement.setString(1, "John Doe");
preparedStatement.addString("John Doe", 1);
preparedStatement.setParam("John Doe", 1);
preparedStatement.addParameter(1, "John Doe");
What does the ResultSet.next() method do in Java JDBC?
It retrieves the previous row from the result set
It moves the cursor to the next row in the result set and returns true if there is a next row
It fetches all rows from the result set at once
It closes the result set after retrieving the first row
In the statement Connection con = DriverManager.getConnection(driver, un, pw); what do driver, un, and pw represent?
driver is the database URL, un is the username, and pw is the password
driver is the JDBC driver class, un is the database URL, and pw is the username
driver is the JDBC driver class, un is the username, and pw is the password
driver is the connection string, un is the database name, and pw is the password
In which scenarios might a SQLException be thrown?
When trying to execute an invalid SQL query
When the database connection is not available
When the provided credentials for database login are incorrect
All of the above
What does the Class.forName("MyClass") do?
Loads the class MyClass.
Execute any static block code of MyClass.
Returns an instance of MyClass.
All of the above.
What type of value is defined by pst.setString(1, id);?
String
Integer
Double
Date
