Rest - Interview Questions and Answers for 'At' - 14 question(s) found - Order By Newest Very frequently asked in HCL Tech ( Based of 4 inputs ) Q1. Write a Program to reverse a string iteratively and recursively Core Java
Ans. Using String method -
new StringBuffer(str).reverse().toString();
Iterative -
Strategy - Loop through each character of a String from last to first and append the character to StringBuilder / StringBuffer
public static String getReverseString(String str){
StringBuffer strBuffer = new StringBuffer(str.length);
for(int counter=str.length -1 ; counter>=0;counter--){
strBuffer.append(str.charAt(counter));
}
return strBuffer;
}
Recursive -
Strategy - Call the method with substring starting from 2nd character recursively till we have just 1 character.
public static String getReverseString(String str){
if(str.length <= 1){
return str;
}
return (getReverseString(str.subString(1)) + str.charAt(0);
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string  StringBuffer  recursion  for loop  control statements  loop statement  stringbuffer.append  java.lang.String  java.lang.StringBuffer  String Manipulation Asked in 8 Companies   frequent Q2. How to find whether a given integer is odd or even without use of modulus operator in java? Core Java
Ans. public static void main(String ar[])
{
int n=5;
if((n/2)*2==n)
{
System.out.println("Even Number ");
}
else
{
System.out.println("Odd Number ");
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   tricky questions   interesting questions   modulus operator Asked in 4 Companies   frequent Very frequently asked in companies using SOA. Q3. What are RESTful Web Services ? Rest
Ans. REST or Representational State Transfer is a flexible architecture style for creating web services that recommends the following guidelines -
1. http for client server communication,
2. XML / JSON as formatiing language ,
3. Simple URI as address for the services and,
4. stateless communication. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   web services   rest   java   j2ee  architecture Asked in 14 Companies intermediate   frequent Q4. What will be the output of following Code ?
class BuggyBread {
public static void main(String[] args) {
String s2 = "I am unique!";
String s5 = "I am unique!";
System.out.println(s2 == s5);
}
} Core Java
Ans. true, due to String Pool, both will point to a same String object. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   tricky questions   interesting questions   string   string pool   .equal   == intermediate   frequent Ans. eq, ge, gt , between, in , isNull, isEmpty, isNotnull, ne , like, lt , or , not Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate   hibernate criteria   hibernate restrictionFrequently asked question in companies using Rest Web services. Q6. Different between POST and PUT in Rest Rest
Ans. PUT requests are only meant to place the object as it is on server. For example - You want a file to be uploaded and then placed in a particular folder or you want an Employee object to be persisted in the table.
POST requests are also meant to transfer information from client to server but that information once received at the server is evaluated , modified or refactored before persisting. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Put vs Post   Rest Asked in 5 Companies   frequent Q7. Should a DB Insert Request be a POST or PUT request in Rest Rest
Ans. It depends on whether the request object is persisted as it is
or
its first dismantled, modified or refactored and then inserted into DB.
In first case, it should be a PUT request whereas in second case it should be a POST Request. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Put vs Post   Rest Asked in 2 Companies intermediate   frequent Q8. How to specify the or/and combination restrictions within Criteria in Hibernate ? Hibernate
Ans. session.createCriteria(Employee.class).add( Restrictions.or(Restrictions.like("name", "A%"),Restrictions.like("name", "B%"))).list(); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  hibernate  Criteria  Restrictions Q9. Can you name few matchers that can be used with hamcrest assertThat method ? Junit
Ans. is
not
equals
anyOf
hasSize Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  assertThat.hamcrest Q10. If given a choice , which one would you prefer to send params to the Get service , path params or query param ? Web Service
Ans. If the number of params is quite large , I would prefer to either split it with majority of it in query params or all in query params.
If the params are all mandatory , I would keep it as path params. If it's optional , I would keep it as query param so as to keep consistent base url.
If we don't require the param name , then they can be kept as path params as query params necessitates the usage of param name. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Web Service  Get service  path vs query param  Rest  query param  path param Q11. Is it necessary to specify "employeeId" with @PathParam("employeeId") in this case,
@Path("/{employeeId}")
public String employeeInfo(@PathParam("employeeId") Long employeeId){
} Rest
Ans. No, it's optional as the name of path param required is same as method param name in this case.
Even the declaration as following should work -
@Path("/{employeeId}")
public String employeeInfo(@PathParam Long employeeId){
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Web Service  Get service  Rest  path param Q12. Difference between stateless and stateful rest services ? Rest
This question was recently asked at 'Sofi'.This question is still unanswered. Can you please provide an answer. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  rest services  stateful rest service  stateless rest service Asked in 1 Companies Asked in General Electric online assignment. Q13. Write a Program for coin changer application. The program should set the cash box with available currency and then should render the exact change. If enough cash is not available , it should present the appropriate message. Core Java
Ans. import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class CoinChanger {
enum Currency {
DOLLAR(1),QUARTER(.25),DIME(.10),NICKEL(.05),PENNY(.01);
private double currencyValue;
Currency(double value){
currencyValue = value;
}
double getCurrencyValue(){
return this.currencyValue;
}
}
private static Map<Currency, Integer> cashBox;
private static Map<Currency, Integer> change;
static {
cashBox = new TreeMap<Currency, Integer>();
change = new TreeMap<Currency, Integer>();
initializeCashBox();
}
public static void main(String[] args) {
double amountToReturn = 18.79f; // Set the amount to be changed here
for(Entry<Currency, Integer> entry:cashBox.entrySet()){
Currency currency = (Currency)entry.getKey();
int coinCount = (int)(amountToReturn/(entry.getKey().getCurrencyValue()));
int availableCurrency = (int)(entry.getValue());
if(coinCount > availableCurrency){
coinCount = availableCurrency;
}
change.put(currency, coinCount);
if(coinCount > 0){
amountToReturn = amountToReturn - (coinCount * entry.getKey().getCurrencyValue());
}
}
System.out.println(change);
if(amountToReturn > .1){
System.out.println("Not enough cash");
}
}
private static void initializeCashBox(){
//set the cash box
cashBox.put(Currency.DOLLAR, 50);
cashBox.put(Currency.QUARTER, 0);
cashBox.put(Currency.DIME, 50);
cashBox.put(Currency.NICKEL, 50);
cashBox.put(Currency.PENNY, 50);
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  coin changer application  coin changer app Asked in 1 Companies Q14. Write a Program to right shift single character in a string
Ans. public class RightShiftCharacter {
public static void main(String args[]) {
String str = "Hello";
char[] charArray = str.toCharArray();
for(int count=charArray.length-1;count>0;count--){
charArray[count] = charArray[count-1];
}
String newString = new StringBuilder().append(charArray).toString();
System.out.println(newString);
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string manipulation.right shift character Q15. How can we create objects if we make the constructor private ? Core Java
a. We can't create objects if constructor is private b. We can only create objects if we follow singleton pattern c. We can only create one object d. We can create new object through static method or static blockAns.d. We can create new object through static method or static block
Q16. What will be the output of executing following class ?
public class BuggyBread {
static {
System.out.println("Static Block");
}
{
System.out.println("Initialization Block");
}
BuggyBread(){
System.out.println("Constructor");
}
public static void main(String[] args){
System.out.println("Main Method");
}
}
Core Java
a. Static Block
Main Method b. Static Block
Instance Initialization Block
Main Method c. Static Block
Constructor
Main Method d. Static Block
Instance Initialization Block
Constructor
Main MethodAns.a. Static Block
Main Method
Q17. What will be the output upon executing following class ?
public class BuggyBread {
static {
System.out.println("Static Block");
}
{
System.out.println("Instance Initialization Block");
}
BuggyBread(){
System.out.println("Constructor");
}
public static void main(String[] args){
System.out.println("Main Method");
new BuggyBread();
}
} Core Java
a. Instance Initialization Block
Constructor
Static Block
Main Method b. Static Block
Instance Initialization Block
Constructor
Main Method c. Main Method
Static Block
Instance Initialization Block
Constructor d. Static Block
Main Method
Instance Initialization Block
ConstructorAns.d. Static Block
Main Method
Instance Initialization Block
Constructor
Q18. With the following code, Which is a valid way to initialize ?
public class BuggyBread {
private String element1;
private String element2;
private BuggyBread(String element1, String element2){
this.element1 = element1;
this.element2 = element2;
}
public static class Builder {
private String element1;
private String element2;
Builder(BuggyBread buggybread){
element1 = buggybread.element1;
element2 = buggybread.element2;
}
Builder withElement1(String element1){
this.element1 = element1;
return this;
}
Builder withElement2(String element2){
this.element2 = element2;
return this;
}
BuggyBread build(){
BuggyBread buggybread = new BuggyBread(element1,element2);
return buggybread;
}
}
} Core Java
a. BuggyBread buggybread = new BuggyBread(); b. BuggyBread buggybread = new BuggyBread("element1","element2"); c. BuggyBread.Builder builder = new BuggyBread.Builder(); d. BuggyBread.Builder builder = new BuggyBread.Builder("element1","element2");Ans.d. BuggyBread.Builder builder = new BuggyBread.Builder("element1","element2");
Q19. What will be the output of following ?
public class BuggyBread {
private int x;
private Integer y;
BuggyBread(int x,int y){};
public static void main(String[] args){
BuggyBread buggybread = new BuggyBread(1,2);
System.out.println(buggybread.x);
System.out.println(buggybread.y);
}
} Reference Core Java
a. 0 0 b. 0 null c. null 0 d. null nullAns.b. 0 null
Q20. Which of the following is true for == operator ? Core Java
a. For primitives, == checks if the variables on left and right have same data type b. For primitives, == checks if the variables on left and right have same value c. For Objects, == checks if the references on left and right have same data type d. For Objects, == checks if the references on left and right have same valueAns.b. For primitives, == checks if the variables on left and right have same value
Q21. Which of the following is equivalent to following logic ?
Not X && Not Y Core Java
a. x || Y b. Not(X || Y) c. Not(X && Y) d. Not X && YAns.b. Not(X || Y)
Q22. Which of the following http response means the resource is not found ? Java EE
a. 500 b. 200 c. 404 d. 400Ans.c. 404
Q23. What is Dirty Read in Database Transactions ? Database
a. Data Read by Transaction 2 which hasn't yet updated by Transaction 1 b. Data Read by Transaction 2 which has been updated and commited by Transaction 1 c. Data Read by Transaction 2 which has been updated but not commited by Transaction 1 d. Inability of Transaction 2 to read Data when Transaction 1 is updating.Ans.c. Data Read by Transaction 2 which has been updated but not commited by Transaction 1
Q24. Which of following annotation is used to initialize objects before executing set of tests ? Reference Junit
a. @Test b. @Ignore c. @After d. @BeforeAns.d. @Before
a. Static members are shared by all objects of the class. b. We can override static methods. c. Static methods operate on static variables only. d. Static Elements are accessed using class name.Ans.b. We can override static methods.
a. Feature to load the dependencies from Cache b. Feature to load all objects and relationships in advance before they can be used c. Feature to not load dependencies and relationship in advance and load when required d. Feature to not load the dependencies and relationships at allAns.c. Feature to not load dependencies and relationship in advance and load when required
Q27. Which of the following is not the benefit of Lazy Initialization in Hibernate ? Hibernate
a. Laod When required provides better performance b. Object stays lighter c. Less number of Database calls d. Less load on DatabaseAns.c. Less number of Database calls
Q28. The use of volatile keyword facilitates .. Core Java
a. Making Use of Cache for better Performance b. Avoiding use of Cache c. Making use of Backward as well as Forward Cache d. Keeping only one copy of variable in CacheAns.b. Avoiding use of Cache
Q29. Static Polymorphic in Java is achieved through .. Core Java
a. Method Overloading b. Method Overriding c. Variable Overloading d. Variable OverridingAns.a. Method Overloading
Q30. Which of following are serialized ? Core Java
a. static variables b. transient variables c. instance variables d. method local variablesAns.c. instance variables