wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Computer Programming 1 (Final Examination)

Total questions: 60

Worksheet time: 30hrs 0mins

Name
Class
Date
1.

What is an array in Java?

a)

A fixed-size collection of elements of the same type

b)

A dynamic list that automatically resizes

c)

A class that stores only strings

d)

A tool for GUI design

2.

Which of the following correctly declares a one-dimensional array?

a)

int[] numbers();

b)

int numbers[];

c)

array int numbers;

d)

int numbers();

3.

Array indexing in Java starts at?

a)

0

b)

−1

c)

Any value

d)

1

4.

What is the length of the array int[] a = new int[5];?

a)

5

b)

4

c)

0

d)

Depends on values

5.

Which of the following is true about JavaFX?

a)

It cannot handle event

b)

It allows creating GUI applications in Java

c)

It is used for console applications only

d)

It is the predecessor of Swing

6.

What is the main difference between Swing and JavaFX?

a)

Swing is older; JavaFX supports modern GUI features

b)

JavaFX cannot handle buttons

c)

Swing does not allow layouts

d)

JavaFX is command-line only

7.

Which JavaFX component is used to display text?

a)

JButton

b)

Label

c)

TextField

d)

JPanel

8.

To use JavaFX in your IDE, you need to:

a)

Install JavaFX SDK and configure the IDE

b)

Only install JDK

c)

Install Python

d)

Configure database connectivity

9.

In Java, functions are called:

a)

Functions

b)

Classes

c)

Procedures

d)

Methods

10.

Which of the following is the correct syntax to pass a parameter to a method?

a)

methodName(int x)

b)

methodName x

c)

methodName {x}

d)

methodName[x]

11.

Which block is used to handle exceptions in Java?

a)

try-catch

b)

if-else

c)

switch-case

d)

for-loop

12.

What type of error occurs when code runs but produces incorrect results?

a)

Logical error

b)

Syntax error

c)

Runtime error

d)

Compile-time error

13.

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

a)

new int(5)

b)

new int[5]

c)

int[5]

d)

new array[5]

14.

What is the output of the following code?

a)

40

b)

30

c)

20

d)

10

15.

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?

a)

int[][] arr = new int[4];

b)

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

c)

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

d)

int arr[4][3];

16.

Which loop is most appropriate for accessing every element in an array when the number of elements is already known?

a)

for loop

b)

do-while loop

c)

Any of the above

d)

while loop

17.

Given the 2D array below, which line correctly accesses the last element in the first row?

a)

matrix[1][3]

b)

matrix[2][0]

c)

matrix[0][2]

d)

matrix[1][2]

18.

Which code correctly accesses the third element of the array?

a)

scores(2);

b)

scores.get(2);

c)

scores[3];

d)

scores[2];

19.

What is wrong with this code snippet?

a)

Accessing an invalid index

b)

Syntax error

c)

No error

d)

NullPointerException

20.

Which statement correctly prints the element "Date"?

a)

System.out.println(fruits(3));

b)

System.out.println(fruits.get(3));

c)

System.out.println(fruits[3]);

d)

System.out.println(fruits{3});

21.

Evaluate this code:

a)

Prints 1 2 3

b)

Throws ArrayIndexOutOfBoundsException

c)

Prints 3

d)

Syntax error

22.

What is the output of the following?

a)

20

b)

30

c)

3

d)

10

23.

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?

a)

Button

b)

Label

c)

CheckBox

d)

TextField

24.

You need to arrange several buttons vertically in your GUI so they appear one below the other. Which layout should you use?

a)

GridPane

b)

HBox

c)

VBox

d)

StackPane

25.

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?

a)

Scene

b)

TextField

c)

Stage

d)

VBox

26.

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)

A text field

b)

A button

c)

A window

d)

A text label

27.

You want the user to enter their name in your GUI application. Which JavaFX component should you use?

a)

Label

b)

Button

c)

VBox

d)

TextField

28.

You want the user to choose their favorite color from a predefined list. Which JavaFX component is most appropriate?

a)

HBox

b)

Label

c)

ComboBox

d)

TextField

29.

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?

a)

Close the window

b)

Initialize components

c)

Display the window

d)

Hide the window

30.

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?

a)

To close the window

b)

To hide the window

c)

To display a text/title on the top of the window

d)

To initialize components

31.

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?

a)

StackPane

b)

GridPane

c)

VBox

d)

HBox

32.

In Scene Builder or JavaFX, if you want to remove all components from a layout, what would happen?

a)

The layout is duplicated

b)

Only the first component is removed

c)

The layout is closed

d)

All components disappear from the layout

33.

What does the following Java method return?

a)

15

b)

2

c)

53

d)

8

34.

What will this code output?

a)

No output

b)

Hello Java!

c)

null

d)

Error: greet() must be static

35.

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?

a)

The program crashes

b)

Nothing happens

c)

A window pops up

d)

The word "Hello" is printed

36.

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?

a)

To create a new Java program

b)

To perform a specific task or calculation

c)

To store data in a variable

d)

To display a window automatically

37.

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?

a)

Syntax error

b)

Compile-time error

c)

Logical error

d)

Runtime error

38.

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?

a)

Logical error

b)

Runtime error

c)

Compile-time error

d)

Syntax error

39.

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?

a)

Delete the user input

b)

Ignore the error and continue

c)

Restart the program manually

d)

Use a try-catch block to handle the error

40.

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?

a)

Using wrong variable name

b)

Incorrect indentation

c)

Dividing a number by zero

d)

Missing semicolon

41.

What will happen when this code runs?

a)

Prints 0

b)

Does nothing

c)

Prints 3

d)

Throws ArrayIndexOutOfBoundsException

42.

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?

a)

Restarting the program

b)

try-catch-finally

c)

Using a try–catch block

d)

Using a print method

43.

What is the output of the following code?

a)

16

b)

4

c)

8

d)

12

44.

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?

a)

int[][] arr = new int[3][2];

b)

int[][] arr = new int[3];

c)

int[][] arr = new int[2][3];

d)

int arr = new int[3][2];

45.

What will be the value printed by this code?

a)

40

b)

50

c)

30

d)

5.0

46.

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?

a)

The array resizes automatically

b)

A runtime exception occurs

c)

The value becomes 0

d)

The program ignores it

47.

A programmer wants to store the scores of 10 students in a single row. Which type of array should they use?

a)

A table of elements arranged in rows and columns

b)

A file that contains only characters

c)

A list of elements stored in a single row

d)

A dynamic structure that grows automatically

48.

What will this code display?

a)

2

b)

Error

c)

3

d)

4

49.

Which loop correctly prints all elements in an array called numbers?

a)

for(int i=1; i<=numbers.length; i++) { }

b)

for(int i=0; i<numbers.length-1; i++) { }

c)

for(int x : length) { }

d)

for(int i=0; i<numbers.length; i++) { System.out.println(numbers[i]); }

50.

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?

a)

To handle events automatically

b)

To store data in arrays

c)

To represent the main window of the application

d)

To display text only

51.

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?

a)

The main method

b)

A function that returns values

c)

The container for all visual content shown in a Stage

d)

A loop for arrays

52.

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?

a)

Application.java

b)

Button

c)

Stage

d)

Scene

53.

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?

a)

To store the Button in a variable

b)

To change the Pane’s color

c)

To make the Button appear in the user interface

d)

To delete the Button automatically

54.

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?

a)

/bin

b)

/src/main/resources

c)

/src/main/java/package

d)

/src/main/java

55.

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?

a)

Main.java

b)

Controller.java

c)

View.fxml

d)

Application.java

56.

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?

a)

It stores arrays

b)

It compiles the JavaFX project

c)

It handles user events and interacts with UI components

d)

It defines the main window

57.

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?

a)

Logical error

b)

Runtime error

c)

Semantic error

d)

Compile-time error

58.

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?

a)

Semantic error

b)

Runtime error

c)

Compile-time error

d)

Logical error

59.

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?

a)

Compile-time error

b)

Runtime error

c)

Logical error

d)

Semantic error

60.

While running a Java program, the following error message appears.
What type of event does this indicate in Java?

a)

Exception

b)

Reserved word

c)

Method

d)

Statement