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

  1. 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’

  1. What is a widening conversion and why is it important/
  1. Define the following terms (any term defined in bold in your text or on slides is fair game)
  2. Syntax error
  3. Reserved word
  4. Identifier
  5. Data type
  6. Boolean variable
  7. Precision of a floating point variable
  8. Unary operator
  9. Precedence
  10. Integral expression
  11. Implicit type coercion
  12. Numeric string
  13. Named constant
  14. Variable
  15. Assignment Statement
  16. Concatenation
  17. Escape Character
  18. Class
  19. Method
  20. Reference variable
  21. Instance
  22. Arguments
  23. Tokenizer
  24. Import
  1. 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

  1. 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);

  1. Write Java statements to accomplish the following
  2. Declare int variables x, y and z
  3. Initialize float variable a that has already been declared
  4. Assign the long integer I to the integer variable k
  5. Declare an instance of class BufferedReader called key to read from thekeyboard
  6. Import all classes in the package java.swing
  7. 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.

  1. Create an input box to read an integer i2, you may assume that all required classes have already been imported
  2. Declare a constant CONVERSIONFACTOR with value 1.456
  3. Print the values of the integer variable x and the integer expression(x+2)*3-z to the console
  4. Read the double value of the variable temp from the keyboard.Assume BufferedReader keyboard has already been declared.
  5. 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
  6. Declare an instance, outFile, of class PrintWriter to write to a file a:\input.txt
  1. Explain the difference between the following two Java statements

import java.swing.*;

import java.swing.JOptionPane;

  1. 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

  1. 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);

  1. 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.