Home » Posts tagged 'interview question'
Tag Archives: interview question
Java Puzzlers on local variable initialization
Intent of my blog
Some time back, i posted a blog on common interview questions on overriding.The blog was very popular on dzone so i decided to write some of the java puzzlers on local variable initialization.One thing which should be kept in mind is that Local variables should be initalized before they are used.Knowing this fact try to answer these questions.
Local Variable Initialization Puzzlers
Question1
public class Question1{
public static void main(String[] args) {
int x;
int y = 10;
if (y == 10) {
x = y;
}
System.out.println("x is " + x);
}
}
Question 2
class Question2{
public static void main(String[] args) {
int x;
if(true){
x = 10;
}
System.out.println("x is " + x);
}
}
Question 3
class Question3{
public static void main(String[] args) {
int x;
final int y = 10;
if(y == 10){
x = 10;
}
System.out.println("x is " + x);
}
}
Question 4
class Question4{
static int y = 10;
public static void main(String[] args) {
int x ;
if(y == 10){
x = 10;
}
System.out.println("x is " + x);
}
}
Question 5
class Question5{
static final int y = 10;
public static void main(String[] args) {
int x ;
if(y == 10){
x = 10;
}
System.out.println("x is " + x);
}
}
Again, like the previous post, i am not posting the solutions because i dont want to take away the fun. So, play with these and have fun.
Overriding Question asked in interviews
Intent of my Blog
Overriding is the concept which is very much asked in the interview.In almost every interview i have given or taken overriding questions were present.So, i decided to document all the possible overriding questions.Try out these questions and have fun.
What is Overriding?
According to wikipedia
“Method overriding, in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass.”
Overriding Questions
Question 1
public class OverridingQuestion1 {
public static void main(String[] args) {
A a = new A();
a.execute();
B b = new B();
b.execute();
a = new B();
a.execute();
b = (B) new A();
b.execute();
}
}
class A {
public void execute() {
System.out.println("A");
}
}
class B extends A {
@Override
public void execute() {
System.out.println("B");
}
}
Question 2
class A1 {
private void prepare(){
System.out.println("Preparing A");
}
}
class B1 extends A1 {
public void prepare(){
System.out.println("Preparing B");
}
public static void main(String[] args) {
A1 a1 = new B1();
a1.prepare();
}
}
Question 3
public class OverridingQuestion3 {
public static void main(String[] args) {
A2 a2 = new A2();
System.out.println(a2.i);
B2 b2 = new B2();
System.out.println(b2.i);
a2 = new B2();
System.out.println(a2.i);
}
}
class A2{
int i = 10;
}
class B2 extends A2{
int i = 20;
}
Question 4
public class OverridingQuestion4 {
public static void main(String[] args) {
A3 a3 = new A3();
a3.execute();
B3 b3 = new B3();
b3.execute();
a3 = new B3();
a3.execute();
b3 = (B3)new A3();
b3.execute();
}
}
class A3{
public static void execute(){
System.out.println("A3");
}
}
class B3 extends A3{
public static void execute(){
System.out.println("B3");
}
}
Question 5
public class OverridingQuestion5 {
public static void main(String[] args) {
A4 a4 = new B4();
a4.execute();
}
}
class A4{
public void execute() throws Exception{
System.out.println("A");
}
}
class B4 extends A4{
@Override
public void execute(){
System.out.println("B");
}
}
As if now i can think of the above 5 questions. If you have any other overriding question, please put that in comment.
I am not posting solution to these questions. So, try these, and post the solution in comments.
Does Java Puzzlers make Good Interview Questions?
Intent of my Blog
Recently i wrote an blog entry on one of the java puzzlers that was asked to me in an interview.I received some of the comments which said this is unfair to ask java puzzlers in an interview.So i thought of writing an blog which discusses whether java puzzlers make good interview questions or not.
Does Java Puzzlers make Good Interview Questions?
I think before answering this question that should we ask java puzzlers in an interview or not we need to define some guidelines about what makes a good interview question. And if java puzzlers fit within those guidelines then we can ask the them in an interview.I am giving my personal point of view please add in your comments if you think something different.
Guideline for a Good Technical Interview Question
- Question should be How not what = Should be Practical which means that Interviewer should not ask the definition of some term or concept the interview question should be such that it discusses practical application of concept.Asking how has the advantage that interviewer gets the correct feedback about interviewee that interviewee actually understand the concept.
- Question should be on simpler concepts = Asking a difficult question doesn’t make an interview question good.In my personal opinion interview question should be about the concepts which a developer normally use. You can vary the difficulty level of question depending upon position you are hiring but the concept should be simple.For example you can ask questions on overriding which can be simple or difficult but the concept of overriding is such that every java developer should know.
- Question can be Extended = A good interview question should be such that you can build your interview on that question which means that if you ask a question on overriding you can start with the easier question and then build your interview by asking questions that increases in difficulty.
- Question should not be specific to API = The question that i mentioned in my post was good but it was specific to the HashSet remove method arguments. Let me explain, when you create an hashset like HashSet<Short> s = new HashSet<Short>() you might expect that when you are doing s.remove(i-1) should remove only short objects but when you take a look at the remove method it takes Object .This is something specific to api which most of the developers might not know. So asking such a question becomes useless.
- Question should provide a learning point = A good interview question should provide a value add to the interviewee. It might be possible that interviewee knows everything which is great and you can hire him/her. But even if he/she doesn’t know the answer they can at least learn a good technical point.
Does java puzzlers fit these guidelines?
In my view java puzzler fit some of the guidelines:-
- All Java Puzzler are about How not What so java puzzlers can provide interviewer the practical understanding of the interviewee.
- Java Puzzlers are about simpler concepts but the Puzzlers are not simple because they discuss the trap or corner cases of the API. You can use these concepts for interviews but the questions are very specific to api and most of the times should not be asked in interview.
- Java Puzzlers can be extended but again because they are not easy most of the times you will not get the correct answer.
- Java Puzzlers are specific to API and they require very good understanding of java api.
- Java Puzzlers definitely provide a learning value to interviewee because these questions touches the corner cases of the api which normally developers doesn’t know.
Conclusion
In my view you can ask some of the java puzzlers in an interview as java puzzlers definetly provide a value. Sometimes you should only take a concept and build you question on that and sometime take the whole question. If you think the java puzzler you are asking is difficult and a normal developer who hasn’t read java puzzlers book can’t answer please dont make that question a decider question means your decision to hire a person should not be based only on java puzzler. Java Puzzlers are definetly very good questions and you should use them wisely in interview.
These are some of my view points. Please share your also.
Java Puzzlers are asked in interviews
Intent of my Blog
This blog is about one of the question that i was asked in an interview two years back. I forgot this question but yesterday i again faced this question and this is a “java puzzler”.
Question
Yesterday while looking for videos on “java puzzlers” i found out link on youtube by Joshua Bloch.When i started viewing this video one of the puzzle reminded of a question i was asked in an interview two years back. Although this question is not in Java(TM) Puzzlers: Traps, Pitfalls, and Corner Cases but it is one of the question i thought worth sharing.Try this question and have fun.
public class JavaPuzzler{
public static void main(String[] args) {
HashSet<Short> s = new HashSet<Short>();//1
for(short i = 0; i<100;i++){//2
s.add(i);//3
s.remove(i-1);//4
}
System.out.println(s.size());//5
}
}
Before viewing the answer of this question please try guessing the answer to this question and then run code in eclipse to check whether you what you were thinking matches the answer.If the answer amaze you it is a java puzzler.
Answer
Answer of this puzzler is 100.
To understand why we get 100 as answer lets try to understand the code line by line.
In line 1 we created an HashSet which is of type java.lang.Short .
In line 2 we are doing a for loop
In line3 we are adding short primitive which will be autoboxed to Short object to the HashSet Collection.
In line 4, we are trying to remove an element from HashSet, which we added just before the current element. But there is a small gotcha (can you guess what is it?), the gotcha is when we do s.remove(i-1), first of all expression (i -1) is evaluated in which short is widened to int and then int is converted to Integer object. This is due to autoboxing in java 1.5 version.If you look into the javadoc of remove method of HashSet, you will find that it takes an Object, so you can remove Integer objects from HashSet of Short objects, but this will not work as a result none of the Short objects will get removed.
So in line 5 we get output as 100.
Hope you find this puzzle interesting, i will strongly recommend viewing this presentation.
You can share any of yours interview question which you think is a “java puzzler”.