Vous êtes sur la page 1sur 4

Section A: Multiple Choice Questions

1. If a method is declared static in a particular class, what does that mean?

A. The method cannot modify any variables


B. It can only be called from within the class
C. It can access instance variables/methods
D. You do not need an instance of the class to access it

2. What does this statement do?

Song[] music;

A. Creates a new array of references to Song objects, called music


B. Creates a new array of references to music objects, called Song
C. Declares a variable called music whose type is an array of Song objects
D. Declares a variable called music whose type is null

3. What is the most specific result of the following code?

Integer[] someInts = new Integer[100];


double sum = 0.0;
for ( Integer i : someInts )
sum += i;
System.out.println( sum / someInts.length );

A. The code causes runtime error


B. Output is 0.001
C. Output is 0.0
D. The code will not compile

4. Which of the following statements regarding abstract class is false?

A. An abstract class must have abstract methods


B. An abstract class can be extended
C. A subclass can override a concrete method in a superclass to declare it abstract
D. An abstract class can be used as a data type

1
5. An immutable class cannot have _______.

A. private data fields


B. public data fields
C. no-arg constructor
D. static data fields

6. Assume an employee should work for one and only one company. What is the best
suitable relationship between class Company and class Employee?

A. Encapsulation
B. Aggregation
C. Inheritance
D. Composition

7. You can declare two variables with the same name __________.

A. in a method one as a formal parameter and the other as a local variable


B. in a block but with different types
C. in two nested blocks in a method
D. in two separate blocks in a method

8. Analyze the following code:

class Circle {
private double radius;

public Circle(double radius) {


radius = radius;
}
}

A. The program has a compilation error because it does not have a main method
B. The object of this class will always have radius 0
C. The program has a compilation error because you cannot assign radius to radius
D. The program does not compile because Circle does not have a default constructor

2
9. Which of the following statements is correct?
A. A method can be overridden in the same class
B. If a method overloads another method, these two methods must have the same
signature and return type
C. If a method overrides another method, these two methods must have the same
signature and return type
D. A method in a subclass cannot overload a method in the superclass

10. Analyze the following code:

public class Test {


public static void main(String[] args) {
String s = new String("Welcome to Java");
Object o = s;
String d = (String)o;
}
}

A. When assigning s to o in Object o = s, a new object is created


B. When casting o to s in String d = (String)o, a new object is created
C. When casting o to s in String d = (String)o, a compile error occurs
D. s, o, and d reference the same String object

3
Section B: Programming Questions

(a) A Room is an abstract class that has two data members, humidity and temperature, to indicate
the conditions of the room and both are private and type double. This class has one
constructor that accepts both humidity and temperature as arguments and set the object's
humidity and temperature accordingly. BedRoom and LivingRoom are two concrete
subclasses of the Room class. Create the three classes and provide appropriate accessor and
mutator methods for the classes.

(b) IOperate is an interface that defines a single method control(Room r), which is used to
control the humidity and temperature of a room.

(c) An AirCond (air conditioner) class implements the IOperate interface and reduces the room
temperature and humidity level every time the control(Room r) method is called. If the room
object is a BedRoom, then the control method reduces the temperature and humidity of the
bedroom by 10. If the room object is a LivingRoom, it reduces the living room’s temperature
and humidity by 5. Create the AirCond class according to the above description.

(d) Draw the UML class diagram that shows the relationships of the above classes. Your class
diagram should provide details of all the data members and methods of each class

Vous aimerez peut-être aussi