Obj Oriented Prog & Design Question and Answers
( Suggestion : keep refreshing the page for updated content & Search Questions Using Find )
Q.1) 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 errог. [6 Marks] 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]); } }
B. Find the output of the following program: (4 Marks) 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.
A.
There are several syntax errors in the code. I’ll correct them and then explain the output.
public class mid1 { public static void main(String[] args) { String[] words = {"apple", "banana", "orange", "grape"}; // corrected syntax: String[] words int len; // corrected syntax: int len; for (int i = 0; i < words.length; i++) { String word = words[i]; // corrected syntax: String word = words[i]; len = word.length(); // corrected syntax: 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); } } }
Now, let’s analyze the corrected code:
- The program defines an array of strings named
words
. - It iterates over each word in the
words
array. - For each word, it checks if the length is greater than 5.
- If yes, it prints the first 5 characters of the word in uppercase.
- If no, it prints the word as it is.
- It then prints the word and its length.
Given the words
array: {“apple”, “banana”, “orange”, “grape”}
Here’s the output:
APPLE Word=apple length=5 BANAN Word=banana length=6 ORANG Word=orange length=6 GRAPE Word=grape length=5
So, the output of the program is:
APPLE Word=apple length=5 BANAN Word=banana length=6 ORANG Word=orange length=6 GRAPE Word=grape length=5
B.
The error in the given Java program is an “ArrayIndexOutOfBoundsException”. This occurs because the loop tries to access the array element at index numbers.length
, which is out of bounds since array indices in Java start from 0 and end at length - 1
.
Here’s the corrected version of the program:
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[2]); // Index 2 corresponds to the third element } }
Changes made:
- In the loop condition, changed
i <= numbers.length
toi < numbers.length
. This ensures that the loop iterates over valid indices. - In the loop, modified the assignment to
numbers[i]
to(i + 1) * 2
to properly fill the array with multiples of 2 starting from 2. - Changed
numbers[3]
tonumbers[2]
in theSystem.out.println
statement since array indices start from 0 and we’re aiming for the third element.
Q.2) The following program contains some of the variables with values and some of the quotes said 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)); } StatDr APJQuotes) { System.out.println(“If you want to shine like a sun. First burn like a sun “+(6+2)); } void APJO { } System.out.println(” “+(b+a+3)) public static void main(String ar[]) { int v=3: 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.
the program step by step to understand the order of outputs:
- Static Variables Initialization Block (
static
block):b
is assigned the value ofa
, which is1
.quote
is printed: “You have to dream before your dreams can come true”.- The value of
b
is printed: “1”. - “Dreams convert into thought and thought convert into actions” is printed.
- The value of
(b+1)
is printed:(1+1) = 2
.
So far, the output will be: You have to dream before your dreams can come true 1 Dreams convert into thought and thought convert into actions 2
- Main Method:
- Inside the
main
method:v
is declared but never used.DearStudent
object is instantiated.- “You cannot change your future, but you can change your habits and surely your habits will change your future” is printed.
- The value of
(b+y)
is printed:(1+3) = 4
. - “So, now I say I do not copy the answers from others and do not use online resources and loyally collect my score” is printed.
DrKalam
method is invoked.APJ
method ofDearStudent
object is invoked.
The output so far: You have to dream before your dreams can come true 1 Dreams convert into thought and thought convert into actions 2 You cannot change your future, but you can change your habits and surely your habits will change your future 4 So, now I say I do not copy the answers from others and do not use online resources and loyally collect my score
- Inside the
- DrKalam Method:
- Inside the
DrKalam
method:- “Excellence is a continuous process and not an accident” is printed.
Now the final output will be: You have to dream before your dreams can come true 1 Dreams convert into thought and thought convert into actions 2 You cannot change your future, but you can change your habits and surely your habits will change your future 4 So, now I say I do not copy the answers from others and do not use online resources and loyally collect my score Excellence is a continuous process and not an accident
- Inside the
So, the order of outputs will be:
- “You have to dream before your dreams can come true 1”
- “Dreams convert into thought and thought convert into actions 2”
- “You cannot change your future, but you can change your habits and surely your habits will change your future 4”
- “So, now I say I do not copy the answers from others and do not use online resources and loyally collect my score”
- “Excellence is a continuous process and not an accident”
Q.3) 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.
here’s a simple Java code implementing the class Eatables
with the specified attributes and methods:
import java.util.Scanner; public class Eatables { private double chocolateCost; private double biscuitCost; public Eatables(double chocolateCost, double biscuitCost) { this.chocolateCost = chocolateCost; this.biscuitCost = biscuitCost; } public void purchaseChocolate(int quantity) { double totalCost = quantity * chocolateCost; System.out.println("You purchased " + quantity + " chocolate(s). Total cost: $" + totalCost); } public void purchaseBiscuits(int quantity) { double totalCost = quantity * biscuitCost; System.out.println("You purchased " + quantity + " biscuit(s). Total cost: $" + totalCost); } public void displayBill() { // Just a placeholder method for displaying bill, you can modify it as per your requirement System.out.println("Thank you for shopping with us!"); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the cost of chocolate: "); double chocolateCost = scanner.nextDouble(); System.out.println("Enter the cost of biscuit: "); double biscuitCost = scanner.nextDouble(); Eatables eatables = new Eatables(chocolateCost, biscuitCost); System.out.println("How many chocolates do you want to purchase?"); int chocolateQuantity = scanner.nextInt(); eatables.purchaseChocolate(chocolateQuantity); System.out.println("How many biscuits do you want to purchase?"); int biscuitQuantity = scanner.nextInt(); eatables.purchaseBiscuits(biscuitQuantity); eatables.displayBill(); } }
This code defines a class Eatables
with private attributes chocolateCost
and biscuitCost
, along with methods purchaseChocolate
, purchaseBiscuits
, and displayBill
as per your requirements. It also includes a main
method to demonstrate the usage of the class.
For More Updates Join Our Channels :