Which command prints text without moving to a new line?
System.out.print();
Write the correct declaration of an integer variable named score
int score;
Which primitive type would you use for true/false values?
boolean
What is the operator for remainder? (Name + Symbol)
Modulo and %
What kind of conversion happens when casting from a double to an int?
Narrowing Conversion
What is the difference between System.out.print and System.out.println?
System.out.print does not go to a new line after printing whereas System.out.println goes to a new line after printing.
If I want to input a decimal number but I want to save memory, which type should I use when declaring the variable?
float
Which of these is NOT a primitive type?
A) int
B) double
C) boolean
D) String
D) String
What is the output of this code?
int a = 10;
a++;
a *= 1;
System.out.println(a);
Which of these correctly casts a double d into an int?
A) int d = (int) 7.9;
B) double d = (int) 7.9;
C) int d = 7.9;
A) int d = (int) 7.9;
Which of these will cause a compile error?
A) System.out.println("Hello");
B) System.out.println(Hello);
C) System.out.print("Hello World");
System.out.println(Hello);
Assume Scanner scan = new Scanner(System.in). Which line correctly inputs a decimal?
scan.nextDouble();
What is the default data type for decimals in Java?
Double
What is (19 % 7) * 2 equal to?
6
What is the output of this code?
int x = 5;
int y = 2;
double z = x/y;
System.out.println(z);
2.0
What is the output of this code?
String x = "Hi";
String y = "Hello";
System.out.print(x + " or " + y);
Why should you always match Scanner methods to the correct data type (What error will this cause)?
An "incompatible types" error.
Which data type would you use for the answer to “What is your name?”
String
What is the output of this code?
int x = 6;
x += 4;
x /= 3;
x--;
System.out.println(x);
2
Write the Code:
Declare a variable x and initialize it to 3. Implicitly cast it to a double and store it into a new variable. Explicitly cast it back to an integer and store it into a new variable.
int x = 3;
double y = x;
int z = (int) y;
Fix the error in this line of code (2 fixes):
system.out.println(Hello World);
System.out.println("Hello World");
Write two separate lines of code: one to declare a Scanner variable, and one to initialize it.
Scanner scan;
scan = new Scanner(System.in);
Mr. Queen, my scanner class says "error: cannot find symbol" even though the declaration and initialization syntax is correct. What am I most likely missing from the top of my code?
Declare and initialize an integer variable 10. Then, using a unary operator, increase it by 1. After that, find the remainder when dividing it by 2. If we then printed out the variable, what would be the output?
1
Explain the difference between widening and narrowing conversions in Java. Give an example of each using code.
Widening conversions are when you implicitly cast an integer to a double:
double var = 3;
Narrowing conversions are when you explicitly cast a double to an integer:
int x = 5;
double y = (int) x;