sol

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
Invalid Identifier: Identifier cannot contain character #
2ndTry
Invalid Identifier: Identifier cannot start with a digit
switch
Reserved Word
secondValue Valid Identifier: contains only alphabetic characters digits $
or _, and does not start with a digit
$cost
Valid Identifier: contains only alphabetic characters digits $
or _, and does not start with a digit
average$cost Valid Identifier: contains only alphabetic characters digits $
or _, and does not start with a digit
for
Reserved Word
WHILE
Valid Identifier: contains only alphabetic characters digits $
or _, and does not start with a digit (while is a reserved
word not WHILE)
Loop
Valid Identifier: contains only alphabetic characters digits $
or _, and does not start with a digit
throw
Reserved word
integer
Valid Identifier: contains only alphabetic characters digits $
or _, and does not start with a digit
squirrel_food Valid Identifier: contains only alphabetic characters digits $
or _, and does not start with a digit
_newprog
#counted
Invalid Identifier: Identifier cannot contain character #
my.school
Invalid Identifier: Identifier cannot contain character .
ideal
Valid Identifier: contains only alphabetic characters digits $
or _, and does not start with a digit
New
Valid Identifier: contains only alphabetic characters digits $
or _, and does not start with a digit ( new is Reserved word)
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
int
23.5
double or float
‘0’
7.8E-03
-276589876
‘ 876’
2147483650
‘+’
true
1.986E56
‘8’
-32.4789877689
‘hello’
char
double or float
int
invalid, character variable contains only 1 character
long
char
boolean
double (not float)
char
double (not float)
invalid, character variable contains only 1 character
3. What is a widening conversion and why is it important.
A conversion between different data types that can be done without losing any
accuracy in the converted number, the number is being converted from a type
with less width or accuracy to a type with more width or accuracy.
For example int to long (32 bits to 64 bits width), any int can be expressed as a
long.
4. Define the following terms (any term defined in bold in your text or on slides is
fair game) You can check against the definitions in your text
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
int
b) afloat + bd
double (afloat converted to double)
c) astr + bstr
String
d) afloat % bfloat
Invalid % is defined only for integers
e) cstr – cint
int
(cst is converted to int)
f) cfloat * ad
double (cfloat is converted to double)
g) ashort / blong
long (ashort is converted to long)
h) (ashort + bshort) * afloat
float (ashort + bshort, an int, is converted to
float)
i) along % bint
j) aint % bshort
k) afloat + aint * bint
l) 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)
“is a”
b) a + b
“This is a testof the string class”
c) b.length();
19
d) c.charAt(5);
‘i’
e) b.indexof(‘t’);
10
f) b.substring(3,13).toUpperCase();
“ THE STRING”
g) a.equals(c);
true
7. Write Java statements to accomplish the following
a. Declare int variables x, y and z
int x, y, z;
b. Initialize float variable a that has already been declared
a=12.5;
c. Assign the long integer I to the integer variable k
k = (int)I;
d. Declare an instance of class BufferedReader called key to read from the
keyboard
BufferedReader key =
new BufferedReader(new InputStreamReader(System.in) );
e. Import all classes in the package java.swing
import 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.
DecimalFormat threeDecimal= new DecimalFormat(“0.000”);
System.out.println(threeDecimal.format(Big));
g. Create an input box to read an integer i2, you may assume that all required
classes have already been imported
i2 = Integer.parseInt(JOptionPane.showInputDialog(“Enter an integer”));
h. Declare a constant CONVERSIONFACTOR with value 1.456
final double CONVERSIONFACTOR = 1.456;
i. Print the values of the integer variable x and the integer expression
(x+2)*3-z to the console
System.out.println(x + “ “ + ( (x+2)*3-z) );
j. Read the double value of the variable temp from the keyboard into double
variable D. Assume BufferedReader keyboard has already been declared.
D = Double.parseDouble(keyboard.readLine());
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
tokenizer = new StringTokenizer( keyboard.readLine());
i1 = Integer.parseInt(tokenizer.nexToken());
i2 = Integer.parseInt(tokenizer.nexToken());
i3 = Integer.parseInt(tokenizer.nexToken());
l. Declare an instance outFile of class PrintWriter to write to a file at
a:\input.txt
PrintWriter outFile = new PrintWriter(new FileWriter( a:\\input.txt));
8. Explain the difference between the following two Java statements
import java.swing.*;
import java.swing.JOptionPane;
The first statement will import all classes in the swing library
The second statement will import only the class JOptionPane from the swing
library
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);
reference
variable
factor
address
2598
double object
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);
reference
variable
a
i23
reference
variable
factor
c
address
Address 1
integer object
43
?
address
?
W
Which variables have not been initialized? What is the value of each of the
variables that has not been initialized?
The variables factor and i23 have not been initialized. Because they have not
been initialized their values are indeterminate (shown as ? in figure).
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);
a = ‘43’, i23=43, factor=?, c = ‘W’
c++;
a = ‘43’, i23=43, factor=?, c = ‘X’
a = new Integer(7);
a = ‘7’, i23=43, factor=?, c = ‘X’, The address of the reference variable a is
changed by this statement
i23 /= Integer.parseInt(a);
a = ‘7’, i23=43/7=6, factor=?, c = ‘X’
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