Friday 30 September 2011

What is the difference between the instanceof and getclass, these two are same or not ?

instanceof is a operator, not a function while getClass is a method of java.lang.Object class. Consider a condition where we use
if(o.getClass().getName().equals(“java.lang.Math”)){ }
This method only checks if the classname we have passed is equal to

Friday 23 September 2011

Expain the reason for each keyword of public static void main(String args[])?

public- main(..) is the first method called by java environment when a program is executed so it has to accessible from java environment. Hence the access specifier has to be public.

static: Java environment should be able to call this method without creating an instance of the class , so this method must be declared as static.

What is a view? How it is related to data independence?

A view may be thought of as a virtual table, that is, a table that does not really exist in its own right but is instead derived from one or more underlying base table. In other words,

Describe the three levels of data abstraction?

The are three levels of abstraction:

  • Physical level: The lowest level of abstraction describes how data are stored.

  • Logical level: The next higher level of abstraction, describes what data are stored in database and what relationship among those data.

  • View level: The highest level of abstraction describes only part of entire database.

Predict the output or error(s) for the following:

main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;

for(j=0;j<5;j++) {
printf(” %d “,*c);
++q;     }

for(j=0;j<5;j++){

printf(” %d “,*p);

++p;     }
}

Answer:

2 2 2 2 2 2 3 4 6 5

Explanation: 

Predict the output or error(s) for the following:

main()

            {

            static int var = 5;

            printf(“%d “,var–);

            if(var)

                        main();

            }

Answer:

5 4 3 2 1

Explanation:

When static storage class is given, it is initialized once. The change in the value of

Predict the output or error(s) for the following:

main()

{

            float me = 1.1;

            double you = 1.1;

            if(me==you)

printf(“I love U”);

else

                        printf(“I hate U”);

}

Answer:

I hate U

Explanation:

For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession

Predict the output or error(s) for the following:

main()

{

            char s[ ]=”man”;

            int i;

            for(i=0;s[ i ];i++)

            printf(“\n%c%c%c%c”,s[ i ],*(s+i),*(i+s),i[s]);

}

Answer:

mmmm

aaaa

nnnn

Explanation:

s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally  array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the  case of  C  it is same as s[i].

Predict the output or error(s) for the following:

 

void main()

{

            int  const * p=5;

            printf(“%d”,++(*p));

}

Answer:

                        Compiler error: Cannot modify a constant value.

Explanation:

p is a pointer to a “constant integer”. But we tried to change the value of the “constant integer”.

Monday 19 September 2011

What does it mean that a class or member is final?

o final – declare constant
o finally – handles exception
o finalize – helps in garbage collection

Variables defined in an interface are implicitly final. A final class can’t be extended i.e., final class may not be subclassed. This is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve. A final method can’t be overridden when its class is inherited. You can’t change value of a final variable (is a constant). finalize() method