Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

UNIT 2

Total questions: 25

Worksheet time: 13mins

Name
Class
Date
1.

Which concept allows two methods to have the same name but different parameters?

a)

a) Overriding

b)

b) Overloading

c)

c) Dynamic dispatch

d)

d) Encapsulation

2.

In Java, objects are passed to methods:

a)

a) By reference

b)

b) By copy of the object

c)

c) By value

d)

d) By pointer

3.

A static nested class can access:

a)

a) Only static members of outer class

b)

b) All members of outer class

c)

c) Only private members

d)

d) Nothing from outer class

4.

Which keyword is used for inheritance?

a)

a) implements

b)

b) extend

c)

c) inherit

d)

d) None of the above

5.

Default package access modifier (no keyword) means:

a)

a) private

b)

b) protected

c)

c) package-private

d)

d) public

6.

If a method returns an object, what is returned?

a)

a) A new copy of the object

b)

b) The object’s reference

c)

c) Object memory address

d)

d) All of the above

7.

When overriding a method:

a)

a) Return type must always be exactly the same

b)

b) Static methods can be overridden

c)

c) Return type can be covariant

d)

d) Return type can be anything

8.

The super() call in a constructor must be:

a)

a) Last statement

b)

b) First statement

c)

c) Optional anywhere

d)

d) Not allowed

9.

In runtime polymorphism, which decides which method is executed?

a)

a) Object type

b)

b) Reference type

c)

c) Compiler

d)

d) JVM garbage collector

10.

Which is true?

a)

a) Abstract class cannot have constructors

b)

b) Abstract class can have both abstract and concrete methods

c)

c) Abstract class cannot have variables

d)

d) Abstract class must only contain abstract methods

11.

If two methods differ only in return type but have the same parameters:

a)

a) It is overloading

b)

b) It is overriding

c)

c) Compile error

d)

d) Runtime error

12.

class A { int x=5; }

void change(A a)

{ a.x=10;

}

public static void main(String[] args)

{

A obj = new A();

change(obj);

System.out.println(obj.x);

}

OUTPUT?

a)

a) 5

b)

b) 10

c)

c) Error

d)

d) Null

13.

Can an abstract class have final methods?

a)

a) No, because final prevents overriding

b)

b) Yes, to prevent subclasses from overriding specific methods

c)

c) No, abstract class must only have abstract methods

d)

d) Only if declared private

14.

class A{

static void test()

{

System.out.println("A");

}

}

class B extends A {

static void test(){

System.out.println("B");

}

}

public class Test {

public static void main(String[] args){

A obj=new B();

obj.test(); }

}

OUTPUT?

a)

a) A

b)

b) B

c)

c) Both

d)

d) Error

15.

class A {

int x=10;

}

class B extends A {

int x=20;

void print(){

System.out.println(super.x); }

}

public class Test {

public static void main(String[] args){

new B().print(); }

}

OUTPUT?

a)

a) 20

b)

b) Error

c)

c) 10 20

d)

d) NULL

16.

If a class doesn’t override a default method from interface:

a)

a) Compile error

b)

b) Method from interface is used

c)

c) JVM crashes

d)

d) Must be abstract class

17.

class A {

static void test() {

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

}

class B extends A {

static void test() {

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

}

public class Test {

public static void main(String[] args) {

A obj = new B();

obj.test();

}

}

OUTPUT?

a)

a) A

b)

b) B

c)

c) Compile error

d)

d) Runtime error

18.

abstract class A {

void hello() {

System.out.println("Hello A"); }

}

interface B { void hi(); }

class C extends A implements B {

public void hi() {

System.out.println("Hi B"); }

}

public class Test {

public static void main(String[] args) {

C obj = new C();

obj.hello(); obj.hi();

}

}

OUTPUT?

a)

a) Hello A

b)

b) Hi B

c)

c) Hello A \n Hi B

d)

d) Compile error

19.

class Outer {

static class Inner {

void msg() {

System.out.println("Inner"); }

}

}

public class Test {

public static void main(String[] args) {

Outer.Inner obj = new Outer.Inner();

obj.msg();

}

}

OUTPUT?

a)

a) Outer

b)

b) Inner

c)

c) Compile error

d)

d) Runtime error

20.

class Test {

int x;

Test(int x) { this.x = x; }

Test update() {

return new Test(x + 10);

}

public static void main(String[] args) {

Test t1 = new Test(5);

Test t2 = t1.update();

System.out.println(t2.x);

}

}

OUTPUT?

a)

a) 5

b)

b) 10

c)

c) 15

d)

d) Compile error

21.

If a class is declared final:

a)

a) It cannot be extended

b)

b) It cannot be instantiated

c)

c) It cannot contain variables

d)

d) It cannot contain constructors

22.

An anonymous inner class:

a)

a) Can extend exactly one class or implement exactly one interface

b)

b) Can extend multiple classes

c)

c) Can implement multiple interfaces

d)

d) Can have constructors

23.

If a class doesn’t override a default method from interface:

a)

a) Compile error

b)

b) Method from interface is used

c)

c) JVM crashes

d)

d) Must be abstract class

24.

If two interfaces define same method signature and a class implements both:

a)

a) Compile error

b)

b) Must override method once in class

c)

c) Must override twice

d)

d) JVM chooses automatically

25.

What happens if a method returns an object that is already destroyed by garbage collector?

a)

a) Runtime error

b)

b) NullPointerException

c)

c) Impossible — GC never destroys objects still referenced

d)

d) Object resurrected