Java Programming Language
Java Logic Statement Syntax Exercises #2

These exercises are designed to evaluate your knowledge, comprehension, application and analysis of the Java logic statement syntax. It is expected that you should be able to correctly answer all or nearly all of the questions and problems presented. You should do these exercises by yourself.

1. Given the following Java expressions evaluate the expressions, remembering about wrap around if the type size is too small, type conversion and casting, give the result and indicate any side effects that may occur.

a. ((4 * 3 + 3) / 5 * 3)++ % 3;

 

b. (4 * 3 + 3) / 5 * 3++;

 

c. (4 * 3 + 3) / 5 * ++3;

 

d. byte b = 64 * 3;

 

e. int v1 = 3.14 * 2;

 

f. long v2 = 1024 * (byte) 4;

 

g. String s = "The value of v2 is: "+v2;

 

h. byte b = (byte) int i = 13;

 

i. !((true && false) || true && false);

 

j. (!(true && false) || true && false);

 

k. (!(true && false) || false && true);

 

l. char c = 'Z'--;

 

m. short s = 0xC0 | 3;

 

n. byte b = 0xAA ^ 0xFF;

 

o. int x = 23, y = 32; int max = (x > y) ? x : y;

 

p. -27 + +32;

 

q. int zz = 12; zz*=2;

 

 

2. For each of the following write expressions in Java syntax and the value of the expression evaluation based on the given an English statement.

a. Add 3 to 12 then multiply by 3 and subtract 1, all of this is added to 6 and then divided by 3. Assign this to the int variable v1. What is the value of v1?

 

b. Using the value of the variable v1 from (a.) add 2 and then assign the variable v2 to the modulus of that value. What is the value of v2?

 

c. Add 3.14 to .00159 then multiply by 2 and then by 10. Assign this expression to the variable v3. What is the value of v3?

 

d. Given the boolean variable b1, b2, b3 and b4, write an expression that evaluates if all of the variables is equal to true. No value is necessary to be evaluated.

 

e. Given the boolean variable b1, b2, b3 and b4, write an expression that evaluates if any of the variables is equal to false. No value is necessary to be evaluated.

 

f. Given the boolean variable b1, b2, b3 and b4, write an expression that evaluates if b1 and b3 are both false and if either b2 or b4 is true. No value is necessary to be evaluated.

 

g. Compare if the value of v2 decremented by one is greater than or equal to v1. Is the comparison true or false?

 

h. Given v4 is 32, compare v4 with v1 to test if they are not equal. Are they equal?

 

i. Combine the string "hello" and "there" together with a space between the words.

 

j. Given the byte value 11001011, shift the value 2 bits to the left and then three bits to the right with sign extension and then and that value with 00110110 and then or it with 11001011. Write this expression. What is the resulting value?

 

k. Write an expression to take the two's complement of the byte value 00001100. What is the two's complement value?

 

l. Write an expression that test boolean value of the comparison in (h.) above and if it is true then assign the variable v6 to v4 otherwise assign v6 to v1. What is the value of v6?

 

 

3. Identify the following Java statement as one of the types: expression, compound, empty, labeled or variable declaration.

Java Statement

Statement Type

int a = 3;

 

;

 

byte[] b;

 

exit:

 

a = (a+2)%2;

 

--c;

 

long v1;

 

{ x=3; y=x*2; }

 

 

4. Identify valid & invalid code fragment of keyword statements, if valid, what does the code do, if invalid, why?

  1. int a = 1, b = 2 c=3;

 

  1. float f1 = 1.0;
  2.  

  3. String s;
  4.  

  5. final char c = *;
  6.  

  7. char c = "x";
  8.  

  9. if x==1 { System.out.println("Hello"); }

 

f.

char c = B;
switch (c)
{
case A : break;
case B : r=1; t=2;
case C: r=5; break;
}
System.out.println(c);

 

g.

while (a<10);
{
	a+=1;
}

 

h.

do
{
	a-=1
} until (a<-10);

 

i.

for (j=0, k=2; j*k<100; j++)
{
	if (j == 25) continue;
	System.out.println(j);
}

j.

a = squareRoot(9);
System.out.println(a);

k.

if (data !=null && i < data.length && data[i] != -1)