Download Quiz2_43key

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COP 2551_043key
Chapter 2 Quiz
Name ______________________KEY_____________
6/3/2004
Given the following declarations, answer questions 1, 2, & 3.
int x = 2;
int y = 10;
int z = 23;
1.
What is the value of the Java expression
x + y * z
232
2.
What is the value of the Java expression
z – y / x
18
3.
What is the value of the Java expression
x + z % y
5
4.
Write a complete Java program that will store 88 in a variable
named keys and will then use the variable and a println
statement to output the message below (no comments or skipped
lines are required, but use indentation as demonstrated in the
text and in class).
A piano has 88 keys.
public class Keys
{
public static void main(String[] args)
{
keys = 88;
System.out.println("A piano has " + keys + " keys.");
}
}
5.
All floating point literals are of type
a.
b.
c.
d.
6.
either
either
either
any of
true or false
<====
0 or 1 (zero or one)
yes or no
the above can be assigned to a Boolean variable
You are less likely to lose information when you use a
widening conversion. Which of the following is a widening
conversion?
a.
b.
8.
<====
Which of the following can be assigned to a Boolean variable?
a.
b.
c.
d.
7.
float
double
int
long
Storing a short in an int.
Storing a double in a float.
<====
Given the following declaration:
String name;
For 5 points each, show two methods for creating the String
object "John Doe" and assigning it to the reference variable
called name.
name = new String("John Doe");
name = "John Doe";
9.
DateFormat and DecimalFormat are both classes that are part of
Java's text package (java.text). Write a single statement
that will allow either of those classes to be referenced in
your program without the need to always use the explicit
package name.
import java.text.*;
10.
If the String variable major is initialized to
"Computer Science", what is returned by major.charAt(4)?
a.
b.
c.
d.
'p'
"Comp"
'u'
"Compu"
<====
5 POINTS EXTRA CREDIT
The Math class has a method named pow that is defined as:
static double pow (double num, double power)
Suppose you want to raise 3 to the 5th power and assign the
result to a variable declared as double num;
Write the Java statement that will do this:
double num = java.Math.pow(3,5);
or
double num = Math.pow(3,5);
// assuming there was an import statement
// "double" is not needed if you assume "num" was already
declared as a double.
Any of the above will be accepted even if the assumptions were
not stated.