Showing posts with label Core Java Interview Questions. Show all posts
Showing posts with label Core Java Interview Questions. Show all posts

Core Java Polymorphism Interview Questions

SDET

  1)What is polymorphism and what are the types of it in Java?

Word Polymorphism made of two words, poly and morphism. Poly means many and morphism means form so polymorphism means many form. Method overloading and method overriding are example of polymorphism in Java.

2)What is method overriding?

When child class contains same method name and same arguments but different implementation then at runtime child class is called. It is called method overriding or runtime polymorphism.

3)What is method overloading?

Method with same name but different arguments or different order of arguments is called method overloading. It is also called compile time polymorphism because it occurs at compile time.
  1. public class SumOfDigits{
  2. public static void m1(String a, int b){
  3. System.out.println("first method");
  4. }
  5. public static void m1(int a, String b){
  6. System.out.println("Second method");
  7. }
  8. public static void m1(int a, double b){
  9. System.out.println("Second method");
  10. }
  11. public static void main(String [] args){
  12. SumOfDigits.m1("ritesh",2);
  13. SumOfDigits.m1(3, "ranjan");
  14. SumOfDigits.m1(5, 6.5);
  15. }
  16. }
4)Can we overload static method?
Yes we can overload static method. But we can't overload static method with non static method.
5)can we overload main() method?
Yes we can overload main() method. main() method is static method .

6)can we override static method?
No, we can't override static method because static method resolution is based on reference class and done at compile time.
7)Can you prevent overriding a method without using final modifier?
Yes, there is a way we can prevent overriding a method without using final keyword. If we declare parent class constructor as private then parent class method won't be called in child class.
8)Can we override constructor in Java?
No, we can't override constructor in Java because constructor is associated with class  not instance of class.
9) Can we override a method which throws run-time exception without throws clause?
Yes, we can override a method which throws run-time exception. There is no rule of run-time exception in overriding.
10) Can override a method if parent class method throws checked exception?
Yes, Because there is no rule if parent class method throws checked exception. If child class method throws checked exception then parent class method must throw same checked exception or parent of that checked exception.
11)Can overloaded method have different return type?
No, overloaded method can't have different type because Java won't be able to identify which method to be called. It throws ambiguous error. 

Abstract Class Interview Questions in Java

SDET

  1) Abstract class must have only abstract methods. True or false?

No, Abstract class can have both abstract method and concreate method.

2) Is it compulsory for a class which is declared as abstract to have at least one abstract method?

No, Abstract class may or may not have abstract method.

It is just we can't create object of Abstract class.

4) Why final and abstract can not be used at a time?

If we make class or method final then we can't modify class and method but Abstract means something which is not complete and it needs modification further. Abstract class or method is modified in the subclass.

5) Can we instantiate a class which does not have even a single abstract methods but declared as abstract?

No, we can't instantiate Abstract class.

6) Can we declare abstract methods as private? Justify your answer?

No. Abstract methods can not be private. Abstract method must be overridden in subclass and if we declare abstract method as private then it can't be overridden.

7) Can we declare abstract method as static?

No we can't declare abstract method as static because static method can't be overridden and abstract method must be overridden.

8) Can abstract method declaration include throws clause?

Yes. Abstract methods can be declared with throws clause.

9) Can abstract class have constructors in Java?

Yes abstract class can have constructor in Java. Since we can't create instances of abstract class but abstract class constructors are used to initialize abstract class instance variables in subclass constructor. Also JVM create default constructor if we don't define any constructor. Let's understand need of abstract class constructor through examples.

  1. abstract class Parent {
  2. int a,b;
  3. public Parent(int a, int b){
  4. this.a=a;
  5. this.b=b;
  6. }
  7. }
  8. public class Child extends Parent {
  9. int a,b,c;
  10. public Child(int a, int b, int c){
  11. super(a,b);
  12. this.c=c;
  13. System.out.println("Value of a is "+a);
  14. System.out.println("Value of b is "+b);
  15. System.out.println("Value of c is "+c);
  16. }
  17. public static void main(String [] args){
  18. Child c = new Child(1,2,3);
  19. }
  20. }
  21.  
10) Can abstract class contains main method in Java?
Yes abstract class can contains main method in Java. It'll be used to execute abstract class until you don't create instance of abstract class.

String Java Concept Interview Questions

SDET

 1)  What is String in Java?

 String in Java is sequence of characters.

2) Is String a keyword in Java?

 No, String is not a keyword in Java. It is final class in Java

3) In how many ways String object can be created in Java?

There are two ways we can create string object in Java. First method is using new operator and object created by this method is stored in Heap memory. Second Method is using string literal and object created by this method is stored in string concept pool(SCP).

 String obj = new String("ritesh"); // using new operator

 String name  = "ritesh";  // using literal

4) What is string constant pool(SCP)?

String constant pool is memory space inside Heap memory and it is used to store string literal. It is immutable.

5) What is immutable object?

Once we create object we can not make any change in object and if we try to make changes , a new object is created. It is called immutable object. Let's understand it with string class object since string is immutable.

String obj = new String("ritesh");

System.out.println(obj) // output = ritesh

String newObj = obj.concat("ranjan")

System.out.println(obj) // output = ritesh

System.out.println(newObj) // output = riteshranjan

6) What is difference between String, String buffer and String Builder?

1. String is immutable class in Java whereas both String Buffer and String Builder are mutable.

2. String is stored in both Heap memory space and SCP whereas String Buffer and String Builder are stored in Heap memory space only.

3. String object is created using "new" operator and using string literals but both String Buffer and String Builder object are created using 'new' operator.

4. String and String Buffer are thread safe but String Builder is not thread safe.

5. String class override .equals() method of Object class for content comparison but String Buffer and String Builder don't override .equals() method. 

7) What is difference between .equals() method and '==' operator?

Both .equals() method of Object class and "==" operator are used for address comparison whereas .equals() method of String class override .equals() method of Object class for content comparison.

8) How many object will be created in following case?

String s1 = new String("ritesh");

String s2 = new String("ritesh");

String s3 = "ritesh";

String s4 = "ritesh";

Three object will be created in this case. Two object in Heap memory and one is SCP.


8) What are different constructors of String class?

There are five different constructors in String class.
1.Empty constructor
    String s = new String()
2. String s = new String(String literal);
3. String s = new String(StringBuffer s);
4. String s = new String(char [] c );
    char [] c = {d, e, f, g};
5. String s = new String(byte [] b);
    byte [] b = {25,15,69,45};

9) What are important methods of String?
Different methods in String class are-
.equals(), concat(), subString(), equalsIgnorCase(), length(), toLowerCase, toUpperCase(), replace(), charAt(), trim(), contains(), compare() and etc.