Java-MCQs on Interfaces

 In  Java, we have class, abstract class and interface.

An interface is a complete abstract class as it cant have any complete method.

Both abstract class and interface cant be used to instantiate an abject.

Both could be used as a reference variable to refer the object created by the extended class(abstract class)

or implemented class(interface) 

Interface is another way of abstraction in Java.

We write only abstract methods in interface and that are implemented in the implementing class.

The methods declared in interface are public abstract.

So while overriding we must declare public while ignoring the abstract as we are going to write the body for the overridden method.  

So the methods cant have a body, but only method definitions are there in any interface.

The variables declared in interface are public static final. They must be initialized inside the interface.

They canty be changed in the implemented classes.

One interface can extend another interface.

Interfaces could be nested inside another interface or class.


interface inter1{


void add(int a,int b);


}


class useinter1 implements inter1{


void add(int a,int b){


System.out.println(a+b);


}


public static void main(String ar[]){


inter1 i1=new inter1();


i1.add(1,2);


}


}

/*

1.compile time error:add() trying to assign weaker access was public

  inter1 is of type interface cant be instantiated

2.3

3.runtime error

4.None of the above

Answer:1

*/

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

interface inter1{

int counter=10;

void add(int a,int b);


}


class useinter2 implements inter1{


public void add(int a,int b){


System.out.println(a+b);


}


public static void main(String ar[]){


inter1 i1=new useinter2();

counter++;



}


}

/*

1.compile time error: cannot assign a value to final variable counter

counter++;

2.no output

3.runtime error

4.None of the above

Answer:1

*/

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

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