0% found this document useful (0 votes)
7 views

CAT JAVA

Uploaded by

angeekevinee
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)
7 views

CAT JAVA

Uploaded by

angeekevinee
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
You are on page 1/ 10

ACADEMIC YEAR: 2024--2025

LEVEL: YEAR2(A,B,C,D) TERM: ……I…….. Date: 5 November 2024

CAT

OBJECT ORIENTED PROGRAMMING WITH JAVA

Timing: 180 minutes

Maximum:100 Marks

Instructions:

1. The test is written

2. Follow the guidelines provided and seek clarifications if need be.

3. The test is closed to any resource material.

4. Collaboration and any kind or cheating is prohibited and should be resulted

into a GOOD ZERO.

INSTRUCTORS

MUSANINYANGE MAHORO Larisse

HABANABASHAKA Jean Damascene


SECTION A: Each Question worth 2.5 Marks(50 Marks)

1. Java is a platform as well as a language.


a)yes
b)No

2. There are four platforms of the Java programming language.Tick all that apply

a)Standard Edition(Java SE) is a computing platform for development and


deployment of portable code for desktop and server environments.
b)Micro Edition or Java ME is a computing platform for development and deployment
of portable code for embedded and mobile devices
c)Java Development Kit (JDK):Development tools are all included in the Java
Development Kit
d)Java Enterprise Edition (Java EE) currently rebranded as Jakarta EE
e)Java Fx; OpenJFX is an open-source, next-generation client application platform
for desktop, mobile and embedded systems built on top of Java.
f) Java Runtime Environment (JRE) This a virtual machine which runs programs
which have been compiled

3. Tick all that apply


a) JDK is inside JVM
b) JVM is inside JDK
c) JRE is inside JVM
d) JVM and JRE are inside JDK
e) JVM and JDK are inside JRE
f)JRE and JDK are inside JVM

4. Everything in Java is written inside a class


a)Yes
b)No
5. Every class created in java is a subclass of Object class
a)Yes
b)No
6. Private access modifier means class within the same package have access
a)Yes
b)No
7. Choose one that is correct
a) Instance variable means class variable
b) Instance variable is different from a class variable
c) None of the above is true
8. You can assign a value of a double variable to an integer variable in java.
double p=12.5;
int a=p;
a) Yes
b) No
9. The array size in java is obtained using the following
int [] school=new int[10];
a) school.size
b)school.size();
c)school.length;
d)school.length();
e) None of the above
10. In java Reference Equality means Object Equality
a)Yes
b)No
11. Reference equality(x == y) Checks if the variables reference the same object in
memory
a)Yes
b)No
12. Object equality(x.equals(y))Checks if the objects which the variables reference are
equal which means they have the same meaning or content
a) Yes
b) No
13. A Constant in java should be named using Uppercase with underscores
a) Yes
b) No
14.Encapsulation in java is achieved through the following
a) Making a class private and a constructor public
b) Making instance variable public and declaring getters and setters
c) Making instance variable public and declaring public getters and setters
d) Making instance variable private and declaring public getters and setters
e) Making instance variable public and declaring private getters and setters
f)None of the above
15. Polymorphism means many form and this is achieved in java using
a) Encapsulation and instantiation
b) Overloading and Overriding
c) Overloading and inheritance
d) Inheritance and abstraction
e) None of the above
16. In OOP a state means a variable and a behaviour means a method
a)Yes
b)No
17.An overridden method has the same name, parameters and return type as the method it
is overriding
a)Yes
b)No
18. Abstract classes can be instantiated and they may contain a mix of methods declared
with or without an implementation.
a)Yes
b)No
19. Which one in the following is true
a) A variable declared within an abstract class is static final
b) A variable declared within interface is static final
c) A variable inside a class is always static
d)None of the above

20.A constructor is a method which has the same name as the class, and no return
type. We can use it to initialise data members
a)Yes
b)No

SECTION B: Choose 4 Questions Only./ 20 Marks


21. Explain the purpose of Serialization and deserialization in Java I/O. /5 Marks
Sample Answer: Serialization is a mechanism of converting the state of an object into
a byte stream. Deserialization is the reverse process where the byte stream is used
to recreate the actual Java object in memory. This mechanism is used to persist the
object.

22. Explain the difference between FileReader and FileInputStream./5 Marks


Sample Answer:
● FileInputStream to allows bytes to be read from a file
● FileReader is used to read a single character of data.
23. Explain the difference between checked and unchecked exceptions. Give one example
in each category./5 Marks
Sample Answer
Checked Exceptions
Compiler checks and deals with exceptions. Compiler error is returned if exception is not
handled
Examples:
FileNotFoundException, IOException
• Unchecked Exceptions:
•Compiler does not check and deal with exceptions
Examples:
ArrayIndexOutOfBounds Exception, NullPointerException,ArithmeticException
24. Mention the best practices for exception handling in Java./5 Marks
Sample Answer(Proper description on the use of the following keywords)
Java exception handling is managed via five keywords:
try, catch, throw, throws, and finally.
25. Write a try-catch block with multiple catch clauses to handle
NullPointerException and ArrayIndexOutOfBoundsException exceptions
separately. Explain the importance of handling different exceptions./5 Marks
Marking: Try catch catch :4 Marks importance 1 Mark
try{
//To do statements
}catch(NullPointerException e){
System.out.println("Exception"+e.getMessage();
//System.out.println(“Exception”+e.toString();
//e.printStackTrace();
//System.out.println(“Exception Occured”);
}catch(ArrayIndexOutOfBoundsException ex){
System.out.println("Exception"+ex.getMessage());
//System.out.println(“Exception”+e.toString();
//e.printStackTrace();
//System.out.println(“Exception Occured”);
}
//Optional Block
finally{
//System.out.println(“Something”);
}
Sample Answer(Minimum 2 importance added):
• It allows for more Accurate Error Reporting
• Facilitates Debugging and Troubleshooting
• Improves the Security of the Program
• Provides a Better user Experience
• Grouping and Differentiating Error Types

SECTION C: This section is mandatory. Attempt all Questions. /30 Marks

21. Complete the following table using Yes and No./10 Marks(Marking: Missed item 0.5
Mark)
Access Modifier private protected default public

Within a class Yes Yes Yes Yes

Same package No Yes Yes Yes

Subclass No Yes No Yes

Outside Package No No No Yes

22. Encapsulate the Product class and overload its constructor at 3 times./10 Marks
Product{id, name, supplier, cost, manufactureDate}:
( Marking : Private Data members 3 Marks, Proper datatype choice 1 Mark, Public
getters and setters 3 Marks and overloading 3 Marks)
Sample Answer:
import java.time.LocalDate;
public class Product {
private int id;
private String name;
private String supplier;
private int cost;
private LocalDate manufacturedDate;
public Product() {
}
public Product(int id, String name, String supplier, int
cost, LocalDate manufacturedDate) {
super();
this.id = id;
this.name = name;
this.supplier = supplier;
this.cost = cost;
this.manufacturedDate = manufacturedDate;
}
public Product(String name, String supplier, int cost,
LocalDate manufacturedDate) {
this.name = name;
this.supplier = supplier;
this.cost = cost;
this.manufacturedDate = manufacturedDate;
}
// Other constructors can be added either by changing the
order of arguments or
// reducing the parameters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSupplier() {
return supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public LocalDate getManufacturedDate() {
return manufacturedDate;
}
public void setManufacturedDate(LocalDate
manufacturedDate) {
this.manufacturedDate = manufacturedDate;
}
// Added but not mandatory
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ",
supplier=" + supplier + ", cost=" + cost
+ ", manufacturedDate=" + manufacturedDate
+ ", getId()=" + getId() + ", getName()=" + getName()
+ ", getSupplier()=" + getSupplier() + ",
getCost()=" + getCost() + ", getManufacturedDate()="
+ getManufacturedDate() + ", getClass()="
+ getClass() + ", hashCode()=" + hashCode() + ", toString()="
+ super.toString() + "]";
}
}

23. Create Person class with id, name, identity, dob and inherits it using Account class which
have accountNumber and balance attribute. The account class should have additional
methods which are deposit and withdraw which take balance as a parameter. The amount to
deposit should be positive and the amount to withdraw should be NOT greater than the
available balance.
Create a BankingApp class with the main method to test the deposit and withdraw methods
of the Account class./10 Marks
Sample Answer:(Marking class Person 3 Marks,class Account 5 Marks(use of extends
1 Mark, Good subclass constructor 2 Marks, Withdraw 1 Mark, Deposit 1 Mark,
Banking App 2 Marks),

class Person {
private int id;
private String name;
private String identity;//The student can use also other
types like integers
private LocalDate dob;//The student can use Date
//or
//protected int id;
//protected String name;
//protected String identity;
//protected LocalDate dob;
public Person() {
}
public Person(int id, String name, String identity,
LocalDate dob) {
super();
this.id = id;
this.name = name;
this.identity = identity;
this.dob = dob;
}
public Person(String name, String identity, LocalDate
dob) {
super();
this.name = name;
this.identity = identity;
this.dob = dob;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdentity() {
return identity;
}
public void setIdentity(String identity) {
this.identity = identity;
}
public LocalDate getDob() {
return dob;
}
public void setDob(LocalDate dob) {
this.dob = dob;
}
}
class Account extends Person{
private String account; //Account also can be integer
private double balance; //balance can also be integer
public Account() {}
public Account(int id, String name, String identity,
LocalDate dob, String account, double balance) {
super(id, name, identity, dob);
this.account = account;
this.balance = balance;
}
public Account( String name, String identity, LocalDate
dob, String account, double balance) {
super( name, identity, dob);
this.account = account;
this.balance = balance;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void withDraw(double balance) {
if(this.balance>=balance&& balance>0) {
this.balance-=balance;
}
else
return;
}
public void deposit(double balance) {
if(balance>0) {
this.balance+=balance;
}
else
return;
}
}
class BankingApp{
public static void main(String[]args) {
Account account1= new
Account(1,"Mugisha","111626G",LocalDate.now(),"20240909",200.5
);
account1.deposit(300);
account1.withDraw(100);
System.out.println(account1.getBalance());
}
}

Good Luck!!!!

You might also like