Senior Java Developer Interview Questions

4K

Senior Java Developer interview questions shared by candidates

Top Interview Questions

Sort: Relevance|Popular|Date
Atlassian
Senior Java Developer was asked...13 April 2016

A company uses a format to exchange messages with us. You need to validate the input. The character encoding ASCII. Valid characters are between 0x20 (space) and 0x7E (~). write validate function to generate valid output or the error message.

9 Answers

public static void printValidMassage(){ String msg="204a6176617e"; String msg1=convertHextoChars(msg); boolean isValid=(msg1.startsWith(" ") && msg1.endsWith("~")) ? true : false; } private static String convertHextoChars(String hex) { StringBuffer sb = new StringBuffer(); for (int i=0; i Less

public static void printValidMassage(){ String msg="204a6176617e"; String msg1=convertHextoChars(msg); boolean isValid=(msg1.startsWith(" ") && msg1.endsWith("~")) ? true : false; System.out.println(isValid); } private static String convertHextoChars(String hex) { StringBuffer sb = new StringBuffer(); for (int i=0; i Less

for (int i=0; i

Show more responses
Morgan Stanley

Core Java questions & this person was also not aware about the immutability concept properly except what is given on internet. I had to teach him the immutability of class & object, else for this person both are same.

6 Answers

Many companies are doing the same either companies are just giving illusion to people that they are hiring or the incompetent interviewers are not selecting any worthy candidate for the safety of their own job. In short, all seems to be a kind of fraud going on in name of hiring. Less

I had the same experience. I was supposed to be interviewed by some Morgan Stanley person on 16th April 2020, then this Morgan Stanley person called me to reschedule the call. Then in evening, this person never showed his face in video conference & he also didn't seem to have proper technical understanding. I think his name was 'Dhaval' & I don't feel that he will select any person more capable. Less

I also had the similar experience, I think either stupid people work here or stupid people come to take the interviews. Less

Show more responses
Veeva Systems

public class Person { Person father; Person mother; Gender gender; Integer age; List<Person> children; int level = 0; public enum Gender { Male, Female; } } For the above class, you basically have to implement 2 methods. public List<Person> getOldestSisters() public List<Person> getGreatestAncestors()

5 Answers

/** * Returns the Persons which are the greatest number of levels up the family tree * from this instance. Examples: * Given a tree where this person instance only has a mother and father, return mother and father. * Given a tree where this person instance has a mother &amp; father, and a grandfather on the mother's side, return only the grandfather on the mother's side. * @return List of Person */ public List getGreatestAncestors() { // If this is the root of the tree, return empty array because there is no ancestor for the tree if (this.father == null &amp;&amp; this.mother == null) { return new ArrayList(); } List fList = new ArrayList(); List mList = new ArrayList(); if (this.father != null) { fList = this.father.getGreatestAncestors(); } if (this.mother != null) { mList = this.mother.getGreatestAncestors(); } List results = new ArrayList(); for (Person p : fList) { if (results.contains(p)){ continue; } results.add(p); } for (Person p : mList) { if (results.contains(p)){ continue; } results.add(p); } return results; } Less

I cranked this out in about 30 minutes. I believe it works quite well. I've also included the corresponding unit tests: file: Person.java ------------------------------------------------------------------------------------------------------------- package Command; import java.util.ArrayList; import java.util.HashSet; import java.util.List; public class Person { Person father; Person mother; Gender gender; Integer age; List children; int level = 0; public enum Gender { Male, Female; } Person(Person dad, Person mom, Gender gender, int age, int level) { this.father = dad; this.mother = mom; this.gender = gender; this.age = age; this.level = level; } public void setChildren(List children) { this.children = children; } public void addChild(Person child) { this.children.add(child); } public List getOldestSisters () { // given the current person (self), determine parents // then get children of those parents // Determine gender of each child // Where they are female, get ages // return females with age &gt; mine // Note: must check on both sides father/mother as there may be a mixed marriage // Combine list of children - Exclude YOU as you cannot be your own sister. // Use a set to eliminate duplicates. HashSet allChildren = new HashSet(); // Can't add null to a hashSet so screen for it. if ((father != null) &amp;&amp; (father.children != null)){ allChildren.addAll(father.children); } if ((mother != null) &amp;&amp; (mother.children != null)) { allChildren.addAll(mother.children); } // If you are not in this list, there is an issue! if (allChildren.contains(this)) { allChildren.remove(this); // System.out.println("Removing self from list."); } else { System.out.println("Error: You are not a child of your parents! Adopted?"); } // Filter down to only women and get any older than me: int myAge = this.age; List oldestSisters = new ArrayList(); for (Person child : allChildren) { if (child.gender == Gender.Female) { if (child.age &gt; myAge) { oldestSisters.add(child); } } } return oldestSisters; } public List getGreatestAncestors() { if ((this.father == null) || (this.mother == null)) { return null; // You must have two parents to have ancestors } // Find root parents List myParents = getParents(this); return getElders(myParents); } private List getElders(List parents) { List elders = new ArrayList(); List myParents = new ArrayList(); boolean newElders = false; for (Person parent : parents) { myParents = getParents(parent); if (myParents.isEmpty()) { elders.add(parent); } else { elders.addAll(myParents); newElders = true; } } if (newElders == true) { return getElders(elders); } return elders; } private List getParents(Person person) { List parents = new ArrayList(); if (person.father != null) parents.add(person.father); if (person.mother != null) parents.add(person.mother); return parents; } } // For the above class, you basically have to implement 2 methods. // public List getOldestSisters() // public List getGreatestAncestors() Less

package Command; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; import static org.junit.jupiter.api.Assertions.*; class PersonTest { // Create grand parents Person grandFatherDad = new Person(null, null, Person.Gender.Male, 78, 1); Person grandMotherDad = new Person(null, null, Person.Gender.Female, 81, 1); Person grandFatherMom = new Person(null, null, Person.Gender.Male, 69,1); Person grandMotherMom = new Person(null, null, Person.Gender.Female, 89, 1); // Create parents / aunts and uncles Person father = new Person(grandFatherDad, grandMotherDad, Person.Gender.Male, 48, 2); Person mother = new Person(grandFatherMom, grandMotherMom, Person.Gender.Female, 47, 2); Person aunt1 = new Person(grandFatherMom, grandMotherMom, Person.Gender.Female, 47, 2); // Twin sis to mom Person aunt2 = new Person(grandFatherDad, grandMotherDad, Person.Gender.Female, 37, 2); Person uncle1= new Person(grandFatherDad, grandMotherDad, Person.Gender.Male, 35, 2); // Children (me and bros and sis) Person self = new Person(father, mother, Person.Gender.Male, 18, 3); Person brother1 = new Person(father, mother, Person.Gender.Male, 16, 3); Person sister1 = new Person(father, mother, Person.Gender.Female, 15, 3); Person sister2 = new Person(father, mother, Person.Gender.Female, 14, 3); @BeforeEach void setUp() { // Create children / sibling groups List children = new ArrayList(); children.add(father); children.add(aunt1); children.add(uncle1); grandFatherDad.setChildren(children); grandMotherDad.setChildren(children); children.clear(); children.add(mother); children.add(aunt2); grandFatherMom.setChildren(children); grandMotherMom.setChildren(children); children.clear(); children.add(self); children.add(brother1); // Dad had brother with other wife children.add(sister2); father.setChildren(children); // mom came with her own daughter from prior marriage children.clear(); children.add(self); children.add(sister1); children.add(sister2); mother.setChildren(children); } @AfterEach void tearDown() { } @Test void getOldestSisters() { // When there are no older sisters and I am male: //Person me = new Person(father, mother, Person.Gender.Male, 48); List olderSisters = null; olderSisters = self.getOldestSisters(); assertTrue(olderSisters.isEmpty(), "No older sisters."); // When there is one older sister and I am male Person sister3 = new Person(father, mother, Person.Gender.Female, 50, 3); mother.addChild(sister3); father.addChild(sister3); olderSisters = self.getOldestSisters(); assertTrue(olderSisters.contains(sister3)); assertTrue(olderSisters.size() == 1, "One older full sister."); // Youngest Sister has two older sisters (one full, one half) olderSisters.clear(); olderSisters = sister2.getOldestSisters(); assertTrue(olderSisters.contains(sister1)); assertTrue(olderSisters.contains(sister3)); assertTrue(olderSisters.size() == 2, "One older full, one older half"); } @Test void getGreatestAncestors() { List ancestors = self.getGreatestAncestors(); assertTrue(ancestors.size() == 4); assert(ancestors.contains(grandFatherDad)); assert(ancestors.contains(grandFatherMom)); assert(ancestors.contains(grandMotherDad)); assert(ancestors.contains(grandMotherMom)); ancestors = father.getGreatestAncestors(); assertTrue(ancestors.size() == 2); assert(ancestors.contains(grandFatherDad)); assert(ancestors.contains(grandMotherDad)); ancestors = mother.getGreatestAncestors(); assertTrue(ancestors.size() == 2); assert(ancestors.contains(grandFatherMom)); assert(ancestors.contains(grandMotherMom)); ancestors = grandFatherDad.getGreatestAncestors(); assertNull(ancestors, "getGreatestAncestors():Persons with no ancestors should return null."); } } Less

Show more responses
EXFO

During the first 2 rounds of 2 hours, almost everything the interviewer tried to ask which he might have collected from internet for the interviews.

5 Answers

This shows the cheap mentality of its people &amp; how such kind of people waste the valuable time of both candidates &amp; company. Less

Right, this is some kind of fraud being done by these companies to exploit the candidates &amp; take their important details for own benefits. And I think Govt should take some actions on such processes. Less

Well said &amp; agree with you that it is not a worthy company to try for where people are wasting time of the candidates. Less

Show more responses
Interactions

Print numbers 1 to 100 in sequence using multiple threads.

4 Answers

I don't know how did you do it, but there are various ways to do this &amp; depending on the requirements &amp; planning one can pick one. But for this small range, one can throw these many threads with right coordination. But if this range is high or CPU intensive task then better to have a pool of threads. But such questions are trivial &amp; again not sure what your interviewer was looking as the solution. Less

I just created multiple threads giving one number to each, for simple implementation &amp; just told that it is not the right &amp; should use thread pooling to avoid multiple threads &amp; he also didn't ask to try to use pool. He was complaining in the feedback that I created too many threads but he never asked me to optimize that also. Plus his ask was that all the numbers shouldn't be printed by a single thread &amp; other threads are idle, so I printed via different threads. And it gave another point to reject &amp; I am happy that I didn't give him even better solution to such people. Many interviewers use interviews to get the ideas or prepare for their own interviews with other companies, but till they get next offer letter such people try to avoid any competition in their current team. Less

I just created multiple threads giving one number to each, for simple implementation &amp; just told that it is not the right &amp; should use thread pooling to avoid multiple threads &amp; he also didn't ask to try to use pool. He was complaining in the feedback that I created too many threads but he never asked me to optimize that also. Plus his ask was that all the numbers shouldn't be printed by a single thread &amp; other threads are idle, so I printed via different threads. And it gave another point to reject &amp; I am happy that I didn't give him even better solution to such people. Many interviewers use interviews to get the ideas or prepare for their own interviews with other companies, but till they get next offer letter such people try to avoid any competition in their current team. Less

Show more responses
Wallet Hub

Write Java code to check if a string is a palindrome.

4 Answers

public class Palindrom { final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); public boolean isPalindrom(String source) { int length = source.length(); String reverse = ""; for (int i = length - 1; i &gt;= 0; i--) { reverse = reverse + source.charAt(i); } return source.equals(reverse); } } Less

public static void main(String[] args) { String[] arrPalindrom = new String[]{"test","tsest","view", "viv"}; for(String item: arrPalindrom) System.out.println(item + " is " + (isItemPalindrom(item)?"palindrom": "not palindrom")); } static boolean isItemPalindrom(String str){ if(str.equals(new StringBuilder(str).reverse().toString())){ return true; } return false; } Less

static boolean isPalindrome(String str){ boolean success=true; s=s.toLowerCase(); for(int i=0;i Less

Show more responses
ADP

Write a query to get the required data regarding employees.

4 Answers

You can use &amp; modify below query - select e.ename, dep.loc from emp e, (select d.deptno, d.dname, d.loc from dept d, (select deptno dno, count(*) ct from emp group by deptno order by deptno desc) dn where rownum = 1 and dn.dno = d.deptno) dep where e.deptno = dep.deptno order by e.ename; Less

I think query will be like given below. Check or change a bit for your requirements. select EMP.ENAME, lc.LOCNAME from employee emp, company org, location lc where emp.cID = ORG.CID and org.locID = lc.locID and lc.locID in ( select place.locID from ( select loc.locID, count(*) ct from employee e, company c, location loc where e.cID = c.cID and c.locID = loc.locID group by loc.locID order by ct desc) place where rownum = 1); Enjoy :) Less

These people in ADP have been in such a comfort zone that they don't like to pick more capable people who can challenge their capability disturbing their comfort zone &amp; rest who cares about the profits of the company till the time salary is getting credited in the account with less or no work. So continue to show-off &amp; keep capable people away. Less

Show more responses
Acuitus

A rustic village contains one million married couples and no children. Each couple has exactly one child per year. Each couple wants a girl, but also wants to minimize the number of children they have, so they will continue to have children until they have their first girl. Assume that children are equally likely to be born male or female. Let p(t) be the percentage of children that are female at the end of year t. What is p(t)? "Can't tell" is a potential answer if you don't have sufficient information.

4 Answers

p(t) = 50% at all years (with some rounding) P(1) = 1,000,000 children, 500,000 of them girls, 500,000 of them boys P(2) = 500,000 more children (only those who had boys have another child), 250,000 girls, 250,000 boys so overall, there are now 750,000 girls, and 750,000 boys P(3) = 250,000 more children, 125,000 girls, 125,000 boys so there are now 875,000 girls (with 500,000 being only children, 250,000 having one brother, and 125,000 having 2 brothers), and 875,000 boys So, except for some rounding when the previous year's number of all boy families is an odd number, you will always have 50% of each Less

can't tell

The answer is 2^-t

Show more responses
Plymouth Rock Assurance

Write an algorith to print a series of roman numerals.

4 Answers

public class Roman { public static void main(String args[]) { String digits[] = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }; String prefixes[] = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", "C" }; int i = 1; System.out.println("Roman Numbers from 1 to 100: "); do { if (0 0); } } Less

public class Roman { public static void main(String args[]) { String digits[] = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }; String prefixes[] = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", "C" }; int i = 1; System.out.println("Roman Numbers from 1 to 100: "); do { if (0 0); } } Less

do { if (0 0);

Show more responses
ADP

Java question was around String pattern searching which you can do easily in around 20 minutes, if you can keep your mind calm where that clock is also ticking.

4 Answers

Suggestion - Give only 10-15 minutes for the MCQs then you can easily do remaining SQL &amp; Java code questions easily in remaining time. Less

Anyone looking for the exact code then leave the message.

Answers of both these questions are over internet &amp; here on Glassdoor.

Show more responses
Viewing 1 - 10 of 4,016 interview questions

Glassdoor has 4,016 interview questions and reports from Senior java developer interviews. Prepare for your interview. Get hired. Love your job.