Java -MCQS on Abstract Classes

 Abstract classes are incomplete classes as we cant create instance using the abstract class.

But it could be used as a ref variable for creating an object using the sub class

An abstract class may have complete methods(concrete methods) and also abstract methods.

An abstract method is a declaration of a method. So there cant be body like {}


The access modifier could be default protected and public.

An abstract class must be extended by any other concrete class .

Forcing inheritance is the concept here as we write an abstract class to have a basic requirement and other developers will write the complete class or concrete class by extending it.

While overriding an abstract method we must follow the access modifier rules.

Equal access or more access must be given.

default access in base class- default, protected and public access  we may give

protected access-protected and public access we may give

An abstract class not necessarily must have an abstract method.

But if a class has one or more abstract methods then it must be declared as abstract class.

The InputStream and OutPutStream in java.io package are abstract classes.

Like that we have Reader and Writer classes in java.io are also abstract classes in jdk.



1.Determine the output:

abstract class abs{


abstract void add(int a,int b){


System.out.println(a+b);




}


public static void main(String ar[]){


abs a1=new abs();


a1.add(1,2);


}}


1.3

2.compile time error add() abstract method cant have

  a body

3.compile time error abs is abstract cant be instantiated  

4.2 and 3

 Answer 4.



------------------------------------------------------------


//Determine the output:

abstract class abs1{

 public static void main(String[] ar){

System.out.println("abs1 main method called");

}


}

/*1.abs1 main method called

  2.compile time error

  3.Runtime error

  4.None of the above

Ans:1  ( an abstract class may have main method.but cant be instantiated

*/

--------------------------------------------------------------

//Determine the output:

abstract class abs2{

void add(int a){

a++;

System.out.println(a);

}

 public static void main(String[] ar){

System.out.println("abs2 main method called");

}


}

/*1.abs2 main method called

  2.compile time error

  3.Runtime error

  4.None of the above

Ans:1  ( an abstract class may have main method.,may have a complete method also

*/

---------------------------------------------------------

//Determine the output:

 class abs3{

abstract void add(int a);

 public static void main(String[] ar){

System.out.println("abs3 main method called");

}


}

/*1.abs3 main method called

  2.compile time error-a class with any abstract method must

    be declared as abstract

  3.Runtime error

  4.None of the above

Ans:2  ( an abstract class may have main method.,may have a complete method also

*/


------------------------------------------

//Determine the output:

 abstract class abs4{

abstract void add(int a);


}

class myabs extends abs4{

 public static void main(String[] ar){

System.out.println("myabs main method called");

}

}

/*1.myabs main method called

  2.compile time error-myabs is not abstract and  does not override abstract method add()

   

  3.Runtime error

  4.None of the above

Ans:2  ( the sub class must override the abstract methods in base class 

if its an abstract one

*/

-----------------------------------------------

//Determine the output:

 abstract class abs4{

abstract void add(int a);


}

class myabs2 extends abs4{

private void add(int a){

a++;

System.out.println(a);

}

 public static void main(String[] ar){

System.out.println("myabs2 main method called");

}

}

/*1.myabs2 main method called

  2.compile time error-attempting to assign weaker access privileges; was

   

  3.Runtime error

  4.None of the above

Ans:2  ( the sub class must override the abstract methods in base class 

if its an abstract one-here while overriding we must give equal access or more

access. was default in add() so we must give default or protected or public not private

*/

--------------------------------


//Determine the output:

 abstract class abs4{

abstract void add(int a);


}

class myabs4 extends abs4{

 void add(int a){

a++;

System.out.println(a);

}

 public static void main(String[] ar){


abs4 a1=new myabs3();

a1.add(10);

}

}

/*1.11

  2.compile time error-attempting to assign weaker access privileges; was

   

  3.Runtime error

  4.None of the above

Ans:1  ( base class ref variable subclass object creation is right. 

*/


-----------------------------------

//Determine the output:

 abstract class abs4{

abstract void add(int a);


}

class myabs5 extends abs4{

 void add(int a){

a++;

System.out.println(a);

}

void sub(int a){

a=a-1;

System.out.println(a);

}


 public static void main(String[] ar){


abs4 a1=new myabs5();

a1.add(10);

a1.sub(10);

}

}

/*1.11

     9

  2.compile time error-sub() cannot find symbol

     a1 is of type abs4

   

  3.Runtime error

  4.None of the above

Ans:2  ( base class ref variable cannot access subclass methods not found in base class. 

*/

----------------------------------

No comments:

Post a Comment

python training with real time projects

java script-text to speech-assignment-Fatima Solkar Final Year-BE I.T FAMT-Ratnagiri-Maharastra

Page Title Mandvi Beach Ratnagiri At a distance of 2 km from Ratnagiri Bus Stand, Mandvi Beach i...

Technical Skills for B.Com and B.B.A