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

Anskey

The document is an answer key for a Continuous Internal Assessment for the CS3391 Object Oriented Programming course. It includes definitions, distinctions, and examples related to key concepts in Java such as access specifiers, constructors, inheritance, and method overloading. Additionally, it outlines primitive data types and the use of packages in Java programming.

Uploaded by

pradeeplavrp09
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Anskey

The document is an answer key for a Continuous Internal Assessment for the CS3391 Object Oriented Programming course. It includes definitions, distinctions, and examples related to key concepts in Java such as access specifiers, constructors, inheritance, and method overloading. Additionally, it outlines primitive data types and the use of packages in Java programming.

Uploaded by

pradeeplavrp09
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

B.E / B.

Tech DEGREE CONTINUOUS INTERNAL ASSESSMENT-I


September – 2024
Third Semester
Department of Computer Science and Engineering
CS3391 – OBJECT ORIENTED PROGRAMMING
(Regulations 2021)

ANSWER KEY

PART A – (11 x 2 = 22 marks)


1. Define access specifier and list its types.
These Specifiers determine whether a field or method in a class, can be used or invoked by another method in
another class or sub-class.
Types of Access Specifiers: 1. public 2. private 3. protected 4. default (no specifier).

2. Distinguish between method and constructor.


Constructor:
Initializes a new object.
No return type and same name as the class.

Method:
Performs specific tasks.
Has a return type and can have any name.

3. What is an array? How multidimensional arrays are implemented in JAVA?


An array is a data structure that stores a fixed-size sequential collection of elements of the same type.
It allows you to store multiple values in a single variable. Multidimensional arrays are arrays of
arrays. They can be declared and initialized as follows:
int[][] twoDArray = new int[3][4]; // 3 rows and 4 columns.

4. Define inheritance.
Inheritance is a fundamental concept in object-oriented programming where a new class (subclass)
inherits properties and behaviors (methods) from an existing class (superclass). This allows for code
reusability and the creation of a hierarchical relationship between classes.

5. Java language is platform independent. Justify your answer.


Java is platform-independent because its compiled bytecode can run on any operating system with a
Java Virtual Machine (JVM). The JVM interprets the bytecode, allowing the same Java program to
run on different platforms without modification.

6. What is JVM?
The Java Virtual Machine (JVM) is an abstract computing machine that enables a computer to run
Java programs. The JVM interprets compiled Java bytecode and converts it into machine code that
can be executed by the host
7. What is meant by abstract class?
In Java, we can have an abstract class without any abstract method. This allows us to create classes
that cannot be instantiated, but can only be inherited. 4) Abstract classes can also have final methods
(methods that cannot be overridden).

8. Define static inner classes.


Nested Classes. The Java programming language allows you to define a class within another class. A
nested class is a member of its enclosing class. Static nested classes (inner classes) have
access to other members of the enclosing class, even if they are declared private.
9. Illustrate about the keyword super.
In Java Programming/Keywords/super. It is used inside a sub-class method definition to call a
method defined in the super class. Only public and protected methods can be called by the super
keyword. It is also used by class constructors to invoke constructors of its parent class.

10. Differentiate method overloading and method overriding.


Method Overloading:Occurs within the same class.Methods have the same name but different
parameters.
Method Overriding:Occurs in a subclass.Methods have the same name and parameters as in the
superclass.

11. What is a package?


A package in Java is a way to group related classes, interfaces, and sub-packages. It helps organize
code, avoid name conflicts, and control access to classes.

PART B – (1 x 13 = 13 marks)

12. a) Elaborate on constructor with example. (13)

Definition (2 Marks)
A constructor in Java is a special method used to initialize objects. It is called when an
instance of a class is created and can set initial values for object attributes.
Types (2 Marks)
Default Constructor: A constructor with no parameters. It initializes objects with default
values.
Parameterized Constructor: A constructor that takes arguments to initialize objects with
specific values2.
Syntax (3 Marks)
class ClassName {
// Default Constructor
ClassName() {
// Initialization code
}

// Parameterized Constructor
ClassName(int param1, String param2) {
// Initialization code
}
}
Program (4 marks)
public class Car {
String model;
int year;

// Default Constructor
public Car() {
model = "Unknown";
year = 0;
}

// Parameterized Constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}

public static void main(String[] args) {


// Using Default Constructor
Car car1 = new Car();
System.out.println("Car1 Model: " + car1.model + ", Year: " + car1.year);

// Using Parameterized Constructor


Car car2 = new Car("Toyota", 2021);
System.out.println("Car2 Model: " + car2.model + ", Year: " + car2.year);
}
}
Explanation (2 Marks)
Default Constructor: When car1 is created, the default constructor initializes model to “Unknown”
and year to 0.
Parameterized Constructor: When car2 is created, the parameterized constructor initializes model to
“Toyota” and year to 2021

12 b) Outline the primitive data types in Java with example. (13)

Primitive Data Types in Java (1 Mark Each)


byte: An 8-bit signed integer. Range: -128 to 127.
byte b = 100;
short: A 16-bit signed integer. Range: -32,768 to 32,767.
short s = 10000;
int: A 32-bit signed integer. Range: -2,147,483,648 to 2,147,483,647.
int i = 100000;
long: A 64-bit signed integer. Range: -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807.
long l = 100000L;
float: A 32-bit floating-point number. Range: approximately ±3.40282347E+38F.
float f = 10.5f;
double: A 64-bit floating-point number. Range: approximately
±1.79769313486231570E+308.
double d = 10.5;
char: A 16-bit Unicode character. Range: ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535 inclusive).
char c = 'A';
boolean: Represents one bit of information. Only two possible values: true and false.
boolean bool = true;
Program (5 Marks)
public class PrimitiveDataTypes {
public static void main(String[] args) {
byte b = 100;
short s = 10000;
int i = 100000;
long l = 100000L;
float f = 10.5f;
double d = 10.5;
char c = 'A';
boolean bool = true;

System.out.println("byte: " + b);


System.out.println("short: " + s);
System.out.println("int: " + i);
System.out.println("long: " + l);
System.out.println("float: " + f);
System.out.println("double: " + d);
System.out.println("char: " + c);
System.out.println("boolean: " + bool);
}
}
13 a) Explain in detail about basics of Inheritance and elaborate on any two types of
inheritance in detail. (15)
Basics of Inheritance (5 Marks)
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class
(subclass or derived class) to inherit the properties and behaviors (methods) of another class
(superclass or base class). This promotes code reusability and establishes a natural hierarchical
relationship between classes.
Key points:
Reusability: Inheritance allows the reuse of existing code, reducing redundancy.
Extensibility: New functionalities can be added to existing classes without modifying them.
Polymorphism: Inheritance supports method overriding, enabling polymorphism.
Hierarchy: Establishes a parent-child relationship between classes.
Maintenance: Easier to maintain and manage code due to the hierarchical structure.
Types of Inheritance in Java
Java supports several types of inheritance, each serving different purposes in object-oriented
programming. Here are the main types:
Single Inheritance: A subclass inherits from one superclass.
Multilevel Inheritance: A class is derived from another class, which is also derived from another
class.
Hierarchical Inheritance: Multiple classes inherit from a single superclass.
Hybrid Inheritance: A combination of two or more types of inheritance. Note that Java does not
support multiple inheritance directly through classes but can achieve it through interfaces.

1. Single Inheritance
In single inheritance, a subclass inherits from only one superclass.
Program Example:
// Superclass
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}

// Subclass
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}

public class SingleInheritance {


public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark(); // Subclass method
}
}
Explanation:
The Dog class inherits the eat method from the Animal class.
The Dog class also has its own method bark.
When an object of Dog is created, it can call both eat and bark methods.
2. Multilevel Inheritance
In multilevel inheritance, a class is derived from another class, which is also derived from another
class.
Program Example:
// Base class
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}

// Derived class
class Mammal extends Animal {
void walk() {
System.out.println("This mammal walks.");
}
}

// Further derived class


class Dog extends Mammal {
void bark() {
System.out.println("The dog barks.");
}
}

public class MultilevelInheritance {


public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal
dog.walk(); // Inherited from Mammal
dog.bark(); // Defined in Dog
}
}
Explanation:
The Dog class inherits from Mammal, which in turn inherits from Animal.
The Dog class can access methods from both Mammal and Animal classes.
This demonstrates the hierarchical chain of inheritance.

13 b i) Elaborate on Method Overloading with an example. (7)


Definition (2 marks):
Method overloading is a feature in object-oriented programming that allows a class to have multiple
methods with the same name but different parameter lists. It helps in improving the readability and
usability of code by allowing a single method name to handle different types of inputs or operations.
Syntax (2 marks):

class ClassName {
void methodName(int a) {
// method with one integer parameter
}

void methodName(int a, int b) {


// overloaded method with two integer parameters
}

void methodName(double a) {
// overloaded method with one double parameter
}
}
Program (3 marks):
class Calculator {
// Method to add two integers
int add(int a, int b) {
return a + b;
}

// Overloaded method to add three integers


int add(int a, int b, int c) {
return a + b + c;
}

// Overloaded method to add two double values


double add(double a, double b) {
return a + b;
}

public static void main(String[] args) {


Calculator calc = new Calculator();

// Calling the overloaded methods


System.out.println("Sum of 2 integers: " + calc.add(10, 20));
System.out.println("Sum of 3 integers: " + calc.add(10, 20, 30));
System.out.println("Sum of 2 doubles: " + calc.add(10.5, 20.5));
}
}
Output:
Sum of 2 integers: 30
Sum of 3 integers: 60
Sum of 2 doubles: 31.0

13 b) ii) What are packages? Explain how will you import a package in Java with example (8)
Package (4 marks):
In Java, a package is a mechanism used to group related classes and interfaces together,
essentially organizing the code to avoid naming conflicts and making it easier to manage.
Packages act like folders in a directory structure, providing a way to categorize and control
access to different parts of a program. Java has two types of packages:
Built-in packages: These are the predefined packages provided by Java, such as java.util,
java.lang, java.io, etc.
User-defined packages: These are custom packages created by developers to group related
classes and interfaces together.
By using packages, it becomes easier to:
Avoid class name conflicts.
Provide access control (using public, protected, and private access modifiers).
Manage and maintain the code efficiently.
Importing a Package (4 marks):
To use classes from other packages in Java, you need to import the package. This can be done
using the import keyword, which allows access to classes and interfaces in the specified
package. Java provides two ways to import packages:
Import a specific class:
import packageName.ClassName;
This imports only the specific class you need from the package.
Import all classes in a package:
import packageName.*;
This imports all the classes from the specified package.

Example (Java code):


// Defining a user-defined package
package mypackage;

// Creating a class in the package


public class MyClass {
public void displayMessage() {
System.out.println("Hello from MyClass in mypackage!");
}
}

// Main class to import and use the package


import mypackage.MyClass; // importing the specific class
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass(); // Creating an object of MyClass
obj.displayMessage(); // Calling the method from the imported class
}
}
Explanation:
The package mypackage contains the class MyClass.
The import statement import mypackage.MyClass; is used to import this class into the Main
class.The Main class creates an instance of MyClass and calls its displayMessage method to
print a message.

You might also like