WorksheetsUNIT 2
Total questions: 25
Worksheet time: 13mins
Which concept allows two methods to have the same name but different parameters?
a) Overriding
b) Overloading
c) Dynamic dispatch
d) Encapsulation
In Java, objects are passed to methods:
a) By reference
b) By copy of the object
c) By value
d) By pointer
A static nested class can access:
a) Only static members of outer class
b) All members of outer class
c) Only private members
d) Nothing from outer class
Which keyword is used for inheritance?
a) implements
b) extend
c) inherit
d) None of the above
Default package access modifier (no keyword) means:
a) private
b) protected
c) package-private
d) public
If a method returns an object, what is returned?
a) A new copy of the object
b) The object’s reference
c) Object memory address
d) All of the above
When overriding a method:
a) Return type must always be exactly the same
b) Static methods can be overridden
c) Return type can be covariant
d) Return type can be anything
The super() call in a constructor must be:
a) Last statement
b) First statement
c) Optional anywhere
d) Not allowed
In runtime polymorphism, which decides which method is executed?
a) Object type
b) Reference type
c) Compiler
d) JVM garbage collector
Which is true?
a) Abstract class cannot have constructors
b) Abstract class can have both abstract and concrete methods
c) Abstract class cannot have variables
d) Abstract class must only contain abstract methods
If two methods differ only in return type but have the same parameters:
a) It is overloading
b) It is overriding
c) Compile error
d) Runtime error
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) 5
b) 10
c) Error
d) Null
Can an abstract class have final methods?
a) No, because final prevents overriding
b) Yes, to prevent subclasses from overriding specific methods
c) No, abstract class must only have abstract methods
d) Only if declared private
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
b) B
c) Both
d) Error
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) 20
b) Error
c) 10 20
d) NULL
If a class doesn’t override a default method from interface:
a) Compile error
b) Method from interface is used
c) JVM crashes
d) Must be abstract class
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
b) B
c) Compile error
d) Runtime error
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) Hello A
b) Hi B
c) Hello A \n Hi B
d) Compile error
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) Outer
b) Inner
c) Compile error
d) Runtime error
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) 5
b) 10
c) 15
d) Compile error
If a class is declared final:
a) It cannot be extended
b) It cannot be instantiated
c) It cannot contain variables
d) It cannot contain constructors
An anonymous inner class:
a) Can extend exactly one class or implement exactly one interface
b) Can extend multiple classes
c) Can implement multiple interfaces
d) Can have constructors
If a class doesn’t override a default method from interface:
a) Compile error
b) Method from interface is used
c) JVM crashes
d) Must be abstract class
If two interfaces define same method signature and a class implements both:
a) Compile error
b) Must override method once in class
c) Must override twice
d) JVM chooses automatically
What happens if a method returns an object that is already destroyed by garbage collector?
a) Runtime error
b) NullPointerException
c) Impossible — GC never destroys objects still referenced
d) Object resurrected
