Please navigate to the bottom of the page for Table of Contents

Saturday, June 18, 2011

Basic JAVA interview questions

Having covered a lot of topics in .NET, let’s shift our attention to some JAVA questions. In this post, we will cover a few basic JAVA interview questions to get started. We will advance in complexity in later posts.

SO, first, let’s start with constructors.

Question: What is the difference between constructors and other regular methods?

Constructors must have the same name as the class name and cannot return a value. The constructors are called only once per creation of an object while regular methods can be called many times.

Question: What happens if you do not provide a constructor?

Java does not actually require an explicit constructor in the class description. If you do not include a constructor, the Java compiler will create a default constructor in the byte code with an empty argument. If a class includes one or more explicit constructors, the java compiler does not create the default constructor.

Question: Can you call one constructor from another?

Yes, the key is to use the this() construct.

public Shape(int id) {
this.id = id; // "this" means this object
}

public Shape(int id, String type) {
this(id); // calls constructor public Shape(int id)
this.type = type; // "this" means this object
}



Question: How can you invoke the base class constructor in the derived class?


You can use the super() syntax. Note that this has to be the first statement in the function.


public Circle(int id) {
super(id); //must be the very first statement in the constructor.
}



Question: How can you invoke base class methods in the derived class?


To call a regular method in the base class use “super.myMethod( )”. This can be called at any line.


public class Circle extends Shape 
{
public void setUp()
{
super.initialize();
// do circle stuff
}
}



public void reset() throws Throwable
{
try
{
// Do stuff here to rest your object
}
catch (Throwable t) {}
finally
{
super.reset(); //clean up your parent class. Unlike constructors super.regularMethod() can be called at any line.
}
}



Question: Explain static initializers with an example.


When a class is loaded, all blocks that are declared static and don’t have function name (i.e. static initializers) are executed even before the constructors are executed. As the name suggests they are typically used to initialize static fields.


public class StaticInitializer 
{
public static final int x = 5;
public static final int y;
//note that since y is final, it can be initialized only once.

//Static initializer block, which is executed only once when the class is loaded.
static
{
if(x == 5)
y = 10;
else
y = 5;
}

//constructor is called only after static initializer block
public StaticInitializer(){}
}


The code above sets the value of x to 5 and y to 10.

28 comments:

  1. Given stuff and logical thinking are very good!

    ReplyDelete
  2. When coming to the java point there will be definitely asking the Questions
    on the constructors!So i hope this is the good introduction for the java questions!

    ReplyDelete
  3. ya this is very very important to the interview, because constructors are the base for the objects creation....

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. Write a program where the user enters the number of asterisk for the first row printed. The user will then enter another number (that is less than the number entered first). This number will be the number that each row will be decreased by. For example: User will enter 8 first and 3 second (row 1 - 8 asterisks, row 2 - 5 asterisks, row 3 - 2 asterisks. What will be printed is:

    ********
    *****
    **
    You can't print less asterisks than the decrement value (in the example you can't go below 2 because 3 was the decrement number).

    ReplyDelete
    Replies
    1. This is quite simple:
      static void PrintStars(int startCount, int decrementValue)
      {
      // error checks
      if ((startCount > 0) && (startCount >= decrementValue))
      {
      while (startCount >= 0)
      {
      // print current star count
      Console.WriteLine(new String('*', startCount));

      // decrement for next round
      startCount -= decrementValue;
      }
      }
      else
      {
      Console.WriteLine("Please provide valid input values");
      }
      }

      Delete
    2. Sain Bainuu,


      Thank you! Thank you! Thank you! Your blog was a total game changer!


      Hello people, So I am having trouble uploading a file to my bucket. I have the proper credentials all set. The problem is when I try doing a
      putRequest('bucket', 'keyName', 'fileFullPath') it will only save the file name and not the bytes. So I try sending it as an input stream to a byte array. This way it says there is no such file at specified location? I am running linux so It says no specified file at /home/{user}/pictures/example.jpg This is the problem I believe because it is searching from /home and not /




      It was cool to see your article pop up in my google search for the process yesterday. Great Guide.
      Keep up the good work!


      Thanks a heaps,

      Delete
    3. Hola,


      What a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this.

      I have two methods and they do almost same thing, but at some most inner nested level one of method have additional command.

      Java Code:
      method2(){
      ... same code ...
      for {
      ... same code ...
      if{
      ... same code ...
      }
      }
      }






      Follow my new blog if you interested in just tag along me


      in any
      social media platforms!


      Shukran,

      Delete
  6. Copper Wire Co. sells spools of copper wiring for $100 each. Write a program that display the status of an order. The program should have a function that asks for the following information:

    The number of spools ordered.
    The number of spools in stock.
    If there are special shipping and handling charges.

    (Shipping and handling is normally $10 per spool.) If there are special charges, it should ask for the special chargers per spool.

    Output: number of spools shipped, number of spools back ordered, subtotal of portion ready to ship, total shipping and handling charges on portion ready to ship, total of order ready to ship.

    Validate: do not accept numbers less than one for spools ordered. Do not accept a number less than 0 for spools in stock or shipping and handling charges.

    Once an order is printed, ask the user for the next order. Use a sentinel value to stop (q for quit or -1)

    ReplyDelete
  7. very nice. for java examples, visit http://java2novice.com site

    ReplyDelete
  8. I came across your guide and found exact Basic-java Interview Q/A’s which I was looking for. Thanks for such a useful information’s. But I had found one good website related to Java interview Q/A’s. More Info: java-interview-questions

    ReplyDelete

  9. The share your really gives us excitement. Thanks for your sharing. If you feel tired at work or study try to participate in our games to bring the most exciting feeling. Thank you!
    hotmail sign in | red ball game | 192.168.1.1

    ReplyDelete
  10. Looking for best selenium training in Chennai, Credo Systemz is the no 1 selenium Training institute in Chennai offering professional selenium course by selenium experts.

    ReplyDelete
  11. I will do niche blog comment Just in 5$ .All comment relevant with your niche and UNIQUE .This off-page seo will increase your traffic and promote your business

    ReplyDelete
  12. Your good knowledge and kindness in playing with all the pieces were
    very useful. I don’t know what I would have done if I had not
    encountered such a step like this.


    java Training in Bangalore



    java training in chennai

    ReplyDelete
  13. Thanks for sharing such agreat article, really very helpful information. Keep sharing.

    Best Software company in New jersey, USA

    ReplyDelete


  14. wow...nice blog, very help full information. Thanks for sharing.

    ReplyDelete

  15. Grate blog to read its very nice explanation about Internet Of Things
    Internet Of Things

    ReplyDelete
  16. Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.
    Best Cloud Solutions | Austere Technologies

    ReplyDelete
  17. Great article, really very helpful content you made. Thank you, keep sharing.

    Best IT Security Services | Austere Technologies

    ReplyDelete
  18. Really great blog, it's very helpful and has great knowledgeable information. Thanks for sharing, keep updating such a good informative blog.

    Quality Managment Services | Austere Technologies

    ReplyDelete
  19. Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles
    Austere Technologies|Mobility

    ReplyDelete
  20. Good one.I appreciate you for sharing this knowledge.Thank you so much for the examples.Its very helpful for me and newbies.I learned much .Have a look on yii2 development company,

    ReplyDelete
  21. Excellent information you made in this blog, very helpful information. Thanks for sharing.

    Best chartered accountant courses| Avinash college of commerce

    ReplyDelete
  22. Great article, really very helpful content you made. Thank you, keep sharing.

    Best Software Testing Services | Austere Technology

    ReplyDelete
  23. I must be thankful for sharing your ideas.Thank you for the good writeup.its a brellint job
    java training in chennai
    java course in chennai

    ReplyDelete