Interview Questions

  1. Can an object exist without a class in Java?

No, all objects are instances of a class which defines their state and behavior.

  1. What is the output of printing an object reference directly?

It prints the object's hashcode, not the value stored in the object.

  1. Can multiple classes have member variables with the same name?

Yes, variable names are scoped locally to each class. There is no conflict between classes.

  1. Are primitive types considered objects in Java?

No, primitive types like int, boolean etc. are not objects. Only non-primitive types are objects.

  1. Can a class declare methods without declaring member variables?

Yes, classes can contain only methods without member variables. Methods provide behavior only.

  1. Is it mandatory for every class in Java to extend Object class?

Yes, either explicitly or implicitly all classes inherit from java.lang.Object in Java.

  1. Can main() method in Java be declared in a class instead of public static void main(String[] args)?

Yes, main() method can be declared in a class but must be declared public static for runtime.

  1. Are nested classes considered members of the enclosing class?

Yes, nested classes are members of the enclosing class and obey the enclosing class's access modifiers.

  1. Can variables be declared directly inside a class without any methods?

Yes, member variables can be declared at the class level outside of any method, constructor or block.

  1. Can a class have methods with the same name but different arguments?

Yes, methods can be overloaded by having the same name but different method signatures.

  1. Are classes in Java passed by value or by reference?

Classes are passed by reference in Java, while primitive types are passed by value.

  1. Can member variables be declared protected or private in Java?

Yes, member variables can have any access modifier including protected and private along with public and default access.

  1. How are static variables and methods shared between objects?

Static members belong to the class and are not tied to any instance, so are shared across all instances.

  1. Can objects be created and initialized separately in Java?

Yes, declaration and assignment can be separate statements. The reference must be initialized before use.

  1. Can we access class members directly within static methods?

No, static methods cannot access instance members directly. Instance reference is required.

  1. Are nested classes within interfaces static or non-static?

Nested classes within interfaces are implicitly static and public since interfaces cannot have instance state.

  1. Can abstract classes have non-abstract methods in Java?

Yes, abstract classes can have any mix of abstract and non-abstract methods.

  1. Can inheritance be multilevel in Java?

Yes, a subclass can inherit from a superclass which inherits from its own parent class leading to multilevel inheritance.

  1. Is multiple inheritance allowed between classes in Java?

No, each class can only directly inherit from one superclass to avoid complexity and ambiguity.

  1. Can an abstract class extend a concrete class in Java?

Yes, abstract classes can extend concrete classes while inheriting implementation and overriding as needed.

Last updated