“Paper 1” Type question

advertisement
“Paper 1” Type question
Consider the following Java class:
1.
public class TestClass {
2.
3.
public static void main (String args []) {
4.
5.
int sum=0, num;
6.
7.
for(int i=1; i <= 10; i++) {
8.
num = Keyboard.readInt();
9.
sum+=num;
10.
}
11.
12.
System.out.println(“Answer is “ + sum);
13.
}
14. }
1. From the program above, identify
a. A variable
b. A standard class
c. A Java keyword
2. What is the meaning of the statement in line 9?
3. Give an example of variable initialisation
4. How many numbers are processed by this program?
5. What is the purpose of the above program?
6. Rewrite the above code in the main method using a while loop instead of a for loop.
“Paper 2A” type question
Consider the following Java class:
public class Circle {
double radius;
}
1. What is the difference between an object and a class?
2. Write another class called Shapes, and in its main method, declare c as being an object of type
Circle. Assign the value 3.4 to c’s radius.
3. Copy the above class and add the following methods:
double getArea () {
}
which returns the area of the circle and
double getPerimeter() {
}
which returns the perimeter of the circle.
4. Write a code snippet (not a full program), to create 10 circles with random radius and display their
area and perimeter on screen. Do not use arrays.
“Paper 2B” type question
Consider the following Java class:
public class Box {
int width, height;
}
1. What is the difference between an object and a class?
2. Copy the above class and add the following method:
void displayArea () {
}
Inside it implement Java statements which displays the area of the box on the screen. And the
method:
void displayPerimeter() {
}
which displays the perimeter.
3. Write another class called Shapes, and in its main method, declare b as being an object of type Box.
Assign the value 10 to its width and 15 to its height. Call the method displayArea() to display the
area on screen.
Download