Technical Tips, Tricks and Articles

Archive for the ‘interview’ Category

Comparing Wrapper objects values in range –128 to 127

leave a comment »

While debugging through a bug that I encountered in one of my ongoing project. I came to know a strange behavior of Java. Consider the following code segment

1 Integer a = 10;
2 Integer b = 10;
3 
4 if (a == b){
5 	System.out.println("Two values are equal");
6 	}

If you look closely at line no 4, then you would realize that the code is actually comparing “Java objects”. Since “a” and “b” are not wrapper classes not primitive types.  But even then the condition is being true. Ok, some how this makes sense (at least till now unless you read this code segment)

1 Integer a = 150;
2 Integer b = 150;
3 
4 if (a == b){
5 	System.out.println("Two values are equal");
6 	}

Astonishingly, for the above code, the if statement would not execute true. Why?

Well, ever since Java has introduced “auto-boxing” feature, you’ll face this issue. Why is it so? Actually, compiler try to optimize things as much it can. For this, Java has a pool of values from –128 to 127. Whenever, there is some auto-boxing between these values, the reference object for the wrapper class will get the same memory location. In both cases, we are comparing “object references” but in first case, since the value was in the range for both objects hence, Java assigned the same reference location to both objects, making the condition “true” on comparing memory location for two objects.

Now, the question is why between –128 to 127?. Well, the smallest range of values for smallest wrapper class is between –128 to 127 i.e. Byte.

Written by ..alee

July 5, 2009 at 1:06 pm

Posted in code, interview, java, programming

Tagged with , ,

Interview Questions (.NET)

leave a comment »

  1. What’s the difference of using System.Text.StringBuilder over System.String?
    Answer: System.String is immutable. System.StringBuilder was designed with the
    purpose of having a mutable string where a variety of operations can be performed.

  2. What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
    Answer:
    The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements
    into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element’s object, resulting in a different, yet identical object.

  3. How can you sort the elements of the array in descending order?
    Answer:
    By calling Sort () and then Reverse () methods.
  4. What’s the .NET collection class that allows an element to be accessed using a unique key?
    Answer:
    Hashtable.

Written by ..alee

December 19, 2006 at 3:54 pm

Interview Questions (.NET)

leave a comment »

  1. What’s the difference of using System.Text.StringBuilder over System.String?
    Answer: System.String is immutable. System.StringBuilder was designed with the
    purpose of having a mutable string where a variety of operations can be performed.

  2. What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
    Answer:
    The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements
    into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element’s object, resulting in a different, yet identical object.

  3. How can you sort the elements of the array in descending order?
    Answer: By calling Sort () and then Reverse () methods.

  4. What’s the .NET collection class that allows an element to be accessed using a unique key?
    Answer: Hashtable.

  5. Can you allow a class to be inherited, but prevent the method from being over-ridden?
    Answer: Yes. Just leave the class public and make the method sealed.

  6. When do you absolutely have to declare a class as abstract?
    Answer:
    1. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.
    2. When at least one of the methods in the class is abstract.
  7. What accessibility modifier for methods you may specify inside the interface?
    Answer:
    They all must be public, and are therefore public by default.
  8. What’s the implicit name of the parameter that gets passed into the set method/property of a class?
    Answer:
    Value. The data type of the value parameter is defined by whatever data type the property is declared as.
  9. What’s the difference between // comments, /* */ comments and /// comments?
    Answer:
    // is single-line comment, /**/ is multi-line comment & /// is document comment
  10. What is CorDBG and what is DbgCLR?
    Answer:
    Cordbg is a runtime debugger.
  11. What does assert() method do?
    Answer:
    In debug compilation, assert takes in a Boolean condition as a parameter, and shows the error dialog if the condition is false. The program proceeds without any interruption if the condition is true.
  12. What’s the difference between the Debug class and Trace class?
    Answer:
    Documentation looks the same. Use Debug class for debug builds, use Trace class for both debug and release builds.
  13. What happens if you inherit multiple interfaces and they have conflicting method names?

    Answer: It’s up to you to implement the method inside your own class, so implementation is left entirely up to you. This might cause a problem on a higher-level scale if similarly named methods from different interfaces expect different data, but as far as compiler cares you’re okay. You have to provide interfacename.methodname.

Written by ..alee

December 16, 2006 at 3:49 pm

Some Databases

leave a comment »

  • How do you implement one-to-one, one-to-many and many-to-many relationships while designing tables?
    Answer:
    • One-to-One relationship can be implemented as a single table and rarely as two tables with primary and foreign key relationships.
    • One-to-Many relationships are implemented by splitting the data into two tables with primary key and foreign key relationships.
    • Many-to-Many relationships are implemented using a junction table with the keys from both the tables forming the composite primary key of the junction table.

It will be a good idea to read up a database designing fundamentals text book.

  • What’s the difference between a primary key and a unique key?
    Answer:
    Both primary key and unique key enforces uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a non clustered index by default. Another major difference is that, primary key doesn’t allow NULLs, but unique key allows one NULL only.
  • Define candidate key, alternate key, composite key.
    Answer:
    • A candidate key is one that can identify each row of a table uniquely. Generally a candidate key becomes the primary key of the table. If the table has more than one candidate key, one of them will become the primary key, and the rest are called alternate keys.
    • A key formed by combining at least two or more columns is called composite key.
  • What is a transaction and what are ACID properties?
    Answer:
    A transaction is a logical unit of work in which, all the steps must be performed or none. ACID stands for Atomicity, Consistency, Isolation, Durability. These are the properties of a transaction.
  • What are defaults? Is there a column to which a default can’t be bound?
    Answer:
    A default is a value that will be used by a column, if no value is supplied to that column while inserting data. IDENTITY columns and timestamp columns can’t have defaults bound to them.
  • What’s the difference between DELETE TABLE and TRUNCATE TABLE commands?
    Answer:
    DELETE TABLE is a logged operation, so the deletion of each row gets logged in the transaction log, which makes it slow. TRUNCATE TABLE also deletes all the rows in a table, but it won’t log the deletion of each row, instead it logs the deallocation of the data pages of the table, which makes it faster. Of course, TRUNCATE TABLE can be rolled back.
  • What is RAID and what are different types of RAID configurations?
    Answer:
    RAID stands for Redundant Array of Inexpensive Disks, used to provide fault tolerance to database servers. There are six RAID levels 0 through 5 offering different levels of performance, fault tolerance. MSDN has some information about RAID levels and for detailed information, check out the RAID advisory board’s homepage.
  • What is blocking and how would you troubleshoot it?
    Answer:
    Blocking happens when one connection from an application holds a lock and a second connection requires a conflicting lock type. This forces the second connection to wait, blocked on the first.
  • What is an extended stored procedure? Can you instantiate a COM object by using T-SQL?
    Answer:
    An extended stored procedure is a function within a DLL (written in a programming language like C, C++ using Open Data Services (ODS) API) that can be called from T-SQL,just the way we call normal stored procedures using the EXEC statement. Yes, you can instantiate.

Written by ..alee

December 13, 2006 at 3:38 pm

Posted in databases, interview, sql

Tagged with , , ,

OO Questions for .NET/Java

leave a comment »

  1. To which is protected class-level variable available to?
    Answer: It is available to any sub-class (a class inheriting this class).
  2. Are private class-levl variables inherited?
    Answer: Yes, but those are not accessible. Although those are not visible or accessible via the class interface, yet those are inherited.
  3. Describe the accessiblilty modifier "protected internal"?
    Answer: It is available to classes that are within the same assembly and derived from specific base class.

Written by ..alee

November 16, 2006 at 3:52 pm