OOP Final Model Paper 1st Sem

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Institute of Software Engineering

Graduate Diploma in Software


Engineering Final Examination,
Semester 1, 2024
ITS1033 Object Oriented Programming |
Model Paper

Time Duration: 3 Hours


This paper has 7 pages including this page and Any
appendices.

Authorized Materials: No Materials are authorized.


Calculators: Calculators are NOT permitted.
Dictionaries: Dictionaries are NOT permitted.
Drawing Instruments: Drawing instruments are NOT permitted.

Instruction to Invigilators
The examination paper IS TO BE REMAINED in the examination hall.
Students are to be provided with answer sheets.
Provide extra answer sheets on request.
Students may not remove any part of the examination paper from the examination
hall and must return any used and unused answer sheets with their examination paper.

Instruction to students
The total marks for this paper is 100.
Ensure your student ID is written on all answer sheets during writing time.
Students are to base their answers on Java 11+.
This paper has 6 questions.
State clearly and justify any assumptions made.
Write legibly in blue or black pen.
Mobile phones, tablets, laptops, and other electronic devices, wallets and purses are
prohibited in the examination hall.
Section A:Multiple Choice Questions (20 marks)

This section consists of 20 multiple-choice questions, each worth 1 mark. Please answer all
questions to the best of your ability.

1. What is the output of the following Java code?


public class Test {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5};
for (int i = 0; i < nums.length; i++) {
nums[i] = nums[i] * 2;
}
System.out.println(nums[2]);
}
}
a) 2
b) 3
c) 6
d) 4

2. Which of the following correctly describes the purpose of the hashCode() method in Java?
a) Generates a random hash value for every object
b) Determines where objects are stored in a hash-based collection
c) Converts objects into unique string identifiers
d) Is used to compare objects by content

3. What is the result of the following code?

public class Test {


public static void main(String[] args) {
String str1 = "hello";
String str2 = new String("hello");
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
}
}
a) true true
b) false false
c) false true
d) true false

4. Given the following code:

class A {
private int x = 10;
}
class B extends A {
public int getX() { return x; }
}
public class Test {
public static void main(String[] args) {
B obj = new B();
System.out.println(obj.getX());
}
}
What is the result?
a) 10
b) Compile-time error
c) 0
d) Runtime exception

5. What does the transient keyword do in Java?


a) Prevents a variable from being serialized
b) Declares a variable as final
c) Makes a variable accessible in all classes
d) Prevents a method from being overridden

6. Which of the following statements is true about generics in Java?


a) Generics enforce compile-time type safety
b) Generics can be used with primitive data types
c) Generics are determined at runtime
d) Generics only apply to classes, not methods
7. What is the output of the following code?

class Parent {
void display() { System.out.println("Parent"); }
}
class Child extends Parent {
void display() { System.out.println("Child"); }
}
public class Test {
public static void main(String[] args) {
Parent p = new Child();
p.display();
}
}
a) Parent
b) Child
c) Compile-time error
d) Runtime exception

8. What does the following code snippet demonstrate?

public class Test {


public static void main(String[] args) {
String[] fruits = {"Apple", "Orange", "Banana"};
for (String fruit : fruits) {
if ("Orange".equals(fruit)) continue;
System.out.println(fruit);
}
}
}
a) Break statement
b) Continue statement
c) Throwing an exception
d) Infinite loop
9. Which of the following statements is true about method overloading in Java?
a) Methods must have the same return type
b) Methods must have different parameter types or number of parameters
c) Overloading is resolved at runtime
d) It involves inheritance
10. What is the output of the following code?

public class Test {


public static void main(String[] args) {
int x = 0;
int y = 10;
try {
System.out.println(y / x);
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
} finally {
System.out.println("Finally block executed");
}
}
}
a) Arithmetic Exception
b) Finally block executed
c) Arithmetic Exception and Finally block executed
d) Runtime error
11. What is serialization in Java?
a) The process of converting an object into bytes for storage or transmission
b) A method of sorting objects in a collection
c) A way to execute multiple threads in sequence
d) The process of cloning an object
12. Consider the following code:
interface A { void display(); }
class B implements A {
public void display() { System.out.println("B"); }
}
class C extends B {
public void display() { System.out.println("C"); }
}
public class Test {
public static void main(String[] args) {
A obj = new C();
obj.display();
}
}
What is the output?
a) B
b) C
c) Compile-time error
d) Runtime exception

13. Which statement correctly explains the use of the final keyword with a class?
a) The class cannot be instantiated
b) The class cannot have child classes
c) The class cannot have methods
d) The class cannot define constants

14. What will be the output of this program?


public class Test {
public static void main(String[] args) {
int i = 0;
for (i = 0; i < 5; i++) {}
System.out.println(i);
}
}
a) 4
b) 5
c) Compile-time error
d) Infinite loop

15. Consider the following method:

public void method(Object o) {


System.out.println("Object");
}
public void method(String s) {
System.out.println("String");
}
What will be the output of the following code?
Test t = new Test();
t.method(null);

a) Object
b) String
c) Compile-time error
d) Runtime error

16. Which of the following is true about multithreading in Java?


a) Threads in Java are started using the Thread.run() method
b) A thread can be run only once in its lifecycle
c) The synchronized keyword prevents race conditions by locking methods and
variables
d) Threads cannot be interrupted once started

17. What is reflection in Java?


a) The ability of a class to invoke its own methods
b) A feature that allows examination and modification of class behavior at runtime
c) A design pattern used for object creation
d) A technique to serialize objects

18. What will the following code print?

public class Test {


public static void main(String[] args) {
int x = 5;
int y = 0;
assert y != 0 : "Divide by zero error";
System.out.println(x / y);
}
}
a) Divide by zero error
b) Compile-time error
c) Runtime exception
d) AssertionError
19. What is method hiding in Java?
a) When a child class method hides the parent class method using the same
signature
b) When a parent class method hides the child class method
c) When method overloading is not possible in child classes
d) When static methods in a class cannot be accessed by subclasses

20. What is the output of the following Java program?

class A {
static void method() { System.out.println("Static A"); }
}
class B extends A {
static void method() { System.out.println("Static B"); }
}
public class Test {
public static void main(String[] args) {
A a = new B();
a.method();
}
}
a) Static A
b) Static B
c) Compile-time error
d) Runtime exception
Section B: Short Answer Questions (60 marks)

(Answer only 3 questions. Each question carries 20 marks.)

1.

a. Explain how Java achieves platform independence.


(5 marks)
● Discuss the concept of Java Virtual Machine (JVM) and its role in achieving platform
independence.
● Explain the role of bytecode and how it is executed on any platform.
● Highlight the difference between compiling in Java and traditional languages like
C/C++.
● Provide a brief example of a simple Java program and describe how it runs on
different operating systems.
b. Describe the structure and components of a Java class file.
(5 marks)
● Explain the purpose of the class header and its main elements (e.g., class name,
access modifiers).
● Describe the fields, methods, and constructors sections of a class.
● Define the method signature and its importance in distinguishing overloaded
methods.
● Explain what is stored in the constant pool and its relevance to method execution.
c. What are the differences between compile-time and runtime errors?
(5 marks)
● Define compile-time errors and provide examples (e.g., syntax errors, type
mismatches).
● Define runtime errors and provide examples (e.g., NullPointerException,
ArrayIndexOutOfBoundsException).
● Describe how compile-time errors are caught by the compiler, while runtime errors
occur during program execution.
● Explain how Java's exception handling helps catch runtime errors.
d. What is the difference between equals() and == in Java?
(5 marks)
● Describe how == compares references, checking if two variables point to the same
object.
● Explain how equals() compares the content of two objects for equality.
● Provide an example where == and equals() give different results.
● Discuss how overriding the equals() method in custom classes can provide
meaningful comparisons.
2.

a. Explain the concept of method overriding and how Java supports runtime polymorphism.
(5 marks)
● Define method overriding and explain how it allows a subclass to provide a specific
implementation of a method from the parent class.
● Discuss how Java uses dynamic method dispatch to achieve runtime polymorphism.
● Provide an example of overriding a method in a subclass and show how
polymorphism works.
● Explain the importance of runtime polymorphism in achieving flexible and
maintainable code.
b. Differentiate between HashMap and TreeMap in Java.
(5 marks)
● Describe how HashMap stores key-value pairs using hashing and discuss its
performance in terms of retrieval.
● Explain how TreeMap stores key-value pairs in a red-black tree, providing sorted
order of keys.
● Provide a performance comparison of both data structures in terms of insertion,
deletion, and lookup.
● Describe use cases where each data structure is preferred based on requirements
like ordering or speed.
c. What is the role of constructors in Java? Explain constructor overloading with an example.
(5 marks)
● Define a constructor and explain its role in object initialization.
● Discuss constructor overloading and how Java supports multiple constructors with
different parameters.
● Provide a Java code example demonstrating constructor overloading.
● Explain how overloading constructors improves flexibility in creating objects with
varying initializations.
d. Explain the significance of the toString() method in Java. How is it used in custom classes?
(5 marks)
● Describe the purpose of the toString() method and how it provides a string
representation of an object.
● Explain the default behavior of toString() and why it is recommended to override it in
custom classes.
● Provide a Java code example where toString() is overridden to display meaningful
information about an object.
● Discuss the impact of overriding toString() on improving debugging and output
readability.
3.

a. How does Java handle memory management? Describe the concept of garbage collection.
(5 marks)
● Explain how Java uses automatic memory management through the Garbage
Collector.
● Discuss how heap memory is allocated and freed for objects during the program's
lifecycle.
● Describe the process of garbage collection, including the mark-and-sweep algorithm.
● Provide an example of how objects become eligible for garbage collection when no
longer referenced.
b. What is an interface in Java? How does it differ from an abstract class?
(5 marks)
● Define an interface and describe its role in specifying methods that must be
implemented by a class.
● Compare interfaces and abstract classes, discussing the differences in method
implementation and inheritance.
● Provide an example of a class implementing multiple interfaces.
● Explain when to use interfaces versus abstract classes in real-world scenarios.
c. What is method overloading in Java? Explain the rules for method overloading.
(5 marks)
● Define method overloading and describe how it allows multiple methods with the
same name but different parameters.
● Explain the rules for method overloading, including differences in method signatures,
number/type of parameters, and return types.
● Provide a Java code example that demonstrates method overloading in a class.
● Discuss how method overloading improves code flexibility and readability.

d. What is a default method in an interface? How does Java 8 introduce default methods?
(5 marks)
● Define a default method in an interface and explain how it provides a method body
within the interface.
● Discuss how Java 8 introduced default methods to allow interfaces to evolve without
breaking existing implementations.
● Provide an example of an interface with a default method and explain how it is used
in implementing classes.
● Describe use cases where default methods are beneficial for backward compatibility.
4.

a. Explain the concept of inheritance in Java.


(5 marks)
● Define inheritance and explain how a class can inherit properties and behaviors from
a parent class.
● Provide an example demonstrating inheritance with a base class and a subclass.
● Discuss how inheritance supports code reuse and reduces redundancy in object-
oriented programming.
● Explain the types of inheritance supported in Java (e.g., single inheritance) and why
multiple inheritance is not allowed with classes.
b. Explain the difference between static and instance methods in Java.
(5 marks)
● Define a static method and explain how it belongs to the class, not instances, and
can be called without creating an object.
● Define an instance method and explain how it requires an instance of the class to be
called.
● Provide a code example showing both a static method and an instance method in a
class.
● Discuss when to use static methods (e.g., utility methods) versus instance methods.

c. What is the difference between checked and unchecked exceptions in Java?


(5 marks)
● Define checked exceptions and explain how they must be either handled with try-
catch or declared using throws.
● Define unchecked exceptions (runtime exceptions) and explain how they don’t need
to be explicitly handled.
● Provide an example of a checked exception (IOException) and demonstrate how it is
handled.
● Provide an example of an unchecked exception (NullPointerException) and explain
when it is encountered during runtime.
d. What is the significance of the final keyword in Java?
(5 marks)
● Define the final keyword and explain its use with variables, methods, and classes.
● Provide an example of a final variable and explain how it ensures that the variable
cannot be reassigned after initialization.
● Describe how marking a method as final prevents overriding in subclasses.
● Explain how declaring a class as final prevents it from being subclassed.
Section C: Essay Questions (20 marks)

This section consists of 2 questions. You are required to choose and answer only 1 question from
the available options.

1. Develop an E-commerce Shopping Cart System in Java

Design a simple E-commerce Shopping Cart System that manages product listings,
customers, and shopping carts. The system should allow customers to add products to their
cart, view the cart, and check out. The system should include:

● Products: Each product has a productID, name, description, price, and


stockQuantity.
● Customers: Customers have a customerID, name, email, and shoppingCart.
● Shopping Cart: Each shopping cart contains multiple products with the ability to add
or remove products.

Requirements:

● Use encapsulation to protect the internal data of your classes.


● Implement a method to calculate the total cost of the items in the shopping cart and
check for sufficient stock before allowing purchases.
● Ensure that products are removed from stock upon successful checkout and handle
insufficient stock scenarios using exception handling.
● Use polymorphism to allow for different types of products (e.g., electronics, clothing,
books) that have varying attributes and behaviors.

Provide detailed code snippets for key parts of the system, such as adding products to the
cart and processing payments. Discuss how your design could be scaled to support more
complex features like discounts, product categories, and order history.
2. Create a Bank Account Management System in Java

Design a Bank Account Management System to manage customer accounts, deposits,


withdrawals, and account balances. Your system should handle the following:

● Accounts: Different types of bank accounts such as SavingsAccount,


CheckingAccount, and FixedDepositAccount, each with different attributes like
interest rates and withdrawal limits.
● Customers: Each customer has a customerID, name, and a list of accounts.
● Transactions: Track deposits, withdrawals, and transfers between accounts.

Requirements:

● Use inheritance to model different types of accounts, ensuring that each account
type has specific behavior (e.g., minimum balance for SavingsAccount, fixed maturity
for FixedDepositAccount).
● Implement methods for deposit, withdrawal, and balance inquiry for each account
type.
● Ensure that error handling is in place to prevent overdrawing or exceeding
withdrawal limits.
● Implement a system to calculate interest on deposits for applicable accounts.

Provide code examples to demonstrate key functionality, such as withdrawals and transfers
between accounts. Discuss how the system could be expanded to include features like
loans, recurring payments, and account statements.

You might also like