Documente Academic
Documente Profesional
Documente Cultură
MID-TERM EXAMINATION
SEMESTER II, 2016/2017 SESSION
This Question Paper consists of 16 Printed Pages including cover page containing 3 parts:
INSTRUCTION(S) TO CANDIDATES
1
PART A: MULTIPLE CHOICE QUESTIONS (MCQ) - 20 MARKS
Instruction: Please CIRCLE the correct answer.
2. If your program needs to write data to a file or to read from a database table, but the
file or the database table does not exist, an error would occur. What kind of error is
this?
class Increment {
public static void main(String args[]){
int a = 3;
int b = 2;
System.out.print(++a * 8 + b--);
}
}
A. 24 B. 27 C. 26 D. 34 E. 35
A. 45 B. 44 C. 46 D. 50 E. Zero
2
6. What is the output when the following code is executed?
int x = 2;
int y = 5;
if (x + y < 8 && (x != y)) {
if (y > x && x != 12) {
System.out.print("A ");
} else {
System.out.print("B ");
}
System.out.print("C ");
} else {
if (x >= 0 && ! (x < 7)) {
System.out.print("D ");
} else {
System.out.print("E ");
}
System.out.print("F ");
}
if (y > x * 2) {
System.out.print("G ");
}
7. The following method takes an array of integers as input and returns the smallest value
in the array. However, the condition in the if statement is missing:
8. When you invoke a method with a parameter, the value of the argument is passed to
the parameter. This is referred to as _________.
3
9. Analyze the following code:
x = new int[2];
11. Given the following class definition, which of the following method is invalid to be
placed after the comment line?
4
12. Which of the following statement is TRUE?
A. Selection sort repeatedly selects the smallest number and swaps it with the last
number in the list.
B. Linear search algorithm is more efficient than binary search as it eliminate at
least half of the array after each comparison.
C. It is unnecessary to sort the array elements before implementing binary search
on arrays.
D. Binary search is useful for finding an element in a small array or unsorted array.
E. Binary search is more efficient than linear search for large arrays, but it requires
that the array be presorted.
14. Providing access to an object only through its member methods or services, while
keeping the details private is called as ________________________.
15. Java programmers concentrate on creating ____________, which contain fields and the
set of methods that manipulate those fields and provide services to clients.
5
17. What is wrong in the following program?
class YourClass {
public int x;
static int y;
void call(int a, int b){
x += a ;
y += b;
}
}
class MyClass {
public static void main(String args[])
{
YourClass obj1 = new YourClass();
YourClass obj2 = new YourClass();
obj1.x = 0;
obj1.y = 0;
obj1.call(2, 4);
obj2.x = 0;
obj2.call(1, 3);
System.out.print(obj1.x + " " + obj1.y);
System.out.print(" "+ obj2.x + " " + obj2.y);
}
}
A. 2 4 1 7 B. 2 4 1 3 C. 2 7 1 7
D. 1 5 2 4 E. 1 3 1 7
6
20. Given the declaration Circle[] x = new Circle[10], which of the following
statement is most accurate.
1. Java programs are considered portable and architecture-neutral. Explain how does these
features are implemented in Java.
(3 marks)
void h()
{
System.out.println(“Inside method h);
}
}
Answer:
7
B.
public class A{
int i =5;
static int k = 2;
return (int)(Math.pow(i,j));
}
}
Answer:
C.
class Test { Answer:
public static void main(String[] args) {
A a = new A();
a.print();
}
}
class A {
String s;
A(String s) {
s = this.s;
}
8
3. Write a full Java program that displays the following pattern
*
**
***
****
*****
******
*******
********
(3 marks)
9
4. Write a method that returns true if two lists, list1 and list2 are strictly identical (list1
and list2 are strictly identical if their corresponding elements are equal), using the
following header:
(4 marks)
5. Given the following UML design for a Event class:
Event
-eventName:String
-eventDate:String
-noOfGuest: int
+ Event ()
+ Event(String, String, int)
+display()
get_event_name(): String
get_event_date(): String
get_no_of_guest(): int
change_event_date(String newDate):void
b. Create an Event object with a specified event name, date, and number of guest.
10
c. Assign the Event object create in (b) as the fifth array element.
d. Change the event date for the event object created in (b) to be “31-03-2017”.
e. Invoke the get_no_of_guest() method of the object in the second array element.
(5 marks)
6. Assume you have a java class called Worker that contains the following object variables
Id, name, position, department and salary. Assume the relevant constructors, and
setter and getter methods have also been written but not shown due to space limit.
Assume you have a test class named Test_Worker that contains two methods, main and
get_total_and_avg_salary as shown below. Suppose in main method we have created 10 worker
objects and stored them in array named workers.
public class Test_Worker {
public static void main(String[] args) {
Worker [] workers= new Worker [10];
…………………
…………………
for (int i = 0; i < workers.length; i++) {
System.out.println("Enter the details of "+ (i+1)+"
worker");
System.out.println("Enter the id");
int id = input.nextInt();
System.out.println("Enter worker name:");
String name = input.next();
………………………
workers[i] = new Worker(id, name, position, department,
salary);
}
}
11
Complete Test_Worker class by adding get_total_and_avg_salary method that attempts to
calculate the total salaries of the 10 workers and the average salary and display them back
to the user. List the required number of parameters (if any) in the method header.
(4 marks)
12
PART C: PROBLEM SOLVING AND CODING QUESTION - 15 MARKS
You are required to provide a Java solution to represent an Item for Vending Machine (VM).
A VM consists of many items that can be (1) added as stock or (2) bought by customers.
When the administrator adds the items, number of stock for that particular item will be
added according to the number of items added. Meanwhile, when a customer buy an item,
the number of stock for that particular item will be reduced by one.
a. Draw a UML class diagram to represent your design. The Item class should at least
contain its name, number of stock, price per unit and total price for the item. The
relevant setter and getter methods are also required. In addition, you also need to
include a method to support the addition of stock and items bought by the
customers. Note: You may have more attributes and methods as required.
b. Implement the class as your design in solution (a).
I. Construct a constructor that will accept the relevant values of the item’s
attributes.
II. Implement the setter and getter methods.
III. Create the method to add the stock of the item by providing the number of
items to be added. Eg: If the current number of item is 10, when the
addStock method is invoked and number of stock is passed as 20, it will add
the current stock with 20 that yields to 30.
IV. Create buyItem method, which will reduce the item by 1. E.g. If the current
stock is 10, and the buyItem method is invoked, the number of stock will be
reduced by 1 and becomes 9.
V. Construct a calculateTotalPrice of the item, which will calculate the price per
unit of the item with the number of stock available.
c. Complete the following test class i.e. VendingMachine to ensure correctness of your
solution. The test class shall do the followings:
I. Create an item with the values :
i. Name: MILO
ii. Price per unit: 1.50
iii. Number of stock :20
II. Display the item in (I).
III. Invoke the addItem method by passing 10 items.
IV. Display the current information of the item.
V. Invoke the buyItem method.
VI. Display the current information of the item.
(Note: Provide your answer in the VendingMachine class)
(15 marks)
13
public class VendingMachine {
//add 10 items
System.out.println("Add 10 items..");
(IV) ______________________________________________________________
milo.calculateTotalPrice();
(V) ______________________________________________________________
Item: MILO | Price per unit: 1.5 | Number of stock: 20 | Total price: 30.0
Add 10 items..
Item: MILO | Price per unit: 1.5 | Number of stock: 30 | Total price: 30.0
Buy one item..
Item: MILO | Price per unit: 1.5 | Number of stock: 29 | Total price: 43.5
14
15
End of Question Paper
16