0% found this document useful (0 votes)
2 views3 pages

Sample Question On Core Java MPT

The document outlines the implementation of two Java classes: Employee and Source, along with their respective methods for exception handling and generating letter combinations from digit inputs. The Employee class includes fields for employee details and a method to validate these details, throwing custom exceptions as needed. The Source class provides a method to return all possible letter combinations for a given string of digits using a mapping of digits to letters.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Sample Question On Core Java MPT

The document outlines the implementation of two Java classes: Employee and Source, along with their respective methods for exception handling and generating letter combinations from digit inputs. The Employee class includes fields for employee details and a method to validate these details, throwing custom exceptions as needed. The Source class provides a method to return all possible letter combinations for a given string of digits using a mapping of digits to letters.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Exception Handling Question :

Your task here is to implement a Java code based on the following specifications.
Note that your code should match the specifications in a precise manner.
Consider default visibility of class unless mentioned otherwise.

class definitions:clas Employee:data fields:


employeeName: String
employeeID: Integer
status: String
method definition:
Employee(Integer id, String name):constructor
visibility: public

class InvalidEmployeeException:
method definitions:
InvalidEmployeeException(String msg):
visibility: public

class ExceptionCheck:
validateEmployee(Employee employee) throws Exception:
Visibility:public
Return type: String

TASK:
Class Employee
-declare the String variable employeeName
-declare the Integer variable employeeID
-declare the String variable status
-define a parameterized constructor to initialize employeeName and employeeID
Class ExceptionCheck
Implement the below method for this class:
1. String validateEmployee(Employee employee) :

 Use try-catch to implement the method.


 throw a user-defined exception InvalidEmployeeException with message
"Employee name invalid" if emloyeeName is null or length of employeeName
is less than 3
 throw a user-defined exception InvalidEmployeeException with message
"Employee id invalid" if emloyeeID is null or employeeID is less than or equal
to 100
 If the length of employeeName is greater than or equal to 3
and employeeID is greater than 100, assign the string "success"
to [Link]
 Define a catch block for handling the InvalidEmployeeException, assign
exception message to [Link] and rethrow the
same InvalidEmployeeException object
 Define a generic Exception handler catch block that handles all the other
exceptions by rethrowing a new Exception object with message "unknown
error occured"
 Return [Link]

class InvalidEmployeeException

 define custom exception


class InvalidEmployeeException by extending the Exception class.
 define a parameterized constructor with a String argument to pass the
message to the super class.

Collection Question:

As the numbers on a keypad, you have been given a string which is made up of
digits from 2 to 9, inclusive. Your task is to return all the possible letter combinations
that the string could represent.
e.g: From the figure below, for the string "23" - the digit 2 maps to the
letters a, b and c AND the digit 3 maps to the letters d, e and f. Hence,
the input string 23 would have the letter combinations as ad, ae, af, bd, be, bf, cd,
ce and cf.
For mapping of digit to letters refer to below figure:
Note:

 The digit 1 does not map to any letters.


 The digit 0(zero) does not map to any letters.
 All English letters are lower-case.

Your task here is to implement a Java code based on the following specifications.
Note that your code should match the specifications in a precise manner. Consider
default visibility of classes, data fields and methods unless mentioned otherwise.
Specifications:
class definitions:
class Source:
method definitons:
letterCombinations(String digits):
return type: ArrayList<String>
visibilty: public

Task:
Implement the below method in Source class:
ArrayList<String> letterCombinations(String digits):

 This method returns an ArrayList<String> having all the possible letter


combinations that the string of digits represents.
 Hint: You can initialize a local variable of type HashMap<Character,
String> and populate it with the mappings as shown above. (e.g: '1' -> "" , '2' -
> "abc" , '3' -> "def", etc.) and can use this HashMap in the method
implementation to arrive at all possible combinations.

Sample Input

23 //Input1
010 //Input2

Sample Output

[ad, ae, af, bd, be, bf, cd, ce, cf] //Output1
[] //Output2

ss

You might also like