Essential Java Basics Quiz Quiz

Explore fundamental Java interview concepts with these easy questions covering data types, OOP principles, arrays, exceptions, and more.

  1. Types of Data Types in Java

    Which of the following is a primitive data type in Java?

    1. int
    2. Array
    3. Interface
    4. String

    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.

  2. Understanding Wrapper Classes

    What is the purpose of wrapper classes in Java?

    1. To convert objects into primitive types
    2. To create new primitive types
    3. To enforce encapsulation
    4. To provide an object representation of primitive data types

    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.

  3. Array Characteristics in Java

    How are arrays sized in Java?

    1. Arrays are fixed-size once created
    2. Arrays automatically double in size
    3. Arrays are always dynamic
    4. Arrays can shrink but not expand

    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.

  4. Java Virtual Machine (JVM)

    What role does the JVM play in Java programming?

    1. It designs the user interface
    2. It stores Java documentation
    3. It executes Java bytecode on different operating systems
    4. It compiles Java source code to bytecode

    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.

  5. Platform Independence in Java

    How does Java achieve platform independence?

    1. Through the use of interfaces
    2. By not allowing inheritance
    3. By compiling code into bytecode interpreted by the JVM
    4. By using only primitive types

    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.

  6. Local vs Global Variables

    Where is a local variable declared in Java?

    1. In a separate file
    2. Inside a method or block
    3. Outside any class
    4. Inside an interface

    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.

  7. Object-Oriented Principles: Encapsulation

    Which concept bundles data and methods in a class, restricting outside access?

    1. Inheritance
    2. Abstraction
    3. Polymorphism
    4. Encapsulation

    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.

  8. Function Overloading Example

    What distinguishes overloaded methods in Java?

    1. The method's return type
    2. Access modifiers
    3. Different method names
    4. Different number or type of parameters

    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.

  9. Static Main Method

    Why must the main method in Java be static?

    1. To provide thread safety
    2. To prevent it from being overridden
    3. To allow program execution without class instantiation
    4. To enable multiple main methods

    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.

  10. Usage of the super Keyword

    How is the 'super' keyword used in Java?

    1. To enforce inheritance restriction
    2. To access parent class methods or variables
    3. To declare a static method
    4. To create a try-catch block

    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.