Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Mock Test Java - 3rd Quarter

Total questions: 50

Worksheet time: 25mins

Name
Class
Date
1.

Which GUI design principle should be followed for a good user experience?

a)

Use as many colors as possible

b)

Keep layouts simple and consistent

c)

Place buttons randomly on the screen

d)

Use small, unreadable fonts

2.

You are asked to create a registration form in Java Swing. Which components should you use?

a)

JLabel, JTextField, JButton

b)

JPanel, JFrame, JLabel

c)

JTextArea, JList, JButton

d)

JTable, JMenuBar, JPasswordField

3.

How can you validate user input in a JTextField before submission?

a)

Use ActionListener and getText()

b)

Use JLabel to store the input

c)

Use JPanel for validation

d)

Use JMenuBar

4.

What happens if you call setVisible(false) on a JFrame?

a)

It hides the window but keeps the program running

b)

It closes the program

c)

It disposes of the JFrame

d)

It restarts the JFrame

5.

What is the difference between JTextField and JTextArea?

a)

JTextField is single-line, JTextArea supports multiple lines

b)

JTextField is editable, JTextArea is not

c)

JTextField stores numbers only

d)

JTextArea is only used in JPanel

6.

You need to create a menu bar with menu items. What should you use?

a)

JToolBar

b)

JMenuBar

c)

JList

d)

JPanel

7.

You want to group multiple GUI components together inside a JFrame. What should you use?

a)

JToolBar

b)

JMenuBar

c)

JTextArea

d)

JPanel

8.

You need a password input field where text is hidden. What component should you use?

a)

JTextField

b)

JPasswordField

c)

JLabel

d)

JButton

9.

You need to create a button that displays a message when clicked. Which component should you use?

a)

JTextField

b)

JPasswordField

c)

JLabel

d)

JButton

10.

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?

a)

Use JTextField for username, JPasswordField for password, and JButton for submit

b)

Use JLabel for username, JLabel for password, and JButton for submit

c)

Use JTextField for both username and password

d)

Use JCheckBox for password visibility

11.

You want to display a text message inside a JFrame. Which component should you use?

a)

JTextField

b)

JButton

c)

JLabel

d)

JCheckBox

12.

Which method is required to detect a button click in Java Swing?

a)

actionPerformed()

b)

keyPressed()

c)

onClick()

d)

keyTyped()

13.

You are building a JFrame application where the user selects options from a drop-down list. Which component should you use?

a)

JComboBox

b)

JButton

c)

JLabel

d)

JCheckBox

14.

Which method should be used to center a JFrame on the screen?

a)

setLocationRelativeTo(null);

b)

setBoundsToCenter();

c)

setFrameCenter();

d)

setDefaultLocation();

15.

What is the purpose of the getText() and setText() methods in Swing components like JTextField or JLabel?

a)

getText() is used to set the text of a component, and setText() is used to get the text

b)

getText() retrieves the text entered by the user, and setText() sets the text to be displayed

c)

getText() clears the text in a component, and setText() initializes it

d)

getText() converts text to uppercase, and setText() sets the text format

16.

What JDBC method is used to execute an INSERT, UPDATE, or DELETE statement?

a)

execute()

b)

executeUpdate()

c)

executeQuery()

d)

executeStatement()

17.

Which of the following JDBC interfaces helps retrieve the result of a query?

a)

Statement

b)

ResultSet

c)

Connection

d)

DriverManager

18.

Which JDBC method is used to execute a SELECT query?

a)

execute()

b)

executeUpdate()

c)

executeQuery()

d)

executeStatement()

19.

What is the correct SQL statement to delete only one record where id=10?

a)

DELETE employees WHERE id = 10;

b)

DELETE FROM employees WHERE id = 10;

c)

REMOVE FROM employees WHERE id = 10;

d)

DELETE RECORD FROM employees WHERE id = 10;

20.

How do you update a student's age to 23 where id=5?

a)

CHANGE students SET age = 23 WHERE id = 5;

b)

UPDATE students SET age = 23 WHERE id = 5;

c)

MODIFY students SET age = 23 WHERE id = 5;

d)

SET age = 23 ON students WHERE id = 5;

21.

Which of the following needs to be imported for Java JDBC connectivity JFrame project from netbeans?

a)

javax.sql.*;

b)

Java.sql.*;

c)

Javac.sql.*;

d)

Java.sql;

22.

Which SQL command is used to INSERT a new record in a table?

a)

ADD INTO

b)

INSERT INTO

c)

UPDATE

d)

MODIFY

23.

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

a)

INSERT students VALUES ('John', 'Doe', 22);

b)

INSERT INTO students (first_name, last_name, age) VALUES ('John', 'Doe', 22);

c)

ADD INTO students VALUES ('John', 'Doe', 22);

d)

SAVE ('John', 'Doe', 22) INTO students;

24.

What is the correct SQL statement to retrieve all records from a table named students?

a)

GET ALL FROM students;

b)

SELECT * FROM students;

c)

FETCH * FROM students;

d)

SHOW students;

25.

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?

a)

Event-driven

b)

Action-driven

c)

User-driven

d)

Mouse-driven

26.

What is the correct order of establishing a JDBC connection?

a)

Register driver → Create Statement → Establish Connection → Execute Query

b)

Register driver → Establish Connection → Create Statement → Execute Query

c)

Establish Connection → Register driver → Execute Query → Create Statement

d)

Create Statement → Establish Connection → Register driver → Execute Query

27.

What is ResultSet in JDBC?

a)

A table stored in the database

b)

A function that executes queries

c)

An object that stores the result of an SQL query

d)

A connection object

28.

Which of the following sets the frame to 300 pixels wide by 200 high?

a)

frm.setSize(300, 200);

b)

frm.paint(300, 200);

c)

frm.setSize(200, 300);

d)

frm.setVisible(300, 200);

29.

What is the purpose of the DriverManager class in JDBC?

a)

Executes SQL queries

b)

Manages database connections

c)

Represents a result set

d)

Handles database transactions

30.

Explain the role of the Connection interface in JDBC.

a)

Manages database connections

b)

Represents a result set

c)

Provides information about the database schema

d)

Executes SQL queries

31.

When using ResultSet, which method would you use to retrieve a specific column value of the current row?

a)

getString()

b)

getColumn()

c)

getData()

d)

getRowData()

32.

Describe the function of the Statement interface in JDBC.

a)

Manages database connections

b)

Represents a result set

c)

Executes SQL queries

d)

Provides information about the database schema

33.

What is the significance of the ResultSet interface in JDBC?

a)

Executes SQL queries

b)

Manages database connections

c)

Represents a result set

d)

Provides information about the database schema

34.

How can you handle exceptions in JDBC?

a)

Using try-catch blocks

b)

Using finally blocks only

c)

Ignoring exceptions

d)

Exceptions are not possible in JDBC

35.

In NetBeans IDE, which of the following is the correct way to execute an SQL query using Statement?

a)

Connection conn = DriverManager.getConnection(...); Statement stmt = conn.createStatement(); stmt.execute("SELECT * FROM users");

b)

Statement stmt = new Statement(); stmt.execute("SELECT * FROM users");

c)

Connection conn = new Connection(); conn.execute("SELECT * FROM users");

d)

Statement stmt = new Statement(); stmt.runQuery("SELECT * FROM users");

36.

In NetBeans, where can you find and add external libraries, such as the JDBC driver for MySQL to your Java project?

a)

In the "Libraries" section of the "Projects" window

b)

In the "Run" menu

c)

In the "Properties" of the database

d)

In the "Edit Configuration" window

37.

In NetBeans, when running a Java application with SQL queries, where do you view any errors or output generated by your program?

a)

In the "Output" window

b)

In the "Projects" window

c)

In the "Database Explorer" tab

d)

In the "Services" tab

38.

What happens if you execute an INSERT INTO SQL statement without using executeUpdate() in a Java program within NetBeans?

a)

The INSERT will fail silently

b)

The query will be ignored

c)

The query will throw a compile-time error

d)

The query will not affect the database

39.

Which method would you use to execute a SELECT query using PreparedStatement?

a)

preparedStatement.executeUpdate();

b)

preparedStatement.runQuery();

c)

preparedStatement.executeQuery();

d)

preparedStatement.execute();

40.

Which method is used to change the text the label displays?

a)

changeText

b)

setText

c)

changeLabel

d)

setLabel

41.

What is the purpose of using Class.forName() in a Java JDBC program?

a)

It loads the JDBC driver class into memory for database connectivity

b)

It establishes a connection to the database

c)

It compiles the SQL query

d)

It retrieves the ResultSet from the database

42.

Which of the following is the correct way to load the MySQL JDBC driver using Class.forName()?

a)

Class.forName("com.mysql.jdbc.Driver");

b)

Class.forName("jdbc:mysql.Driver");

c)

Class.load("com.mysql.jdbc.Driver");

d)

DriverManager.load("com.mysql.jdbc.Driver");

43.

Which of the following is used to retrieve data from a SQL database?

a)

INSERT

b)

UPDATE

c)

DELETE

d)

SELECT

44.

Which of the following is used to establish a connection to a database using JDBC?

a)

Connection.createStatement()

b)

Connection.prepareStatement()

c)

DriverManager.getConnection()

d)

Statement.executeQuery()

45.

How do you set a parameter in a PreparedStatement for a String value?

a)

preparedStatement.setString(1, "John Doe");

b)

preparedStatement.addString("John Doe", 1);

c)

preparedStatement.setParam("John Doe", 1);

d)

preparedStatement.addParameter(1, "John Doe");

46.

What does the ResultSet.next() method do in Java JDBC?

a)

It retrieves the previous row from the result set

b)

It moves the cursor to the next row in the result set and returns true if there is a next row

c)

It fetches all rows from the result set at once

d)

It closes the result set after retrieving the first row

47.

In the statement Connection con = DriverManager.getConnection(driver, un, pw); what do driver, un, and pw represent?

a)

driver is the database URL, un is the username, and pw is the password

b)

driver is the JDBC driver class, un is the database URL, and pw is the username

c)

driver is the JDBC driver class, un is the username, and pw is the password

d)

driver is the connection string, un is the database name, and pw is the password

48.

In which scenarios might a SQLException be thrown?

a)

When trying to execute an invalid SQL query

b)

When the database connection is not available

c)

When the provided credentials for database login are incorrect

d)

All of the above

49.

What does the Class.forName("MyClass") do?

a)

Loads the class MyClass.

b)

Execute any static block code of MyClass.

c)

Returns an instance of MyClass.

d)

All of the above.

50.

What type of value is defined by pst.setString(1, id);?

a)

String

b)

Integer

c)

Double

d)

Date