Sunteți pe pagina 1din 16

Name: ___________________________________________________

Matric no: ___________________________________________________


Section: ___________________________________________________
Lecturer: ___________________________________________________

INTERNATIONAL ISLAMIC UNIVERSITY MALAYSIA

MID-TERM EXAMINATION
SEMESTER II, 2016/2017 SESSION

KULLIYYAH OF INFORMATION AND COMMUNICATION


TECHNOLOGY

Programme : BCS/BIT Level of Study : Undergraduate

Time : 09:00 am – 11.30 am Date : 31st MARCH 2017

Duration : 2 hours and 30 minutes

Course Code : CSC 1103 Section(s) : All

Course Title : Object Oriented Programming

This Question Paper consists of 16 Printed Pages including cover page containing 3 parts:

Part A: MCQ Questions (20 MARKS)


Part B: Subjective Questions (25 MARKS)
Part C: Coding (15 MARKS)
You are required to answer ALL questions in the question booklet.

INSTRUCTION(S) TO CANDIDATES

DO NOT OPEN UNTIL YOU ARE ASKED TO DO SO


Any form of cheating or attempt to cheat is a serious offence, which may lead to dismissal.

1
PART A: MULTIPLE CHOICE QUESTIONS (MCQ) - 20 MARKS
Instruction: Please CIRCLE the correct answer.

1. Based on the following figure, what will be produced in (a)?

A. A bytecode of the HelloJava.java source code i.e Hello.class will be created.


B. A bytecode of the HelloJava.java source code i.e HelloJava.class will be created.
C. A new source code will be created i.e. HelloJava1.java
D. A copy of the HelloJava.
E. None of the above

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?

A. syntax error B. compile error C. logic error


D. runtime error E. common error

3. What is the output of the following code?

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

4. Analyze the following code:


Suppose that x is 7. What is x after the evaluation of the following expression:

a. (x >= 3) && ( ++x > 3)


b. (x-- < 6) || (x > 6)

A. 7,6 B. 6,7 C. 7,7 D. 8,6 E. 8,8

5. How many times is the println statement executed?


for (int i = 0; i < 10; i++)
for (int j=0; j < i; j++)
System.out.println(i * j);

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 ");
}

A. ADG B. ACE C. ACG D. ACF E. AG

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:

private int smallestValue (int [ ] values) {


int smallestSofar = values[0];
for (int i ==1; i < values.length; i++) {
if (Missing condition)) {
smallestSoFar = values [i];
}
}
return smallestSoFar;
}
Which of the following boolean conditions can be used to replace the “Missing
condition” part so that the smallestValue( ) method can be executed correctly?

A. smallestSoFar > values[i]


B. values[smallestSoFar] > values [i]
C. smallestSoFar > values[i]
D. !smallestSoFar
E. values[smallestSoFar] > values [i]

8. When you invoke a method with a parameter, the value of the argument is passed to
the parameter. This is referred to as _________.

A. pass by name B. pass by value C. pass by reference

D. method invocation E. None of the above

3
9. Analyze the following code:

public class Test {


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

x = new int[2];

for (int i = 0; i < x.length; i++)


System.out.print(x[i] + " ");
}
}

A. The program displays 0 0 B. The program displays 0 0 3 4

C. The program displays 1 2 3 4 D. The program displays 0 0 0 0

E. The program displays 4 3 2 1

10. Which of the following should be declared as a void method?

A. Write a method that returns a random integer from 1 to 100.


B. Write a method that prints integers from 1 to 100.
C. Write a method that checks whether current second is an integer from 1 to 60.
D. Write a method that converts an uppercase letter to lowercase.
E. Write a method that returns the maximum value in array on integer.

11. Given the following class definition, which of the following method is invalid to be
placed after the comment line?

public class AClass


{
public void amethod(int i, String s){}

//more codes here

A. public void amethod(String s, int i){}


B. public int amethod(int no, String s){}
C. public void amethod(int no, String mystring){}
D. public void amethod(int i, String s, double no) {}
E. public int amethod(int[] no, String s){}

4
12. Which of the following statement is TRUE?

A. Bottom-up approach is to implement one method in the structure chart at a time


from the top to the bottom.
B. Bottom-up approach is to implement all methods in the structure chart at a time
from the bottom to the top.
C. Bottom-up approach is to implement one method in the structure chart at a time
from the bottom to the top.
D. Top-down approach is better than bottom-up and cannot be used together in
one project.
E. Bottom-up approach is better than top-down.

13. Which if 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 ________________________.

A. information hiding B. encapsulation C. inheritance


D. modularity E. abstraction

15. Java programmers concentrate on creating ____________, which contain fields and the
set of methods that manipulate those fields and provide services to clients.

A. Object B. Class C. Constructor


D. Instance E. Package

16. ________ is invoked to create an object.

A. The main method B. A method with a return type

C. A method with the void return type D. A constructor

E. None of the above

5
17. What is wrong in the following program?

public class ShowErrors {


public static void main (String []args){
Rectangle r1 = new Rectangle (4, 7);
System.out.println(r1.width + “ “ + r1. length);
}
}
class Rectangle {
float width = 1;
float length = 1;
}

A. The first constructor (no-arg constructor) in class C is undefined


B. The second constructor in class C is undefined
C. The first and second constructors in class C are undefined
D. The data type of value data field is in inappropriate format
E. The first (no-arg constructor) and second constructors in class ShowErrors are
undefined

18. What is the output of the following codes?

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

19. When invoking a method with an object argument, ___________ is passed.

A. a copy of the object


B. the object is copied, then the reference of the copied object
C. the reference of the object
D. the contents of the object
E. None of the above

6
20. Given the declaration Circle[] x = new Circle[10], which of the following
statement is most accurate.

A. x contains an array of ten int values.


B. x contains a reference to an array and each element in the array can hold a
Circle object.
C. x contains an array of ten objects of the Circle type.
D. x contains a reference to an array and each element in the array can hold a
reference to a Circle object.
E. None of the above

PART B: SUBJECTIVE QUESTIONS - 25 MARKS


Instruction: Answer all questions in the space provided.

1. Java programs are considered portable and architecture-neutral. Explain how does these
features are implemented in Java.

(3 marks)

2. Identify the errors in the following code fragments.


A.
void g()
{
System.out.println(“Inside method g);

void h()
{
System.out.println(“Inside method h);
}
}

Answer:

7
B.

public class A{
int i =5;
static int k = 2;

public static void main (String [] args){


int j=i;
m1();
}

public void m1(){


i = i + k + m2(i,k);
}

public static int m2(int i, int j){

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;
}

public void print() {


System.out.print(s);
}
}
(6 marks; 2 marks each)

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:

public static boolean equals(int [] list1, int [] list2)

(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

Note: You are not required to implement the Event class.

Write a Java statement to:

a. Create an array of Event objects of size 20.

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.

public class Parking {


private int id= 0;
private String name = “no-name” ;
……………
Worker() {
}
Worker (int id, String name, String position, String department,
double hour){
this.id=id;
this.name= name;
……………
}
//Assume the set and get methods for each object variable have also
been listed but not written here 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.

In order to solve the solution, answer the following questions:

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 {

public static void main(String[] args) {


//create an object
(I) _____________________________________________________________
milo.calculateTotalPrice();
(II) ______________________________________________________________
(III) ______________________________________________________________

//add 10 items
System.out.println("Add 10 items..");
(IV) ______________________________________________________________
milo.calculateTotalPrice();
(V) ______________________________________________________________

//buy one item


System.out.println("Buy one item..");
(VI) ______________________________________________________________
milo.calculateTotalPrice();
(VII) ______________________________________________________________
}
}

Sample output for reference:

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

S-ar putea să vă placă și