wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

G10 | Java - Unit Test 01

Total questions: 20

Worksheet time: 20mins

Name
Class
Date
1.

What is the keyword used to declare a variable in Java?

a)

variable

b)

declare

c)

int

d)

string

2.

What is the default value of an int variable in Java?

a)

10

b)

1

c)

0

d)

null

3.

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

a)

To make decisions in the code based on certain conditions

b)

To create a loop

c)

To declare a new variable

d)

To print a message to the console

4.

What is the difference between '==' and 'equals()' in Java?

a)

== operator compares the values of two objects, while equals() method compares the contents of two objects.

b)

equals() operator compares the values of two objects, while == method compares the contents of two objects.

c)

equals() method is used for primitive data types, while == operator is used for objects.

d)

equals() method compares the references of two objects, while == operator compares the contents of two objects.

5.

How do you declare an array in Java?

a)

arrayName type[];

b)

arrayName[] type;

c)

type[] arrayName;

d)

arrayName type[] = new arrayName[];

6.

What is the purpose of the 'while' loop in Java?

a)

To execute the code when a specified condition is false

b)

To execute the code based on a random condition

c)

To execute the code only once

d)

To repeatedly execute a block of code as long as a specified condition is true.

7.

What is the difference between 'do-while' and 'while' loop in Java?

a)

The 'while' loop will always execute the code block at least once

b)

The 'do-while' loop is not supported in Java

c)

The 'do-while' loop will always execute the code block at least once.

d)

The 'do-while' loop can only be used for finite iterations

8.

What is the scope of a local variable in Java?

a)

Limited to the block in which it is declared

b)

Limited to the method in which it is declared

c)

Limited to the class in which it is declared

d)

Accessible throughout the entire program

9.

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

a)

To select one of many code blocks to be executed.

b)

To randomly select a code block to be executed

c)

To execute all code blocks simultaneously

d)

To make the code more confusing

10.

How do you access the last element of an array in Java?

a)

arrayName[last]

b)

arrayName[arrayName.length - 1]

c)

arrayName[-1]

d)

arrayName[arrayName.length]

11.

A while loop carries on as long as...

a)

The condition in the brackets is true

b)

The condition in the brackets is false

c)

i < 10

d)

the for loop has not ended

12.

A while loop has the following format:

a)

while (condition)

{

...

}

b)

while

{

...

}

c)

continue while (condition)

{

...

}

d)

while [condition]

[

...;

]

13.

Which modifiers makes a method accessible without creating an object of the class?

a)

static

b)

public

14.

How do you insert COMMENTS in Java code?

a)

# This is a comment

b)

/* This is a comment

c)

// This is a comment

15.

Which data type is used to create a variable that should store text?

a)

myString

b)

String

c)

Txt

d)

string

16.

What is the main difference between float and double data types?

a)

4.1 is an example of float.

b)

4.11 is an example of double.

c)

Float consists of 8 bytes, and double consists of 4 bytes.

d)

Float consists of 4 bytes, and double consists of 8 bytes.

17.
Given the following method, what would test return if
a = {0, 2, 3, 4} and v = 2?
a)
0
b)
1
c)
2
d)
-1
18.
Given the following method, what would test return if
a = {0, 2, 3, 4} and v = 1?
a)
-1
b)
0
c)
1
d)
2
19.

Find the output of the following snippet.


class Numbers

{

public static void main(String args[])

{

int a=20, b=10;

if((a<b)&&(b++ <25))

{

System.out.println("this is any language logic");

}

System.out.println(b);

}

}

a)

10

b)

12

c)

11

d)

Compilation Error

20.

Java is named after a type of coffee.

a)

TRUE

b)

FALSE