Friday 10 November 2017

TCS Tech Lounge Inheritance and Class Relationships

Public abstract class Test {
Public abstract void methodA();
Public abstract void methodB(){
System.out.println(“Hello”);
}
}
Which changes, independently applied, allow this code to compile?
1.Replace lines 5-7 with a semicolon (“;”)
2.Remove the abstract qualifier from the decaration of Test.
3.Remove the abstract qualifier from the  declaration of methodB.           
4.remove the abstract qualifier from the declaration of methodA
Ans:  1 3

public interface Guard{
void doYourJob();}
abstract public class Dog implements Guard{}
Which of the following statements is correct ?

ANS: The code will compile without any errors


Public class TestOverriding {
Public static void main(String arg[]){
Parrot bird = new Parrot();
bird.fly(); //line 4
}
}
Class Bird{
Private void fly(){
System.out.println(“Bird  is flyting”);
}
}
Class Parrot extends Bird{ //line 12
Public void doStuff(){
System.out.println(“I am parrot, and I am doing stuff”);
}
}
ANS: compilation error at line 4


When method defined in subclass which has same signature as a method in a super class, it is known as method _ _ _ __
Ans: Overriding

Abstract class parent {
Public void display() {
System.out.prinltn(“parent display method”);
}
}
Class Child extends Parent{
Public static void main(String args[]){
Parent obj = new Child();
Obj.display();
}
Select one or more true statements for above code.
1.compilation error as child class is not overriding any method after extending an abstract class
2.Child class has inherited to display method from Prent class
3.on executionwe will get output as Parent display method
4.Compilation error as there is no abstract method in abstract class Parent
ANS: b. 2,3 alone are correct and 1,4 are incorrect

Class overload {
Int x;
Double y;
Int add(int a, int b){
X=a+b;
}
Int add(double c, double d){
Y=c+d;
}
Overload(){
This.x=0;
This.y=0;
}
}
Class Overload_methods{
Public static void main(String args[]){
Overload obj =  new overload():
Int a =2;
Double b = 3.2;
Obj.add(a,a);
Obj.add(b.b);
System.out.println(obj.x+””+obj.y);
}
}  
ANS: Compilation error (return statement is missed)
class super {
public int getLength(){
return
}
Public class Sub extends Super{
Public long getLength(){
Return 5;
}
Public static void main(String[] args){
Super sooper =  new Super();
Sub sub = new Sub();
System.out.println(sooper.getLength()+”,”+sub.getLength());
}
}
ANS: Compilation fails

Consider the following statements
i) Constructors can be abstract
 ii) static methods can be abstract
iii) Private methods can be abstract
Which statements are true?
ANS: All are False


Public class Exam1 {
Public static void main(String[]args){
ContractEmployee arun  = new contractEmployee()’;
Arun.freeTime();
}
}
Class Employee{
Final void timeOut(){
System.out.println(“Its time out zone”)
}
}
Class ContractEmployee extends Employee{
Final void freeTime(){
timeOut():
}}
What is the output of above program?
ANS: Its timeout zone
Which of the options if filled into the blanks of the below program , will make the code compile successfully.
_ _ __ _ myinter
{
Int x;
Abstract void display();
}
Class A _ _ _ _myinter
{
Public void display(){
X=10;
System.out.prinltn(“Implement display method”);
}
}
ANS: c  Balnk 1 – abstract, Blank2-class,Blank3-extends


Public static void main(String args[]){
print(10,10);
}
Private static void print(int I, long l)
{
System.out.println(“Printing integer first and then long”);
}
Private static void print(long l, int i)
{
System.out.println(“Printing long first and then integer”);
}
}
ANS: c  compilation error because of ambiguous method call

class Parent{
int I;
void display(){
System.out.println(i);
}
}
Class child extends Parent {
Int j;
Void display(){
System.out.println(j);
}
}
Public static void main(String[]args){
Parent obj = new Child();
Obj.i=1;
Obj.j=2;
Obj.display(;
}
}
What will be the output of the above program?
ANS: Compilation error

Class SuperClass{
void displaySuper(){
System.out.println(“hello superclass”);
}
}
Class SubClass1 extends SuperClass{
Void displaySubClass1(){
System.out.println(“hello subclass1”);
}
}
Class SubClass2 extends SuperClass{
Void displaySubClass2(){
System.out.println(“hello subclass2”);
}
}
Public class Exam1{
public static void main(String[] args){
SuperClass a  = new SuperClass();
SubClass1 b = new SubClass1()
SubClass2 c = new SubClass2();
B = (SubClass1)c;
b.displaySubClass1()
}
}
ANS: Compile time error

Which declaration prevents creating a subclass?
ANS: Final public class FooBar{}


Class A{
protected int i=10;
}
Class B extends A{
Protected int i=15;
}
public class test1 extends B{
Public static void main(String args[]){
A obj=  new Test1();
System.out.println(“i=”+obj.i);
}
}
What will be the output?
ANS: i=10

Interface DisplayStack{
Void display();
}
Class Parent {
Protected void display(){
System.out.println(“parent display method”);
}
}
Class Child extends parent implements DisplayStack{
Protected void display(){
System.out.println(“Child display method”);
}
}
Select one or more true statements for above code
ANS: b copilation error stating cannot reduce the visibility of the inherited method from DisplayStack

Which of these is supported by method overriding in java
ANS: Polymorphism

Which of following is WRONG statement with respect to rules for overriding methods?
ANS : The return type of both the methods must be the different

Interface myinter {
            Int x;
            Void display();
            }
Class A implements myinter {
Public void display() {
System.out.println(“Implementing method”);
}
Public class Test {
            Public static void main(String[] args){
            A obj1=new A();
            Obj1.display();
}
}
ANS: compiler error
interface myinter {
            int x=0;
            void display();
}
class A implements myinter {
            public void display() {
                        System.out.println("Implementing method");
                        System.out.println(x);
            }
}
public class Dos {
            public static void main(String[] args){
            A obj1=new A();
            obj1.display();
            }   }    
ANS: Implementing method
            0

interface myinter {
            void display();
}
class A implements myinter {
            public void display() {     // changed to public
                        System.out.println("Implementing method");
                       
            }
}
public class Dos {
            public static void main(String[] args){
            A obj1=new A();
            obj1.display();
            }
}
ANS: Implementing method

interface myinter
{
            void display();
}
class A implements myinter
{
            void display()
            {
                        System.out.println("Implementing display method");
            }
}
public class Test {
public static void  main(String[] args)
{
A obj1= new A();
obj1.display();
}
}

ANS: Compile time error

A derived class may become a base class, if another class is derived from it.
ANS: TRUE

Which of the following statements are correct?
1.      If you create a parameterized constructor in your class, you are doing constructor overloading
2.      Compile time polymorphism and method overloading is same, it is done by having same method signature in same class
3.      Just like method overriding, constructor overriding can also be done
4.      Runtime polymorphism is achieved by method overriding i.e having same method signature in Parent and child class
ANS:  1,4 are correct and 2 ,3are incorrect


Protected members of a base class are like - - - - -  - ,
But they may be accessed by derived classes.
ANS: protected members

The property or the ability to take more than one form is called as
ANS: polymorphism

Can we declare an abstract static method
ANS: false

public class Greek{
            int i=1;
            public int get1(){
            System.out.print("Super");
            return i;
            }
public static void main(String args[]){
            Greek ga = new Arabik();
            System.out.print(ga.i);
            System.out.print(ga.get1());
            }
}
class Arabik extends Greek{
            int i=2;
            public int get1(){
            System.out.print("Sub");
            return i;
            }
}ANS: 1Sub2

public class Greek{
            int i=1;
            public int get1(){
            System.out.print("Super");
            return i;
            }
public static void main(String args[]){
            Greek ga = new Arabik();
            System.out.print(ga.i);
            System.out.print(ga.get1());
            }
}
class Arabik extends Greek{
            int i=2;
            public int got(){      ////Small change
            System.out.print("Sub");
            return i;
            }
}
ANS: 1Super1

Which of the following two keywords cannot come together when declaring a method ?
1.static and final
2.final and abstract
3.static and abstract
4.abstract and private
ANS: All are correct

Interface myinter1
{
            Void display();
}
Interface myinter2
{
            Void print();
Interface myinter3 extends myinter1, myinter2
{
            Void show():
}
ANS: yes code will compile successfully

Class parent{
            Final static public void display(){
            System.out.prntln(“Parent display method”);
            }
}
Class Child extends Parent{
            Public void display(){
            Syste.out.println(“Parent display method”);
            }
}
Select one or more true statements for above code
1.static method can not be overridden by instance method
2.Parent class final method can not be overridden in Child class
3.A method can be final and static together
ANS: 1,2 alone correct 3 is worng



Which of these can be overloaded?
ANS: method, constructor

1. The concept of inheritance provides the idea of
Ans: reusability

2. package pkg1;
interface A{
void method1();
}
Ans: method1 visible outside package pkg1

4. What is the output of the following program?
class Foo{
public int a=3;
public void addFive(){
a+=5;
System.out.print("f");
}
}
class Bar extends Foo{
public int a=8;
public void addFive(){
this.a=5;
System.out.println("b");
}
}
public class TestClass{
public static void main(String[] args){
Foo f=new Bar();
f.addFive();
System.out.println(f.a);
}
}
Ans: b 3

5. class Parent{
public Parent(){
System.out.println("Parent");
}
}

class Child extends Parent{
public Child(){
System.out.println("Child");
}
}
public class Test{
public static void main(String args[]){
Parent parentObject=new Child();
}
}
What would be the output of the above program?
Ans: Parent Child

6. public interface Foo{
int k=4;
}
Which of the options are true
1. final int k=4;
2. public int k=4;
3. static int k=4;
4. abstract int k=4;
Ans: Statement 1, 2, 3 in the options are replace the statement int k=4;

7. Protected members of a base class are like________, but they may be accessed by derived classes.
Ans: private members

9. Given the following piece of code
public interface Guard{
void doYourJob();}
abstract public class Dog implements Guard{}
Which of the following statements is correct?
Ans: The code will compile without any errors.

10. A derived class may become a base class, if another class is derived from it
Ans: true

11. More than one class may be derived from a base class
Ans: true

12. In an inheritance situation, you may not pass arguments to a base class constructor
Ans: false

13. The base class access specification can be viewed as a filter that base class members must pass through when becoming inherited members of a derived class
Ans: true

14. When an interface is implemented by an abstract class, the abstract class must provide implementation for the methods present in the interface. State whether above statement is true or false?
Ans: false


**16. interface Standard{
public abstract void standard() throws Exception;
}
Select the correct implementation class for Standard interface
Ans: option A
 public class Television implements Standard {
    public static void main(String args[])throws Exception{
        Standard standard=new Television();
        standard.standard();                               
    }
    public void standard(){
        System.out.println("statment");

    }
}



17. Redefining the instance method of super class by sub class is
Ans: Method overriding

18. What is the output of the following program?
public class Greek{
int i=1;
public int get1(){
System.out.println("Super");
return i;
}
public static void main(String args[]){
Greek ga=new Arabik();
System.out.print(ga.i);
System.out.print(ga.get1());
}
}

class Arabik extends Greek{
int i=2;
public int get1(){
System.out.print("Sub");
return i;
}
}
Ans: 1Sub2

19. Which of these keywords is used to access the members of base class from a sub class?
Ans: super


22. Which of the following two keywords cannot come together when declaring a method?
i. static and final ii. final and abstract iii. static and abstract iv. abstract and private
Ans: All are correct


23. package pkg1;
interface A{
void method1();
}
Ans: method1 visible outside package pkg1


24. If we want subclasses in any package to have access to members of a superclass. What is the most restrictive access that accomplishes this objective?
Ans: protected


25. class X has a method:
int f1(int m){
return m*m;
}
class Y has a method:
String f1(int num){
return "abc";
}
Suppose Y is a subclass of X, then what type of polymorphism is being implemented in this scenario?
Ans: No Polymorphism. The code will have a compilation error


26. public interface Foo{
int k=4;
}
Which of the following options are true
1. final int k=4;
2. public int k=4;
3. static int k=4;
4. abstract int k=4;
Ans: Statement 1,2,3 in the options are replace the statement int k=4;


27. Given:
class Clidders{
public final void flipper(){System.out.println("Clidder");}
}
public class Clidlets extends Clidders{
public void flipper(){
System.out.println("Flip a Clidlet");
super.flipper();
}
public static void main(String[] args){
new Clidlets().flipper();
}
}
What is the result?
Ans: Compilation fails


**28. package abc;
public class Animal{
public Animal(){
System.out.println("inside animal's constructor");
}
public void printName(){
System.out.println("Animal");
}
}
package exam;
import abc.Animal;
public class Cat extends Animal{
public void printName(){
System.out.println("Cat");
}
}
package abc;
import exam.Cat;
public class Test{
public static void main(String[] args){
Animal a=new Cat();
}
}
What is the output of the given program?
Ans: Runtime error


**29. What will be the output of the below program?
public class Test
{
public static void main(String args[])
{
print(10,10);
}

private static void print(int i, long l)
{
System.out.println("Print integer first and then long");
}
}
Ans: Compilation error because of ambiguous method call


30. _____ ______myinter
{
int x;
abstract void display();
}

class A________myinter
{
public void display()
{
x=10;
System.out.println("Implementing display method");
}
}
Ans: Blank1-public, Blank2-interface, Blank3-implements
 

32. Which of the following is true?
Ans: An interface can extend many interfaces

34. How do you define class named Exam1 so that it cannot be subclassed?
Ans: final class Exam1{}

35. Which of the following statements are correct?
      1. Parent obj=new Child();
      2. Parent obj=new Parent();
      3. Child obj=new Child();
      4. Child obj=new Parent();
Ans: Statement 1,2,3 are correct 4 is incorrect

36. How do you define class named Vehicle so that it cannot be subclassed?
Ans: final class Vehicle{}

37. What will be the output of the following code?

interface myinter1
{
void display();
}
interface myinter2
{
void print();
}
interface myinter3 extends myinter1,myinter2
{
void show();
}
Ans: Yes, the code will compile successfully


39. Does following code displays a valid override?
class MyClass{
void add(int i,int ti){
//I will do later
}
}
class MySubclass extends MyClass{
public void add(int i,int ti){
//Will do now
}
}
Ans: true

40. What is the output of the below code?
interface myinter{
int x;
void display();
}

class A implements myinter
{
public void display()
{
System.out.println("Implementing display method");
}
}
Ans: compilation error

class Base{
int i=99;
public void amethod(){
System.out.println(“Base.amethod()”);
}
Base(){
amethod();
}
}
Public class Derived extends Base{
Int i=-1;
Public static void main(String[]arg){
Base b =  new Derived();
System.out.println(b.i);
b.method();
}
Public void amethod(){
System.out.println(“Derivedaamethod()”);
}
}
ANS: Derived.amethod() 99 Derived.amethod()


Interface A{
Public void method1();
}
Interface B{
Public void method1();
}
Public class Test implements A,B{
Public void method1(){
}
}
ANS: compile successfully

Class SuperClass{
static void display(){
System.out.println(“hello super class”);
}
}
Public class SubClass extends SuperClass{
Static void display(){
System.out.println(“hello sub class”);
}
Public static void main(String[]arg){
SuperClass s = new SuperClass();
s.display();
SubClass sub = new SubClass();
Sub.display();
S=sub;
s.display();
sub=s;
sub.display();
}
}
What is the output of the above program
ANS: Compile time error

Class A{
Public void display(int i){
System.out.println(“Printing value of integer:”+i);
}
}
Class B extends A{
Public void display(byte b){
System.out.println(“Printing value of byte:”+b);
}
}
Public class Test{
Public static void main(String[]arg){
A obj1 = new B();
Byte myByte=19;
Obj1.display(myByte);
}
}
ANS: printing value of integer: 19

class A{
            int i=10;
            public void printValue(){
                        System.out.println("Value-A");
            }
}
class B extends A{
            int i=12;
            public void printValue(){
                        System.out.println("Value-B");
            }
}
public class Test{
            public static void main(String[]arg){
                        A a=new B();
                        a.printValue();
                        System.out.println(a.i);
            }
}
ANS: Value-B 10
import java.util.*;
public class Primes2{
            public static void main(String[]args){
                        Integer[] primes = {2,7,5,3};
                        MySort ms = new MySort();
                        Arrays.sort(primes,ms);
                        for(Integer p2:primes)
                                    System.out.print(p2+"");
            }
}
static class MySort implements Comparator{
            public int compare(Integer x, Integer y){
                        return y.compareTo(x);
            }
            public int compare(Object o1, Object o2){
                        return 0;
            }
}
ANS: compilation fails


public class Bclass{
            private int x;
            public void set(int a){ x=a;}
            public void print(){}
}
public class Dclass extends Bclass{
            private int y;
            public void set(int a, int b){
                        //Post condition x=a; y=b;
                        super.set(a);
                        y=b;
            }
public void print(){}
}
Which of the following is the correct definition of the method set of the class DClass?

(i)
   public void set(int a, int b)
   {
      super.set(a);
      y = b;
   }

(ii)

   public void set(int a, int b)
   {
      x = a;
      y = b;
   }

ANS: ONLY II

Interface Myinterface{
Void show();
Void set();
Void just();
}
Class MyClass implements MyInterface{
Public void set(){}
Public void just(){}
}
What happens when above code is processed?
ANS: compilation error

class Parent{
            int i;
            void display(){
                        System.out.println(i);
            }
}
class Child extends Parent{
            int j;
            void display(){
                        System.out.println(j);
            }
}
public class inheritance_demo{
            public static void main(String[]arg){
                        Parent obj=new Child();
                        obj.i=1;
                        obj.j=2;
                        obj.display();
            }
}
ANS: Compilation error

Which of the following access specifiers if substituted instead of the blank will make the code successfully compile?
Interface myinter
{
Void display();
}
Class Test implements myinter
{
_ _ _ _ _ void display()
{
System.out.println(“Displaying data”);
}
}
ANS: public

Read the following code below
Public interface AQuestion{
Public abstract void someMethod() throws Exception;
}
A class implementing this interface should
ANS: Should have the method public void someMethod() which need not throw an
Exception.

abstract final class Parent{
            public void display(){
            System.out.prntln("Parent display method");
            }
}
class Child extends Parent{
            public void display(){
            System.out.println("Child display method");
            }
}
ANS: 1,2 are correct 3 worng

class A{
            public static void display()
            {
                        System.out.println("HI I am Parent");
            }
}
class B extends A{
            public static void display(){
                        System.out.println("Hi I am Child");
            }
}
public class Test{
public static void main(String[] args)
{
            A obj1=new B();
            obj1.display();
}
}
ANS: Hi I am Parent

interface Animal
{
            void soundOff();
}
class Elephant implements Animal
{
            public void soundOff()
            {
                        System.out.println("trumpet");
            }
}
class Lion implements Animal
{
            public void soundOff()
            {
                        System.out.println("roar");
            }
}
class Alpha1
{
            static Animal get(String choice)
            {
                        if(choice.equalsIgnoreCase("meat eater"))
                        {
                                    return new Lion();
                        }
                        else
                        {
                                    return new Elephant();
                        }
            }
}
ANS: new Alpha1().get("veggie").soundOff();

class Test{
            int var1;
            Demo(int a){
                        var1=a;
            }
}
class DerivedTest extends Test{
            int var2, var3, var4;
            public DerivedTest(int x, int y){
            var2 = x; var3=0;
            super(y);
}
public DerivedTest(int x){
            super(x);
           
}
}
class MainClass{
                        public static void main(String arg[]){
            DerivedTest obj=new DerivedTest(10,20);}
            }

ANS: return type missing at demo

5 comments:

  1. Great post.Thanks for one marvelous posting! I enjoyed reading it;The information was very useful.Keep the good work going on!!
    SEO Services in Pakistan

    ReplyDelete
  2. Such an interesting and informative piece of guidance imparted by you. I am glad to discover this information here and I am sure that this might be beneficial for us.
    Web delopment company in India

    ReplyDelete
  3. Thanks for sharing this informative content , Great work
    Read this PSM vs CSM blog from Leanpitch to get a better conclusion : PSM vs CSM

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete