Create Software Components 

Using Java Level 2

 

 

Course Info

Scheme

Resources

Tutorials

Java Demos

Utilities

Links


 

   Lecture 5

Operators and Expressions: Answers

 

Use the code to find the answers to the activities. 

 

Activity A

Activity B

Activity C

Activity D

Activity E


Activity A

 

 

Activity A

  1. Determine the values of x and y at the end of the code fragment

int x = 10, y = 5;

x += 2;

y *= x;

  1. Determine the values of p and q at the end of the code fragment

int p = 10, q = 16;

p -= 2;

q /=  p;

Why not check your answers by using code.

 

 

To check your answers; try the following code:-

import java.awt.*;

public class ActivityA {

    public static void main (String[] args) {

        int x = 10, y = 5;

        int p = 10, q = 16;

        x += 2;

        y *= x;

        p -= 2;

        q /=  p;


        System.out.println ( "x is " + x);
        System.out.println ( "y is " + y);

        System.out.println ( "p is " + p);
        System.out.println ( "q is " + q);

    }
}


Activity B

 

Activity B

  1. Determine the values of x and y at the end of the code fragment

int x = 10, y = 5;

x = y + 2;

y = x + 4;

  1. Determine the values of p and q at the end of the code fragment

int p = 10, q = 5;

p =  4 - q;

q =  4 / p;

  1. Determine the values of rem at the end of the code fragment

int rem = 6 % 4;

Why not check your answers by using code.

 

To check your answers; try the following code:-

import java.awt.*;

public class ActivityB {

    public static void main (String[] args) {

       

        int x = 10, y = 5;

        x = y + 2;

        y = x + 4;

        int p = 10, q = 5;

        p =  4 - q;

        q =  4 / p;  

        int rem = 6 % 4;


        System.out.println ( "x is " + x);
        System.out.println ( "y is " + y);

        System.out.println ( "p is " + p);
        System.out.println ( "q is " + q);

        System.out.println ( "rem is " + rem);

    }
}


Activity C

 

 

Activity C

  1. Determine the values of ans and count at the end of the code fragment

int ans = 1, count = 2;

ans += count++;

  1. Determine the values of p and q at the end of the code fragment

int p = 10, q = 5;

p = ++q + 3;

  1. Determine the values of x and y at the end of the code fragment

int x = 2, y = 5;

y  += 3;

x *= y--;

Why not check your answers by using code.

   

To check your answers; try the following code:-

import java.awt.*;

public class ActivityC {

    public static void main (String[] args) {
       

        int ans = 1, count = 2;

        ans += count++;

        int p = 10, q = 5;

        p = ++q + 3;

        int x = 2, y = 5;

        y  += 3;

        x *= y--;


        System.out.println ( "ans is " +  ans);
        System.out.println ( "count is " +  count);

        System.out.println ( "p is " +  p);
        System.out.println ( "q is " + q);

        System.out.println ( "x is " +  x);
        System.out.println ( "y is " +  y);


    }
}


Activity D

 

 

Activity D

Determine the result of each of the following expressions.

  1. 6 + 3 * 4 / 2 

  2. (6 + 3) * 4 / 2 

  3. -5 * 4 - 6 / 2 

  4. -5 * (4 - 6) / 2 

Why not check your answers by using code.

   

To check your answers; try the following code:-

import java.awt.*;

public class ActivityD {

    public static void main (String[] args) {

 

        System.out.println ( "6 + 3 * 4 / 2 =  " +  (6 + 3 * 4 / 2));
        System.out.println ( "(6 + 3) * 4 / 2 =  " +  ((6 + 3) * 4 / 2));

        System.out.println ( "-5 * 4 - 6 / 2 " +  (-5 * 4 - 6 / 2));
        System.out.println ( "-5 * (4 - 6) / 2 =  " + (-5 * (4 - 6) / 2));

        

     }
}


Activity E

 

Activity E

Given the declaration

byte b2 = 65;

int i, j = 1000;

float f1, f2 = 5.2F;

char ch;

For each of the following, state the final type and value of the operand on the left side of the =.

  1. i = j + b1; 

  2. (int)f2;

  3. f1 = (int) f2 + 10; 

  4. i = (int) f2 * (byte)10.34; 

  5. ch = (char) (b1 + 3);

Why not check your answers by using code.

    

To check your answers; try the following code:-

import java.awt.*;

public class ActivityE {

    public static void main (String[] args) {
        byte b1 = 65;
        int i, j = 1000;
        float f1, f2 = 5.2F;
        char ch;

        i = j + b1;
        f1 = (int) f2 + 10;

        System.out.println ( "i = " + i);
        System.out.println ( "(int)f2 = " + (int)f2);
        System.out.println ( "f1 = " + f1);

        i = (int) f2 * (byte)10.34;
        ch = (char) (b1 + 3);

        System.out.println ( "i = " + i);
        System.out.println ( "ch = " + ch); 
    }
}


That is folks!!

 

  Site Home 

Java Home   

  Forum  

Course Info

Welcome

Overview

Assessment

Qualification

Scheme of Work

Assignments

Resources

Information

Blackboard

Learning Center

Web Materials

Reading

Java Demos

Utilities

Links

Lecture Materials

Tutorials & Notes

Exercises

Activities

Quizzes

 

Site Home

Top

Unit Home

ADR 2002