Abstract Classes, Interfaces Java MCQ


Hello friends, in this post we are going to discuss about Abstract Classes, Interfaces Objective type questions | Abstract Classes, Interfaces Dumps | Core Java Abstract Classes, Interfaces MCQ with Answers

1.Which is a valid method signature in an interface?

a)private int getArea();

b)protected float getVol(float x);

c) public static void main(String [] args);

d)boolean setFlag(Boolean [] test []);

Ans: d

2.Which statement is true about interfaces?

a)Interfaces allow multiple implementation inheritance.

b)Interfaces can extend any number of other interfaces.

c) Members of an interface are never static.

d)Members of an interface can always be declared static.

Ans: a

3.Which statement is true about interfaces?

a)The keyword extends is used to specify that an interface inherits from another interface.

b) The keyword extends is used to specify that a class inherits from an interface.

c) The keyword implements is used to specify that an interface inherits from another interface.

d)The keyword implements is used to specify that a class inherits from another class.

Ans: a

4.Which of the field declaration is legal within the body of an interface?

a) protected static int answer = 42;

b)volatile static int answer = 42;

c)int answer = 42;

d)private final static int answer = 42;

Ans: c

5.Which declaration prevents creating a subclass of a top level class?

a) private class Javacg{}

b)abstract public class Javacg{}

c)final public class Javacg{}

d)final abstract class Javacg{}

Ans: c

6.Here is an abstract method defined in the parent:

public abstract int sumUp ( int[] arr );

Which of the following is required in a non-abstract child?

a)public abstract int sumUp ( int[] arr ) { . . . }

b)public int sumUp ( int[] arr ) { . . . }

c)public double sumUp ( int[] arr ) { . . . }

d)public int sumUp ( long[] arr ) { . . . }

Ans: b

7.Which statement is true for any concrete class implementing the java.lang.Runnable interface?

a)The class must contain an empty protected void method named run().

b)The class must contain a public void method named runnable().

c)The class definition must include the words implements Threads and contain a method called run().

d)The mandatory method must be public, with a return type of void, must be called run(), and cannot take any arguments.

Ans: d

8.Which is a valid declaration within an interface?

a)protected short stop = 23;

b)final void madness(short stop);

c)public Boolean madness(long bow);

d)static char madness(double duty);

Ans: c

9.Can an abstract class define both abstract methods and non-abstract methods ?

a)No-it must have all one or the other.

b) No-it must have all abstract methods.

c)Yes-but the child classes do not inherit the abstract methods.

d)Yes-the child classes inherit both.

Ans: d

10.Which one of the following statements is true ?

a) An abstract class can be instantiated.

b)An abstract class is implicitly final.

c)An abstract class can declare non-abstract methods.

d)An abstract class can not extend a concrete class.

Ans: c

11.What is an abstract method?

a)An abstract method is any method in an abstract class.

b)An abstract method is a method which cannot be inherited.

c)An abstract method is one without a body that is declared with the reserved word abstract.

D)An abstract method is a method in the child class that overrides a parent method.

Ans: c

12.What is an abstract class?

a)An abstract class is one without any child classes.

b)An abstract class is any parent class with more than one child class.

c)An abstract class is a class which cannot be instantiated.

d)An abstract class is another name for “base class.”

Ans: c

13.Which declaration in the below code represents a valid declaration within the interface ?

1. public interface TestInterface {

2.     volatile long value=98L;

3.     transient long amount=67L;

4.     Long calculate(long input);

5.     static Integer getValue();

6. }

Ans: Declaration at line 4.

14.Given:

1. public interface Constants {

2.     static final int SEASON_SUMMER=1;

3.     final int SEASON_SPRING=2;

4.     static int SEASON_AUTUMN=3;

5.     public static const int SEASON_WINTER=4;

6. }

What is the expected behaviour on compiling the above code?

a)Compilation error occurs at line 2.

b)Compilation error occurs at line 3.

c)Compilation error occurs at line 4.

d)Compilation error occurs at line 5.

Ans: d

15.Given the following,

1. abstract class A {

2.     abstract short m1() ;

3.     short m2() { return (short) 420; }

4. }

5.

6. abstract class B extends A {

7.     // missing code ?

8.     short m1() { return (short) 42; }

9. }

Which of the following statements is true?

a) Class B must either make an abstract declaration of method m2() or implement method m2() to allow the code to compile.

b) It is legal, but not required, for class B to either make an abstract declaration of method m2() or implement method m2() for the code to compile.

c) As long as line 8 exists, class A must declare method m1() in some way.

d) If class A was not abstract and method m1() on line 2 was implemented, the code would not compile.

Ans: b

16.Given the following,

1. interface Base {

2.     boolean m1 ();

3.     byte m2(short s);

4. }

Which of the following code fragment will compile?

a)interface Base2 implements Base {}

b) abstract class Class2 extends Base {

public boolean m1() { return true; } }

c) abstract class Class2 implements Base {

public boolean m1() { return (7 > 4); } }

d) class Class2 implements Base {

boolean m1() { return false; }

byte m2(short s) { return 42; } }

Ans: c

1. interface I1 {

2.    int process();

3. }

4. class C implements I1 {

5.     int process() {

6.         System.out.println(“process of C invoked”);

7.         return 1;

8.     }

9.     void display() {

10.        System.out.println(“display of C invoked”);

11.    }

12.}

13.public class TestC {

14.    public static void main(String… args) {

15.        C c = new C();

16.        c.process();

17.    }

18.}

What is the expected behaviour?

a)Compilation error at line 5.

b)Compilation error at line 9.

c) Runtime error occurs.

d)Prints “process of C invoked”.

Ans: d

18.Given:


1. public interface Alpha {

2. String MESSAGE = “Welcome”;

3. public void display();

4. }

To create an interface called Beta that has Alpha as its parent, which interface declaration is correct?

a)public interface Beta extends Alpha { }

b)public interface Beta implements Alpha {}

c)public interface Beta instanceOf Alpha {}

d)public interface Beta parent Alpha { }

Ans: a

19.Given:

1. abstract class AbstractClass {

2.     void setup() { }

3.     abstract int execute();

4. }

5. class EC extends AbstractClass {

6.    int execute() {

7.         System.out.println(“execute of EC invoked”);

8.         return 0;

9.     }

10.}

11.public class TestEC {

12.    public static void main(String… args) {

13.        EC ec = new EC();

14.        ec.setup();

15.        ec.execute();

16.    }

17.}

What is the expected behaviour?

Ans: Prints “execute of EC invoked”.

20.Given the code below:

interface MyInterface {

    void doSomething ( ) ;

}

class MyClass implements MyInterface {

    // xx

}

Choose the valid option that can be substituted in place of xx in the MyClass class .

a)public native void doSomething ( ) ;

b) void doSomething ( ) { /* valid code fragments */ }

c)private void doSomething ( ) { /* valid code fragments */ }

d)protected void doSomething ( ) { /* valid code fragments */ }

Ans: A

21.interface I1 {

    void draw();

}

class C implements I1 {

    xxxxxx

}

Which of the following when inserted at xxxxxx is a legal definition and implementation ?

a)void draw() { }

b)public void draw() { }

c)protected void draw() { }

d)abstract void draw() { }

Ans: B

22.Given the following,

1. interface Count {

2.     short counter = 0;

3.     void countUp();

4. }

5. public class TestCount implements Count {

6.

7.     public static void main(String [] args) {

8.         TestCount t = new TestCount();

9.          t.countUp();

10.   }

11.   public void countUp() {

12.       for (int x = 6; x>counter; x–, ++counter) {

13.          System.out.print(” ” + counter);

14.      }

15.   }

16. }

What is the result?

a)1 2 3

b)0 1 2 3

c)1 2 3 4

d)Compilation fails

Ans: D

23.Given the following,

1. interface DoMath {

2. double getArea(int rad); }

3.

4. interface MathPlus {

5. double getVol(int b, int h); }

Which code fragment inserted at lines 7 and 8 will compile?

a) class AllMath extends DoMath { double getArea(int r); }

b)interface AllMath implements MathPlus { double getVol(int x, int y); }

c)interface AllMath extends DoMath { float getAvg(int h, int l); }

d)class AllMath implements MathPlus { double getArea(int rad); }

Ans:C

24.interface I1 {}

interface I2 {}

class Base implements I1 {}

class Sub extends Base implements I2 {}

class Red {

    public static void main(String args[]) {

        Sub s1 = new Sub();I2 i2 = s1;      // 1

        I1 i1 = s1;                                    // 2

        Base base = s1;                          // 3

Sub s2 = (Sub)base;                    // 4

    }

}

A compile-time error is generated at which line?

Ans:No error will be generated.

1. public interface IDrawable {

2.     static final int SHAPE_CIRCLE=1;

3.     final int SHAPE_SQUARE=2;

4.     static int SHAPE_RECTANGLE=3;

5.     public static const int SHAPE_TRIANGLE=4;

6. }

25.What is the expected behaviour on compiling the above code?

a)  Compilation error occurs at line 2.

b) Compilation error occurs at line 3.

c) Compilation error occurs at line 4.

d) Compilation error occurs at line 5.

Ans: D

26.abstract class MyClass {

void init() { }

3.     abstract int calculate();

4. }

5. class MyImpl extends MyClass {

6.    int calculate() {

7.         System.out.println(“Invoking calculate…”);

8.         return 1;

9.     }

10.}

11.public class TestMyImpl {

12.    public static void main(String[] args) {

13.        MyImpl mi = new MyImpl();

14.        mi.init();

15.        mi.calculate();

16.    }

17.}

What is the expected behaviour?

a) Prints “Invoking calculate…”.

b) Runtime error occurs.

c) Compilation error at line 2.

d) Compilation error at line 14.

Ans:A


Leave a Reply

Your email address will not be published. Required fields are marked *