Anskey
Anskey
ANSWER KEY
Method:
Performs specific tasks.
Has a return type and can have any name.
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.
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).
PART B – (1 x 13 = 13 marks)
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;
}
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.");
}
}
// Derived class
class Mammal extends Animal {
void walk() {
System.out.println("This mammal walks.");
}
}
class ClassName {
void methodName(int a) {
// method with one integer parameter
}
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;
}
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.