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

Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
4
java sample program on a class implementing multiple interfaces:
What is the problem with below Java program?
interface HumanInter{
        public void workWithHands();
}
interface RobotInter{
        public void workWithCpu();
}
interface MyEmp extends HumanInter, RobotInter{
        public void getSal();
}
class HumanEmp implements HumanInter{
        @Override
        public void workWithHands() {
                System.out.println("I'm human, I work with hands");
        }
}
class RobotEmp implements RobotInter{
        @Override
        public void workWithCpu() {
                System.out.println("I'm Robot, I work with Cpu");
        }
}
class MyEmployee implements HumanInter, RobotInter{
        public void disp(){
                System.out.println("I am an employee");
        }
}
  1. Compile time error in class MyEmployee, as it is not possible to implement more than one interface in a class.
  2. Compile time error in class MyEmployee because we are telling to compiler that we will implement both the interfaces HumanInter, and RobotInter - but we are not giving the function body for workWithHands() and workWithCpu().
  3. option 2 is wrong because we have already implemented those two functions in class HumanEmp and class RobotEmp. It is sufficient if we implement interfaces in one place in a given file.
  4. there is no mistake and no compile time error in the given program
Show Answer
Share Facebook gmail Twitter
5
What is the problem in below program, and how to resolve it?
interface HumanInter{
        public void workWithHands();
}
interface RobotInter{
        public void workWithCpu();
}
interface MyEmp extends HumanInter, RobotInter{
        public void getSal();
}
class HumanEmp implements HumanInter{
        @Override
        public void workWithHands() {
                System.out.println("I'm human, I work with hands");
        }
}
class RobotEmp implements RobotInter{
        @Override
        public void workWithCpu() {
                System.out.println("I'm Robot, I work with Cpu");
        }
}
class MyEmployee extends RobotEmp implements HumanInter{
        public void disp(){
                System.out.println("I am an employee");
        }
}
  1. There is no problem in this given program.
  2. There is a compile time error in class MyEmployee because we are not implementing workWithHands() function. If we implement that function, then compile time error will be resolved.
  3. There is a compile time error in class MyEmployee because we are not implementing workWithCpu() function. If we implement that function, then compile time error will be resolved.
  4. we have to do both option 2 and 3 to remove the compile time error.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
7
What is an abstract method in java?
  1. A method without any body.
  2. A method with partial implementation is called as abstract method.
  3. both options are true
  4. both are wrong.
Show Answer
Share Facebook gmail Twitter
8
Which of the below statements are false about an abstract method?
  1. A method without any body should be declared with abstract keyword explicitly, else compiler will throw an error.
  2. If  a class is having at least one abstract method, then that class should be declared as abstract class with explicit keyword abstract in the class level declaration.
  3. A method with body also can be declared as abstract method.
  4. A class can be declared as abstract even if that class doesn't have any abstract methods.
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
13
Can an abstract class contain constructor? if so, why it is allowed?
  1. Since abstract class contains some instance variables, so we have to initialize those variables in constructor. That's why it is allowed.
  2. Since we extend an abstract class into a derived class, and when we create object for derived class a super call will be made by compiler from derived class constructors, so it is OK to have a constructor in the abstract class.
  3. both option 1 and 2 are right.
  4. all options are wrong, because since we can't create object for an abstract class, there is no point in having constructor in the abstract class.
Show Answer
Share Facebook gmail Twitter
14
Is it possible to have an abstract class with all the methods as abstract methods?
  1. It is possible. there is no problem as well.
  2. It won't give any compile time error, but still it makes more sense if we can convert that into an interface as it contains only function signatures.
  3. Compiler will give error, because if an abstract class contains all abstract methods, then it is mandatory to convert it to an interface.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
15
Example for abstract methods in java:
What is the output of below Java program?
class Base{
        public abstract void disp(){
                System.out.println("This is base class");
        }
}
public class AbstractDemo1 {
        public static void main(String[] args) {
                Base b = new Base();
                b.disp();
        }
}
  1. This is base class
  2. Compile time error in class Base, you can't give function body for abstract method disp(). Either we have to remove the keyword abstract or we have to remove the function body for disp() method.
  3. Compile time error in main method, we can't create object for class Base as it is having abstract method.
  4. all are false.
Show Answer
Share Facebook gmail Twitter
16
java example for abstract method:
What is the problem with below Java program?
class Base{
        public void disp();
}
  1. There is no problem in this given program.
  2. Either we have to add body to the disp() method or we have to add abstract keyword to the method disp()
  3. we have to make the class Base as abstract class by adding abstract keyword, as disp() method doesn't have any method body.
  4. all are false.
Show Answer
Share Facebook gmail Twitter
17
Sample program for abstract classes in java:
What is the output of below Java program?
abstract class Base{
        public abstract void disp();
        public void fun(){
                System.out.println("Base fun..");
        }
}
public class AbstractDemo1 {
        public static void main(String[] args) {
                Base b = new Base();
                b.fun();
        }
}
  1. abstract class should not have any function body. here we are having fun() with function body in this abstract class. It gives compile time error. Either we have to remove function fun() body, or we have to remove abstract keyword from class Base.
  2. there is no problem in this program.
  3. Compile time error in main method, we can't create object for class Base as it is declared as abstract class.
  4. Option 3 is correct only if that class is having all abstract methods. Here we are calling function fun(), which is having function body. So there is no mistake in this program.
Show Answer
Share Facebook gmail Twitter
18
Java example for abstract class with out any abstract methods:
What is the problem with below Java program?
abstract class Test{
        public void disp(){
                System.out.println("Test disp");
        }
        public void fun(){
                System.out.println("Test fun");
        }
}
  1. If a class is having all concrete methods, then we can't declare it as abstract class. It gives compile time error.
  2. there is no problem in this program, we can make a class as abstract class even if it is having all concrete methods (non-abstract methods).
  3. option 2 is wrong, because to make a class as abstract at least one abstract method should be there.
  4. none
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
20
What is the problem with below Java program?
interface MyInterface{
        public void test();
        public void disp();
}
class MyClass implements MyInterface{
        @Override
        public void test() {
                System.out.println("MyClass test");
        }
}
  1. There is no mistake in this program. So no error.
  2. When you say implements an interface, you have to implement all functions of the interface in your class, else it gives compile time error. The only way to get out of this error is to implement disp() function also.
  3. When you say implements an interface, you have to implement all functions of the interface in your class, else it gives compile time error. There is no way to get rid of this compile time error.
  4. When you say implements an interface, you have to implement all functions of the interface in your class, else it gives compile time error. There are two ways to solve this.One way is to implement disp() function also. Second way is to make this Myclass as abstract class.
Show Answer
Share Facebook gmail Twitter
Read more papers :