What data type would you use to store the number 9.6?
double
Declare a variable to store the number 5
int number = 5;
What will the following code print?
System.out.print("3");
System.out.print("5");
35
int x = 10;
if (x == 5) { System.out.println("x is 5"); }
else { System.out.println("x is not 5"); }
x is not 5
How many times will this loop run?
for (int i = 0; i < 3; i++) { System.out.println(i); }
3 times
What happens if you try to store a decimal number in an int variable?
If you declare int x = 10; and then assign x = 20;, what is the new value of x?
20
What is the purpose of the else if statement?
To check another condition if the if condition is false.
Write a while loop that prints numbers from 1 to 5.
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
Write a Java code that creates a String variable called letter and assigns it the value "Turtle". Then, print out the value of letter.
String letter = "Turtle"; System.out.println(letter);
Write a Java code that declares a variable a of type int and assigns it the value 3, then prints it out.
int a = 3;
System.out.println(a);
What is the output of this expression
int a = 6 + 3 * 2 - 4 / 2 + 5 % 2;
System.out.println(a);
11
Write an if-else statement to check if a number is positive, negative, or zero.
if (number > 0) {System.out.println("Positive"); }
else if (number < 0) {System.out.println("Negative");}
else { System.out.println("Zero"); }
What does this loop print?
for (int i = 5; i > 0; i--)
{ System.out.print(i + " "); }
5 4 3 2 1
double num = 3;
System.out.println(num);
What is the output?
3.0
Write a Java code that declares two int variables, x and y, assigns x the value 5 and y the value 10, then prints their sum.
int x = 5;
int y = 10;
System.out.println(x + y);
What is the output of the following?
public class Main {
public static void main(String[] args) {
int x = 30;
int y = 6;
System.out.println(5 + 11 * y / 3 - x);
}
}
-3
What is returned by the following method when the input is 10?
public boolean isDivisibleByThree(int number) {
if (number % 3 == 0) {
return true;
} else {
return false;
}
}
false
What is the output of the following code?
public class Main {
public static void main(String args[]) {
for (int i = 1; i <= 20; i+=3) {
if (i % 2 == 0) {
System.out.println(i);
}
}
}
}
4
10
16
Write a Java code that uses a boolean variable called isRaining to check if it’s raining and then prints "It is raining" if isRaining is true or "It is not raining" if isRaining is false.
boolean isRaining = true;
if (isRaining) { System.out.println("It is raining");} else { System.out.println("It is not raining"); }
Write a Java code that declares a variable count of type int, assigns it the value 7, then multiplies it by 2 and prints the result.
int count = 7;
count = count * 2;
System.out.println(count);
What is returned by the method when the input is "HugeString"?
public boolean mysteryMethod(String mysteryInput) {
if (mysteryInput.length() > 9) {
return true;
} else {
return false;
}
}
true;
int x = 5;
if (x > 3)
System.out.println("Hello"); System.out.println("World");
World
for (int i = 1; i <= 50; i+=2) {
System.out.println(i);
}