Unit 3 - Practice Quiz

CSE310

1 Which keyword is used in Java to establish an inheritance relationship between two classes?

A. implement
B. extends
C. inherits
D. super

2 In Java, a class can directly inherit from how many superclasses?

A. Only one
B. Two
C. Unlimited
D. Depends on the JVM

3 What 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

4 Which 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

5 If 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

6 Which annotation helps ensure that a method is correctly overriding a parent method?

A. @Overload
B. @Inherit
C. @Override
D. @Super

7 When 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

8 What 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

9 Given 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

10 Where 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

11 What 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

12 What 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

13 Which 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

14 If a variable is declared as static final, it is effectively a:

A. Global variable
B. Constant
C. Mutable static reference
D. Thread-local variable

15 What 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

16 The 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

17 Evaluate: null instanceof Object

A. true
B. false
C. NullPointerException
D. Compiler Error

18 Which 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

19 Can 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

20 If 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

21 Which keyword is used to declare an interface?

A. class
B. interface
C. abstract
D. implements

22 Variables declared inside an interface are implicitly:

A. private protected
B. public static final
C. default
D. protected abstract

23 Prior to Java 8, methods in an interface were implicitly:

A. private
B. protected
C. public abstract
D. static

24 Java 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

25 How 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;

26 Can a class implement multiple interfaces?

A. No
B. Yes
C. Only if they have no methods
D. Only if the class is abstract

27 If class C implements Interface I, which keyword is used?

A. extends
B. implements
C. uses
D. instances

28 Given: 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

29 What 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

30 How 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()

31 Dynamic Method Dispatch is the mechanism for:

A. Compile-time polymorphism
B. Runtime polymorphism
C. Encapsulation
D. Exception handling

32 What 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

33 What 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

34 Can an interface extend another interface?

A. No
B. Yes, using implements
C. Yes, using extends
D. Yes, using super

35 Which modifier is not allowed for a top-level class?

A. public
B. abstract
C. final
D. private

36 Can 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

37 If 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)

38 Covariant 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

39 Which 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)

40 Using 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

41 Consider: AbstractClass ref = new ConcreteClass(); This is an example of:

A. Polymorphism
B. Encapsulation
C. Strict coupling
D. Static binding

42 Can 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

43 What 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

44 If 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];

45 When does Java resolve calls to private methods?

A. At Runtime
B. At Compile time
C. During class loading
D. When the object is finalized

46 What is the result of in a string concatenation if one operand is a String?

A. 30
B. 1020
C. Error
D. Depends on locale

47 Can 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

48 Which method is used to return the runtime class of an object?

A. getObject()
B. getClass()
C. instanceOf()
D. getType()

49 If 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

50 If 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