Chapter 2 Quiz
To declare a constant MAX_LENGTH inside a method with value 99.98, you write
final double MAX_LENGTH = 99.98;
According to Java naming convention, which of the following names can be variables? (Choose all that apply)
findArea totalLength
What is the value of (double)5/2?
2.5
Math.pow(4, 1 / 2) returns __________.
1.0
If you enter 1 2 3, when you run this program, what will be the output? import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three numbers: "); double number1 = input.nextDouble(); double number2 = input.nextDouble(); double number3 = input.nextDouble(); // Compute average double average = (number1 + number2 + number3) / 3; // Display result System.out.println(average); } }
2.0
To assign a double variable d to a float variable x, you write
x = (float)d;
To assign a value 1 to variable x, you write
x = 1;
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); } }
y is 2.
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 3.
To improve readability and maintainability, you should declare _________ instead of using literal values such as 3.14159.
constants
Analyze the following code. public class Test { public static void main(String[] args) { int month = 09; System.out.println("month is " + month); } }
The program has a syntax error, because 09 is an incorrect literal value.
Every letter in a Java keyword is in lowercase?
True
Are the following four statements equivalent? number += 1; number = number + 1; number++; ++number;
Yes
Which of the following is a valid identifier? (Choose all that apply)
$343 radius
-24 % -5 is _____
-4
The expression 4 + 20 / (3 - 1) * 2 is evaluated to
24
Suppose x is 1. What is x after x += 2?
3
Which of the following expression results in a value 1?
37 % 6
24 % 5 is _____
4
____________ is the Java assignment operator.
=
System _______ seeks to analyze the data flow and to identify the system's input and output.
Analysis
Which of the following assignment statements is incorrect? (Choose all that apply)
i = 1 = j = 1 = k = 1; i == j == k == 1;
Suppose a Scanner object is created as follows: Scanner input = new Scanner(System.in); What method do you use to read an int value?
input.nextInt();
Which of the following are correct ways to declare variables? (Choose all that apply)
int length; int width; int length, width;
To declare an int variable number with initial value 2, you write
int number = 2;
Which of the following assignment statements is illegal? (Choose all that apply)
int t = (int)false; int t = 4.5;
Which of the following are correct names for variables according to Java naming conventions? (Choose all that apply)
radius findArea
Which of the following is a constant, according to Java naming conventions? (Choose all that apply)
MAX_VALUE COUNT
_____________ is a formal process that seeks to understand the problem and document in detail what the software system needs to do.
Requirements specification
To add a value 1 to variable x, you write (Choose all that apply)
x += 1; x = x + 1; x = 1 + x;