1Which keyword is used in Java to establish an inheritance relationship between two classes?
A.implement
B.extends
C.inherits
D.super
Correct Answer: extends
Explanation:The extends keyword is used to create a subclass that inherits attributes and methods from a superclass.
Incorrect! Try again.
2In Java, a class can directly inherit from how many superclasses?
A.Only one
B.Two
C.Unlimited
D.Depends on the JVM
Correct Answer: Only one
Explanation:Java supports single inheritance for classes, meaning a class can extend only one superclass to avoid the diamond problem complexity.
Incorrect! Try again.
3What is the root class of the Java Class Hierarchy?
A.java.lang.Main
B.java.lang.System
C.java.lang.Class
D.java.lang.Object
Correct Answer: java.lang.Object
Explanation:Every class in Java is a descendant, direct or indirect, of the java.lang.Object class.
Incorrect! Try again.
4Which of the following members of a superclass are not inherited by a subclass?
A.Public methods
B.Protected variables
C.Constructors
D.Default (package-private) members in the same package
Correct Answer: Constructors
Explanation:Constructors are not inherited by subclasses, though they can be invoked using the super() keyword.
Incorrect! Try again.
5If a method in a subclass has the same name, return type, and parameters as a method in its superclass, what is this called?
A.Method Overloading
B.Method Overriding
C.Dynamic Binding
D.Static Resolution
Correct Answer: Method Overriding
Explanation:Method Overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
Incorrect! Try again.
6Which annotation helps ensure that a method is correctly overriding a parent method?
A.@Overload
B.@Inherit
C.@Override
D.@Super
Correct Answer: @Override
Explanation:The @Override annotation instructs the compiler to check that the method actually overrides a method in the superclass, generating an error if it does not.
Incorrect! Try again.
7When overriding a method, the access modifier of the overriding method in the subclass:
A.Must be the same as the superclass method
B.Can be more restrictive
C.Cannot be more restrictive
D.Must be private
Correct Answer: Cannot be more restrictive
Explanation:The overriding method cannot assign a weaker access privilege. For example, if the parent is protected, the child must be protected or public.
Incorrect! Try again.
8What is the purpose of the super keyword in Java?
A.To declare a superclass
B.To refer to the immediate parent class object
C.To call a static method
D.To create an instance of the current class
Correct Answer: To refer to the immediate parent class object
Explanation:super is a reference variable used to refer to the immediate parent class object, accessing overridden methods or constructors.
Incorrect! Try again.
9Given the following code: class A { A() { System.out.print("A"); } } class B extends A { B() { System.out.print("B"); } }
What is printed when new B() is called?
A.B
B.BA
C.AB
D.Error
Correct Answer: AB
Explanation:When a subclass constructor is called, the superclass constructor is invoked first (implicitly or explicitly). Thus, 'A' is printed before 'B'.
Incorrect! Try again.
10Where must the call to super() be placed within a subclass constructor?
A.Anywhere inside the constructor
B.Only inside a try-catch block
C.As the very first statement
D.As the very last statement
Correct Answer: As the very first statement
Explanation:The call to super() must be the first statement in a constructor to ensure the parent class is initialized before the child class.
Incorrect! Try again.
11What is the default implementation of the toString() method in the Object class?
A.It returns null
B.It returns the class name followed by the object's hash code in hex
C.It returns a JSON representation of the object
D.It returns the memory address in binary
Correct Answer: It returns the class name followed by the object's hash code in hex
Explanation:The default toString() returns a string consisting of the class name, the @ symbol, and the unsigned hexadecimal representation of the hash code (e.g., ClassName@15db9742).
Incorrect! Try again.
12What is the contract for the equals() method in the Object class by default?
A.It compares the values of all fields
B.It compares the lengths of the objects
C.It behaves exactly like the == operator (reference equality)
D.It always returns true
Correct Answer: It behaves exactly like the == operator (reference equality)
Explanation:The default implementation of equals() in the Object class checks if two references point to the exact same object in memory (this == obj).
Incorrect! Try again.
13Which of the following statements about the final keyword is TRUE?
A.A final method can be overridden
B.A final class cannot be subclassed
C.A final variable can be re-assigned
D.A final class can be abstract
Correct Answer: A final class cannot be subclassed
Explanation:Declaring a class as final prevents inheritance. No other class can extend a final class.
Incorrect! Try again.
14If a variable is declared as static final, it is effectively a:
A.Global variable
B.Constant
C.Mutable static reference
D.Thread-local variable
Correct Answer: Constant
Explanation:A variable that is both static (belongs to class) and final (value cannot change) acts as a constant in Java.
Incorrect! Try again.
15What happens if you try to override a method marked as final?
A.Runtime Exception
B.Compile-time Error
C.Warning
D.The method is hidden
Correct Answer: Compile-time Error
Explanation:A final method cannot be overridden by subclasses. Attempting to do so results in a compilation error.
Incorrect! Try again.
16The instanceof operator returns true if:
A.The object is an instance of the specified class or its subclasses
B.The object is null
C.The object is exactly the specified class, not a subclass
D.The object has a main method
Correct Answer: The object is an instance of the specified class or its subclasses
Explanation:obj instanceof ClassName evaluates to true if obj is an instance of ClassName or any class that inherits from ClassName (or implements the interface).
Incorrect! Try again.
17Evaluate: null instanceof Object
A.true
B.false
C.NullPointerException
D.Compiler Error
Correct Answer: false
Explanation:The instanceof operator returns false if the left-hand operand is null, regardless of the class type on the right.
Incorrect! Try again.
18Which of the following is TRUE regarding an abstract class?
A.It must have at least one abstract method
B.It cannot be instantiated
C.It cannot contain concrete methods
D.It cannot have constructors
Correct Answer: It cannot be instantiated
Explanation:An abstract class cannot be used to create objects directly using the new keyword. It must be subclassed.
Incorrect! Try again.
19Can an abstract class contain a constructor?
A.No, never
B.Yes, but it must be private
C.Yes, it can have constructors
D.Only if the class is static
Correct Answer: Yes, it can have constructors
Explanation:Abstract classes can have constructors, which are called when a concrete subclass is instantiated (usually via super()).
Incorrect! Try again.
20If a concrete class inherits from an abstract class, what must it do?
A.Define a main method
B.Override all constructors
C.Implement all inherited abstract methods
D.Declare itself as final
Correct Answer: Implement all inherited abstract methods
Explanation:A concrete (non-abstract) subclass must provide implementations for all abstract methods inherited from the abstract superclass.
Incorrect! Try again.
21Which keyword is used to declare an interface?
A.class
B.interface
C.abstract
D.implements
Correct Answer: interface
Explanation:The interface keyword is used to define an interface in Java.
Incorrect! Try again.
22Variables declared inside an interface are implicitly:
A.private protected
B.public static final
C.default
D.protected abstract
Correct Answer: public static final
Explanation:All fields (variables) in an interface are implicitly public, static, and final (constants).
Incorrect! Try again.
23Prior to Java 8, methods in an interface were implicitly:
A.private
B.protected
C.public abstract
D.static
Correct Answer: public abstract
Explanation:Before Java 8 features (default/static methods), all methods in an interface were implicitly public and abstract.
Incorrect! Try again.
24Java 8 introduced which type of method in interfaces to allow adding new functionality without breaking existing implementations?
A.final methods
B.default methods
C.protected methods
D.native methods
Correct Answer: default methods
Explanation:default methods allow interfaces to provide a method implementation, ensuring backward compatibility with classes that implement the interface.
Incorrect! Try again.
25How do you define a static method within an interface?
A.static void method() { ... }
B.public abstract static void method();
C.default static void method() { ... }
D.void method() static;
Correct Answer: static void method() { ... }
Explanation:Since Java 8, interfaces can contain static methods with bodies, defined using the static keyword.
Incorrect! Try again.
26Can a class implement multiple interfaces?
A.No
B.Yes
C.Only if they have no methods
D.Only if the class is abstract
Correct Answer: Yes
Explanation:Java supports multiple inheritance of type through interfaces. A class can implement multiple interfaces using a comma-separated list.
Incorrect! Try again.
27If class C implements Interface I, which keyword is used?
A.extends
B.implements
C.uses
D.instances
Correct Answer: implements
Explanation:The implements keyword is used by a class to inherit from an interface.
Incorrect! Try again.
28Given: interface A { default void show() { System.out.println("A"); } } and class B implements A {}. What happens when calling new B().show()?
A.Compiler Error
B.Prints "A"
C.Runtime Error
D.Nothing happens
Correct Answer: Prints "A"
Explanation:Class B inherits the default implementation of show() from Interface A.
Incorrect! Try again.
29What is the Diamond Problem in the context of Java interfaces?
A.Inheriting two interfaces with the same variable name
B.Inheriting two interfaces with default methods of the same signature
C.Using diamond operator <> improperly
D.Implementing an interface that extends itself
Correct Answer: Inheriting two interfaces with default methods of the same signature
Explanation:If a class implements two interfaces that both have a default method with the same signature, the compiler throws an error unless the class overrides the method to resolve the ambiguity.
Incorrect! Try again.
30How do you call a default method of an interface MyInterface from an overriding method in the implementing class?
A.super.method()
B.MyInterface.method()
C.MyInterface.super.method()
D.default.method()
Correct Answer: MyInterface.super.method()
Explanation:The syntax InterfaceName.super.methodName() is used to invoke a specific interface's default method implementation.
Incorrect! Try again.
31Dynamic Method Dispatch is the mechanism for:
A.Compile-time polymorphism
B.Runtime polymorphism
C.Encapsulation
D.Exception handling
Correct Answer: Runtime polymorphism
Explanation:Dynamic Method Dispatch is the process where a call to an overridden method is resolved at runtime rather than compile-time.
Incorrect! Try again.
32What is Upcasting?
A.Converting a superclass reference to a subclass reference
B.Converting a subclass reference to a superclass reference
C.Converting a primitive to an object
D.Converting an object to a primitive
Correct Answer: Converting a subclass reference to a superclass reference
Explanation:Upcasting is casting a child object to a parent class reference (e.g., Parent p = new Child();). It is done implicitly.
Incorrect! Try again.
33What is required to perform Downcasting safely?
A.The static keyword
B.Explicit casting and usually an instanceof check
C.No special syntax
D.The super keyword
Correct Answer: Explicit casting and usually an instanceof check
Explanation:Downcasting (Parent to Child) requires an explicit cast (Child) parentRef and often creates a ClassCastException if the object is not actually an instance of the child class.
Incorrect! Try again.
34Can an interface extend another interface?
A.No
B.Yes, using implements
C.Yes, using extends
D.Yes, using super
Correct Answer: Yes, using extends
Explanation:An interface can inherit from another interface using the extends keyword.
Incorrect! Try again.
35Which modifier is not allowed for a top-level class?
A.public
B.abstract
C.final
D.private
Correct Answer: private
Explanation:Top-level classes can be public or package-private (default). They cannot be private or protected.
Incorrect! Try again.
36Can you create an object of an interface?
A.Yes, using new Interface()
B.No, interfaces cannot be instantiated
C.Yes, if it has default methods
D.Yes, if it has static methods
Correct Answer: No, interfaces cannot be instantiated
Explanation:Like abstract classes, interfaces cannot be instantiated directly using new.
Incorrect! Try again.
37If a method signature in a superclass is public void test(int x), which of the following is a valid override in the subclass?
A.public int test(int x)
B.protected void test(int x)
C.public void test(int x)
D.public void test(double x)
Correct Answer: public void test(int x)
Explanation:Method overriding requires the same name, same parameters, same return type (or covariant), and same or wider access modifier. Option D is overloading, not overriding.
Incorrect! Try again.
38Covariant return types allow an overriding method to:
A.Return a primitive type instead of an object
B.Return a superclass of the original return type
C.Return a subclass of the original return type
D.Return void instead of a type
Correct Answer: Return a subclass of the original return type
Explanation:Since Java 5, an overriding method can return a subtype of the type returned by the overridden method.
Incorrect! Try again.
39Which method signature represents the equals method in Object class correctly?
A.public boolean equals(Object obj)
B.public boolean equals(String str)
C.public int equals(Object obj)
D.public static boolean equals(Object obj)
Correct Answer: public boolean equals(Object obj)
Explanation:The correct signature accepts a parameter of type Object and returns a boolean.
Incorrect! Try again.
40Using final on a method parameter means:
A.The method cannot be overridden
B.The argument value cannot be changed within the method
C.The argument must be a constant
D.The method returns a constant
Correct Answer: The argument value cannot be changed within the method
Explanation:If a parameter is final, you cannot assign a new value to that parameter variable inside the method body.
Incorrect! Try again.
41Consider: AbstractClass ref = new ConcreteClass(); This is an example of:
A.Polymorphism
B.Encapsulation
C.Strict coupling
D.Static binding
Correct Answer: Polymorphism
Explanation:Using a parent (abstract) reference to hold a child (concrete) object allows for runtime polymorphism.
Incorrect! Try again.
42Can a static method be overridden?
A.Yes, always
B.No, it is method hiding
C.Yes, if the subclass is static
D.No, it causes a compile error
Correct Answer: No, it is method hiding
Explanation:Static methods are bound at compile time. Defining a static method in a subclass with the same signature 'hides' the parent method, but does not override it conceptually for polymorphism.
Incorrect! Try again.
43What is a 'marker interface'?
A.An interface with only default methods
B.An interface with no fields or methods
C.An interface used to mark syntax errors
D.An interface with exactly one method
Correct Answer: An interface with no fields or methods
Explanation:A marker interface (like Serializable or Cloneable) is an empty interface used to tag a class so that it can be treated differently by the JVM or frameworks.
Incorrect! Try again.
44If a class Dog extends Animal, which statement creates a polymorphic array?
A.Dog[] dogs = new Dog[5];
B.Animal[] animals = new Dog[5];
C.Animal[] animals = new Animal[5]; (and filling it with Dogs)
D.Dog[] dogs = new Animal[5];
Correct Answer: Animal[] animals = new Animal[5]; (and filling it with Dogs)
Explanation:An array of the superclass type (Animal[]) can hold instances of the subclass (Dog), allowing for polymorphic behavior when iterating through the array.
Incorrect! Try again.
45When does Java resolve calls to private methods?
A.At Runtime
B.At Compile time
C.During class loading
D.When the object is finalized
Correct Answer: At Compile time
Explanation:Private methods cannot be overridden, so the compiler knows exactly which method to call. This is static binding.
Incorrect! Try again.
46What is the result of in a string concatenation if one operand is a String?
A.30
B.1020
C.Error
D.Depends on locale
Correct Answer: 1020
Explanation:In Java, if one operand is a String, the + operator performs string concatenation. (Note: Question uses LaTeX for numbers to satisfy format requirement).
Incorrect! Try again.
47Can an abstract class extend a concrete class?
A.Yes
B.No
C.Only if the concrete class is final
D.Only if in the same package
Correct Answer: Yes
Explanation:There is no rule preventing an abstract class from extending a concrete class.
Incorrect! Try again.
48Which method is used to return the runtime class of an object?
A.getObject()
B.getClass()
C.instanceOf()
D.getType()
Correct Answer: getClass()
Explanation:The getClass() method, inherited from Object, returns the runtime class of the object.
Incorrect! Try again.
49If Interface A has a static method foo(), can Class B (which implements A) call it via B.foo()?
A.Yes
B.No
C.Only if B overrides it
D.Only if foo is public
Correct Answer: No
Explanation:Static methods in interfaces are not inherited. They must be called using the interface name: A.foo().
Incorrect! Try again.
50If a class is declared final, which of the following is implicitly true?
A.All its fields are final
B.All its methods are final
C.It is abstract
D.It is static
Correct Answer: All its methods are final
Explanation:Since a final class cannot be subclassed, none of its methods can ever be overridden. Therefore, they are effectively final.
Incorrect! Try again.
Give Feedback
Help us improve by sharing your thoughts or reporting issues.