Saturday, 18 April 2015

Q. What is the O/P of the following---

 

  if two overloaded methods are:

 

class Test

{

    public static void main(String as[])

    {

        Test t=new Test();

        t.m1(null);

    }

    public void m1(Object obj)

    {

        System.out.println("m1 with obj");

    }

    public void m1(Customer c)

    {

        System.out.println("m1 with Cust");

    }

}

which method will be called and why?


Ans: Jvm will choose the most appropriate methods.Here, we need to know which class is more specialized, Object or Customer.The Customer class is the specialization of Object class.So Customer type method will be called.

 

Thursday, 16 April 2015

Q. What is an SDK ?



Ans: A software development kit (SDK) is typically a set of software development tools whch acts as a platform to start building and developing your application.It includes mainly three things---

1) An IDE (Integrated Development Environment):
it is code editor which allows you to write and edit code eg Eclipse.

2) Performance tools: These include tools for measuring performance, bug finding and also memory leak finding.

3) Build tools:You can use the build tools to build the code into binary package, which can be turned into an app which can then be uploaded.

SDKs also includes sample code and supporting technical notes or other supporting documentation.

Example:
1) iOS xcode for apple devices, visual studios for windows are popular Software Development Kits.

2) The JDK can be called as a subset of a software development kit (SDK).It includes "tools for developing, debugging, and monitoring Java   applications".
 

Tuesday, 14 April 2015

Q. What will happen if final is used and final is removed in the following code.


public class Test

{

    public static void main(String[] args) throws Exception

    {

        final boolean x=true;

        while(x)

        {

            System.out.println("AA");

           

        }

        System.out.println(("BB"));

       

    }}


Ans:If final is used with x then will get C.E  saying Unreachable Code because the condition will always be true. If final is removed then there is a chance that the value of x may be changed (false) and so there is a chance of reaching the statement after while loop and so compilation is successful.


Monday, 13 April 2015

Q. What is the difference between Green Thread and Native Thread?

Ans: Green threads are threads that are scheduled by a virtual machine (VM) instead of natively by the underlying operating system.Green threads emulate multithreaded environments without relying on any native OS capabilities, and they are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread support.


Sunday, 12 April 2015

Q. If the super class implements serializable,then no need that the sub classes would also be implementing serializable interface,then why does HttpServlet class implements Serializable interface when the superclass that it extends, GenericServlet, has already implemented Serializable interface?

Ans: It is not compulsary that if the super class implements serialiable interface then sub class should also implement the same,it is a best practice to declare in the subclass that it also implements the same interface. This way if some one just reads the subclass he will have the complete picture.

Saturday, 11 April 2015

Q. How can we allow duplicate value in set ?



Ans: In the case of Set the hashcode of the object is used to find the place/index where the object will be stored.If somehow we can change the hashcode of the object then the index accessed by using the hashcode will be changed.So same object will be stored in the Set.


Example:
package com.manish.jlc;
import java.util.HashSet;
import java.util.Set;

public class Test1
{
    public static void main(String[] args)
    {
        Set st=new HashSet();
        Student st1=new Student(34);
        st.add(st1);
        st.add(st1);
        st.add(st1);
        st.add(st1);
        System.out.println(st); // [ 34 ]
        st1.age=35;
        st.add(st1);
        System.out.println(st); //[ 35, 35 ]
    }
}
class Student
{
    int age;

    public Student(int age) {
        super();
        this.age = age;
    }
    public int hashCode()
    {
        return this.age;
    }
    public String toString()
    {
        return String.valueOf(age);
    }