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
- 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
- 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’
- What is a widening conversion and why is it important/
- Define the following terms (any term defined in bold in your text or on slides is fair game)
- Syntax error
- Reserved word
- Identifier
- Data type
- Boolean variable
- Precision of a floating point variable
- Unary operator
- Precedence
- Integral expression
- Implicit type coercion
- Numeric string
- Named constant
- Variable
- Assignment Statement
- Concatenation
- Escape Character
- Class
- Method
- Reference variable
- Instance
- Arguments
- Tokenizer
- Import
- 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)cfloat * ad
g)ashort / blong
h)(ashort + bshort) * afloat
i)along % bint
j)aint % bshort
k)afloat + aint * bint
l)astr – bstr
- 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);
- Write Java statements to accomplish the following
- Declare int variables x, y and z
- Initialize float variable a that has already been declared
- Assign the long integer I to the integer variable k
- Declare an instance of class BufferedReader called key to read from thekeyboard
- Import all classes in the package java.swing
- 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.
- Create an input box to read an integer i2, you may assume that all required classes have already been imported
- Declare a constant CONVERSIONFACTOR with value 1.456
- Print the values of the integer variable x and the integer expression(x+2)*3-z to the console
- Read the double value of the variable temp from the keyboard.Assume BufferedReader keyboard has already been declared.
- Read a line of data containing three integers from the keyboard into integer variables i1, i2, and i3 . Assumethe BufferedReader keyboard and the StringTokenizer tokenizer have already been declared
- Declare an instance, outFile, of class PrintWriter to write to a file a:\input.txt
- Explain the difference between the following two Java statements
import java.swing.*;
import java.swing.JOptionPane;
- 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
- 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);
- 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.