Java Tutorial Questions

Topics: Java, Object-oriented programming, Integer Pages: 12 (3113 words) Published: April 10, 2013
Tutorial 1 - Calculation
1. What are the values of the following expression?
(a) 6 / 3 * 7
(b) 7 * 6 / 3
(c) 28 / 5
(d) 16 % 2
(e) 28 % 5
(f) 28 % 3 * 5 + 1
(g) 20 / (4 + 1)
(h) 18 / 2 + 14 / 2
2. What is the value of each of the following boolean expression?     (a) 4 >1
(b) 5 <= 15
(c) 43 <= 43
(d) 2 = = 3
(e) 2 + 5 = = 7
(f) 3 + 8 < = 10
(g) 3 != 9
(h) 13 ! = 13
(i) -4 ! = 4
(j) 2 + 5 * 3 = = 21
3. Are the following identifiers valid?
(a) applet
(b) Applet
(c) a++
(d) --a
(e) 4#R
(f) #44
(g) apps
4. Declare the following
(a) an int variable with an initial value of 0
(b) a long variable with an initial value of 10000
(c) a float variable with an initial value of 3.4
(d) a double variable with an initial value of 34.45     (e) a char variable with an initial value of 4
(f) a boolean variable with an initial value of true 5. Find the largest and smallest byte, short, int, long, float, and double. 6. Are the conversions shown below valid or invalid?  If valid, what is the result.     (a) char c = 'A';

int i = (int) c;
(b) boolean b = true;
int i = (int) b;
(c) float f = 1000.34f;
int i = (int) f;
(d) double d = 1000.34;
int i = (double) d;
(e) int i = 1000;
char c = (char) i;
(f) int i = 1000;
boolean b = (boolean) b;
7. Write a statement or comment to accomplish each of the following:     (a) state that this program will calculate the product of 3 integers.     (b) name the class name as Calculate
(c) declare the variable x, y and z to be of type int.     (d) initialize the variable x and y to be 3, and 4.
(e) assign the result of addition of x and y to variable z.     (f) print out the result as x + y = z.
8. Assume the following:
a = 2, b = 3, c = 4
Evaluate the following expressions:
i. y = c-- + ++b * ++c + b++ * 2
ii. y = ++c * --c + a++ * b-- - a--
iii. y = b++ - ++b + a-- * --c

9. Assume a = 1, and d = 1.0, and each of the expression is independent. What are the results of the following expression (a) a = 46/9
(b) a = 46%9+4*4-2
(c) a = 45+43%5*(23*3%2)
(d) a = 45+45*50%a --
(e) a = 45+1+45*50%(a--)
(f) d-= 3.4*(a+5)*d --
(g) a%=3/a+3

Tutorial 2 – Input & Output

1. Write a program to display the following information: (yourname_display.java)
ID: J02005300
Programme: Diploma in Communication Technology
Subject: CSC246

2. Write a program to input two positive integers and calculate and display their addition, subtraction, multiplication and division. 3. Write a program to read in a value in seconds (the value range from 1 to 10,000).  Convert and print out the seconds in hours, minutes and seconds format (0:00:00). (yourname_secondconversion.java)

4. Write a program to calculate and display the area of a (a) rectangle by reading the width and the height of the rectangle (b) circle by reading the radius of the circle

The output should be:

The area of a rectangle with width = 6 and height = 3 is 18
The area of a circle with radius = 3.5 is 38.9658

(yourname_areas.java)

Tutorial 3 – Selection Structures
1. The following scheme is used to determine the commission rate: (Yourname_commission.java)         Sales amount           commission rate        <=\$ 5000                     10 %         \$5001 - \$10000              15 %         > \$ 10,000               20 % Write a program to read in the sales amount performed by a salesperson and calculate and display how much he/she gets for his commission. 2. Write a program to read a character.  The program should indicate whether the entered character is vowel or consonant. Your program...