Java Chapter Two
-3
-15 % -4 is _____
-3
-15 % 4 is _____
-4.
-24 % -5 is _________.
-4.
-24 % 5 is ________.
0.
-25 % 5 is _________
0
-5 % 5 is _____
4.
24 % 5 is ________________.
0.
25 % 1 is ______________.
0
5 % 1 is _____
1
5 % 2 is _____
2
5 % 3 is _____
1
5 % 4 is _____
0
5 % 5 is _____
A semicolon (;).
A Java statement ends with a __________.
Syntax errors
A compiler checks...
findArea and totalLength.
According to Java naming conventions which of the following names can be variables: FindArea, findArea, totalLength, TOTAL_LENGTH, class.
Yes.
Are the following four statements equivalent? a. number += 1; b. number = number + 1; c. number++; d. ++number;
int length; int width; or int length, width;
How can you declare variables?
Causes overflow.
If a number is too large to be stored in a variable of the float type, it _______.
A runtime error
If a program compiles fine, but it terminates abnormally at runtime, then the program suffers __________.
A float
If you attempt to add an int, a byte, a long, and a float, the result will be a __________ value.
1.0. (Note that 1 / 2 is zero).
Math.pow(4, 1 / 2) returns.
2.0. (Note that the pow method returns a double value, not an integer).
Math.pow(4, 1.0 / 2) returns...
3.
Suppose x is 1. What is x after x += 2?
0.
Suppose x is 1. What is x after x -=1?
The current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time)
System.currentTimMillis() returns...
Math.pow( a, b )
The ____________ method returns a raised to the power of b.
=
The assignment operator in Java is ____________.
24.
The expression 4 +20 / ( 3 - 1 ) * 2 is...
Integer, enter (or space or double space), double, enter.
The following code fragment reads in two numbers: Scanner input = new Scanner (System.in); int i = input.nextInt(); double d = input.nextDouble(); What are the correct ways to enter these two number?
final
The keyterm __________________ must be used to declare a constant.
x += 1; or x = x + 1; or x = 1 + x;
To add a value 1 to variable x, you write...
sum += number; or sum = sum + number;
To add number to sum, you write...
x = (float)d;
To assign a double variable d to a float variable x, you write...
x = 1;
To assign a value 1 to variable x, you write...
int number = 2;
To declare an int variable number with initial value 2, your write...
Constants.
To improve readability and maintainability, you should declare ____________ instead of using literal values such as 3.14759.
System.currentTimeMillis () / 1000 / 60 / 60 % 24
To obtain the current hour, use ___________.
System.currentTimeMillis () / 1000 / 60 % 60
To obtain the current minute, use _____________.
System.currentTimeMillis () / 1000 % 60
To obtain the current second, use __________.
True
True or False. A constant can be defined using using the final keyword.
True
True or False. A variable must be declared before it can be used.
True.
True or False? Every letter in Java keyword is in lowercase?
1
What is 1 % 2?
5.
What is i printed in the following code? public class Test { public static void main(String[] args) { int j = 0; int i = j++ + j * 5; System.out.println("What is i? " + i); } }
6.
What is printed? public class Test { public static void main(String[] args) { int j = 0; int i = ++j + j * 5; System.out.println("What is i? " + i); } }
area3.5
What is the exact output of this code? double area = 3.5; System.out.print("area"); System.out.print.(area);
x is 10.1 and y is 10.0
What is the printout of the following code: double x = 10.1; int y = (int)x; System.out.println("x is " + x + " and y is " + y);
11. 45 / 4 is an integer which will result in 11.
What is the result of 45 / 4?
X is 2.
What is x after the following statements? int x = 1; x *= x + 1;
X is 4.
What is x after the following statements? int x = 2; int y = 1; x *= y +1;
Y is 3.
What is y displayed in the following code? public class Test { public static void main(String[] args) { int x = 1; int y = x++ + x; System.out.println("y is " + y); } }
Y is 2.
What is y displayed? public class Test { public static void main(String[] args) { int x = 1; int y = x + x++; System.out.println("y is " + y); } }
input.nextInt();
What method do you use to read an int value?
radius and findArea.
Which of the following are correct names for variables according to Java naming convensions: a. radius b. Radius c. RADIUS d. findArea e. FindArea
55 = x;
Which of the following are not valid assignment statements? a. x = 55; b. x = 56 + y; c. 55 = x; d. x += 3;
All of the above.
Which of the following are the same as 1545.534: a. 1.545534e+3 b. 0.1545534e+4 c. 1545534.0e-3 d. 154553.4e-2
C and D.
Which of the following assignment statements are correct? a. i = j = k = 1; b. i = 1; j = 1; k = 1; c. i = 1 = j = 1 = k = 1; d. i == j == k == 1;
d. float f = 34.0.
Which of the following assignment statements is illegal: a. float f = -34; b. int t = 23; c. short s = 10; d. float f = 34.0;
37 % 6.
Which of the following expression results in a value of 1? a. 2 % 1 b. 15 % 4 c. 25 % 5 d. 37 % 6
MAX_VALUE
Which of the following is a constant, according to Java naming conventions: a. MAX_VALUE b. Test c. read d. ReadInt
MAX_VALUE and COUNT.
Which of the following is a constant, according to Java naming conventions: MAX_VALUE, Test, read, ReadInt, COUNT.
$343 and radius.
Which of the following is a valid identifier: a. $343 b. class c. 9X d. 8+9 e. radius
a. Casting
Which of the following operators has the highest precedence? a. casting b. + c. * d. /
A and B are the same.
Which of the following statements are the same? (A) x -= x + 4 (B) x = x + 4 - x (C) x = x - (x + 4)
B, D, and E.
Which of the following will yield 0.5? a. 1 / 2 b. 1.0 / 2 c. (double) ( 1 / 2 ) d. (double) 1 / 2 e. 1 / 2.0
d. byte
Which of these data types requires the least amount of memory? a. float b. double c. short d. byte
Long.
Which of these data types requires the most amount of memory: long, int, short, byte.
All of the above.
You can cast a double value to _______. a. byte b. short c. int d. long e. float
Pseudocode.
________ is the code with natural language mixed in with Java code.
=
___________ is the Java assignment operator.
System.
currentTimeMills is a method in the _______ class.
Math
pow is a method in the _______________ class.