You are on page 1of 5

Assignment for Mid-Semester Break

CSC3102
Answer all questions. You should write the answers on papers. Submit on Sunday,
19th September 2010 before noon.

(1) Give 2 examples of High Level Language.


(2) Find and fix the error of the program below:

//This program prints Welcome to UMT!

public class Welcome {

public static void main(String[] args)

System.out.println(“Welcome to UMT!)

(3) Based on program in Question 2, what is the class name of the program?
(4) Write one statement in java code to declare variable A as integer and initialize it to
value 20?

(5) What is the output of the program below?

//This program prints Value of result!

public class Calculation {

public static void main(String[] args)

int result;

result =2+4-1+10/5*2;

System.out.println(“Result is “ + result);

1|Page
(6) Give 3 types of Programming Error.
(7) What the name of this java operator %? Give an example.
(8) What is the output of the program below?

//This program prints Value

public class casting {

public static void main(String[] args)

int result1 = 4;

double result2 = 5.1;

System.out.println( (double) result1);

System.out.println( (int) result2 );

}}

(9) What is the output of the program below?

//This program prints Value of result!

public class casting1 {

public static void main(String[] args)

char letter = „a‟;

int result;

result = (int) letter;

System.out.println( (char)(result+1) );

}}

2|Page
(10) Declare a char variable named letter and a String variable named street.

(11) What will the following code segments print on the screen? (2 m)

int freeze = 32, boil = 212;

freeze = 0;

boil = 100;

System.out.println(freeze + “\n” + boil + “\n”);

(12) Show how the double variables temp, weight, and age can be declared in one
sentence.
(13) Write an assignment statements that perform the following operations with the
variables a, b, c

a. Adds 2 to a and stores the result in b.

b. Multiplies b times 4 and stores the result in a.

c. Divides a by 3.14 and stores the results in b.

d. Subtracts 8 from b and stores the result in a.

e. Stores the character ‘K’ in c.

(14) State which of the following are valid Java identifiers. For those that are not
valid, explain why.

a. length

b. import

c. 6months

d. hello-and-goodbye

3|Page
(15) Write the segment of code to display a dialog box that asks the user to enter his or
her desired annual income. Store the input in a double variable.

(16) Give two different reasons for using named constants.

(17) If you have N eggs, then you have N/12 dozen eggs, with N%12 eggs left over. (This
is essentially the definition of the / and % operators for integers.) Write a program that
asks the user how many eggs she has and then tells the user how many dozen eggs she
has and how many extra eggs are left over.

A gross of eggs is equal to 144 eggs. Extend your program so that it will tell the user
how many gross, how many dozen, and how many left over eggs she has. For example,
if the user says that she has 1342 eggs, then your program would respond with

Your number of eggs is 9 gross, 3 dozen, and 10

since 1342 is equal to 9*144 + 3*12 + 10.

(18) Say that you are interested in the value of the quadratic

3X2 -8X + 4

for several values of X. Write a program that has a double precision variable X. Assign a
value to it. Write statement that computes a value for the quadratic and stores the
result in another double precision variable. Finally write out the result, something like:

At X = 4.0 the value is 20.0

Run the program with several values for X (edit the program for each value of X) and
examine the result. Use values with decimal points, large values, small values, negative
values, and zero. Write all the results for each of the test values.

4|Page
(19) If you know the balance and the annual percentage interest rate, you can compute
the interest on the next monthly payment using the following formula:

Interest = balance X (annualInterestRate / 1200)

Write a program that reads the balance and the annual percentage interest rate and
displays the interest for the next month in two versions: (a) Use dialog boxes to obtain
input and display output; 9b) Use console input and output. A sample run of the
console input and output is shown below:

Enter balance: 100000

Enter annual interest rate: 4.625

The interest rate is 385.41

(20) Write a program that display the following table:

a b pow(a,b)

1 2 1

2 3 8

3 4 81

4 5 1024

5 6 15625

~~Questions end~~

5|Page

You might also like