The InfyAccount team wants to find the average salary of employees using the below code.

Requirement:

  • If the number of months <= 0, the code should display a message as "Invalid data".
  • For validating the number of months, the developer has defined a custom-exception class 'MyException'.

Write a missing code for the MyException class to meet the above requirement.

//Missing Code for MyException class

public class Demo {

public static void printAverage(double totalSalary, int noOfMonths) throws MyException {
if (noOfMonths <= 0)
throw new MyException("Invalid data");
System.out.println("Average salary = Rs. " + totalSalary / noOfMonths);
}

public static void main(String[] args) {
try {
printAverage(400000, 12); // Output: Average salary = Rs. 33333.333333333336
//printAverage(400000, 0); // Output: Invalid data
} catch (MyException e) {
System.out.println(e.getMessage());
}
}

}
Choose the correct option from below.

class MyException extends Exception {
public Exception(String message) {
 super(message);
}
}

class MyException {
public MyException(String message) {
 super(message);
}
}

class MyException extends Exception {
public String MyException(String message) {
 return (message);
}
}

class MyException extends Exception {
public MyException(String message) {
 super(message);
}
}

Verified Answer
Correct Option - d

To get all Infosys Certified L1 Junior Java Programmer Exam questions Join Group https://bit.ly/infy_premium_group

We're passionate about offering best placement materials and courses!! A one stop place for Placement Materials. We daily post Offcampus updates and Placement Materials.

Qtr No. 213, New Town Yehlanka Indore 454775

admin@prepflix.in