NEW
Font size
WorksheetsComputer Programming 1 (Final Examination)
Total questions: 60
Worksheet time: 30hrs 0mins
What is an array in Java?
A fixed-size collection of elements of the same type
A dynamic list that automatically resizes
A class that stores only strings
A tool for GUI design
Which of the following correctly declares a one-dimensional array?
int[] numbers();
int numbers[];
array int numbers;
int numbers();
Array indexing in Java starts at?
0
−1
Any value
1
What is the length of the array int[] a = new int[5];?
5
4
0
Depends on values
Which of the following is true about JavaFX?
It cannot handle event
It allows creating GUI applications in Java
It is used for console applications only
It is the predecessor of Swing
What is the main difference between Swing and JavaFX?
Swing is older; JavaFX supports modern GUI features
JavaFX cannot handle buttons
Swing does not allow layouts
JavaFX is command-line only
Which JavaFX component is used to display text?
JButton
Label
TextField
JPanel
To use JavaFX in your IDE, you need to:
Install JavaFX SDK and configure the IDE
Only install JDK
Install Python
Configure database connectivity
In Java, functions are called:
Functions
Classes
Procedures
Methods
Which of the following is the correct syntax to pass a parameter to a method?
methodName(int x)
methodName x
methodName {x}
methodName[x]
Which block is used to handle exceptions in Java?
try-catch
if-else
switch-case
for-loop
What type of error occurs when code runs but produces incorrect results?
Logical error
Syntax error
Runtime error
Compile-time error
Given the code fragment below, which statement correctly initializes the array so the program will run without errors?
int[] scores; scores = _____; System.out.println(scores.length);
new int(5)
new int[5]
int[5]
new array[5]
What is the output of the following code?
40
30
20
10
You need a 2D array to store the scores of 4 students in 3 subjects. Which declaration correctly creates this 2D integer array so that scores[student][subject] is valid?
int[][] arr = new int[4];
int arr[] = new int[4,3];
int[][] arr = new int[4][3];
int arr[4][3];
Which loop is most appropriate for accessing every element in an array when the number of elements is already known?
for loop
do-while loop
Any of the above
while loop
Given the 2D array below, which line correctly accesses the last element in the first row?
matrix[1][3]
matrix[2][0]
matrix[0][2]
matrix[1][2]
Which code correctly accesses the third element of the array?
scores(2);
scores.get(2);
scores[3];
scores[2];
What is wrong with this code snippet?
Accessing an invalid index
Syntax error
No error
NullPointerException
Which statement correctly prints the element "Date"?
System.out.println(fruits(3));
System.out.println(fruits.get(3));
System.out.println(fruits[3]);
System.out.println(fruits{3});
Evaluate this code:
Prints 1 2 3
Throws ArrayIndexOutOfBoundsException
Prints 3
Syntax error
What is the output of the following?
20
30
3
10
You are designing a registration form for an online event using JavaFX. Users must submit their information after filling out the form. Which JavaFX component should be used to allow this action?
Button
Label
CheckBox
TextField
You need to arrange several buttons vertically in your GUI so they appear one below the other. Which layout should you use?
GridPane
HBox
VBox
StackPane
You are designing a GUI and want to create the main application window where all your components will be displayed. Which JavaFX object represents this window?
Scene
TextField
Stage
VBox
You are designing a JavaFX application using Scene Builder. You added a Button component to your layout and ran the application, but nothing appears on the screen. To troubleshoot, you need to predict what should normally appear when a Button is correctly added and the application runs. Which of the following correctly describes the expected outcome?
A text field
A button
A window
A text label
You want the user to enter their name in your GUI application. Which JavaFX component should you use?
Label
Button
VBox
TextField
You want the user to choose their favorite color from a predefined list. Which JavaFX component is most appropriate?
HBox
Label
ComboBox
TextField
You are building a JavaFX application and have created a Stage, adding all your UI components. When you run the application, you notice that nothing appears on the screen. To identify the issue, you need to consider the main role of a Stage. Which of the following explains the Stage’s purpose in this scenario?
Close the window
Initialize components
Display the window
Hide the window
You want your JavaFX application’s window to clearly indicate its function to the user. To achieve this, you decide to set a title for the Stage. Which of the following explains the purpose of assigning a title to a Stage?
To close the window
To hide the window
To display a text/title on the top of the window
To initialize components
You want to place several UI components so that they overlap/stack each other, appearing on top of one another in the GUI. Which JavaFX container should you use?
StackPane
GridPane
VBox
HBox
In Scene Builder or JavaFX, if you want to remove all components from a layout, what would happen?
The layout is duplicated
Only the first component is removed
The layout is closed
All components disappear from the layout
What does the following Java method return?
15
2
53
8
What will this code output?
No output
Hello Java!
null
Error: greet() must be static
You are developing a Java program and have created a method printHello() that is supposed to display "Hello" on the screen. After calling this method in your program, you want to verify that it works correctly. Which of the following best describes what should happen when the method is executed?
The program crashes
Nothing happens
A window pops up
The word "Hello" is printed
A programmer wants to calculate the sum of two numbers in multiple parts of a program. Which is the main reason for putting this calculation inside a method?
To create a new Java program
To perform a specific task or calculation
To store data in a variable
To display a window automatically
A student writes a program that compiles successfully, but when running it, the program crashes because it tries to divide by zero. What type of error caused this crash?
Syntax error
Compile-time error
Logical error
Runtime error
A student writes a program to calculate the total of two numbers. The program runs without crashing, but it outputs 12 instead of the expected 15. What type of error occurred?
Logical error
Runtime error
Compile-time error
Syntax error
A program asks the user to enter a number. If the user types a letter instead, the program stops with an error. What should the programmer do to prevent the program from crashing?
Delete the user input
Ignore the error and continue
Restart the program manually
Use a try-catch block to handle the error
A student writes a program that divides two numbers. If the second number is 0, the program crashes while running. Which of the following caused this problem?
Using wrong variable name
Incorrect indentation
Dividing a number by zero
Missing semicolon
What will happen when this code runs?
Prints 0
Does nothing
Prints 3
Throws ArrayIndexOutOfBoundsException
A program asks the user to enter a number. Sometimes, the user enters text, which causes the program to crash. How can the programmer handle this exception to prevent the crash?
Restarting the program
try-catch-finally
Using a try–catch block
Using a print method
What is the output of the following code?
16
4
8
12
A programmer wants to store data for 3 students (rows) and 2 subjects per student (columns) in a 2D array. Which of the following correctly creates this array in Java?
int[][] arr = new int[3][2];
int[][] arr = new int[3];
int[][] arr = new int[2][3];
int arr = new int[3][2];
What will be the value printed by this code?
40
50
30
5.0
A programmer creates an array of size 5 and tries to access the element at index 7. What happens when this invalid index is accessed in Java?
The array resizes automatically
A runtime exception occurs
The value becomes 0
The program ignores it
A programmer wants to store the scores of 10 students in a single row. Which type of array should they use?
A table of elements arranged in rows and columns
A file that contains only characters
A list of elements stored in a single row
A dynamic structure that grows automatically
What will this code display?
2
Error
3
4
Which loop correctly prints all elements in an array called numbers?
for(int i=1; i<=numbers.length; i++) { }
for(int i=0; i<numbers.length-1; i++) { }
for(int x : length) { }
for(int i=0; i<numbers.length; i++) { System.out.println(numbers[i]); }
A programmer is designing a JavaFX application and needs the main window to contain all UI components. Considering the responsibilities of JavaFX objects, which choice best represents the role of a Stage?
To handle events automatically
To store data in arrays
To represent the main window of the application
To display text only
A JavaFX developer wants to place buttons, labels, and text fields in the main window. Which component should contain all these elements to ensure proper display and interaction?
The main method
A function that returns values
The container for all visual content shown in a Stage
A loop for arrays
A programmer wants to add a clickable element to a JavaFX application. Considering the JavaFX architecture, which Node should be added to a Scene to allow user interaction?
Application.java
Button
Stage
Scene
A programmer wants a node to appear on the screen so the user can click it. What is the purpose of adding the Button to a Pane in JavaFX?
To store the Button in a variable
To change the Pane’s color
To make the Button appear in the user interface
To delete the Button automatically
A programmer is using Scene Builder to design a JavaFX layout and saves the FXML files. To follow best practices, in which project folder should the FXML files be placed for proper application execution?
/bin
/src/main/resources
/src/main/java/package
/src/main/java
A programmer is building a JavaFX application and needs to ensure it can launch and display a window. Which JavaFX framework class must their class extend to accomplish this?
Main.java
Controller.java
View.fxml
Application.java
A programmer is designing a JavaFX application. They want a class that handles user interactions, such as button clicks, and updates the UI components accordingly. Which statement correctly describes the role of the Controller class?
It stores arrays
It compiles the JavaFX project
It handles user events and interacts with UI components
It defines the main window
A programmer writes a Java program but forgets to add a semicolon at the end of a statement. They need to determine the type of error to fix it correctly. Which type of error does this situation represent?
Logical error
Runtime error
Semantic error
Compile-time error
A programmer runs a Java program that divides a number by zero during execution. They need to determine the type of error in order to handle it properly. Which type of error is this?
Semantic error
Runtime error
Compile-time error
Logical error
A Java program executes without crashing but produces incorrect results instead of the expected sum. Which type of error occurred, and how should the programmer address it?
Compile-time error
Runtime error
Logical error
Semantic error
While running a Java program, the following error message appears.
What type of event does this indicate in Java?
Exception
Reserved word
Method
Statement
