Chap2+3rev

advertisement
CMPT 101/104 Chapter 2 and 3 Review Questions
These questions indicate types of questions to expect in the short answer portion of the
midterm. These questions indicate much of the content that will be examined. Please note
that if a concept has been covered in the chapter or in class it may be examined even if it
does not appear as an example in this review.
Programming problem content covering the first two chapters will be at the level of
difficulty of the programming problems given on assignment 2 or programming exercises
1-5 chapter 2 or 2-5 in chapter 3
1. Consider the following tokens, classify each token as a valid identifier, a reserved
word, or an invalid identifier. If the token is an invalid identifier give a reason
why it is invalid. No reasons are necessary if the token is a valid identifier or a
reserved word
Run#2
2ndTry
switch
secondValue
$cost
average$cost
for
WHILE
loop
throw
integer
squirrel_food
_newprog
#counted
my.school
main
ideal
New
2. Can you represent each of the following values using a primitive data type. If you
can which primitive data type would you choose and why? If you cannot why
not?.
4
23.5
‘0’
7.8E-03
-276589876
‘ 876’
2147483650
‘+’
true
1.986E56
‘8’
-32.4789877689
‘hello’
3. What is a widening conversion and why is it important/
4. Define the following terms (any term defined in bold in your text or on slides is
fair game)
a. Syntax error
b. Reserved word
c. Identifier
d. Data type
e. Boolean variable
f. Precision of a floating point variable
g. Unary operator
h. Precedence
i. Integral expression
j. Implicit type coercion
k. Numeric string
l. Named constant
m. Variable
n. Assignment Statement
o. Concatenation
p. Escape Character
q. Class
r. Method
s. Reference variable
t. Instance
u. Arguments
v. Tokenizer
w. Import
5. Given the following variables
int aint, bint, cint, dint, eint;
short ashort, bshort, cshort;
long along, blong, clong;
double ad, bd, cd, dd;
char achar, bchar, cchar;
float afloat, bfloat, cfloat;
String astr, bstr, cstr;
What is the data type of the value of each of the following arithmetic expressions
and why? If the expression is not valid indicate why it is not valid.
a) aint + bint
b) afloat + bd
c) astr + bstr
d) afloat % bfloat
e) cstr – cint
f)
g)
h)
i)
j)
k)
l)
cfloat * ad
ashort / blong
(ashort + bshort) * afloat
along % bint
aint % bshort
afloat + aint * bint
astr – bstr
6. Given the following data
String a=”This is a test”, b=”of the string class”;
String c, d;
c=a;
d=” ”:
What are the values of each of the following expressions.
a) a.substring(5,9)
b) a + b
c) b.length();
d) c.charAt(5);
e) b.indexof(‘t’);
f) b.substring(3,13).toUpperCase();
g) a.equals(c);
7. Write Java statements to accomplish the following
a. Declare int variables x, y and z
b. Initialize float variable a that has already been declared
c. Assign the long integer I to the integer variable k
d. Declare an instance of class BufferedReader called key to read from the
keyboard
e. Import all classes in the package java.swing
f. Declare an instance of the class Decimal Format to output floating point
numbers with three decimal points of accuracy and print double variable
Big to the console using this format.
g. Create an input box to read an integer i2, you may assume that all required
classes have already been imported
h. Declare a constant CONVERSIONFACTOR with value 1.456
i. Print the values of the integer variable x and the integer expression
(x+2)*3-z to the console
j. Read the double value of the variable temp from the keyboard. Assume
BufferedReader keyboard has already been declared.
k. Read a line of data containing three integers from the keyboard into
integer variables i1, i2, and i3 . Assume the BufferedReader keyboard and
the StringTokenizer tokenizer have already been declared
l. Declare an instance, outFile, of class PrintWriter to write to a file
a:\input.txt
8. Explain the difference between the following two Java statements
import java.swing.*;
import java.swing.JOptionPane;
9. Label the reference variable the address and the Double object in the following
diagram which illustrates what happens in memory because of the following
declaration and initialization
Double factor;
factor = new Double(98.56);
factor
2598
98.56
10. Draw diagrams (similar to question 9) to show the values stored in memory as a
result of the following declarations and initializations. Show memory for all
variables declared below, after all the declarations and initializations have all
executed. Label reference variables, addresses and objects.
Integer a;
int i23;
Double factor;
char c=’W’
a = new Integer(43);
Which variables have not been initialized? What is the value of each of the
variables that has not been initialized?
Each of the following statements are executed in order. After each statement what
are the values of each of the variables declared above? Is the address associated
with reference variable a changed by any of these statements?
i23 = Integer.parseInt(a);
c++;
a = new Integer(7);
i23 /= Integer.parseInt(a);
11. Exercises on assignments 2 and 3 from pages 90-95 and 140-143, as well as all
additional exercises on these pages in the text. Most of these exercises have
solutions given in the text.
Download