Questions asked to me in Barclays technical round.
1. oops concept detailed with real life examples.
2. Inheritance complete and use of super keyword.
3. what will be the output of the below code.
class a{
a(){
System.out.println("Hello a");
}
}
class b extends a{
b(){
System.out.println("Hello b");
}
}
class c{
public static void main(String args[])
{
a obj1 = new a();
b obj2 = new b();
}
}
O/P :-
Hello a
Hello a
Hello b
Reason :
There is a automatic call made to the super constructor.4. Will the below code compile?
class a{
a(int i){
System.out.println("Hello a");
}
}
class b extends a{
b(){
super(6);
System.out.println("Hello b");
}
}
class c{
public static void main(String args[])
{
a obj1 = new a();
b obj2 = new b();
}
}
Answer : NO
Reason : No matching constructor found.5. Will this code work?
class a{
a(){
System.out.println("Hello a");
}
}
class b extends a{
b(){
System.out.println("Hello b");
}
}
class c{
public static void main(String args[])
{
a obj1 = new a();
b obj2 = new b();
b obj3 = new a();
}
}
Answer : NO. compile time Error. Incompatible types a cannot be converted to b.Reason : Object of parent class cannot be created by the child class.
6. Will this code work? if yes, what will be the output.
class a{
a(){
System.out.println("Hello a");
}
}
class b extends a{
b(){
System.out.println("Hello b");
}
}
class c{
public static void main(String args[])
{
a obj1 = new a();
b obj2 = new b();
//b obj3 = new a();
a obj4 = new b();
}
}
Ans : Yes O/P :-
Hello a
Hello a
Hello b
Hello a
Hello b
Hello a
Hello a
Hello b
Hello a
Hello b
7. Declaring object of child through parent.
8. Can a class be declared final if yes how and what are the conditions, explain pros and cons.
9. Can a abstract method in an interface be declared as final.
10. What is a static block in java.
11. How are static blocks used and declared in context of inheritance.
12. Will it be called once or every time.
13. How exceptions should be handled. Explain the order of exceptions to be caught.
Ans : The Specialized exceptions should be caught first and then the more generalized exceptions should be caught.
Comments
Post a Comment