Which data type is used to store a whole number?
int
Find the error: int age = "16";
"16" is a String, not an int.
What is printed?
int x = 8;
x = x + 2;
System.out.println(x);
10
What is the difference between int and double?
int stores whole numbers; double stores decimal numbers.
Which method is used to print something to the console?
System.out.println()
Which data type would you use for true or false?
boolean
Find the error: boolean active = "true";
true should not be in quotation marks.
What is printed?
int x = 10;
int y = 3;
System.out.println(x / y);
3
What is the difference between String and char?
String stores a sequence of characters; char stores one character.
Write a condition that prints "Adult" if age is 18 or older.
if (age >= 18) { System.out.println("Adult"); }
What is the data type of 7L?
long
Find the error: double price = 19; int count = 2.5;
2.5 cannot be stored in an int.
What is printed?
int x = 15;
if (x > 10)
System.out.println("A");
else if (x > 5)
System.out.println("B");
else
System.out.println("C");
A
What is the difference between = and == in Java?
= assigns a value; == compares values.
Which class is commonly used to get user input from the keyboard?
Scanner
Which of these values can be stored in an int variable: 25, 3.14, "25", true?
25
int age = 17;
if (age >= 18);
System.out.println("Adult");
else
System.out.println("Minor");
There is an extra ; after the if condition.
What is printed?
int x = 10;
double y = 4.0;
System.out.println(x / y);
The closing ) and ; are missing from println().
What is the difference between if (x > 5) and if (x >= 5)?
The first excludes 5; the second includes 5.
Which Scanner method is used to read an integer?
nextInt()
Consider: int a = 10; double b = 3; double result = a / b; What is the data type of result, and approximately what value does it contain?
double, approximately 3.333...
int score = 75;
if (score >= 90)
System.out.println("A");
else if (score >= 80)
System.out.println("B");
else if (score >= 70);
System.out.println("C");
The ; after else if (score >= 70) ends the condition. Remove it.
What is printed?
int x = 7;
if (x > 5)
if (x < 10)
System.out.println("A");
else
System.out.println("B");
else
System.out.println("C");
A
What is the difference between System.out.print() and System.out.println()?
print() stays on the same line; println() moves to the next line.
How does the nextLine() method work, and how is it different from next() when reading input with a Scanner?
nextLine() reads the entire line of input, including spaces, until the user presses Enter. next() reads only the next token and stops at whitespace.