Saturday, August 3, 2024

Interface MCQs

 Interface MCQs

What is an interface in Java?


A. A reference type that can contain abstract methods and constants

B. A class that can be instantiated and contains only abstract methods

C. A type that contains only static methods and final variables

D. A type that contains only concrete methods

Which keyword is used to declare an interface in Java?


A. interface

B. abstract

C. class

D. implements

Can an interface extend another interface?


A. Yes, an interface can extend another interface

B. No, an interface cannot extend another interface

C. Yes, but only if the extending interface is abstract

D. No, an interface can only implement another interface

What will be the output of the following code?


java

Copy code

interface Animal {

    void makeSound();

}


class Dog implements Animal {

    public void makeSound() {

        System.out.println("Woof");

    }

}


public class Test {

    public static void main(String[] args) {

        Animal a = new Dog();

        a.makeSound();

    }

}

A. Woof

B. Animal sound

C. Compilation error

D. Woof Animal sound

Which of the following statements is true about interface methods?


A. Interface methods are abstract by default and cannot have a body (prior to Java 8)

B. Interface methods must be static

C. Interface methods must be private

D. Interface methods can have any access modifier

Can an interface have constructors in Java?


A. No, interfaces cannot have constructors

B. Yes, interfaces can have constructors

C. Yes, but only if they are static

D. Yes, but only if they are private

What is the output of the following code?


java

Copy code

interface Shape {

    void draw();

    

    default void color() {

        System.out.println("Default color");

    }

}


class Circle implements Shape {

    public void draw() {

        System.out.println("Drawing Circle");

    }

}


public class Main {

    public static void main(String[] args) {

        Shape s = new Circle();

        s.draw();

        s.color();

    }

}

A. Drawing Circle Default color

B. Default color Drawing Circle

C. Drawing Circle

D. Compilation error

Can an interface extend multiple interfaces?


A. Yes, an interface can extend multiple interfaces

B. No, an interface cannot extend multiple interfaces

C. Yes, but only if the interfaces have the same methods

D. No, an interface can only extend one interface

Which of the following is not allowed in an interface?


A. Instance variables

B. Static final variables

C. Abstract methods

D. Default methods

What will be the result of the following code?


java

Copy code

interface Animal {

    void eat();

    

    default void sleep() {

        System.out.println("Sleeping");

    }

}


class Cat implements Animal {

    public void eat() {

        System.out.println("Cat eating");

    }

}


public class Main {

    public static void main(String[] args) {

        Animal a = new Cat();

        a.eat();

        a.sleep();

    }

}

A. Cat eating Sleeping

B. Sleeping Cat eating

C. Cat eating

D. Compilation error

What is the result of the following code?


java

Copy code

interface A {

    void methodA();

}


interface B {

    void methodB();

}


class C implements A, B {

    public void methodA() {

        System.out.println("Method A");

    }

    

    public void methodB() {

        System.out.println("Method B");

    }

}


public class Main {

    public static void main(String[] args) {

        C c = new C();

        c.methodA();

        c.methodB();

    }

}

A. Method A Method B

B. Method B Method A

C. Method A

D. Method B

Which of the following can be included in an interface starting from Java 8?


A. Static methods

B. Constructors

C. Instance fields

D. Private methods

What is the output of the following code?


java

Copy code

interface Vehicle {

    default void start() {

        System.out.println("Vehicle started");

    }

}


class Bike implements Vehicle {

    public void start() {

        System.out.println("Bike started");

    }

}


public class Main {

    public static void main(String[] args) {

        Vehicle v = new Bike();

        v.start();

    }

}

A. Vehicle started

B. Bike started

C. Compilation error

D. Bike started Vehicle started

Can an interface implement another interface?


A. No, an interface cannot implement another interface

B. Yes, but only if the second interface is abstract

C. Yes, but the interface must not have any methods

D. Yes, an interface can extend another interface but not implement it

What is the output of the following code?


java

Copy code

interface A {

    void show();

    

    default void defaultMethod() {

        System.out.println("Default Method in A");

    }

}


class B implements A {

    public void show() {

        System.out.println("Show in B");

    }

    

    public void defaultMethod() {

        System.out.println("Overridden Default Method in B");

    }

}


public class Main {

    public static void main(String[] args) {

        A a = new B();

        a.show();

        a.defaultMethod();

    }

}

A. Show in B Overridden Default Method in B

B. Show in B Default Method in A

C. Overridden Default Method in B Show in B

D. Compilation error

Which of the following cannot be used to define an interface method?


A. default

B. static

C. private

D. protected

What is the result of the following code?


java

Copy code

interface I {

    int x = 10;

}


public class Main {

    public static void main(String[] args) {

        System.out.println(I.x);

    }

}

A. 10

B. Compilation error

C. 0

D. Interface field cannot be accessed

Can an interface extend another interface that contains default methods?


A. Yes, the extending interface can inherit default methods

B. No, an interface cannot extend another interface with default methods

C. Yes, but the extending interface must override all default methods

D. No, only abstract methods can be inherited

What will be the output of the following code?


java

Copy code

interface I {

    void display();

}


abstract class A implements I {

    void show() {

        System.out.println("Show in A");

    }

}


class B extends A {

    public void display() {

        System.out.println("Display in B");

    }

}


public class Main {

    public static void main(String[] args) {

        B b = new B();

        b.display();

        b.show();

    }

}

A. Display in B Show in A

B. Show in A Display in B

C. Display in B

D. Compilation error

Which of the following is true about interface inheritance?


A. A class can implement multiple interfaces

B. A class can extend multiple interfaces

C. An interface can extend multiple classes

D. An interface can extend only one interface

Answers:

A. A reference type that can contain abstract methods and constants

A. interface

A. Yes, an interface can extend another interface

A. Woof

A. Interface methods are abstract by default and cannot have a body (prior to Java 8)

A. No, interfaces cannot have constructors

A. Drawing Circle Default color

A. Yes, an interface can extend multiple interfaces

A. Instance variables

A. Cat eating Sleeping

A. Method A Method B

A. Static methods

B. Bike started

D. Yes, an interface can extend another interface but not implement it

A. Show in B Overridden Default Method in B

D. protected

A. 10

A. Yes, the extending interface can inherit default methods

A. Display in

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...