OBJ Oriented Prog & Design 2024 Q&A


 OBJ Oriented Prog & Design Question and Answers





( Suggestion :  keep refreshing the page for updated content & Search Questions Using Find )


Q1. Create a class name as Eatbles with attributes like (chocolatecost, biscuitcost) with private access specifier and methods purchaceChocolate, purchaseBiscuits and displayBill method to display the total after customer makes a purchase customer should enter quantity.

Answer:

import java.util.Scanner;

public class Edibles {
    private double chocolateCost;
    private double biscuitCost;

    public Edibles(double chocolateCost, double biscuitCost) {
        this.chocolateCost = chocolateCost;
        this.biscuitCost = biscuitCost;
    }

    public double purchaseChocolate(int quantity) {
        return quantity * chocolateCost;
    }

    public double purchaseBiscuits(int quantity) {
        return quantity * biscuitCost;
    }

    public void displayBill(int chocolateQty, int biscuitQty) {
        double totalCost = purchaseChocolate(chocolateQty) + purchaseBiscuits(biscuitQty);
        System.out.println("Total cost: " + totalCost);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter cost of chocolate: ");
        double chocolateCost = scanner.nextDouble();
        
        System.out.print("Enter cost of biscuit: ");
        double biscuitCost = scanner.nextDouble();

        Edibles edibles = new Edibles(chocolateCost, biscuitCost);

        System.out.print("Enter quantity of chocolates: ");
        int chocolateQty = scanner.nextInt();
        
        System.out.print("Enter quantity of biscuits: ");
        int biscuitQty = scanner.nextInt();

        edibles.displayBill(chocolateQty, biscuitQty);
        
        scanner.close();
    }
}



Q2 (A): Find the error if any in the following Java program. Identify the error in the program and explain why it occurs. Provide the correct code to fix the error.  

public class ErrorProgram
{
public static void main (String[] args)
{
int[] numbers = new int[5];

for (int i = 1; i <= numbers.length; i++)
{
numbers[i] = i * 2;
}
System.out.println("Result:" + numbers[3]);

}
}

Answer: 

The error in the provided Java program occurs due to an ArrayIndexOutOfBoundsException. This error happens because the loop tries to access an index that is out of the valid range of the array. In Java, arrays are zero-indexed, meaning the valid indices range from 0 to length - 1.

In the provided loop: 

for (int i = 1; i <= numbers.length; i++) {
    numbers[i] = i * 2;
}


To fix this error, you should adjust the loop to start from 0 and go up to numbers.length - 1. Here's the corrected code:

public class ErrorProgram {
    public static void main(String[] args) {
        int[] numbers = new int[5];

        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = (i + 1) * 2;
        }

        System.out.println("Result:" + numbers[3]);
    }
}





Q2 (B): Find the output of the following program: 

public class mid1 {

public static void main(String[] args) {

String[] words = {"apple", "banana", "orange", "grape"};

int len;

for (int i = 0; i < words.length; i++) {

String word = words[i];

len=word.length();

if (word.length() > 5) {

System.out.print(" "+word.substring(0,5).toUpperCase() + " ");

} else {

System.out.print(" "+word+"");

}
System.out.println(" "+"Word="+word+" "+"length="+len);

}
}
}


Answer: 

Output:

 apple Word=apple length=5
 BANAN  Word=banana length=6
 ORANG  Word=orange length=6
 grape Word=grape length=5






Q3. The following program contains some of the variables with values and some of the quotes sald by Dr. APJ Abdul Kalam. After the execution of the program, what will be the order of the outputs? Write the output in the proper order and elaborately explain the reason behind the order of the outputs.


class statDrAPJQuotes

{

static int a=1;

static int b;

static String quote="You have to dream before your dreams can come true";

static void DrKalam(int x)

{ System.out.print("Excellence is a continuous process and not an accident"); 
    
}
static

{

b=a;

System.out.print(quote); 

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

System.out.print("Dreams convert into thought and thought convert into actions");

System.out.println(" "+(b+1));

}

statDrAPJQuotes()

{ System.out.println("If you want to shine like a sun. First burn like a sun "+(b+2));
}
void APJ()

{ System.out.println(" "+(b+a+3));
}


public static void main(String ar[])

{

int y=3;

statDrAPJQuotes DearStudent=new statDrAPJQuotes();

System.out.print("You cannot change your future, but you can change your habits and surely your habits will change your future");

System.out.println(" "+(b+y));

System.out.println("So, now I say I do not copy the answers from others and do not use online resources and loyally collect my score");

statDrAPJQuotes.DrKalam(1);

DearStudent.APJ();
}
}


Answer: 

Output Order:

  1. You have to dream before your dreams can come true 1 (from the static initializer)
  2. Dreams convert into thought and thought convert into actions 2 (from the static initializer)
  3. You cannot change your future, but you can change your habits and surely your habits will change your future 6 (from main)
  4. Excellence is a continuous process and not an accident (from DrKalam method called in main)
  5. 7 (from APJ method called in main)
  6. If you want to shine like a sun. First burn like a sun 9 (from the constructor)
    • Note: This output might appear before output 5 depending on the implementation of constructors vs. static initializers in the specific Java environment.

Explanation:

1. Static Initializer:
  • Executes once before the first object creation.
  • Initializes b to the value of a (1).
  • Prints the quote and b.
  • Prints another quote and b + 1 (2).

2.main Method:
  • Creates an object DearStudent of class statDrAPJQuotes.
  • Prints a quote and b + y (b is still 1 from the static initializer, y is 3, so the output is 4).
  • Prints another quote.
  • Calls DrKalam method (output 4).
  • Calls DearStudent.APJ method (output 5).


3.main Method:
  • Might execute before or after the constructor depending on the Java environment.
  • Prints a quote and b + 2 (b is still 1, so the output is 3).
Key Points:
  • Static variables (a and b) are shared by all objects of the class.
  • Static initializers run only once before the first object creation.
  • Constructor execution timing might vary slightly between Java environments.
Noteworthy Observation:
  • The output "If you want to shine like a sun..." might appear before "7" in some environments due to potential differences in how Java handles static initializer and constructor execution order. This is a subtle detail that might not be consistent across all Java implementations.