Homework 2 QUIZ 9/7/18
What do we mean by number wrap-around? Give an example of when this happens!
Number wrap-around happens because of overflow and underflow. It occurs with int and float operators; When as resulting value in int is larger than 32 bits, the value is treated as a negative. EX. int = 2147483647 System.out.print(int); --> -2147483648
True or False: A variable must be declared before it can be used.
True
Briefly describe the difference between variable assignment and variable initialization.
Variable assignment sets aside a piece of storage for use. Variable initialization specifies the initial value to be stored in an object, which is not the same as when you assign a value for the first time in the code.
What will the following code segments print on the screen? int a, x = 23; a = x % 2; System.out.println(x + "\n" + a);
23 1
Describe in your own words what a named constant is. What is the difference between a variable and a literal and a named constant? Why is it a good idea to use named constants rather than literals in your programs?
A named constant is very similar to a variable, but a named constant's value cannot be changed. A variable can have an assigned value given by the programmer. A literal is a syntactical value written into Java's program. A named constant is a variable whose name cannot be changed after using the "final" tool. It is a good idea because you will receive an error if the named constant changes.
True or False: If one of an operator's operands is a double and the other operand is an int, Java will automatically convert the value of the double to an int.
False
Assume that a program uses the named constant pi to represent the value 3.14. The program uses the named constant in several statements. What is the advantage of using the named constant instead of the actual value 3.14 in each statement?
If the value were to be changed for specific reasons, it would be easy to do so with named constant as only the initialized value would have to be changed.
What does a variable declaration tell the Java compiler about a variable?
The variable name and variable's data type
Which type of operator lets you manually convert a value, even if it means that a narrowing conversion will take place? a. cast b. binary c. uploading d. dot
cast
As expression adds a double variable and a long variable. Of what data type will the result be?
double
Which keyword is used to declare a named constant? a. constant b. namedConstant c. final d. concrete
final
An expression adds a byte variable and a short variable. Of what data type will the result be?
short
When Java converts a lower-ranked value to a higher-ranked type, it is called a . a. 4-bit conversion b. escalating conversion c. widening conversion d. narrowing conversion
widening conversion