Practice Questions for Java
Problem 1 : Palindromic Name
Problem Statement
Parents recently had a kid and they are a huge fan of palindromic strings. Now their relatives have
suggested creating a name for their child from both the parent's first names. Now find a way to
create a palindromic name from their parents' name. Take a substring of size at least one from both
of their names and concatenate them which should create a palindrome. If this is possible in any
way, return "YES" else return "NO".
Input Format
• The first line contains the string name.
Constraints
• 2<=len(stn)<=100
Output Format
• Return 'YES' if possible else return 'NO'.
Evaluation Parameters
• Sample Input
maria
mira
• Sample Output
Yes
• Explanation
If you remove the substring, 'ari' from 'maria' and 'ira' from 'mira' then the final string after
concatenation will be 'ariira'. This name is a palindrome so we return 'YES'.
Problem 2 : Stock Market
Complete the classes using the Specifications given below. Consider default visibility of classes,
data fields, and methods unless mentioned otherwise.
Specifications
Task
Class Stocks
-define all the data members as per the given specifications.
-define the constructor with public visibility.
-Implement the below methods for this class:
-String checkRate(float rate) throws Exception:
• Write a code that checks for the Profit and Loss in the stock rate.
• If the given parameter rate is 80% of the stockRate then return a string "Hope to raise".
• If the given parameter rate is less than 80% of the stockRate then throw StockLowException
with a message "Under Loss".
• If the given parameter rate is greater than 80% of the stockRate then throw
StockHighException with a message "Under Profit".
Class StockHighException extends Exception:
-Define StockHighException class derived from the Exception class.
Class StockLowException extends Exception:
-Define StockLowException class derived from the Exception class.
Sample Input
class definitions:
class Stocks:
float stockRate
visibility : public
Stocks(float stockRate) : Constructor with public visibility
method definition:
checkRate(float Rate) throws Exception:
return type: String
visibility: public
class StockHighException extends Exception:
method definition:
StockHighException(String msg)
visibility: public
class StockLowException extends Exception:
method definition:
StockLowException(String msg)
visibility: public
Sample Input:
Stocks stock = new Stocks(10.0f);
stock.checkRate(5.0f);
Sample Output
StockLowException : Under Loss
Problem 3 : Library Management System
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.
Specifications
class definitions:
class Book:
Data members:
name: String
IFSC: String
author: String
issuedBy: String
available: boolean
Book(String name, String IFSC, String author, boolean available): paramete
rized constructor for the data members
class Library:
Method definition:
validateIFSC(Book details) throws Exception
return type: String
visibility: public
issueBook(Book details, String name) throws Exception
return type: String
visibility: public
class InvalidIFSCException:
method definitions:
InvalidIFSCException(String str)
visibility: public
class BookUnavailableException:
method definitions:
BookUnavailableException(String str)
visibility: public
Task
Class Book
- define the String variable name.
- define the String variable IFSC.
- define the String variable author.
- define the String variable issuedBy.
- define the boolean variable available.
-Define parameterized constructor for all the data members.
Class Library
Implement the below methods for this class:
-String validateIFSC(Book details):
• Write a code to validate the IFSC code of the book.
• IFSC code is valid if it consists of 16 numeric characters. Valid IFSC = "9898121234346264".
throw an InvalidIFSCException with the message "IFSC is invalid" if the IFSC code is not
valid, else return a message "IFSC is valid".
-String issueBook(Book details, String name):
• Write a code to issue the book.
• If availability(false) of the book is not present then throw a BookUnavailableException
with the message "Book is unavailable".
• If the book is available(true) then set available to false, and issuedBy to name after that
return the message "Book is issued successfully".
Class InvalidIFSCException
• define custom exception class InvalidIFSCException by extending the Exception class.
• define a parameterized constructor with a String argument to pass the message to the super
class.
Class BookUnavailableException
• define custom exception class BookUnavailableException by extending the Exception
class.
• define a parameterized constructor with a String argument to pass the message to the super
class.
Sample Input
Book data = new Book("Arthashastra", "9898121234346264", "Kautilya", true);
Library obj = new Library();
----------------------------------------------------
String ans1 = obj.validateIFSC(data);
String ans2 = obj.issueBook(data, "Steve");
Sample Output
IFSC code is valid
Book is issued successfully
Problem 4 : Update table DATABASE
Environment Specifications & Instructions
Type of Database
MySQL
Database Name to be used
school
Problem Statement
You are given a table student with following attribute:
• rollno varchar(10)
• name varchar(5)
• class varchar(5)
Your task is to change name varchar size to 25 and make rollno as a primary key.
Problem 5 : Remove First and Last
Problem Statement
Given a string Str and a character C, your task is to remove the first and last occurrence of the
character C from the string Str.
Input Format
• First line of input consists of a string value, denoting str
• Second line of input consists of a character denoting C
Constraints
• 1 <= str.length <=100
Output Format
• Print the string after the above operation
Sample Input
Codedecode
Sample Output
coddecod
Explanation
First and last occurrence of 'e' is removed.
Problem 6 : Insert Space
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:
visibility: public
method definition:
insertSpace(String s): method that uses lambda expression to format a
given string, where a space is inserted between each character of string.
return type: String
Task
Create a Source class and implement below given method:
• insertSpace(String s): Use lambda expression to format a given string, where a space is
inserted between each character of string
Implement using Lambda expressions.
NOTE
• Do not use any for loops or other control structures.
• Use the Stream API methods for your implementations, else the test-cases might fail.
Sample Input
capgemini
Sample Output
capgemini
NOTE:
• The above Sample Input and Sample Output are only for demonstration purposes and will
be obtained if you implement the main() method with all method calls accordingly.
• Upon implementation of main() method, you can use the RUN CODE button to pass the
Sample Input as input data in the method calls and arrive at the Sample Output.