Select chapter



basics (7.papers)
constructors (1.papers)
packages (1.papers)
inheritance (7.papers)
misc (3.papers)
exceptions (5.papers)
threads (4.papers)
overloading (1.papers)
jsp (3.papers)
servlets (9.papers)
jdbc (2.papers)
util (1.papers)
xml (2.papers)
IO stream (3.papers)
Hide

Sponsored Links


  

java interface abstractclass interview questions and answers

1
What is an interface in java?
  1. Interfaces are reference types just like classes, except that it can contain only constants, inner types, and only method signatures with out any body.
  2. Interfaces are reference types just like classes, except that it can contain only constants, and only method signatures with out any body.
  3. Interfaces are reference types just like classes, except that it can contain only constants.
  4. Interfaces are reference types just like classes, except that it can contain only method signatures with out any body.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
6
What is not true about an Interface?
  1. You can't create object for an interface.
  2. You can't create variable for an interface.
  3. you can't declare variables in interfaces.
  4. You can assign an object of a class which implements an interface, to thaerft intace variable.
Show Answer
Share Facebook gmail Twitter
7
Why interfaces can't be instantiated in java? Why it is not possible to create objects for an interface?
  1. Because java documentation says so.
  2. Because interfaces will not have implementations, so there is no point of creating objects.
  3. Its wrong, we can instantiate interfaces just like classes.
  4. some times you can instantiate and some times you can't. It depends on what you have in that interface.
Show Answer
Share Facebook gmail Twitter
8
Write CircleInterface interface for this class.
class Circle implements CircleInterface{
        public static final double pi = 3.14;
        int r;
        public Circle(int rad){
                r = rad;
        }
        @Override
        public void findArea() {
                System.out.println(pi*r*r);
        }
}
  1. interface CircleInterface{
            double pi = 3.14;
            void findArea();
    }
  2. interface CircleInterface{
            int r;
            double pi = 3.14;
            void findArea();
    }
  3. interface CircleInterface{
            double pi = 3.14;
            int r;
            public Circle(int rad);
            public void findArea();
    }
  4. interface CircleInterface{
            final double pi = 3.14;
            int r;
            public Circle(int rad);
            public void findArea();
    }
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
11
Why interfaces can't contain constructors?
  1. Because you can't create objects for that class, so there is no point in having constructors.
  2. Because you can't have instance variables, so no point in having constructors.
  3. both options are true
  4. both are wrong
Show Answer
Share Facebook gmail Twitter
12
What is the output of below Java program?
interface CircleInterface{
        double pi = 3.14;
        void findArea();
}

class Circle implements CircleInterface{
        int r;
        public Circle(int rad){
                r = rad;
        }
        @Override
        public void findArea() {
                System.out.println(pi*r*r);
        }
        public void disp(){
                System.out.println("circle disp..");
        }
}
public class InterfaceDemo {
        public static void main(String[] args) {
                CircleInterface c = new Circle(3);
                c.findArea();
                c.disp();                
        }
}
  1. 28.26
    circle disp..
  2. Compile time error in main function, you can't access c.disp() because disp is not a function of CircleInterface.
  3. Compile time error in main, you can't assign circle object to circle interface variable.
  4. Compile time error in Circle class, you can't have disp() function, because CircleInterface doesn't have that function.
Show Answer
Share Facebook gmail Twitter
13
What is the output of below Java program?
interface CircleInterface{
        double pi = 3.14;
        void findArea();
}

class Circle implements CircleInterface{
        int r;
        public Circle(int rad){
                r = rad;
        }
        @Override
        public void findArea() {
                System.out.println(pi*r*r);
        }
        public void disp(){
                System.out.println("circle disp..");
        }
}
public class InterfaceDemo {
        public static void main(String[] args) {
                CircleInterface c = new Circle(3);
                c.findArea();
                ((Circle)c).disp();                
        }
}
  1. 28.26
    circle disp..
  2. Compile time error in main function, you can't access c.disp() because disp is not a function of CircleInterface.
  3. Run time exception in main, as you can't typecast CircleInterface variable back to Circle variable, like shown in the program. ((Circle) c).disp(); This line will throw run time exception.
  4. Compile time error in main, as you can't typecast CircleInterface variable back to Circle variable, like shown in the program. ((Circle) c).disp(); This line will throw compile time error.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
15
Example for using and implementing an interface in java:
What is the output of below Java program?
interface Vehicle{
        public void run();
}
class Bicycle implements Vehicle{
        public void run(){
                System.out.println("I can run on 2 wheels");
        }
}
class MotorCycle extends Bicycle{
        public void disp(){
                System.out.println("MotorCycle displays");
        }
}
public class InterfaceDemo2 {
        public static void main(String[] args) {
                Bicycle b = new Bicycle();
                b.run();
                MotorCycle m = new MotorCycle();
                m.run();
                m.disp();
        }
}
  1. Compile time error in MotorCycle class, we can't write disp() method in MotorCycle class, because that function is not available in Bicycle as well as in Vehicle interface.
  2. Compile time error in MotorCycle class, as we are not giving the function body for run() method.
  3. I can run on 2 wheels
    I can run on 2 wheels
    MotorCycle displays
  4. Compile time error, we can't call run() function on MotorCycle object m, because that function belongs to Vehicle interface which we are not implementing in MotorCycle.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
18
example for creating interface variable in java:
Is there any problem with below Java code?

interface RectInterface{
        public void findArea();
}
class Test{
        public void check(RectInterface r){
                r.findArea();
        }
}
  1. It gives compile time error, as we can't call r.findArea().
  2. Option 1 is true, because we can't create object for interfaces, so with out creating object we can't call instance methods. So it gives compile time error in check() function.
  3. It won't give any compile time error, and there is no possibility of run time exception also.
  4. It won't give any compile time error, but there is possibility of run time exception if we don't pass an object of a concrete class which implements RectInterface Directly or indirectly.
Show Answer
Share Facebook gmail Twitter
19
Is there any problem with below Java code?
interface RectInterface{
        public void findArea();
}
class Test{
        public void check(RectInterface r){
                r.findArea();
        }
}
public class InterfaceDemo3 {
        public static void main(String[] args) {
                RectInterface ri = null;
                Test t = new Test();
                t.check(ri);
        }
}
  1. it gives compile time error in t.check(ri); in main method, because ri is null, which we can't pass to check().
  2. it gives compile time error in main method in the line, RectInterface ri=null; because you can't create object for interface.
  3. it gives run time exception, NullPointerException.
  4. both option 2 and 3 are true.
Show Answer
Share Facebook gmail Twitter
20
Why multiple inheritance of interfaces are possible? and why not multiple inheritance of classes?
  1. Because interfaces will contain only function signatures. If two interfaces have same function signatures, and when you try to derive both the interfaces into a derived interface, then one function declaration will be inherited as both are same. (Since it is the function body which matters, so it compiler don't care about which declaration to take).
  2. A class can't have two definitions with same function signature and prototype, as compiler will get confused, which one to take. Similarly when you have two base classes which have same function in those classes with same signature and prototype, then when you derive those classes into derived class, compiler will be confused which definition to take, as the signature is same. That's why multiple inheritance of classes is not possible directly.
  3. both options are true
  4. both are wrong
Show Answer
Share Facebook gmail Twitter
Read more papers :