0% found this document useful (0 votes)
39 views5 pages

Answer Key OOPS

Uploaded by

hodcse
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)
39 views5 pages

Answer Key OOPS

Uploaded by

hodcse
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
You are on page 1/ 5

Answer Key

CS3391-Object Oriented Programming

Part-A

1. Define Objects and Classes in Java

A class is a blueprint or template for creating objects. It defines the attributes (also known as
fields or properties) and behaviors (methods) that the objects created from it will have.

An object is an instance of a class. When a class is defined, no memory is allocated until an


object of that class is created.

2. Explain the features of Java.

 Simple
 Object-Oriented
 Platform-Independent
 Secure

3. Define Abstraction and Encapsulation.

Abstraction is the concept of hiding complex implementation details and showing only the
essential features of an object to the outside world

Encapsulation is the concept of bundling the data (variables) and methods (functions) that
operate on the data into a single unit or class, while restricting access to some of the object's
components

4. Define constructor

A constructor is a special method used to initialize objects. It is called when an instance of a


class (an object) is created, and it sets the initial state of the object by assigning values to its
attributes

5. Explain What is Javadoc?

Javadoc is a documentation tool in Java that generates HTML documentation for your code
by parsing special comments in your source files

6. Express a Java programming structure to display “Hello World?

public class HelloWorld


{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
7. Distinguish the difference between super class and sub class?
Superclass
Also called Parent Class or Base Class
A superclass is a class that is extended by another class
Subclass
Also called Child Class or Derived Class
A subclass is a class that inherits from a superclass.

8. List the various access specifiers supported by OOPS

Here are the four main access specifiers

 Public
 Private
 Protected
9. Explain the use of extend keyword with suitable example?

The extends keyword in Java is used to create a subclass by inheriting properties and
behaviors from an existing superclass.

10. Classify inheritance and polymorphism

Inheritance is a mechanism in OOP where a new class (called the subclass or derived class)
is based on an existing class (called the superclass or base class).

Polymorphism means "many forms" and allows objects of different classes to be treated as
objects of a common superclass.

Part-B

11(a).Explain what are the features of Object Oriented Programming?

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects,"


which can contain data and code that manipulates that data.
Encapsulation
Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data
into a single unit, typically a class.
Abstraction
Abstraction is the concept of hiding the complex implementation details and exposing only the
essential features of an object
Inheritance
Inheritance is the mechanism by which one class (subclass) can inherit attributes and methods from
another class (superclass)
Polymorphism
Polymorphism allows methods to perform different functions based on the object that invokes them
Classes and Objects
A class is a blueprint for creating objects. It defines the data attributes and methods that the objects
created from the class can use
Message Passing
Objects communicate with each other through message passing, which involves calling methods on
objects to request actions or data.
(OR)
11(B). Describe Java Buzzwords?
Java buzzwords are a set of keywords that encapsulate the core principles and features of the Java
programming language.
1. Simple
2. Object-Oriented
3. Platform-Independent
4. Secure
5. Robust
6. Multithreaded
7. Architecture-Neutral
8. Interpreted
9. High Performance
10. Distributed
11. Dynamic
12. Simple

12(a) Express a Java Program to create class named Shape that contains two integers and an empty
method named print Area (). Provide three classes named Rectangle, Triangle and Circle such that
each one of the classes extends the class Shape. Each one of the classes contains only the method
print Area () that prints the area of the given shape.

class Shape
{
// Two integers to store dimensions
int dimension1;
int dimension2;

// Empty method to be overridden


void printArea()
{
// This method will be overridden in subclasses
}
}

// Rectangle class
class Rectangle extends Shape
{
// Constructor to initialize dimensions
Rectangle(int length, int width)
{
this.dimension1 = length;
this.dimension2 = width;
}
// Overriding printArea() method
@Override
void printArea() {
int area = dimension1 * dimension2; // Area = length * width
System.out.println("Area of Rectangle: " + area);
}
}
// Triangle class
class Triangle extends Shape
{
// Constructor to initialize dimensions
Triangle(int base, int height)
{
this.dimension1 = base;
this.dimension2 = height;
}
// Overriding printArea() method
@Override
void printArea()
{
double area = 0.5 * dimension1 * dimension2; // Area = 0.5 * base * height
System.out.println("Area of Triangle: " + area);
}
}
// Circle class
class Circle extends Shape
{
// Constructor to initialize radius
Circle(int radius)
{
this.dimension1 = radius; // Use dimension1 for radius
}
// Overriding printArea() method
@Override
void printArea()
{
double area = Math.PI * dimension1 * dimension1; // Area = π * radius^2
System.out.println("Area of Circle: " + area);
}
}
// Main class to demonstrate the functionality
public class Main
{
public static void main(String[] args)
{
// Creating objects of each shape
Rectangle rectangle = new Rectangle(5, 10);
Triangle triangle = new Triangle(4, 6);
Circle circle = new Circle(3);
// Calling printArea() for each shape
rectangle.printArea(); // Output: Area of Rectangle: 50
triangle.printArea(); // Output: Area of Triangle: 12.0
circle.printArea(); // Output: Area of Circle: 28.274333882308138
}}
(OR)
12(b) Explain Inheritance and its types with example?
Inheritance is a fundamental concept in object-oriented programming (OOP) that
allows one class (the subclass or derived class) to inherit the properties and behaviors
(methods) of another class (the superclass or base class).
Types of Inheritance
There are several types of inheritance in Java:
 Single Inheritance
 Multilevel Inheritance
 Hierarchical Inheritance
 Multiple Inheritances (through interfaces)
 Hybrid Inheritance (through interfaces)
1. Single Inheritance
In single inheritance, a class inherits from one superclass only.
2. Multilevel Inheritance
In multilevel inheritance, a class inherits from a superclass, and another class inherits from
that subclass
3. Hierarchical Inheritance
In hierarchical inheritance, multiple subclasses inherit from the same superclass
4. Multiple Inheritances (through Interfaces)
Java does not support multiple inheritances with classes to avoid ambiguity, but it allows
multiple inheritances through interfaces.
5. Hybrid Inheritance (through Interfaces)
Hybrid inheritance is a combination of two or more types of inheritance. In Java, it is
typically implemented using interfaces

You might also like