Java Interview Questions – 2

Ques 1. What are the Object and Class classes used for?

Ans. The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program.

 

Ques 2. Can you write Java code for declaration of multiple inheritance in Java ?

Ans.

Class C extends A implements B

{

}

 

Ques 3. What do you mean by multiple inheritance in C++ ?

Ans. Multiple inheritance is a feature in C++ by which one class can be of different types. Say class teachingAssistant is inherited from two classes say teacher and Student.

Ques 4. Write the Java code to declare any constant (say gravitational constant) and to get its value.

Ans.

Class ABC

{

static final float GRAVITATIONAL_CONSTANT = 9.8;

public void getConstant()

{

system.out.println(“Gravitational_Constant: ” + GRAVITATIONAL_CONSTANT);

}

}

 

Ques 5. What are the disadvantages of using threads?

Ans. DeadLock.

 

Ques 6. Given two tables Student(SID, Name, Course) and Level(SID, level) write the SQL statement to get the name and SID of the student who are taking course = 3 and at freshman level.

Ans.

SELECT Student.name, Student.SID FROM Student, Level WHERE Student.SID = Level.SID

AND Level.Level = “freshman” AND Student.Course = 3;

 

Ques 7. What do you mean by virtual methods?

Ans. virtual methods are used to use the polymorhism feature in C++. Say class A is inherited from class B. If we declare say fuction f() as virtual in class B and override the same function in class A then at runtime appropriate method of the class will be called depending upon the type of the object.

 

Ques 8. What do you mean by static methods?

Ans. By using the static method there is no need creating an object of that class to use that method. We can directly call that method on that class. For example, say class A has static function f(), then we can call f() function as A.f(). There is no need of creating an object of class A.

 

Ques 9. What do mean by polymorphism, inheritance, encapsulation?

Ans.

Polymorhism: is a feature of OOPl that at run time depending upon the type of object the appropriate method is called.

Inheritance: is a feature of OOPL that represents the “is a” relationship between different objects(classes). Say in real life a manager is a employee. So in OOPL manger class is inherited from the employee class.

Encapsulation: is a feature of OOPL that is used to hide the information.

 

Ques 10. What are the advantages of OOPL?

Ans. Object oriented programming languages directly represent the real life objects. The features of OOPL as inhreitance, polymorphism, encapsulation makes it powerful.

 

Ques 11. How many methods do u implement if implement the Serializable Interface?

Ans. The Serializable interface is just a “marker” interface, with no methods of its own to implement.

 

Ques 12. Are there any other ‘marker’ interfaces?

Ans.

java.rmi.Remote

java.util.EventListener

 

Ques 13. What is the difference between instanceof and isInstance?

Ans. instanceof is used to check to see if an object can be cast into a specified type without throwing a cast class exception. isInstance() determines if the specified object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is nonnull and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.

 

Ques 14. why do you create interfaces, and when MUST you use one?

Ans. You would create interfaces when you have two or more functionalities talking to each other. Doing it this way help you in creating a protocol between the parties involved.

 

Ques 15. What’s the difference between the == operator and the equals() method? What test does Object.equals() use, and why?

Ans. The == operator would be used, in an object sense, to see if the two objects were actually the same object. This operator looks at the actually memory address to see if it actually the same object. The equals() method is used to compare the values of the object respectively. This is used in a higher level to see if the object values are equal. Of course the the equals() method would be overloaded in a meaningful way for whatever object that you were working with.

 

Ques 16. Discuss the differences between creating a new class, extending a class and implementing an interface; and when each would be appropriate.

Ans.

* Creating a new class is simply creating a class with no extensions and no implementations. The signature

is as follows

public class MyClass()

{

}

* Extending a class is when you want to use the functionality of another class or classes. The extended class inherits all of the functionality of the previous class. An example of this when you create your own applet class and extend from java.applet.Applet. This gives you all of the functionality of the java.applet.Applet class. The signature would look like this

public class MyClass extends MyBaseClass

{

}

* Implementing an interface simply forces you to use the methods of the interface implemented. This gives you two advantages. This forces you to follow a standard(forces you to use certain methods) and in doing so gives you a channel for polymorphism. This isn’t the only way you can do polymorphism but this is one of the ways.

public class Fish implements Animal

{

}

 

Ques 17. Name four methods every Java class will have.

Ans.

public String toString();

public Object clone();

public boolean equals();

public int hashCode();

 

Ques 18. What does the “abstract” keyword mean in front of a method? A class?

Ans. Abstract keyword declares either a method or a class. If a method has a abstract keyword in front of it, it is called abstract method.Abstract method has no body. It has only arguments and return type. Abstract methods act as placeholder methods that are implemented in the subclasses. Abstract classes can’t be instantiated.If a class is declared as abstract,no objects of that class can be created.If a class contains any abstract method it must be declared as abstract.

 

Ques 19. Does Java have destructors?

And. No garbage collector does the job working in the background

 

Ques 20. Are constructors inherited? Can a subclass call the parent’s class constructor? When?

Ans. You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it’s superclasses. One of the main reasons is because you probably don’t want to overide the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.

 

Ques 21. Does Java have “goto”?

Ans. No

 

Ques 22. What does the “final” keyword mean in front of a variable? A method? A class?

Ans.

FINAL for a variable : value is constant

FINAL for a method : cannot be overridden

FINAL for a class : cannot be derived