Interview Questions

Q:

When this. non-static datamember is compulsory inside non-static method

Answer

Whenever there is local- variable/parameter inside the method with same name as data member.


Ex:


Class Sample {


 int a;


  void show() {


   int a=5;


   system.out.println(a); // 5


   system.out.println(this.a);//23


  }


}


class MainApp {


  Public static void main(String[] args) {


  Sample s = new Sample();


   s.a = 23;


   s.show();


   }


}


 

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1807
Q:

What is the differnce among public, protected and package access data?

Answer

If we are not using packages and if we are not using inheritance then there is no difference among public, protected and package access data.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1584
Q:

In a class if private data member is declared then how to set data to that private data member?

Answer

By using method.


The method used to set the data is called setter method.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1449
Q:

In a class if private data member is declared then how to get value of that data member?

Answer

By using method.


The method used to get the data is called getter method.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1371
Q:

What is difference between methods and blocks?

Answer

Method contains name, parameters, return type and executable body. But block does not contains name, parameters, return type.It contains only executable body.


Methods are executed by method call statements. Blocks are executed automatically with class loading and with object creation.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1595
Q:

What is Constructor?

Answer

Constructor is not a special method.


Constructor is block of code that is executed automatically whenever object of the class is created.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1591
Q:

How to call argument constructor?

Answer

new ClassName( arg1, arg2, .... argN);


Ex:


Class sample {


   Sample(int a) {


   System.out.println("arg con a =" +a);


   }


}


class Demo {


    Public static void main( String[] args) {


     Sample s1= new Sample(23);


     Sample s2= new Sample(45);


    }


}

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1563
Q:

What is different among String, StringBuffer, StringBuilder?

Answer

String class objects are immutable objects and they represents strings( sequence of characters)


StringBuffer class objects are mutable objects representing strings and they are Thread safe


StringBuild class objects are also mutable objects representing strings and they are not Thread safe

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1550