Q1. What all OOPs principles available in Java?
Ans: Inheritence, Polymorphism, Incapsulation.
Q2. What is the difference between a constructor and a method?
Ans: A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
Q3. What is an abstract class?
Ans: Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie. you may not call its constructor), abstract class may contain static data.
Any class with an abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.
Q4. What is the difference between an Interface and an Abstract class?
Ans: An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract.
An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.
Q5. Explain different way of using thread?
Ans: The thread could be implemented by using Runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance, the only interface can help.
Q6. Is it possible to overload main() method of a class?
Ans:Yes, we can overload main() method as well. But every time public static main(String[] args) will be called automatically by JVM. Other methods need to call explicitly. But you can't override main().
Q7. Can we inherit the constructors?
Ans: No, we cannot inherit constructors. We can call super class constructors from subclass constructor by using super() call.
Q8. What is the purpose of garbage collection in Java, and when is it used?
Ans: The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused.
A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.
Q9. What is the base class of all classes?
Ans: java.lang.Object
Q10. What is platform?
Ans: A platform is basically the hardware or software environment in which a program runs. There are two types of platforms software and hardware. Java provides software-based platform.
Q11. What is method overloading?
Ans: Method overloading is a type of polymorphism that includes two or more methods with the same name in same class but, the condition is that it should have different arguments, otherwise an error might occur. This method overloading is very advantageous, as it allows you to implement many methods with the same syntax and semantics. The Overloaded methods that should follow the criteria are as follows:
Q14. How static variable work in java?
Ans: Static keyword/variable in java -
Q15. What is ClassLoader in Java?
Ans: When a Java program is converted into .class file by Java Compiler which is collection of byte code class loader is responsible to load that class file from file system,network or any other location. This class loader is nothing but also a class from which location they are loading the class according to that class loaders are three types :
1.Bootstrap
2.Extension
3.System class loader.
Q16: What is Contract between hashcode and equal method?
Ans : It is very important to understand the contract between equals and hashcode. The general contract of hashCode is.
Whenever hashCode method is invoked on the same object more than once during the execution of the application, the hashCode method consistently return same integer value.
MongoDB Interview Questions - Click Here
Ans: Inheritence, Polymorphism, Incapsulation.
Q2. What is the difference between a constructor and a method?
Ans: A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
Q3. What is an abstract class?
Ans: Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie. you may not call its constructor), abstract class may contain static data.
Any class with an abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.
Q4. What is the difference between an Interface and an Abstract class?
Ans: An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract.
An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.
Q5. Explain different way of using thread?
Ans: The thread could be implemented by using Runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance, the only interface can help.
Q6. Is it possible to overload main() method of a class?
Ans:Yes, we can overload main() method as well. But every time public static main(String[] args) will be called automatically by JVM. Other methods need to call explicitly. But you can't override main().
Q7. Can we inherit the constructors?
Ans: No, we cannot inherit constructors. We can call super class constructors from subclass constructor by using super() call.
Q8. What is the purpose of garbage collection in Java, and when is it used?
Ans: The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused.
A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.
Q9. What is the base class of all classes?
Ans: java.lang.Object
Q10. What is platform?
Ans: A platform is basically the hardware or software environment in which a program runs. There are two types of platforms software and hardware. Java provides software-based platform.
Q11. What is method overloading?
Ans: Method overloading is a type of polymorphism that includes two or more methods with the same name in same class but, the condition is that it should have different arguments, otherwise an error might occur. This method overloading is very advantageous, as it allows you to implement many methods with the same syntax and semantics. The Overloaded methods that should follow the criteria are as follows:
- Overloaded methods can change the return type and access modifier
- Overloaded methods can declare new or broader checked exceptions
- A method can be overloaded in the same class or in a subclass
Q12. What is method overriding?
ANS: Method overriding is a type of polymorphism that is different from
the overloading method, as it allows you to declare the method with the
same arguments. The advantage of using this is that it defines the
behavior of the specific subclass. It doesn’t provide very strict
restrictive access modifier. The method that marked as public and
protected can’t be overridden. You also cannot override a method marked
final and static.
Q13. What is super?
Ans: "super" is a keyword used in Java language. This keyword is used to
access the method and member variables of the super-class. It can refer
the member or the hidden variable of the super-class. It can also invoke
the overridden method. "super" should be used to access the hidden
variable and it should be the first keyword written in the constructor.
Q14. How static variable work in java?
Ans: Static keyword/variable in java -
- Each class has one copy of each of its static members in memory.
- Each instance of the class has access to that single static memory location.
- The single member is same for every instance.
- Static member does not have access to instance members.
- Static code is loaded before the class is instantiated and stays in memory until the JVM exits as opposed to instance variable which are loaded and unloaded which is called “Dynamic” code.
Q15. What is ClassLoader in Java?
Ans: When a Java program is converted into .class file by Java Compiler which is collection of byte code class loader is responsible to load that class file from file system,network or any other location. This class loader is nothing but also a class from which location they are loading the class according to that class loaders are three types :
1.Bootstrap
2.Extension
3.System class loader.
Q16: What is Contract between hashcode and equal method?
Ans : It is very important to understand the contract between equals and hashcode. The general contract of hashCode is.
Whenever hashCode method is invoked on the same object more than once during the execution of the application, the hashCode method consistently return same integer value.
- If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
- It is not required that if two objects are unequal according to the equals(java.lang.Object)method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
MongoDB Interview Questions - Click Here
Nice interview questions n answers
ReplyDelete