Q: What is Oops ?
A: Object oriented programming organizes a program around its data, i. e. , objects and a set of well defined interfaces to that data. An object-oriented program can be characterized as data controlling access to code.
Q: What is the difference between Assignment and Initialization?
A: Assignment can be done as many times as desired whereas initialization can be done only once.
Defference between Initialization and assignment
Initialization means whenever we initialize any var. at
declaration time.So as we initialize var. in init() in java
programming.For i.e.
prblic init()
{
int a=101;
}
Assignment means whenever we assigne any value in any var.
except const. variable.
int a,b;
a=100; //right
b=a; //right
const c;
c=2 or c=a; //Worng
const d=20; //that is initialization
Q: What is the use of bin and lib in JDK?
A: Bin contains all tools such as javac, appletviewer, awt tool, etc., whereas lib contains API and all packages.
Q: How to find LeapYear in java?
A:
public class LeapYear {
public static void main (String[] args) {
int theYear;
System.out.print("Enter the year: ");
theYear = Console.in.readInt();
if (theYear < 100) {
if (theYear > 40) {
theYear = theYear + 1900;
}
else {
theYear = theYear + 2000;
}
}
if (theYear % 4 == 0) {
if (theYear % 100 != 0) {
System.out.println(theYear + " is a leap year.");
}
else if (theYear % 400 == 0) {
System.out.println(theYear + " is a leap year.");
}
else {
System.out.println(theYear + " is not a leap year.");
}
}
else {
System.out.println(theYear + " is not a leap year.");
}
}
}
Q: What is casting?
A: Casting is used to convert the value of one type to another. There are two way type cast can possible 1. Java's automatic conversions(if two types are compatible and destination type is larger than the source type then automatic type conversion take place) e.g. byte to int. 2.Casting Incompatible Types e.g.
double d = 3.532;
int i = (int) d;
byte b = (byte) 5.3;
Q: What is Java Overriding ?
A: The argument list should be exactly the same as that of the overridden method.
The return type should be the same or a subtype of the return type declared in the original overridden method in the super class.
The access level cannot be more restrictive than the overridden method's access level. For example: if the super class method is declared public then the overridding method in the sub class cannot be either private or public. However the access level can be less restrictive than the overridden method's access level.
Instance methods can be overridden only if they are inherited by the subclass.
A method declared static cannot be overridden but can be re-declared.
A method declared final cannot be overridden.
A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.
Constructors cannot be overridden.
Example :
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and run");
}
public void bark(){
System.out.println("Dogs can bark");
}
}
public class TestDog{
public static void main(String args[]){
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move();// runs the method in Animal class
b.move();//Runs the method in Dog class
b.bark();
}
}Q: Struts Hibernate Spring example.A: Integrate Struts, Hibernate and Spring
Q: What is the difference between an argument and a parameter?
A: While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.
Q: finalize() in java.
A: Every class inherits the finalize() method from java.lang.Object
The method is called by the garbage collector when it determines no more references to the object exist
Object finalize method performs no actions but it may be overridden by any class
Nomally it should be overridden to clean-up non-Java resources ie closing a file
protected void finalize() throws Throwable {
try {
close(); // close open files
} finally {
super.finalize();
}
}
No comments:
Post a Comment