0% found this document useful (0 votes)
15 views6 pages

Exercise Questions June 2025

The document contains a series of multiple-choice questions related to Java programming concepts, including instance variables, constructors, method overloading, and array handling. Additionally, it includes programming tasks requiring the design of classes for a fan, hotel room reservation system, and hotel management. The questions and tasks assess knowledge of Java syntax, object-oriented principles, and class design.

Uploaded by

m-6368933
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Exercise Questions June 2025

The document contains a series of multiple-choice questions related to Java programming concepts, including instance variables, constructors, method overloading, and array handling. Additionally, it includes programming tasks requiring the design of classes for a fan, hotel room reservation system, and hotel management. The questions and tasks assess knowledge of Java syntax, object-oriented principles, and class design.

Uploaded by

m-6368933
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Part I: Multiple Choice Questions:

1 A(n) instance variable is declared inside a class definition and not inside any specific
method.

a) true
b) false

2 To assign a double variable d to an int variable x, you write

a) x = (long)d
b) x = (int)d;
c) x = d;
d) x = (float)d;

3 A constructor

a) is only needed when a class requires user input


b) specifies a return value of void
c) must always be explicitly supplied by the programmer
d) is used to initialize instance variables when the object is created

4 A variable that is declared inside a method is called ________ variable.

a) an instance
b) a local
c) a global
d) a class

5 The import statement

a) is not needed in Java


b) tells the compiler it can search for a particular class within the specified
package
c) places your source code into a package
d) cannot use the asterisk to specify a class search within a package

6 A package in Java

a) typically consists of related classes that reside in the same directory


b) consists of related classes residing across multiple directories
c) cannot be created by a programmer
d) typically consists of unrelated classes residing in the same directory
7 Read the code below. What will be the result of attempting to compile and run the
code below.

public class AQuestion {


public void method(int number) {
[Link]("Integer Version");
}
public void method(String s) {
[Link]("String Version");
}
public static void main(String args[]) {
AQuestion question = new AQuestion();
[Link](null);
}
}

a) The code does not compile.


b) The code compiles cleanly and shows "Integer Version".
c) The code compiles cleanly and shows "String Version"
d) The code throws an Exception at Runtime.

8 Analyze the following code:

public class Test {


public static void main(String[] args) {
int[] x = {0, 1, 2, 3, 4, 5};
xMethod(x, 5);
}

public static void xMethod(int[] x, int length) {


for (int i = 0; i < length; i++)
[Link](" " + x[i]);
}
}

a) The program displays 0 1 2 3 4.


b) The program displays 0 1 2 3 4 and then raises a runtime exception.
c) The program displays 0 1 2 3 4 5.
d) The program displays 0 1 2 3 4 5 and then raises a runtime exception.

9 Assume the signature of the method xMethod is as follows.

public static void xMethod(double[] a)

Which of the following could be used to invoke xMethod?

a) xMethod(5);
b) xMethod({3, 4});
c) xMethod(new int[2]);
d) xMethod(new double[2]);
e) None of the above.
10 A default constructor must have the same name but different number or types of
parameter.

a) true
b) false

11 Given the declaration Circle x = new Circle(), which of the following statement is
most accurate.

a) x contains an object of the Circle type.


b) You can assign an int value to x.
c) x contains a reference to a Circle object.
d) x contains an int value.

12 An array reference variable can be used in which of the following ways?

a) As a local variable
b) As a parameter of a method
c) As a return value of a method
d) All of the above

13 Analyze the following code:

class Test {
public static void main(String[] args) {
[Link](xMethod((double)5));
}

public static int xMethod(int n) {


[Link]("int");
return n;
}

public static long xMethod(long n) {


[Link]("long");
return n;
}
}

a) The program displays int followed by 5.


b) The program displays long followed by 5.
c) The program runs fine but displays things other than given in a and b.
d) The program does not compile.
e) None of the above.

14 Array variable are reference variables.

a) true
b) false
15 Each Java class must contain a main method.

a) true
b) false

16 A(n) ____ field belongs to the class, not to any object of the class.

a) static
b) instance
c) private
d) public

17 With a UML class diagram, .


I. data members show a + to indicate public access
II. the name of the class appears at the bottom section of the diagram
III. methods are not shown
IV. objects of the class appear in the middle section of the diagram

a) I only.
b) I and II.
c) I and III.
d) all of the above.

18 Based on the statement below, which of the following codes will cause an error?

ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();

a) BankAccount anAccount = new BankAccount(1729);


[Link](2, anAccount);
b) [Link](new BankAccount(1015);
c) int i = [Link]();
anAccount = [Link](i);
d) import [Link];

19 Arrays suffer from a significant limitation: ____.

a) index values range from 0 to length + 1


b) you cannot determine the number of elements in the array
c) you cannot copy values from one array to another
d) their length is fixed

20 An instance method must have a parameter variable.

a) true
b) false
Part II: Program Writing

Q1. Design and write a Java class named Fan to represent a fan.
The data items for the class are colour, speed (whose value is either 1,2 or 3) and status
(whose value is either ‘ON’ or ‘OFF’).
The class contains only the accessor methods to retrieve the value of instance variables.
The class has a constructor with 1 parameter that represents the fan colour.
The constructor initialises speed to 1 and status to ‘OFF’.
The class has an instance method named setSpeed.
The method has 1 parameter representing the speed for a fan.
The method modifies status to ‘ON’ and changes fan speed to the parameter value.

Q2. (a) A hotel room reservation application has a class Room that represents a hotel
room and a class Guest that represents a guest who has checked into the room.
The class Guest has instance variables name (type String) that represents the name
of the guest who has checked in to a room and dateIn (type String) that represents
the date of check in, with accessor methods only to retrieve the instance variables.
It also has a constructor with 2 parameters to initialise the instance variables.

Write the class definition for class Room. It has instance variables number (type
int) that represents the room number and guest (type Guest) that represents a guest
who has checked into the room. You must include the followings:

• Accessor methods only to retrieve the instance variables.


• A constructor with 1 parameter that represents the room number to initialise
number. The constructor initialises guest to null.
• An instance method named checkIn() that has 2 parameters that represent the
name of the guest who has checked in and the date of the check in. The
method creates a Guest object and assigns it to instance variable guest. The
method does not return anything.
• An instance method named checkOut() that does not have any parameters and
does not return anything. The method assigns null to instance variable guest.
• An instance method named isAvailable() that returns false if instance variable
guest currently refers to a Guest object and true if it is null. The method does
not have any parameters. [Note: true and false are type boolean].
(15 marks)
(b) Refer to the Room class and Guest class in Q2. (a), write the class definition for
class Hotel. It has instance variables numberOfRooms that represents the number
of rooms in the hotel and rooms which is an array that can store up to 3 Room
objects. You must include the followings:

• Accessor methods only to retrieve the instance variables.


• A constructor that receives an array of Room objects of size 3 as a parameter.
The constructor assigns the array to rooms and initialises numberOfRooms to
3.
• An instance method named findRoom() that has a parameter that represents a
room number. The method returns the Room object that has the matching
room number. If no Room object matches the room number, the method
returns null.

(c) Refer to the Hotel class in Q2. (b), write the main() method for another class
HotelApp that:
• creates 3 Room objects with room numbers 101, 102, and 103 and stores them
in an array.
• then creates a Hotel object to represent a hotel with 3 rooms represented by
the 3 Room objects.
[Note: No need to do any input. Hardcode the values]

You might also like