wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Programming Fundamentals

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

What are the four main data types in Java?

a)

int, double, char, boolean

b)

long, decimal, list, map

c)

float, string, byte, short

d)

array, object, set, tuple

2.

How do you declare a variable in Java?

a)

dataType = variableName;

b)

var variableName = dataType;

c)

dataType variableName;

d)

variableName: dataType;

3.

What is the purpose of the 'if' statement in Java?

a)

To execute code conditionally based on a boolean expression.

b)

To create a loop for repeated execution.

c)

To declare a method in a class.

d)

To define a variable in Java.

4.

Explain the difference between '==' and '.equals()' in Java.

a)

'==' is for reference comparison, while '.equals()' is for value comparison.

b)

'==' is used for primitive types, while '.equals()' is for objects.

c)

'.equals()' checks for null, while '==' does not.

d)

'==' compares values, while '.equals()' compares references.

5.

What is a switch statement and when would you use it?

a)

A switch statement is used to simplify code when you have multiple conditions based on the same variable, improving readability and maintainability.

b)

A switch statement is used to handle asynchronous operations in JavaScript.

c)

A switch statement is a type of loop that iterates over an array.

d)

A switch statement is a method for defining classes in object-oriented programming.

6.

How do you create a constant variable in Java?

a)

Declare a variable without any keyword, e.g., 'int MY_CONSTANT = 10;'.

b)

Use the 'static' keyword to declare a constant variable, e.g., 'static int MY_CONSTANT = 10;'.

c)

Use the 'const' keyword to declare a constant variable, e.g., 'const int MY_CONSTANT = 10;'

d)

Use the 'final' keyword to declare a constant variable, e.g., 'final int MY_CONSTANT = 10;'.

7.

What is the output of the following code: int x = 5; if(x > 3) { System.out.println("Hello"); }?

a)

Hello

b)

Goodbye

c)

Greetings

d)

Hi there

8.

Describe the use of loops in Java. What types of loops are available?

a)

Java supports 'loop', 'iterate', and 'cycle' as loop types.

b)

The types of loops available in Java are 'for', 'while', and 'do-while'.

c)

The available loops in Java include 'for-each', 'looping', and 'repeat-until'.

d)

The types of loops in Java are 'foreach', 'repeat', and 'until'.

9.

What is the difference between 'while' and 'do-while' loops?

a)

The main difference is that 'while' checks the condition before execution, while 'do-while' checks it after, guaranteeing at least one execution.

b)

'Do-while' loops are faster than 'while' loops.

c)

'While' guarantees at least one execution, unlike 'do-while'.

d)

Both loops check the condition before execution.

10.

How can you handle exceptions in Java?

a)

Implement a switch-case structure for exception handling.

b)

Use try-catch blocks to handle exceptions in Java.

c)

Use if-else statements to manage exceptions in Java.

d)

Ignore exceptions to prevent program crashes in Java.