Object Oriented Programming with JAVA (4341602) - Winter 2024 Solution

Solution guide for Object Oriented Programming with JAVA (4341602) Winter 2024 exam

Question 1(a) [3 marks]

Write down the difference between oop and pop.

Answer:

AspectOOPPOP
ApproachBottom-up approachTop-down approach
FocusObjects and classesFunctions and procedures
Data SecurityData hiding through encapsulationNo data hiding
Problem SolvingDivide problem into objectsDivide problem into functions

Mnemonic: "Objects Bottom, Procedures Top"

Question 1(b) [4 marks]

What is byte code? Explain JVM in detail.

Answer:

Byte Code: Platform-independent intermediate code generated by Java compiler from source code.

JVM Components:

  • Class Loader: Loads .class files into memory
  • Memory Area: Heap, stack, method area storage
  • Execution Engine: Interprets and executes bytecode
  • Garbage Collector: Automatic memory management

Mnemonic: "Byte Code Runs Everywhere"

Question 1(c) [7 marks]

Write a program in Java to sort the elements of an array in ascending order

Answer:

Java

Key Points:

  • Bubble Sort: Compares adjacent elements
  • Time Complexity: O(n²)
  • Space Complexity: O(1)

Mnemonic: "Bubble Up The Smallest"

Question 1(c OR) [7 marks]

Write a program in java to find out maximum from any ten numbers using command line argument.

Answer:

Java

Key Points:

  • Command Line: args[] array stores arguments
  • parseInt(): Converts string to integer
  • Validation: Check array length

Mnemonic: "Arguments Maximum Search"

Question 2(a) [3 marks]

What is wrapper class? Explain with example.

Answer:

Wrapper Class: Converts primitive data types into objects.

PrimitiveWrapper Class
intInteger
charCharacter
booleanBoolean
doubleDouble
Java

Mnemonic: "Wrap Primitives Into Objects"

Question 2(b) [4 marks]

List out different features of java. Explain any two.

Answer:

Java Features:

  • Simple: Easy syntax, no pointers
  • Platform Independent: Write once, run anywhere
  • Object Oriented: Based on objects and classes
  • Secure: No explicit pointers, bytecode verification

Detailed Explanation:

  • Platform Independence: Java bytecode runs on any platform with JVM
  • Object Oriented: Supports inheritance, encapsulation, polymorphism, abstraction

Mnemonic: "Simple Platform Object Security"

Question 2(c) [7 marks]

What is method overriding? Explain with example.

Answer:

Method Overriding: Child class provides specific implementation of parent class method.

Java

Key Points:

  • Runtime Polymorphism: Method called based on object type
  • @Override: Annotation for method overriding
  • Dynamic Binding: Method resolution at runtime

Mnemonic: "Child Changes Parent Method"

Question 2(a OR) [3 marks]

Explain Garbage collection in java.

Answer:

Garbage Collection: Automatic memory management that removes unused objects.

Key Points:

  • Automatic: No manual memory deallocation
  • Mark and Sweep: Identifies and removes unused objects
  • Heap Memory: Works on heap memory area

Mnemonic: "Auto Clean Unused Objects"

Question 2(b OR) [4 marks]

Explain static keyword with example.

Answer:

Static Keyword: Belongs to class rather than instance.

Java

Static Features:

  • Memory: Loaded at class loading time
  • Access: Can be accessed without object
  • Sharing: Shared among all instances

Mnemonic: "Class Level Memory Sharing"

Question 2(c OR) [7 marks]

What is constructor? Explain copy constructor with example.

Answer:

Constructor: Special method to initialize objects.

Java

Constructor Types:

  • Default: No parameters
  • Parameterized: Takes parameters
  • Copy: Creates object from existing object

Mnemonic: "Default Parameter Copy"

Question 3(a) [3 marks]

Explain super keyword with example.

Answer:

Super Keyword: References parent class members.

Java

Super Uses:

  • Variables: Access parent class variables
  • Methods: Call parent class methods
  • Constructor: Call parent class constructor

Mnemonic: "Super Calls Parent"

Question 3(b) [4 marks]

List out different types of inheritance. Explain multilevel inheritance.

Answer:

Inheritance Types:

TypeDescription
SingleOne parent, one child
MultilevelChain of inheritance
HierarchicalOne parent, multiple children
MultipleMultiple parents (via interfaces)

Multilevel Inheritance:

Java

Mnemonic: "Single Multi Hierarchical Multiple"

Question 3(c) [7 marks]

What is interface? Explain multiple inheritance with example.

Answer:

Interface: Contract that defines what class must do, not how.

Java

Interface Features:

  • Multiple Inheritance: Class can implement multiple interfaces
  • Abstract Methods: All methods are abstract by default
  • Constants: All variables are public, static, final

Mnemonic: "Multiple Abstract Constants"

Question 3(a OR) [3 marks]

Explain final keyword with example.

Answer:

Final Keyword: Restricts modification, inheritance, or overriding.

Java

Final Uses:

  • Class: Cannot be extended
  • Method: Cannot be overridden
  • Variable: Cannot be reassigned

Mnemonic: "Final Stops Changes"

Question 3(b OR) [4 marks]

Explain different access controls in Java.

Answer:

Access Modifiers:

ModifierSame ClassSame PackageSubclassDifferent Package
public
protected
default
private

Mnemonic: "Public Protected Default Private"

Question 3(c OR) [7 marks]

What is package? Write steps to create a package and give example of it.

Answer:

Package: Group of related classes and interfaces.

Steps to Create Package:

  1. Declare: Use package statement at top
  2. Compile: javac -d . ClassName.java
  3. Run: java packagename.ClassName
Java

Package Benefits:

  • Organization: Groups related classes
  • Access Control: Package-level protection
  • Namespace: Avoids naming conflicts

Mnemonic: "Declare Compile Run"

Question 4(a) [3 marks]

Explain thread priorities with suitable example.

Answer:

Thread Priority: Determines thread execution order (1-10 scale).

Java

Priority Constants:

  • MIN_PRIORITY: 1
  • NORM_PRIORITY: 5
  • MAX_PRIORITY: 10

Mnemonic: "Min Normal Max"

Question 4(b) [4 marks]

What is Thread? Explain Thread life cycle.

Answer:

Thread: Lightweight process for concurrent execution.

Thread States:

  • New: Thread created but not started
  • Runnable: Ready to run
  • Running: Currently executing
  • Blocked: Waiting for resource
  • Dead: Execution completed

Mnemonic: "New Runnable Running Blocked Dead"

Question 4(c) [7 marks]

Write a program in java that create the multiple threads by implementing the Runnable interface.

Answer:

Java

Key Points:

  • Runnable Interface: Better than extending Thread class
  • Thread.sleep(): Pauses thread execution
  • Multiple Threads: Run concurrently

Mnemonic: "Implement Runnable Start Multiple"

Question 4(a OR) [3 marks]

List four different inbuilt exceptions. Explain any one inbuilt exception.

Answer:

Inbuilt Exceptions:

  • NullPointerException: Accessing null object
  • ArrayIndexOutOfBoundsException: Invalid array index
  • ArithmeticException: Division by zero
  • NumberFormatException: Invalid number format

ArithmeticException: Thrown when arithmetic operation fails.

Java

Mnemonic: "Null Array Arithmetic Number"

Question 4(b OR) [4 marks]

Explain Try and Catch with suitable example.

Answer:

Try-Catch: Exception handling mechanism.

Java

Exception Handling Flow:

  • Try: Code that may throw exception
  • Catch: Handles specific exceptions
  • Finally: Always executes

Mnemonic: "Try Catch Finally"

Question 4(c OR) [7 marks]

What is Exception? Write a program that show the use of Arithmetic Exception.

Answer:

Exception: Runtime error that disrupts normal program flow.

Java

Exception Types:

  • Checked: Compile-time exceptions
  • Unchecked: Runtime exceptions
  • Error: System-level problems

Mnemonic: "Runtime Error Disrupts Flow"

Question 5(a) [3 marks]

Explain ArrayIndexOutOfBound Exception in Java with example.

Answer:

ArrayIndexOutOfBoundsException: Thrown when accessing invalid array index.

Java

Key Points:

  • Valid Range: 0 to array.length-1
  • Negative Index: Also throws exception
  • Runtime Exception: Unchecked exception

Mnemonic: "Array Index Range Check"

Question 5(b) [4 marks]

Explain basics of stream classes.

Answer:

Stream Classes: Handle input/output operations.

Stream TypeClasses
Byte StreamsInputStream, OutputStream
Character StreamsReader, Writer
File StreamsFileInputStream, FileOutputStream
Buffered StreamsBufferedReader, BufferedWriter

Stream Features:

  • Sequential: Data flows in sequence
  • One Direction: Either input or output
  • Automatic: Handles low-level details

Mnemonic: "Byte Character File Buffered"

Question 5(c) [7 marks]

Write a java program to create a text file and perform read operation on the text file.

Answer:

Java

Key Points:

  • FileWriter: Creates and writes to file
  • BufferedReader: Efficient reading
  • Exception Handling: Handle IOException

Mnemonic: "Create Write Read Close"

Question 5(a OR) [3 marks]

Explain Divide by Zero Exception in Java with example.

Answer:

ArithmeticException: Thrown during divide by zero operation.

Java

Key Points:

  • Integer Division: Only integer division by zero throws exception
  • Floating Point: Returns Infinity for floating point division
  • Runtime Exception: Unchecked exception

Mnemonic: "Zero Division Arithmetic Error"

Question 5(b OR) [4 marks]

Explain java I/O process.

Answer:

Java I/O Process: Mechanism for reading and writing data.

I/O Components:

  • Stream: Sequence of data
  • Buffer: Temporary storage for efficiency
  • File: Persistent storage
  • Network: Remote data transfer

I/O Types:

  • Byte-oriented: Raw data (images, videos)
  • Character-oriented: Text data
  • Synchronous: Blocking operations
  • Asynchronous: Non-blocking operations

Mnemonic: "Stream Buffer File Network"

Question 5(c OR) [7 marks]

Write a java program to create a text file and perform write operation on the text file.

Answer:

Java

Key Points:

  • FileWriter: Writes character data to file
  • BufferedWriter: More efficient for large data
  • Auto-close: Use try-with-resources for automatic closing

Mnemonic: "Create Write Close Handle"