Вы находитесь на странице: 1из 5

Part 1.

Multiple Choice Questions (15 marks 15 minutes)

1. One of the following is included in Java SDK:


a. NetBeans IDE c. Java enabled web browser
b. Java debugger d. Operating system
2. One of the following can be used to write in-line comments:
a. !* c. \
b. // d. !!
3. Which one of the following data types is a primitive data type in Java:
a. Array c. String
b. StringBuffer d. long
4. The value of x after evaluating the following expression double x = 9.0/2.0 + 9/2 is:
a. 8.0 c. 8.5
b. 9 d. 9.0
5. Which of the following is used to declare and initialize an array in Java?
a. int numbers;
b. int numbers[];
c. int[] numbers = {1, 3, 4};
d. int[] numbers = new int[3];
6. When s=Egypt , then the output from System.out.println(s.length()) is :
a. 4 c. 6
b. 5 d. The compiler gives a run-time
error
7. _________methods correspond to a simple request for information and cannot alter the state of the
object; for example, anObject.getX();
a. Accessor c. Constructor
b. Mutator d. Setter
8. Which of the following is true for constructors?
a. They have the same name as a c. They are invoked when an object
class is instantiated
b. They do not specify a return type d. All of the above
9. Which of the following is a method of the Scanner class that returns a token from this scanner?
a. next() c. read()
b. nextLine() d. readLine()
10. Which of the following is an unchecked exception?
a. FileNotFoundException c. IOException
Page 1 of 4
b. EOFException d. RunTimeException

Page 2 of 4
Part2. Essay: Answer the following questions. (30 marks 40 minutes)

1. What do we mean when we say that Java is a threaded language? (4 marks)


4 marks
Threaded means that Java allows a program to do several things at once.
2. What is the difference between primitive variables and reference variables? Give an example.(6 marks)
6 marks
Primitive variables store data values. (2 marks)
e.g. int x = 5; (1 mark)
Reference variables do not themselves store data values, but are used to refer to objects.(2 marks)
e.g. String s = Ali; (1 mark)

3. (a) What does casting mean in Java? Give a casting example for primitive types.(7 marks for a and b)
(b) When is a cast required?
7 marks
Casting means converting from one type to another. (2 marks)
e.g. double d = 2.1; int x = (int) d; (2 marks)
A cast is required if the type you are assigning (on the right-hand side of = ) occupies a larger space
in memory than the type you are assigning to (on the left-hand of =) (3 marks)
4. (a) What is a constructor in Java? (6 marks for a and b)
(b) What is a default constructor?
6 marks
A constructor is a special type of subroutine called at the creation of an object. Constructors are
used to give an object an initial state when it is created (3 marks)
A default constructor is a special constructor provided by java in the case that we dont write any
constructor. The default constructor has no arguments. (3 marks)
5. Briefly explain the basic steps for creating a stream in Java. (7 marks)
7 marks
Step 1. Open the stream: you need to define some objects here(2 marks)
Step 2. Until there is no more data, keep reading in a read, or writing in a write: You need to use
the methods of the objects defined in step 1 to read the stream. (3 marks)
Step 3. Close the stream(2 marks)

Page 3 of 4
Part 3. Problem solving: Answer the following questions. (55 marks 65 minutes)

1. Develop a Java program that includes the following: (30 marks)


(a) A public class Circle that:
- has a private instance variable, double radius.
- has a zero-argument constructor that sets the default value of radius to 1.0.
- has a one-argument constructor that sets radius to a given value.
- has a public method, getRadius(), for retrieving the radius
- has a public method getArea()which returns the area of the circle that is equal to .r2,
where r is the radius, and is a constant equal to 3.14159.
(b) A public class Cylinder that:
- extends Circle class.
- has one public instance variables, double height
- has a zero-argument constructor that sets the default value of height to 1.0.
- has a two-argument constructor that sets the values of height and radius to given
values. You should invoke the superclass one-argument constructor for setting radius.
- has a public method, getHeight(), for retrieving the height.
- has a public method, getVolume(), for computing and returning the volume of cylinder
where: volume = base area X height. Use superclass method getArea() to get the base
area.
- overrides the Objects toString() method in order to returns a string consisting of a
cylinders radius, height, base area, and volume; for example,
radius: 5, height: 10, base area=78.5398, volume=785.398
27 marks for all statements (as distributed below)
+ 3 marks for overall logical flow and structure of the program

(a)
public class Circle {(1 marks)
private double radius; (1 mark)
public Circle() {radius = 1.0;} (2 marks)
public Circle(double r) {radius = r;} (2 marks)
public double getRadius() {return radius;} (2.5 marks)
public double getArea() {return radius*radius*3.14159} (2.5 marks)
}
(b)
public class Cylinder extends Circle {(2 marks)
private double height; (1 mark)
public Cylinder() {height = 1.0;} (2 marks)
public Cylinder(double radius, double h) {(2 marks for the constructor)
super(radius);
height = h;
}
public double getHeight() {return height;} (2 marks)
public double getVolume() {return getArea() * height;} (3 marks)
public String toString() { return "Cylinder: radius=" + getRadius() + ", height=" + getHeight()
+ ", base area=" + getArea() + ", volume=" + getVolume();(4 marks)
Page 4 of 4
}
}
2. Write a Java program that asks the user to enter his/her first name and last name as two separate
inputs, and then concatenates the first name and last name separated by a space. The program
should finally display a greeting message to the concatenated name, e.g. Hi Ahmed Ali. You dont
need to write any import statements. (13 marks)
11 marks for all statements (as distributed below)
+ 2 mark for overall logical flow and structure of the program

public class shortQ {


public static void main(String[] args) {
String first, last, full; (1 mark)
Scanner in = new Scanner(System.in); (2 marks)
System.out.println("Enter your first name");(1 mark)
first = in.nextLine();(2 marks)
System.out.println("Enter your last name");(1 mark)
last = in.nextLine();(2 marks)
full = first + " " + last; (1 mark)
System.out.println("welcome " + full); (1 mark)
}
}
Note to tutors: of course several other java classes could be used for input, e.g.
BufferedReader and JOptionPane.
3. Write a Java program to write the statement This is easy! to a text file c:\mta.txt. Use
trycatch statement to handle FileNotFoundException, and display a simple error
message in case an exception happens. You dont need to write any import statements. (12 marks)
10 marks for all statements (as distributed below)
+ 2 mark for overall logical flow and structure of the program

public class WriteTextToFile {


public static void main(String[] args) {
try { (1 mark)
PrintWriter pr = new PrintWriter("c:/mta.txt");(3 marks)
pr.println("This is easy!"); (2 marks)
pr.close();(1 marks)
} catch (FileNotFoundException ex) {(2 marks)
System.out.println("Error!"); (1 marks)
}
}
}

Page 5 of 4

Вам также может понравиться