Java Interview Questions
- 0of 0 votes
AnswersHow many elements will the set object in the below snippet will contain after the program executes to last print statement?
- Flash May 13, 2017 in Indiapublic class ShortSetTest { public static void main(String[] args){ Set shortSet = new HashSet(); for(short i = 0; i < 100; i++){ shortSet.add(i); shortSet.remove(i-1); } System.out.println(shortSet); } }
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswerWhat would the output of the following snippet?
- Flash May 13, 2017 in Indiapublic class TrickyNum<X extends Number> { private X x; public TrickyNum(X x){ this.x = x; } private double getDouble(){ return x.doubleValue(); } public static void main(String[] args) { TrickyNum<Integer> a = new TrickyNum<Integer>(new Integer(1)); System.out.println(a.getDouble()); } }
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswersWhat happens when you try to compile below class?
- Flash May 13, 2017 in Indiapublic class EnumTest { public EnumTest(){ System.out.println("Inside Constructor"); } public void printType(){ enum PrinterType {DOT,INK,LASER} } }
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswersGiven below code snippet, what would be the output?
- Flash May 13, 2017 in Indiapublic class DemoThread extends Thread { private int x = 2; public static void main(String[] args) throws Exception { new DemoThread().method(); } public DemoThread(){ System.out.println("test"); x = 5; start(); } public void method() throws Exception { join(); x = x - 1; System.out.println(x); } public void run(){ System.out.println("run"); x *= 2; } }
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswersFor code snippet given below, what would be the output?
- Flash May 13, 2017 in IndiaString s8 = "AB"; String s9 = s8.intern(); if(s9.equals(s8) && s9 == s8) System.out.println("All Equal"); if(s9.equals(s8) && s9!=s8) System.out.println("equals Equal"); if(!s9.equals(s8) && s9==s8) System.out.println("== Equal"); if(!s9.equals(s8) && s9!=s8) System.out.println("Nothing Equal");
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswersProgramming Exercise – FX Calculator
- princerajesh.indukuri May 06, 2017 in India
Your task is to create a console-based currency converter application.
Requirements
The application allows a user to convert an amount in a specific currency to the equivalent amount in another currency.
Your calculator should allow a user to enter an amount in any of the known currencies, and provide the equivalent amount in another currency.
Your calculator should parse console input like "<ccy1> <amount1> in <ccy2>", and provide a suitable response.
For example:
%> AUD 100.00 in USD
AUD 100.00 = USD 83.71
%> AUD 100.00 in AUD
AUD 100.00 = AUD 100.00
%> AUD 100.00 in DKK
AUD 100.00 = DKK 505.76
%> JPY 100 in USD
JPY 100 = USD 0.83
If the rate cannot be calculated, the program should alert the user:
%> KRW 1000.00 in FJD
Unable to find rate for KRW/FJD
When displaying amounts back to the user, they should be formatted with the precision given below:
AUD=2 decimal places| Report Duplicate | Flag | PURGE
Student Student Java - 1of 1 vote
AnswersWrite a function that returns true if the binary representation of an integer is a palindrome.
- maxxwizard May 03, 2017 in United States for Marketplace
9 = 1001 = palindrome
8 = 1000 = not palindrome| Report Duplicate | Flag | PURGE
Amazon SDE1 Java - 0of 0 votes
AnswersWrite a Program in Java, You have an Employee class |
- Utsav April 09, 2017 in India
class Employee{
String name;
Integer id,
Employee manager
}
Each employee has a manager and the manager of CEO is null. Find all direct and indirect reportees of a manager.
Eg. Say Employee e1 reports to CEO,
e2 and e3 reports to e1,
e4 and e5 reports to e2,
e6 reports to e3.
Then by giving e1 as input, output should be e2, e3, e4, e5 and e6.| Report Duplicate | Flag | PURGE
Morgan Stanley Senior Software Development Engineer Java - 0of 0 votes
Answers
- xankar February 26, 2017 in United Statesimport java.time.Duration; import java.time.LocalTime; import java.util.List; import java.util.Map; // Your goal is to write business logic for a very simple Restaurant booking system // You are encouraged to refactor exisiting code, create other classes, write helper methods etc // You also need to make sure that the implementation works correctly class Reservation { public String name; public int partySize; public LocalTime startTime; } class Table { public int tableNumber; public int maxPartySize; } class Restaurant { public List<Table> tables; public LocalTime openTime; public LocalTime closeTime; public Map<Integer, Duration> reservationDurationsPerPartySize; // Returns a Table if Reservation could be booked, null otherwise // Booking rules: // 1) Reservation could be made only when the Restaurant is open. // 2) Only one Reservation can be seatted a Table at any time. // 3) Reservation can be seatted only at a Table of the same or a bigger size. // 4) Reservation should stay on the same Table for the whole Duration. // 5) Reservation Duration is determined by PartySize. public Table bookReservation(Reservation reservation) { //TODO: } }
| Report Duplicate | Flag | PURGE
Opentable Backend Developer Algorithm Data Structures Java Problem Solving - 0of 0 votes
AnswersGiven set of N number of points/Co-ordinates[(x1,y1),(x2,y2), (x3,y3), (x4,y4), (x5,y5), etc] find if any of them form square.
- xankar February 26, 2017 in United States| Report Duplicate | Flag | PURGE
Pure Storage Backend Developer Algorithm Data Structures Java - 0of 4 votes
AnswersYou have L, a list containing some digits (0 to 9). Write a function answer(L) which finds the largest number that can be made from some or all of these digits and is divisible by 3. If it is not possible to make such a number, return 0 as the answer. L will contain anywhere from 1 to 9 digits. The same digit may appear multiple times in the list, but each element in the list may only be used once.
- Parth Patel February 21, 2017 in United States
{{
Test cases
==========
Inputs:
(int list) l = [3, 1, 4, 1]
Output:
(int) 4311
Inputs:
(int list) l = [3, 1, 4, 1, 5, 9]
Output:
(int) 94311
}}
My Solution:
{{
package com.google.challenges;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Answer {
public static int answer(int[] l) {
// Your code goes here.
ArrayList<Integer> list0 = new ArrayList<>();
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
int sum =0;
Arrays.sort(l);
for(int i = 0; i<l.length; i++){
if(l[i] % 3 == 0){
list0.add(l[i]);
}else if(l[i] % 3 == 1){
list1.add(l[i]);
}else{
list2.add(l[i]);
}
sum += l[i];
}
if(sum%3==0){
StringBuilder strNum = new StringBuilder();
for(int i = l.length-1; i >= 0; i--)
{
strNum.append(l[i]);
}
return Integer.parseInt(strNum.toString());
}else if(sum%3 == 1){
if(list1.size()>0){
Collections.sort(list1);
list1.remove(0);
}else if(list2.size() >= 2){
Collections.sort(list2);
list2.remove(1);
list2.remove(0);
}else{
return -1;
}
}else if(sum%3 == 2){
if(list2.size()>0){
Collections.sort(list2);
list2.remove(0);
}else if(list1.size() >= 2){
Collections.sort(list1);
list1.remove(1);
list1.remove(0);
}else{
return -1;
}
}
list0.addAll(list1);
list0.addAll(list2);
StringBuilder strNum = new StringBuilder();
Collections.sort(list0);
for(int i = list0.size()-1; i >= 0; i--)
{
strNum.append(list0.get(i));
}
return strNum.length() > 0 ? Integer.parseInt(strNum.toString()) : -1;
}
}
}}
But here I am able to pass 4 test cases out of 5. Therefore I am looking for scenario which is left to check.
Can someone help me?| Report Duplicate | Flag | PURGE
Google Software Engineer Google FooBar 24x7 Google chrome technical support number 1-888-201-2039 Arrays Computer Science Java Problem Solving - 0of 0 votes
AnswersAlex has recently decided to learn about how to design compilers. As a first step he needs to find the number of different variables that are present in the given code.
- misterraj.ruchit February 18, 2017 in India
So Alex will be provided N statements each of which will be terminated by a semicolon(;). Now Alex needs to find the number of different variable names that are being present in the given statement. Any string which is present before the assignment operator denotes to a variable name.
Input Format: :
The first line contains a single integer N
Each of the next N lines contains a single statement.
It is guaranteed that all the given statements shall be provided in a valid manner according to the format specified.
Output Format: :
Print the number of different variable name that are present in the given statements.
Sample Input
2
foo = 3;
bar = 4;
Sample Output
2
Explanation
Foo and Bar are only two variables used inside the statements so answer is 2.| Report Duplicate | Flag | PURGE
Aricent Software Engineer Java - 0of 0 votes
AnswersWrite a Code
- ruchitraj93 February 14, 2017 in India
Steve is going to throw a party at his place tonight.He needs to visit two shops near his home-the first shop is d1 meters away from his place,the second shop is d2 meters away from his place, and there are d3 meters between these two shops.Calculate the minimum distance he needs to walk to visit both both shops and return back home. Steve always start from his palce.He can only travel using these 3 routes.HE can use any route any amount of time necessary,the only thing he needs to achieve is the minimum distance.
Find the minimum distance Steve has to walk to visit both shops and return home.
input: 1,1,1
output: 4
input: 10,20,30
output: 60| Report Duplicate | Flag | PURGE
Practo Software Developer Java - 0of 0 votes
AnswersWrite a Java Program in which a class takes four integer arguments as input(a, b, c and d). Do addition of (a+b) on one thread, addition of (c+d) on another thread and multiplication of(a+b) * (c+d)) on main thread.
- Utsav February 06, 2017 in United States
Like: Thread1 = (a+b)
Thread2 = (c+d)
Main Thread = (Thread1 * Thread2)| Report Duplicate | Flag | PURGE
JP Morgan Senior Software Development Engineer Java - 0of 0 votes
AnswersLinkedList Reverse
- kumarami663 February 05, 2017 in India| Report Duplicate | Flag | PURGE
Amazon Quality Assurance Engineer Java - 5of 5 votes
AnswersGiven a string, find the longest substring with k distinct characters.
- getPDat February 03, 2017 in United States
e.g - “aaaabbbb”, k = 2, “aaaabbbb”
“asdfrttt” k = 3, “asd”, “frttt”
[Telephonic Question]| Report Duplicate | Flag | PURGE
Google Software Developer Java - 1of 1 vote
Answersinput = 8
output =
1 1 1 1 1 1 1 2
3 2 2 2 2 2 2 2
3 3 3 3 3 3 3 4
5 4 4 4 4 4 4 4
5 5 5 5 5 5 5 6
7 6 6 6 6 6 6 6
7 7 7 7 7 7 7 8
- vshah9031 January 25, 2017 in Indiaimport java.util.*; class Test { int n; Scanner sc; void readNumber() { sc=new Scanner(System.in); n = sc.nextInt(); } void printPattern() { // write your logic here } public static void main(String[] args) { Test t = new Test(); t.readNumber(); t.printPattern(); } }
| Report Duplicate | Flag | PURGE
Staff Engineer Java - 0of 0 votes
AnswersObject Oriented Design Problem
- tushar.1738 January 22, 2017 in India
--------------------------------
Design an OO parking lot. What classes and functions will it have. It should say, full, empty and also be able to find spot for Valet parking. The lot has 3 different types of parking: regular, handicapped and compact.
Use Case:
1. Customer are given a ticket that they can use to redeem to get their vehicle back
2. Parking spots come in three sizes, small, med, large
3. Thee types of vehicles, small[Two Wheeler], med[Car], large[Bus]
-a small vehicle can park in a small, medium, and large spot
-a medium vehicle can park in a medium and large spot
-a large vehicle can park in a large spot
4. There are multiple entry gate to park vehicle. So Vehicle should asign nearest posible parking spot| Report Duplicate | Flag | PURGE
Coding Java Object Oriented Design - 0of 0 votes
AnswersYou are given an array of integers. Find the minimum difference between two prime numbers(Positive or negative) in the array when present with minimum time complexity and provide the test data to test the this code.
- test January 22, 2017 in United States| Report Duplicate | Flag | PURGE
Amazon Quality Assurance Engineer Java - 1of 1 vote
AnswersRemove 3 consecutive duplicates from string.
- neerdis January 17, 2017 in India
INPUT:aabbbaccddddc
OUTPUT:cdc| Report Duplicate | Flag | PURGE
Amazon Developer Program Engineer Java - 0of 0 votes
AnswersSituation - You have millions of records(Strings) and user is typing "abc" in search box . Your task is to display strings from records in the sequence.
- surendrapandey3788 January 12, 2017 in India
e.g String in the record contain "abc" starting should appear first, then string which has "abc" in the second, should apprear later as below.
String - fabcsdf,asdfabc,dfadsfsdfabc,abckdf,ddfabc...
Displays suggestion like....
abckdf
fabcsdf
ddfabc
asdfabc
dfadsfsdfabc
Question - Which data structure you would use to stor this ?
And how will you implement to get this(shorted) result| Report Duplicate | Flag | PURGE
Nagarro Java Developer Java - 0of 0 votes
AnswersGiven 2 sorted linked lists, return a linked list that has all the elements and is sorted.
- mh4wt@virginia.edu January 09, 2017 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java - 0of 0 votes
AnswerGiven 3 strings "s" ssearch" and "sreplace", search string s for the substring ssearch and for every instance of ssearch you find, replace that part of the string with sreplace
- mh4wt@virginia.edu January 09, 2017 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java - 0of 0 votes
AnswersGiven an NxN Boolean matrix, find how many true regions there are in the matrixj
- mh4wt@virginia.edu January 09, 2017 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java - 0of 0 votes
AnswersCreate a basic minesweeper game that allows for board creation with custom height, width and number of mines. Create a <click> function that will take in a board location and return whether the user has won, lost, or the number of surrounding mines.
- mh4wt@virginia.edu January 09, 2017 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java - 0of 0 votes
AnswersGiven a string, print out all of the unique characters and the number of times it appeared in the string
- mh4wt@virginia.edu January 09, 2017 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java String Manipulation - 0of 0 votes
AnswersWrite a code for reversing letters of string in java.
- kreetanshu December 26, 2016 in India| Report Duplicate | Flag | PURGE
Amazon Testing / Quality Assurance Java - 0of 0 votes
AnswersGiven a singly linked list of integers, write a function in java that returns true if the given list is palindrome, else returns false
- mh4wt@virginia.edu December 18, 2016 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java Linked Lists - -3of 3 votes
AnswersConvert an unordered tree to a binary tree
- mh4wt@virginia.edu December 18, 2016 in United States| Report Duplicate | Flag | PURGE
Microsoft Intern Java Trees and Graphs