Ira Pohl MT 1. CMPS012a Review questions
To get the answers - write up and run as code.

You should also study the text review questions and review your programming assignments.


For each of following tokens, specify whether it is a legal identifier, a keyword, an operator, or none of these.

 

| identifier | keyword | operator | illegal |

firstNumber
second_one
third__
4th
And
&
__2
print
double
real

 

 

2. Assume the following declarations:

int i = 1, j = 3, k = 4;

 

Fill in the integer value of each expression (assuming they are evaluated in

the order they are written).

 

expression value

 

i == 0 && j != 0 ______

 

j % i ______

 

i / j ______

 

2 * i + 1 ______

 

j <= k ______

 

!!(k == 0) ______

 

(i + j) * k-- ______

 

k > 2 && i > 6 ______

 

j == 2 || i != 4 ______

 

k = ++j ______

 

Also what would be the equivalent parenthesized expressions?

Example: The last expression fully parenthesized is ( (k > 2) && (i > 6))

 

 

3. What does the following program print ?

 

 

class Test3{
public static void main(String[] args)
{
       int i = 5, j, sum = 0;

       for ( j = 0; i != 0; i--) {
          sum += i * i;
          System.out.println("sum = " + sum);
       }
}
}

 

 

4. What gets printed?

 

class Test4{

/* not indented properly */

public static void main(String[] args)

{

int i = 3, j = 6, k = 2;

if (i != 3) if (j == 6)
System.out.println( i = i + j % k);
else
System.out.println( i = i - j % k );
System.out.println( i + 2);
System.out.println( i = i + j % k);
}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

5. Now the integer value for the character for '0' is 48, and

for 'A' is 65, and for 'a' is 97.

 

What is printed?

 

class Test5{

public static void main(String[] args)
{
   char c = '0';
   int i = 4;

   System.out.println(c);
   System.out.println(c + i);
   System.out.println((char)(c + 20));
}
}

 

 

6. Show the output that would be produced by the following program:

 



class Test6{
public static void main(String[] args)
{
   int x = 1, y = 2, k = 10;

   System.out.println("x = " + x + " y = " + y + " k = " + k);
   k = foo(x, y);
   System.out.println("x = " + x + " y = " + y + " k = " + k);
}

static int foo(int y, int x)

{
   int tmp = 5;


   tmp = x;
   x = y;
   y = tmp;
   return (x + y);
}
}