BCA Semester V - Java Programming Using Linux
Detailed Answers for 15-Mark Questions
1. Object-Oriented Programming vs Procedure-Oriented Programming
Definition:
Procedure-Oriented Programming (POP) is a paradigm based on functions or procedures that operate on data.
Object-Oriented Programming (OOP) focuses on objects that encapsulate both data and behavior.
Comparison Table:
- POP focuses on functions; OOP focuses on objects.
- POP uses global data; OOP encapsulates data within objects.
- POP has less security; OOP achieves data hiding.
- POP uses top-down approach; OOP uses bottom-up approach.
OOP Concepts:
1. Class: Blueprint of an object.
2. Object: Instance of a class with identity, state, and behavior.
3. Encapsulation: Wrapping data and methods together.
4. Abstraction: Hiding internal details using abstract classes and interfaces.
5. Inheritance: Acquiring properties of one class into another.
6. Polymorphism: Same operation behaves differently in different situations.
Advantages:
- Code reusability, security, easy maintenance, and better modularity.
2. Constructor Overloading with Example
Definition:
Constructor overloading allows defining multiple constructors in a class with different parameter lists.
Example:
class Employee {
String name; int id; double salary;
Employee() { name="Unknown"; id=0; salary=0.0; }
BCA Semester V - Java Programming Using Linux
Detailed Answers for 15-Mark Questions
Employee(String n, int i) { name=n; id=i; salary=25000; }
Employee(String n, int i, double s) { name=n; id=i; salary=s; }
void display(){ System.out.println(name+" "+id+" "+salary); }
public static void main(String[] args){
Employee e1=new Employee();
Employee e2=new Employee("John",101);
Employee e3=new Employee("Alice",102,40000);
e1.display(); e2.display(); e3.display();
}
}
Output:
Unknown 0 0.0
John 101 25000.0
Alice 102 40000.0
Advantages:
Provides multiple ways of object initialization and improves code flexibility.
3. Exception Handling in Java
Definition:
An exception is an abnormal event disrupting the program flow. Exception handling ensures normal program
continuation.
Keywords:
try - block of code that may throw exceptions
catch - handles exceptions
finally - executes always
throw - explicitly throws an exception
throws - declares exceptions in method signature
Example:
BCA Semester V - Java Programming Using Linux
Detailed Answers for 15-Mark Questions
class Divide {
public static void main(String[] args){
int a=10, b=0;
try {
int c=a/b;
} catch(ArithmeticException e){
System.out.println("Division by zero not allowed");
} finally {
System.out.println("Execution completed");
}
}
}
Advantages:
Avoids abnormal termination, centralizes error handling, improves reliability.
4. Applet Lifecycle and Example
Applet:
A small Java program executed inside a web browser or applet viewer.
Lifecycle Methods:
1. init(): Initialization
2. start(): Starts execution
3. paint(): Renders graphics
4. stop(): Suspends applet
5. destroy(): Cleans up resources
Example:
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet {
public void paint(Graphics g){
BCA Semester V - Java Programming Using Linux
Detailed Answers for 15-Mark Questions
g.drawString("Welcome to Java Applet", 50, 50);
}
}
HTML code:
<applet code="MyApplet.class" width="300" height="150"></applet>
Explanation:
Browser calls init() once, start() every time page is loaded, paint() for display, stop() when minimized, destroy() when
closed.
5. Layout Managers in Java Swing
Definition:
Layout Managers control component arrangement within containers.
Types:
1. FlowLayout: Components in a row (default for JPanel).
2. BorderLayout: Divides container into North, South, East, West, Center.
3. GridLayout: Equal-sized grid cells.
4. BoxLayout: Stacks components vertically/horizontally.
5. CardLayout: Multiple panels like cards, one visible at a time.
Example:
import java.awt.*;
import javax.swing.*;
class LayoutDemo {
public static void main(String[] args){
JFrame f=new JFrame("Layout Example");
f.setLayout(new BorderLayout());
f.add(new JButton("North"), BorderLayout.NORTH);
f.add(new JButton("South"), BorderLayout.SOUTH);
f.add(new JButton("East"), BorderLayout.EAST);
BCA Semester V - Java Programming Using Linux
Detailed Answers for 15-Mark Questions
f.add(new JButton("West"), BorderLayout.WEST);
f.add(new JButton("Center"), BorderLayout.CENTER);
f.setSize(400,300);
f.setVisible(true);
}
}
Advantages:
Simplifies GUI design, adapts to window resizing, reduces manual positioning.