OOP Answer-Key
OOP Answer-Key
Q1. Suppose a and b are two integer variables. How do you test whether exactly one of them is zero?
Identify the correct statement. (0.5)
System.out.println((char)i);
}
} (0.5)
1. ** a
2. 1100001
3. A
4. ‘97’
Q6. Which of these access specifiers can be used for an interface? (0.5)
1. **public
2. protected
3. private
4. All of these
Q7. Which of these keywords must be used to monitor for exceptions? (0.5)
1. catch
2. **try
3. finally
4. throw
Q8. What will be the output of the following code?
class exception_handling
{
public static void main(String arg[])
{
try{
System.out.println("Exception" + " " + 1 / 0);
}
catch(ArithmeticException e){
System.out.println("handled");
}
}
}
(0.5)
1. Exception
2. Exception handled
3. **handled
4. No output
Type: DES
Q11. Write a Java program to calculate the total salary of 'n' employees by implementing salary and
allowances interfaces. The salary is calculated based on Basic Pay, HRA (12% of basic pay), and DA
(6.5% of Basic Pay). The allowances are calculated based on Travel Allowance (TA), Medical
Allowance (MA), and Food Allowance (FA).
Gross Salary (GS)=Basic Pay + HRA+DA.
Allowances= TA + MA + FA
Assuming, IT = 10% of GS, PF = 12% of Basic pay, Professional Tax=200.
Net Salary (NS) = GS + Allowances - (IT+ PF +PT)
Note: The program should ask the user for the number of employees and prompts for inputs like Basic
Pay, TA, MA, and FA for each employee. Calculate and print the net salary for each employee. (4)
Scheme:
salary interface with a method → 0.5 mark
Allowance interface with a method → 0.5 mark
Implementation of above interfaces by a class → 2 marks
Main method with Array of objects to process ‘n’ students → 1 mark
import java.util.Scanner;
class EmployeeDemo
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
return lowestIndex;
}
System.out.println("Before swapping:");
x.printArray();
y.printArray();
ArraySwap.swapLowestValues(x, y);
System.out.println("\nAfter swapping:");
x.printArray();
y.printArray();
}
}
Q13. Create a class namely "CarInfo" that stores information about a car, such as: its unique identifier
(integer), model name (string), manufacturer (string), year of manufacture (integer), and price (float).
Implement the Cloneable interface to enable the creation of a copy of the CarInfo object. Create
another class with a main method to demonstrate object cloning . (3)
Scheme:
CarInfo class definition with implementation of Cloneable interface: 1 mark
overridden clone () with correct logic in the CarInfo class: 1 mark
Main method with Object cloning statement: 1 mark.
public CarInfo(int identifier, String modelName, String manufacturer, int yearOfManufacture, float
price)
{
this.identifier = identifier;
this.modelName = modelName;
this.manufacturer = manufacturer;
this.yearOfManufacture = yearOfManufacture;
this.price = price;
}
class ObjectCloneDemo
{
public static void main(String[] args)
{
CarInfo car1 = new CarInfo(1, "Model A", "Company X", 2023, 25000.0f);
System.out.println("Original car: " + car1);
try
{
CarInfo car2 = car1.clone();
System.out.println("Cloned car: " + car2);
car2.setModelName("Model B");
System.out.println("Modified cloned car: " + car2);
System.out.println("Original car: " + car1);
}
catch (CloneNotSupportedException e)
{
System.out.println("Object Cloning failed !!!");
}
}
}
Q14. Consider a parent class as Animal with a method as makeSound(). Create two child classes as
Dog and Cat and demonstrate dynamic method dispatch in the main class for invoking the
makeSound() method of respective classes through parents class reference. (3)
Scheme:
class Animal {
void makeSound() {
System.out.println("Some sound");
}
}
Q15. Illustrate three String class methods with appropriate examples and provide syntax for the same.
. (3)
Scheme:
Three methods, 1 M each with example and syntax
Q16. Write a Java program to demonstrate how to create and use a user-defined exception. The
program should throw a custom exception called InvalidAgeException when a user enters an age
below 18. Use the throw keyword to throw this exception manually. Explain how the custom exception
is handled in the program. (3)
Scheme:
// User-defined exception class ----- 1M
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
Q17. Differentiate between checked and unchecked exceptions with examples. (2)
Compile-time exceptions (checked exceptions) are checked at compile time. The compiler ensures
these exceptions are either handled using try-catch blocks or declared using the throws keyword.
Examples: IOException, SQLException.
Runtime exceptions (unchecked exceptions) occur during program execution and are not checked
by the compiler. Examples: ArithmeticException, NullPointerException.
Q18. Write a Java program that demonstrates method overloading with variable-length arguments
(varargs) to calculate the product of integers. Explain the impact of overloading with variable-length
arguments on code flexibility. (2)
Scheme: 1.5 M for program with overloading,
0.5 for explanation
Code:
public class OverloadVarArgsExample {
// Method with fixed arguments
public static int multiply(int a, int b) {
return a * b;
}
Q18. For the given output below, fill in the code snippet.
Output:
Outer data: 10
Inner data: 20
//Program:
class Outer {
private int outerData = 10;
class Inner {
inner.displayData();
outer.createInnerInstance();
} . (2)
Scheme:
class Outer {
private int outerData = 10;
class Inner {
private int innerData = 20;