wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Easy

Total questions: 40

Worksheet time: 20mins

Name
Class
Date
1.

Which data type is used if you want to store the value "Welcome"?

a)

Text

b)

Characters

c)

String

d)

Boolean

2.

Which data type is used to represent the value True?

a)

float

b)

boolean

c)

Array

d)

ٍString

3.

Which is the correct statement for output in Java?

a)

system.out.print();

b)

System.out.println();

c)

System.Out.Printf();

d)

System.out.show();

4.

Which is the correct way to define a String variable?

a)

String name = "John";

b)

String name = "John"

c)

String name = John;

d)

String name "John";

5.

Which statement about choosing variable names is FALSE?

a)

Variable names can contain letters, digits, underscores, and dollar signs.

b)

Variable names are NOT case sensitive

c)

Reserved words (like Java keywords, such as int or String) cannot be used as variable names

d)

Variable names can also begin with $ and _

6.

Which of the following is a reference data type?

a)

byte

b)

Array

c)

char

d)

boolean

7.

Which of the following is the correct way of defining a char variable?

a)

char myGrade = 'B';

b)

char myGrade = "B";

c)

char myGrade 'B';

d)

char myGrade = 'B'

8.

x /= 3


Which of the following statements will produce the exact same results as the statement above?

a)

x = 3 / x

b)

x = x / 3

c)

x / 3

d)

x /x = 3

9.

What is the output of the code below?


int x = 5;

System.out.println(x++);

System.out.println(x);

a)

5

5

b)

6

6

c)

5

6

d)

6

5

10.

Which of these commands has errors? Select multiple

a)

System.out.print("Hello World")

b)

System.out.print(Hello World);

c)

System.out.Print("Hello World);

d)

System.out.print("Hello World");

11.

A data type which is typical in form and make use of digits and a decimal point

a)

integer

b)

double

c)

char

d)

boolean

12.

True or False : Java is case sensitive, which means identifier Hello and hello would have different meaning in Java

a)

True

b)

False

13.

A Java statement is one or more lines of code terminated by a semicolon. True or False?

a)

False

b)

True

14.

A Java block is formed by enclosing statements by open and close parentheses. True or False?

a)

True

b)

False

15.

Indentation plays an important role in Python. What about indentation in Java?

a)

 It plays the same role

b)

It causes an error

c)

Indentation is used for readability purposes only. Adding extra space at the beginning of lines of java code would not cause an error.

d)

It cannot be determined

16.

What is the ‘\n’ in the following code called?

System.out.println(" Hello \n world!");

a)

newline character

b)

tab character

c)

jump line

d)

no line character

17.

What are \n \t all examples of?

a)

keywords

b)

escape sequence

c)

functions

d)

none of the mentioned

18.
Which operator is used to compare two values for equality?
a)
==
b)
=
c)
!=
d)
equals()
19.
Which keyword is used to define a method in Java that does not return?
a)
this
b)
return
c)
class
d)
void
20.
Which loop executes at least once, even if the condition is false?
a)
for loop
b)
while loop
c)
do-while loop
d)
for-each loop
21.
Which method is used to read an integer input from the user in Java?
a)
readInt()
b)
nextInt()
c)
next()
d)
nextLine()
22.
What is the value of the expression 5 % 2?
a)
0
b)
1
c)
2
d)
2.5
23.

Which statement is used to exit a LOOP in Java?

a)
break
b)
continue
c)
return
d)
System.exit()
24.
Which operator is used to perform logical AND in Java?
a)
&&
b)
||
c)
!
d)
&
25.

Which statement is used to skip the current iteration of a loop in Java and start back at the top?

a)
break
b)
continue
c)
default
d)
for
26.

Which of the following lines of code will throw an error?

a)

String hello = "hello";

b)

char letter = 'A';

c)

String name = 'Montse';

d)

int num = 15;

27.

Which of the following lines of code will throw an error?

a)

String name = "Ana";

b)

double price = 10.99

c)

int age = 22;

d)

char grade = 'B';

28.

What is the correct boolean statement for the writing the logic of the following statement:

I buy pizza if it is cheap and I am hungry.

a)

boolean buyPizza = isCheap && amHungry;

b)

boolean buyPizza = !isCheap && amHungry;

c)

boolean buyPizza = isCheap || amHungry;

d)

boolean buyPizza = isCheap || !amHungry;

29.

What is the best variable type for storing a single letter grade of an exam? (Without a + or -, just the letter)

a)

char

b)

String

c)

int

d)

double

30.

What is the correct way to write the code for the following statement:

I will buy coffee if I am tired or it is not expensive.

a)

boolean buyCoffee = amTired && !expensive;

b)

boolean buyCoffee = amTired || !expensive;

c)

boolean buyCoffee = !amTired || expensive;

d)

boolean buyCoffee = !amTired || !expensive;

31.

Which of the following lines of code contains an error?

a)

float number = 10.00f;

b)

int number = 10;

c)

long number = 10.00f;

d)

String number = "10";

32.

What is the purpose of the 'main' method in Java?

a)

It is the entry point of the program

b)

It declares variables

c)

It creates objects

d)

It imports external libraries

33.

What does the 'import' keyword do in Java?

a)

It defines the main method

b)

It creates objects

c)

It declares variables

d)

It imports external libraries

34.

What is the purpose of the 'import java.util.Scanner;' statement in the given code?

a)

To define the main method

b)

To read input from a file

c)

To bring in the Scanner class from the java.util library

d)

To create a new Scanner object

35.

What is the default value of a String variable in Java?

a)

void

b)

null

c)

0

d)

undefined

36.

int z=1.5;

What will be the value of z?

a)

z=1.5

b)

run time error

c)

compilation error

d)

z=1.15

37.

This loop will output

a)

0

1

2

3

4

5

6

7

8

9

10

b)

1 10

c)

x

d)

0 1 2 3 4 5 6 7 8 9 10

38.

What will be the output for the following?

int num=15;

System.out.println(2*num++);

System.out.println(num);

a)

15

16

b)

30

16

c)

16

17

d)

32

16

39.

What would be the output for the following:

System.out.println(20/3);

a)

6.66666666667

b)

6.7

c)

7

d)

6

40.

What would be the output for the following:

int num = 5;

do{

System.out.println(++num);

while (num<10);

a)

6

7

8

9

10

b)

6

7

8

9

c)

5

6

7

8

9

10

d)

5

6

7

8

9