Programming Practices Questions
Programming Practices Questions
INDEX
S.No. Description Date Teacher’s Sign
1. Installation of J2SDK
Write a program to show
2.
Scope of Variables
Write a program to show
3. Concept of CLASS in JAVA
Write a program to show Type
4. Casting in JAVA
Write a program to show How
5. Exception Handling is in JAVA
Write a Program to show
6. Inheritance
Write a program to show
7. Polymorphism
Write a program to show
8. Access Specifiers (Public,
Private, Protected) in JAVA
Write a program to show use
9. and Advantages of
CONTRUCTOR
Write a program to show
10. Interfacing between two
classes
2|Page
Program – 1
Installation of J2SDK
Installing the JDK (Java Development Kit) is a crucial step for any Java
programmer. Following good programming practices ensures that the
installation process is smooth and aligns with industry standards. Here are
some programming practices to consider when installing the JDK:
1. Verify System Requirements: Before installing the JDK, make sure your
system meets the minimum requirements specified by the JDK provider. Check
the supported operating systems and hardware specifications.
2. Download from Official Source: Always download the JDK installer from
the official website of the JDK provider, such as Oracle or OpenJDK. Avoid
downloading from third-party sources to ensure authenticity and avoid
potential security risks.
3. Choose the Right Version: Select the appropriate JDK version based on
your project requirements and the features you need. In an academic setting,
you might be required to use a specific JDK version, but in a real-world
scenario, consider using the latest stable version for access to the latest features
and bug fixes.
6. Update PATH Variable: Update the system's `PATH` variable to include the
JDK's `bin` directory. This allows you to run Java commands from any
directory without specifying the full path.
8. Keep JDK Updated: Regularly check for updates and security patches from
the JDK provider's website. Staying up-to-date with the latest JDK version
helps improve performance and ensures your code benefits from the latest
features and security fixes.
Program – 2
Write a program to show Scope of Variables
Description:
This Java program demonstrates the concept of variable scope in Java. It
declares a class-level variable (`classVar`) and a method-level variable
(`methodVar`). The program shows how these variables can be accessed within
different methods and how method-level variables are limited to their declaring
method.
Algorithm:
1. Declare a class `ScopeDemo`.
2. Declare a class-level variable `classVar` and set it to 10.
3. Define the `main` method:
a. Declare a method-level variable `methodVar` and set it to 20.
b. Print the values of `classVar` and `methodVar`.
c. Call the `accessLocalVariable` method.
4. Define the `accessLocalVariable` method:
a. Print the value of `classVar`.
Program: ScopeDemo.java
accessLocalVariable();
Output:
Class-level variable (classVar): 10
Method-level variable (methodVar): 20
Accessing classVar in another method: 10
6|Page
Program – 3
Write a program to show Concept of CLASS in JAVA
Description:
In Java, a class is a blueprint or a template that defines the structure and
behavior of objects. It encapsulates data (attributes) and methods (functions)
that operate on that data. Objects are instances of classes, and they allow us to
create multiple instances of the same type with their own unique data.
Algorithm:
1. Define a class named `Person`.
2. Declare private data members (attributes) such as `name`, `age`, and
`gender`.
3. Define a constructor to initialize the attributes when a `Person` object is
created.
4. Create getter and setter methods to access and modify the attributes of a
`Person`.
5. Define a method `displayInfo()` to display the information of a `Person`
object.
Program: Person.java
this.name = name;
this.age = age;
this.gender = gender;
// Getter methods
return name;
return age;
return gender;
// Setter methods
this.name = name;
this.age = age;
this.gender = gender;
8|Page
Program: Main.java
System.out.println("Person 1 Info:");
person1.setAge(31);
9|Page
person1.displayInfo();
Output:
Person 1 Info:
Name: Alice
Age: 30
Gender: F
Program – 4
Write a program to show Type Casting in JAVA
Description:
Type casting in Java allows you to convert a variable from one data type to
another. Sometimes, you may need to explicitly convert the variable to perform
certain operations or assignments where data types are not compatible.
Algorithm:
1. Declare two variables of different data types, e.g., `int` and `double`.
2. Perform an operation or assignment that requires type casting.
3. Explicitly cast one variable to the desired data type.
4. Print the results before and after type casting.
Program: TypeCastingDemo.java
Output:
Implicit casting: int to double
int value: 50
double value: 50.0
Program – 5
Write a program to show How Exception Handling is in JAVA
Description:
Exception handling in Java allows you to gracefully handle runtime errors or
exceptional situations that may occur during program execution. By using try-
catch blocks, you can identify and handle exceptions to prevent program
termination and provide meaningful error messages to the users.
Algorithm:
1. Define a Java program that includes a method that may potentially throw an
exception.
2. Use a try-catch block to handle the exception.
3. Within the try block, call the method that can throw an exception.
4. Catch the exception in the catch block and handle it appropriately.
Program: ExceptionHandlingDemo.java
import java.util.Scanner;
try {
} finally {
scanner.close();
System.out.println("Program completed.");
if (number == 0) {
return 10 / number;
Output:
Enter a number: 0
Exception caught: Cannot divide by zero.
Program completed.
14 | P a g e
Program – 6
Write a Program to show Inheritance
Description:
Inheritance is an important concept in object-oriented programming (OOP). It
allows one class (subclass or derived class) to inherit the properties and
behaviors of another class (superclass or base class). This enables code reuse
and promotes a hierarchical relationship between classes.
Algorithm:
1. Define a superclass (base class) with common attributes and methods that
are to be inherited.
2. Define a subclass (derived class) that extends the superclass.
3. The subclass automatically inherits all the non-private attributes and
methods from the superclass.
4. Add additional attributes and methods specific to the subclass if needed.
Program: InheritanceDemo.java
class Animal {
String species;
int age;
this.species = species;
this.age = age;
}
15 | P a g e
String breed;
this.breed = breed;
}
16 | P a g e
dog1.displayInfo();
dog1.makeSound();
Output:
Species: Canine
Age: 5
Breed: Labrador
Dog barks: Woof! Woof!
Inheritance allows the `Dog` class to inherit the common attributes and
methods from the `Animal` class, while still having its unique characteristics.
This promotes code reuse and a hierarchical relationship between the classes.
17 | P a g e
Program – 7
Write a program to show Polymorphism
Description:
Polymorphism is another fundamental concept in object-oriented programming
(OOP). It allows a class to take multiple forms or have multiple behaviors. In
Java, polymorphism is achieved through method overriding and method
overloading.
Algorithm:
1. Define a superclass with a method that can be overridden in the subclass.
2. Define a subclass that extends the superclass and overrides the method with
a different implementation.
3. Use method overloading to create multiple methods with the same name but
different parameter lists.
Program: PolymorphismDemo.java
// Superclass
class Animal {
// Subclass
@Override
System.out.print("Woof! ");
System.out.println();
// Another Subclass
@Override
Output:
Dog barks: Woof! Woof!
Cat meows: Meow! Meow!
Dog barks: Woof! Woof!
Woof! Woof! Woof!
Program – 8
Write a program to show Access Specifiers (Public, Private,
Protected) in JAVA
Description:
Access specifiers in Java define the visibility or accessibility of classes,
attributes, methods, and constructors within and outside the class. There are
four types of access specifiers: `public`, `private`, `protected`, and the default
(no specifier).
Algorithm:
1. Define a class with attributes and methods using different access specifiers.
2. Create an object of the class and demonstrate accessing the attributes and
methods from different contexts (inside the class, in the same package, and in a
different package).
Program: AccessSpecifierDemo.java
void defaultMethod() {
Program: Main.java
22 | P a g e
demo.publicMethod();
// Private attribute and method cannot be accessed from outside the class
// demo.privateMethod();
// demo.protectedMethod();
// Default attribute and method can be accessed from the same package
demo.defaultMethod();
}
23 | P a g e
Output:
Public attribute: 10
Public method can be accessed from anywhere.
Default attribute: 40
Default method can be accessed within the same package.
24 | P a g e
Program – 9
Write a program to show use and Advantages of CONTRUCTOR
Description:
In Java, a constructor is a special method used to initialize objects of a class.
Constructors have the same name as the class and are called automatically
when an object is created using the `new` keyword. They are useful for setting
initial values, allocating resources, and performing necessary tasks when an
object is created.
Advantages of Constructors:
1. Object Initialization: Constructors ensure that objects are properly
initialized with the required initial values when they are created.
2. Automatic Invocation: Constructors are automatically called when an
object is created, so you don't need to manually initialize the object after its
creation.
3. Overloading: You can have multiple constructors with different parameter
lists (constructor overloading), allowing you to create objects with different
initializations.
4. Memory Allocation: Constructors can be used to allocate resources or
perform memory-related tasks when an object is created.
5. Encapsulation: Constructors help encapsulate the object creation process
within the class, ensuring that the object is always in a valid state.
Algorithm:
1. Create a class with a constructor to demonstrate its usage and advantages.
2. Define attributes that represent the object's state.
3. Implement a constructor to initialize the attributes.
4. Create objects using the constructor and observe the initialization process.
25 | P a g e
Program: ConstructorDemo.java
this.name = name;
this.age = age;
public ConstructorDemo() {
this.name = "Unknown";
this.age = 0;
person1.displayInfo();
person2.displayInfo();
Output:
Object created with constructor: Alice, 30 years old
Default object created: Unknown, 0 years old
Name: Alice
Age: 30
Name: Unknown
Age: 0
27 | P a g e
Program – 10
Write a program to show Interfacing between two classes
Description:
In Java, interfaces allow two or more classes to interact with each other without
sharing a common parent class. An interface defines a contract, specifying a set
of abstract methods that classes implementing the interface must provide
concrete implementations for. This concept is known as "interfacing" between
classes.
Algorithm:
1. Create an interface with abstract method signatures.
2. Implement the interface in one or more classes.
3. Provide concrete implementations for the interface methods in each
implementing class.
4. Use the interface to interact between objects of different classes.
Program:
interface Shape {
this.radius = radius;
@Override
this.length = length;
this.width = width;
@Override
}
29 | P a g e
printArea(circle);
printArea(rectangle);
Output:
Area: 78.53981633974483
Area: 24.0