Font size
WorksheetsBasic Java with CodeHS
Total questions: 50
Worksheet time: 48mins
We use Scanner Library in java for...
We use For loop ..........
We use Random Library in java for...
We use While loop ..........
++ increases the value of a variable by 1
assignment operator
decrement operator
increment operator
sentinel
What would the new value of A be?
A=1;
a++;
1
2
3
4
What's the value of below?
int i=5;
System.out.println(i++);
System.out.println(i);
System.out.println(++i);
5
6
7
6
6
7
6
7
8
5
5
6
Which statement(s) are equivalent to i = i + 1?
i += 1
i++
i -= 1
i--
what is modulo operation used for ?
performs division
performs division and gives no remainder
performs division and gives remainder
does not perform division
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 = 2 b = 3 c = 4 d = 1
a = 2 b = 4 c = 4 d = 1
Program does not compile.
a = 1 b = 2 c = 4 d = 2
How would you declare a variable storing the tax rate?
int taxRate = 5.1;
taxRate = "5.1";
double taxRate = 5.1;
double taxRate = "5.1";
I do hereby declare thee Sir Tax Rate
How would you declare a variable storing a person's name?
string name = "Elroy";
name String = "Elroy";
String name = Elroy;
String name = "Elroy";
Me llamo Elroy
How would you declare a variable that tells you that someone passed a class?
boolean passed = 'true';
boolean passed = true;
passed = true;
String passed = "true";
Trick question - no one passes this class
Which declaration will throw an error?
double num = 8;
int averageGrade = 89.7;
boolean done = false;
String done = "true";
What prints out "I love Java" successfully?
System.out.println(I love Java);
Systemoutprintln("I love Java);
System.out.println("I love" + " Java");
System.out.println("I love Java")
What is love? Baby dont hurt me. Dont hurt me. No more.
Decide if the boolean expression evaluates to true or false. Given:
a = 1, b = 0, c = 3
a == b
true
false
Decide if the boolean expression evaluates to true or false. Given:
a = 1, b = 0, c = 3
a != c
true
false
Decide if the boolean expression evaluates to true or false. Given:
a = 1, b = 0, c = 3
a < c
true
false
Decide if the boolean expression evaluates to true or false. Given:
a = 1, b = 0, c = 3
a + b > c
true
false
Decide if the boolean expression evaluates to true or false. Given:
a = 1, b = 0, c = 3
a % c > b
true
false
How many stars are printed?
7
3
4
5
Infinite Loop
In total, how many times is the inner loop executed?
5
10
15
50
Infinite Loop
How many times does the loop print a *
40
20
24
30
Infinite Loop
What does the following code print?
A rectangle of 8 rows with 5 stars per row
A rectangle of 8 rows with 4 stars per row
A rectangle of 6 rows with 5 stars per row
A rectangle of 6 rows with 4 stars per row
Infinite Looo
What does the following code print?
A rectangle of 9 rows with 5 stars per row
A rectangle of 6 rows with 6 stars per row
A rectangle of 7 rows with 5 stars per row
A rectangle of 7 rows with 6 stars per row
Infinite Loop
What does the following code print?
5 6 7 8 9
4 5 6 7 8 9 10 11 12
3 5 7 9 11
3 4 5 6 7 8 9 10 11 12
How many times does the following method print a *?
9
6
7
10
1 2 4 8 16 32 64
1 2 4 8 16 32
2 4 8 16 32 64
2 4 8 16 32
4 8 16 32 64
200 66 22 7 2
66 22 7 2
200 66 22 2
200 66 22
7
10
15
17
22
25
2
5
7
9
13
20
-1 0 2 5
-1 0 3 7
-1 1 4 8
0 2 5 9
0 1 3 7
Goal = Create a loop that turns as long as i is less than 10.
What should replace ⭐?
>
<
>=
<=
Goal = Create a loop that increases i by 2 each time.
What should replace ⭐?
i += 2
i ++
i = i + 2
+2
What is a value for ⭐ that would prevent the loop from ever running?
10
0
1
-5
Goal = Create a loop that increases i by 1 each time.
What should replace ⭐?
i += 1
i ++
i + 2
i += 2
What Java code matches this statement:
Their name is Jacob and they are less than 15 years old.
if(name = "Jacob" && age < 15)
if(name == "Jacob" && age < 15)
if(name == "Jacob" || age < 15)
if("name" == "Jacob" && age < 15)
What is a value for ⭐ that would prevent the loop from ever running?
10
15
100
11
Goal = Create a loop that turns as long as i is greater than 10.
What should replace ⭐?
>
<
>=
<=
What is output?
Jackson
Memorial
JacksonMemorial
There is no ouptut
What is output?
0
1
2
3
What is output?
-1
0
1
2
What is output?
-1
0
1
2
What is printed to the screen?
The meaning of life is
other people
The meaning of life is
42
The meaning of life is
(Nothing: the program crashes when you try to divide by zero)
Which line of code concatenates two Strings?
String foo = "Hello " + "World";
String foo = "Hello " & "World";
String foo = "Hello " && "World";
String foo = String.concatenate("Hello", "World");
What is printed to the screen?
foo
bar
foobar
foo
bar
There's no way to tell because Java stores strings weirdly.
