Steven You

A Forged Geek.

Interview Questions - Programming

Tomorrow will be my first day of my new job. After graduation, I have gone through quite a large number of articles regarding interview questions and programming tricks. I am gonna list some interesting ones.

1. Check if at least 2 out of 3 booleans is true

This one come from StackOverflow. I like to solve simple problem in one line which is:

return a ?(b || c):(b && c);

or

return a &&(b || c)||(b && c);

or, here is some awesomeness:

return a ^ b ? c : a

2. Overloading is compile-time static binding and Overriding is run-time dynamic binding

In junior Java developer interviews, this is a popular question, the question would be:

Liquid error: undefined method `Py_IsInitialized’ for RubyPython::Python:Module

What is the output?

If your answer is

Test.methodA(Base)
Test.methodA(Derived)

This is wrong. For your surprise the actual output is

Test.methodA(Base)
Test.methodA(Base)

This is because the overloading is compile-time binding. When the compiler trying to figure out which methodA it is in t.methodA(d), as far as the compiler knows is the definition of d is Base.

So, must be careful when overriding Object.equals(otherObject) function, override like this: public boolean equals(Object other) {...}

3. Java Object life cycle

  1. Created
  2. In use (strongly reachable)
  3. Invisible
  4. Unreachable
  5. Collected
  6. Finalized
  7. Deallocated

4. How to declare i and j to make it be an infinite loop

Liquid error: undefined method `Py_IsInitialized’ for RubyPython::Python:Module

This question also comes from StackOverflow.

If i and j are primitive Java type, i != j means the int(or float, or whatever) value not equal, there will be no answer. However, i != j also means i and j are not the same Object. So, one of the answer is:

Liquid error: undefined method `Py_IsInitialized’ for RubyPython::Python:Module

5. Invoke a java method when given the method name as a string

This question is very easy for scripting languages like Python and Perl. But for strong-type language like Java and C# we need a powerful tool called reflector.

Coding from the hip, it would be something like:

Liquid error: undefined method `Py_IsInitialized’ for RubyPython::Python:Module

The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodName). Then you invoke that method by calling

Liquid error: undefined method `Py_IsInitialized’ for RubyPython::Python:Module

Again, leave out the arguments in .invoke, if you don’t have any. But yeah. Read about Java Reflection.

BTW, solution above is from StackOverflow.

6. what is difference between string literal and new String(“…”)

The question is:

What is difference between Liquid error: undefined method Py_IsInitialized' for RubyPython::Python:Module and Liquid error: undefined methodPy_IsInitialized’ for RubyPython::Python:Module

The answer from StackOverflow is:

When you use a string literal the string can be interned but when you use new String(“…”) you get a new string object.

In this example both string literals refer the same object:

Liquid error: undefined method `Py_IsInitialized’ for RubyPython::Python:Module

Here two different objects are created and they have different references:

Liquid error: undefined method `Py_IsInitialized’ for RubyPython::Python:Module

In general you should use the string literal notation when possible. It is easier to read and it gives the compiler a chance to optimize your code.


7. Memory usage of java collections

This article From Java code to Java heap gives a comprehensive explanation of memory usage of popular java collection data structures.

Comments