0% found this document useful (0 votes)
120 views8 pages

Cos202 Typeb Computer Programming II 200level

The document outlines the course structure and examination details for Computer Programming II at Mewar International University, Nigeria, for the academic session 2025/2026. It includes six questions covering Object-Oriented Programming principles, Java class definitions, polymorphism, encapsulation, Java packages, and event-driven programming. Each question carries equal marks, and the marking scheme emphasizes correct explanations and code logic.

Uploaded by

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

Cos202 Typeb Computer Programming II 200level

The document outlines the course structure and examination details for Computer Programming II at Mewar International University, Nigeria, for the academic session 2025/2026. It includes six questions covering Object-Oriented Programming principles, Java class definitions, polymorphism, encapsulation, Java packages, and event-driven programming. Each question carries equal marks, and the marking scheme emphasizes correct explanations and code logic.

Uploaded by

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

MEWAR INTERNATIONAL UNIVERSITY, NIGERIA

FACULTY OF COMPUTING
DEPARTMENT OF SOFTWARE ENGINEERING
SECOND TERM 2025/2026 ACADEMIC SESSION

COURSE CODE: COS 202 TYPE: B


COURSE: COMPUTER PROGRAMMING II
TIME ALLOWED: 2 HOURS
INSTRUCTIONS: Answer ANY FOUR (4) questions. All questions carry equal marks (15
Marks each).

Question 1:
Explain the four main principles of Object-Oriented Programming (OOP). Provide an
example where two of the principles are applied in Java.
Question 2:
Given the following class definition:
class Car {
String brand;
String color;

// Constructor
Car(String brand, String color) {
[Link] = brand;
[Link] = color;
}

// Method to display car details


void showDetails() {
[Link]("Brand: " + brand + ", Color: " + color);
}
}
(a) What is the role of the constructor in the ‘Car’ class?
(b) Write a program to create two ‘Car’ objects, ‘car1’ and ‘car2’, and display their
information.

Question 3:
a) Explain the difference between compile-time polymorphism and runtime
polymorphism in object oriented programming.
b) Provide an example of method overloading for compile-time polymorphism and
method overriding for runtime polymorphism using java.

Question 4:
Write a Java class ‘Person’ with private fields ‘name’ and ‘age’. Provide getter and
setter methods for both fields to demonstrate encapsulation. Also, explain why
encapsulation is beneficial in programming.

Question 5:
a) Define a Java package.
b) Show how to create and use a Java package.

Question 6:
a) What is event driven programming?
b) Write a recursive method ‘factorial(int n)’ that calculates the factorial of a given
number ‘n’. Provide a Java program that calculates the factorial of 5 using this
method.
MEWAR INTERNATIONAL UNIVERSITY, NIGERIA
FACULTY OF COMPUTING
DEPARTMENT OF SOFTWARE ENGINEERING
SECOND TERM 2025/2026 ACADEMIC SESSION

COURSE CODE: COS 202 TYPE: A


COURSE: COMPUTER PROGRAMMING II
TIME ALLOWED: 2 HOURS
INSTRUCTIONS: Answer ANY FOUR (4) questions. All questions carry equal marks (15
Marks each).

MARKING SCHEME
Instruction to Examiners:
Accept any correct and relevant explanation, even if worded differently.
Penalize only conceptual errors, not minor spelling or syntax errors (unless code
logic is affected).
For code examples, award marks for logic, structure, and correctness, not strict
formatting.

Question 1:
Explain the four main principles of Object-Oriented Programming (OOP).
Provide an example where two of the principles are applied in Java.
Encapsulation: Binding data and methods together and restricting access to data.
Inheritance: Allowing a subclass to inherit properties and methods of a superclass.
Polymorphism: Ability of a method to take different forms depending on the object
invoking it.
Abstraction: Hiding implementation details and exposing essential features only.
Award 2 marks for explanation; mere listing attracts 1 mark each = 8 marks
Encapsulation:
class Person {
private String name;
public void setName(String name) {
[Link] = name;
}
public String getName() {
return name;
}

}
Polymorphism (runtime polymorphism)
class Animal {
void makeSound() {
[Link](“Animal makes a sound”);
}
}

class Dog extends Animal {


@Override
void makeSound() {
[Link](“Dog barks”);
}
}

Inheritance
class Employee {
void work() {
[Link](“Employee is working”);
}
}
class Manager extends Employee {
void manage() {
[Link](“Manager is managing”);
}
}

Abstraction:
abstract class Animal {
// Abstract method
abstract void makeSound();
// Concrete method
void sleep() {
[Link]("Animal is sleeping");
}
}

Any of the two award 3.5 x 2 = 7 marks


Question 2:
Given the following class definition:
class Car {
String brand;
String color;

// Constructor
Car(String brand, String color) {
[Link] = brand;
[Link] = color;
}

// Method to display car details


void showDetails() {
[Link]("Brand: " + brand + ", Color: " + color);
}
}
(a) What is the role of the constructor in the ‘Car’ class?
A constructor is a special method used to initialize objects. In the ‘Car’ class, the
constructor assigns values to ‘brand’ and ‘color’ when an object is created.
The mention of “constructor as a method used to initialize objects” carries full marks
of 5 marks

(b) Write a program to create two ‘Car’ objects, ‘car1’ and ‘car2’, and display their
information.
public class Main {
public static void main(String[] args) {
Student car1 = new Car("Toyota", “White”);
Student car2 = new Car("BMW", “Black”);

[Link]();
[Link]();
}
}
Each object creation carries 3 marks and each call to displayinfo()carries 2 marks = 10
marks
Question 3:
a) Explain the difference between compile-time polymorphism and runtime
polymorphism in object oriented programming.
Compile-time polymorphism – Method overloading (same method name, different
parameters) (2.5 marks)
Runtime polymorphism – Method overriding (same method name, same
parameters, different classes). (2.5 marks)

b) Provide an example of method overloading for compile-time polymorphism


and method overriding for runtime polymorphism using java.

Example of method overloading


class MathOperation {
// Method with two parameter
int add(int a, int b) {
return a + b;
}
// Overloaded method with three parameters
int add(int a, int b, int c) {
return a + b + c;
}
}

Example of method overriding


class Shape {
void draw() {
[Link]("Drawing shape");
}
}
//overridden method
class Rectangle extends Shape {
@override
void draw() {
[Link]("Drawing rectangle");
}
}
}//end of shape
Correct Example of Compile-time Polymorphism (Method Overloading) (5 marks)
Correct Example of Runtime Polymorphism (Method Overriding) (5 marks)

Question 4:
Write a Java class ‘Person’ with private fields ‘name’ and ‘age’. Provide getter and
setter methods for both fields to demonstrate encapsulation. Also, explain why
encapsulation is beneficial in programming.
class Person {
private String name;
private int age;

public void setName(String name) {


[Link] = name;
}

public void setAge(int age) {


if (age > 0)
[Link] = age;
}

public String getName() {


return name;
}

public int getAge() {


return age;
}
}
Correct writing of ‘Person’ class with private fields ‘name’ and ‘age’ carries 4 marks.
Then for each setter and getter method for the two fields carries 2 marks (8 marks)
=12 marks
Benefit of encapsulation
Protects data from unauthorized access
Improves maintainability
Provides controlled access using getters and setters
3 marks for mentioning at least one

Question 5:
a) Define a Java package.
A package is a group of related classes and interfaces that helps in:
· Organizing large programs
· Avoiding class name conflicts
· Controlling access using access modifiers
· Improving code reuse and maintainability
Award 5 marks for correct definition (mentioning of group of related classes,
interfaces is key)
b) Show how to create and use a Java package.
Creating a Package:
package [Link];
public class Student {
public void display() {
[Link](“Student class in package”);
}
}
Creation of package carries 5 marks

Using of package (using import statement):


import [Link];
public class Main {
public static void main(String[] args) {
Student s = new Student();
[Link]();
}
}
Whichever method that was used to access the class with correct use carries 5 marks

Question 6:
a) What is event driven programming?
Event-Driven Programming is a programming paradigm in which the flow of a
program is determined by events such as: mouse clicks, key presses, button actions,
window resizing, menu selections etc. (5 marks)

b) Write a recursive method ‘factorial(int n)’ that calculates the factorial of a


given number ‘n’. Provide a Java program that calculates the factorial of 4
using this method.
static int factorial(int n) {
if (n == 0)
return 1;
return n factorial(n - 1);
}
public static void main(String[] args) {
[Link](factorial(4)); // 24
}
Award 5 marks for a recursive algorithm and 5 marks for calling the method to calculate
factorial of 4 = 10 marks

You might also like