wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Basic Java with CodeHS

Total questions: 50

Worksheet time: 48mins

Name
Class
Date
1.
Which type of data is used to enter the value ("Welcome")?
a)
Text
b)
Characters
c)
String
d)
Boolean
2.
Which type of data is used to enter the value (1234.5)?
a)
Integer
b)
Double 
c)
Char
d)
Void
3.
Which type of data is used to enter the value (True)?
a)
Float
b)
Boolean
c)
Check box
d)
ٍString
4.

We use Scanner Library in java for...

a)
Give permissions to the user
b)
Correcting software errors
c)
Allows user to read data
d)
Allows the user to enter data
5.

We use For loop ..........

a)
To repeat a specified number of operations.
b)
To repeat a unspecified number of operations.
c)
All answers are correct
6.

We use Random Library in java for...

a)
Choose a set of random numbers
b)
Deploy the program randomly
c)
Store a set of information at random
d)
All answers are correct
7.

We use While loop ..........

a)
To repeat a specified number of operations.
b)
To repeat a unspecified number of operations.
c)
All answers are correct
8.
When we want to print in Java, we use the code ......
a)
system.out.print();
b)
System.out.println();
c)
System.Out.Printf();
d)
System.out.show();
9.

++ increases the value of a variable by 1

a)

assignment operator

b)

decrement operator

c)

increment operator

d)

sentinel

10.

What would the new value of A be?

A=1;

a++;

a)

1

b)

2

c)

3

d)

4

11.

What's the value of below?


int i=5;

System.out.println(i++);

System.out.println(i);

System.out.println(++i);

a)

5

6

7

b)

6

6

7

c)

6

7

8

d)

5

5

6

12.

Which statement(s) are equivalent to i = i + 1?

a)

i += 1

b)

i++

c)

i -= 1

d)

i--

13.

what is modulo operation used for ?

a)

performs division

b)

performs division and gives no remainder

c)

performs division and gives remainder

d)

does not perform division

14.

Find the output for the following.

public class IncDec

{

public static void main(String s[])

{

int a = 1;

int b = 2;

int c;

int d;


c = ++b;

d = a++;


c++;


System.out.println("a = " + a);

System.out.print("b = " + b);

System.out.println("c = " + c);

System.out.print("d = " + d);

}

}

a)

a = 2 b = 3 c = 4 d = 1

b)

a = 2 b = 4 c = 4 d = 1

c)

Program does not compile.

d)

a = 1 b = 2 c = 4 d = 2

15.

How would you declare a variable storing the tax rate?

a)

int taxRate = 5.1;

b)

taxRate = "5.1";

c)

double taxRate = 5.1;

d)

double taxRate = "5.1";

e)

I do hereby declare thee Sir Tax Rate

16.

How would you declare a variable storing a person's name?

a)

string name = "Elroy";

b)

name String = "Elroy";

c)

String name = Elroy;

d)

String name = "Elroy";

e)

Me llamo Elroy

17.

How would you declare a variable that tells you that someone passed a class?

a)

boolean passed = 'true';

b)

boolean passed = true;

c)

passed = true;

d)

String passed = "true";

e)

Trick question - no one passes this class

18.

Which declaration will throw an error?

a)

double num = 8;

b)

int averageGrade = 89.7;

c)

boolean done = false;

d)

String done = "true";

19.

What prints out "I love Java" successfully?

a)

System.out.println(I love Java);

b)

Systemoutprintln("I love Java);

c)

System.out.println("I love" + " Java");

d)

System.out.println("I love Java")

e)

What is love? Baby dont hurt me. Dont hurt me. No more.

20.

Decide if the boolean expression evaluates to true or false. Given:

a = 1, b = 0, c = 3


a == b

a)

true

b)

false

21.

Decide if the boolean expression evaluates to true or false. Given:

a = 1, b = 0, c = 3


a != c

a)

true

b)

false

22.

Decide if the boolean expression evaluates to true or false. Given:

a = 1, b = 0, c = 3


a < c

a)

true

b)

false

23.

Decide if the boolean expression evaluates to true or false. Given:

a = 1, b = 0, c = 3


a + b > c

a)

true

b)

false

24.

Decide if the boolean expression evaluates to true or false. Given:

a = 1, b = 0, c = 3


a % c > b

a)

true

b)

false

25.

How many stars are printed?

a)

7

b)

3

c)

4

d)

5

e)

Infinite Loop

26.

In total, how many times is the inner loop executed?

a)

5

b)

10

c)

15

d)

50

e)

Infinite Loop

27.

How many times does the loop print a *

a)

40

b)

20

c)

24

d)

30

e)

Infinite Loop

28.

What does the following code print?

a)

A rectangle of 8 rows with 5 stars per row

b)

A rectangle of 8 rows with 4 stars per row

c)

A rectangle of 6 rows with 5 stars per row

d)

A rectangle of 6 rows with 4 stars per row

e)

Infinite Looo

29.

What does the following code print?

a)

A rectangle of 9 rows with 5 stars per row

b)

A rectangle of 6 rows with 6 stars per row

c)

A rectangle of 7 rows with 5 stars per row

d)

A rectangle of 7 rows with 6 stars per row

e)

Infinite Loop

30.

What does the following code print?

a)

5 6 7 8 9

b)

4 5 6 7 8 9 10 11 12

c)

3 5 7 9 11

d)

3 4 5 6 7 8 9 10 11 12

31.

How many times does the following method print a *?

a)

9

b)

6

c)

7

d)

10

32.
a)

1 2 4 8 16 32 64

b)

1 2 4 8 16 32

c)

2 4 8 16 32 64

d)

2 4 8 16 32

e)

4 8 16 32 64

33.
a)

200 66 22 7 2

b)

66 22 7 2

c)

200 66 22 2

d)

200 66 22

e)

7

34.
a)

10

b)

15

c)

17

d)

22

e)

25

35.

2

a)

5

b)

7

c)

9

d)

13

e)

20

36.
a)

-1 0 2 5

b)

-1 0 3 7

c)

-1 1 4 8

d)

0 2 5 9

e)

0 1 3 7

37.

Goal = Create a loop that turns as long as i is less than 10.

What should replace ⭐?

a)

>

b)

<

c)

>=

d)

<=

38.

Goal = Create a loop that increases i by 2 each time.

What should replace ⭐?

a)

i += 2

b)

i ++

c)

i = i + 2

d)

+2

39.

What is a value for ⭐ that would prevent the loop from ever running?

a)

10

b)

0

c)

1

d)

-5

40.

Goal = Create a loop that increases i by 1 each time.

What should replace ⭐?

a)

i += 1

b)

i ++

c)

i + 2

d)

i += 2

41.

What Java code matches this statement:

Their name is Jacob and they are less than 15 years old.

a)

if(name = "Jacob" && age < 15)

b)

if(name == "Jacob" && age < 15)

c)

if(name == "Jacob" || age < 15)

d)

if("name" == "Jacob" && age < 15)

42.

What is a value for ⭐ that would prevent the loop from ever running?

a)

10

b)

15

c)

100

d)

11

43.

Goal = Create a loop that turns as long as i is greater than 10.

What should replace ⭐?

a)

>

b)

<

c)

>=

d)

<=

44.

What is output?

a)

Jackson

b)

Memorial

c)

JacksonMemorial

d)

There is no ouptut

45.

What is output?

a)

0

b)

1

c)

2

d)

3

46.

What is output?

a)

-1

b)

0

c)

1

d)

2

47.

What is output?

a)

-1

b)

0

c)

1

d)

2

48.

What is printed to the screen?

a)

The meaning of life is

other people

b)

The meaning of life is

42

c)

The meaning of life is

d)

(Nothing: the program crashes when you try to divide by zero)

49.

Which line of code concatenates two Strings?

a)

String foo = "Hello " + "World";

b)

String foo = "Hello " & "World";

c)

String foo = "Hello " && "World";

d)

String foo = String.concatenate("Hello", "World");

50.

What is printed to the screen?

a)

foo

b)

bar

c)

foobar

d)

foo

bar

e)

There's no way to tell because Java stores strings weirdly.