Explore fundamental Java interview concepts with these easy questions covering data types, OOP principles, arrays, exceptions, and more.
Which of the following is a primitive data type in Java?
Explanation: The 'int' type is a primitive data type in Java, used to store integer values. 'String', 'Array', and 'Interface' are non-primitive data types derived from primitives and provide additional functionalities.
What is the purpose of wrapper classes in Java?
Explanation: Wrapper classes allow primitive data types to be represented as objects, which enables their use in collections and offers utility methods. They do not convert objects into primitives, create new types, or enforce encapsulation.
How are arrays sized in Java?
Explanation: Once a Java array is created, its size cannot be changed, making it fixed-size. 'ArrayList' can be used for dynamic resizing but is not the same as an array. Arrays do not automatically resize or shrink.
What role does the JVM play in Java programming?
Explanation: The JVM executes Java bytecode by translating it to machine code for the host operating system, ensuring platform independence. It does not design interfaces, compile source code (the compiler does this), or store documentation.
How does Java achieve platform independence?
Explanation: Java code is compiled to bytecode, which the JVM interprets for any operating system, making it platform-independent. Primitive types, interfaces, and inheritance are unrelated to platform independence.
Where is a local variable declared in Java?
Explanation: Local variables are declared within methods or blocks and can only be used inside their scope. Declaring them outside a class, inside an interface, or a separate file is incorrect for local variables.
Which concept bundles data and methods in a class, restricting outside access?
Explanation: Encapsulation combines data and methods within a class and uses access modifiers to control access. Inheritance deals with subclasses, polymorphism enables method variations, and abstraction hides implementation details.
What distinguishes overloaded methods in Java?
Explanation: Overloaded methods share the same name but differ by parameter lists, not return types, names, or access modifiers. Java identifies which method to call based on parameters alone.
Why must the main method in Java be static?
Explanation: Being static lets the main method run before any objects are created, starting program execution. It is not about preventing overrides, having multiple mains, or ensuring thread safety.
How is the 'super' keyword used in Java?
Explanation: 'super' is used in subclasses to refer to methods or variables from the parent class. It is not for declaring static methods, exception handling, or restricting inheritance.