Java MCQ
Introduction to Java, Basic Syntax and Structure
What are the key features of Java?
Java's key features include platform independence (Write Once, Run Anywhere) through bytecode execution on the Java Virtual Machine (JVM), object-oriented programming, automatic memory management (garbage collection), strong type checking, and built-in support for multithreading.
What are Java's primitive data types?
Java has eight primitive data types: byte (8-bit), short (16-bit), int (32-bit), long (64-bit) for integers; float (32-bit), double (64-bit) for floating-point numbers; char (16-bit Unicode) for characters; and boolean for true/false values. These are not objects and are stored directly in memory.
How is Java different from other programming languages?
Java uses curly braces {} to define code blocks (like C/C++) rather than indentation (like Python). It's also strongly typed, meaning variable types must be declared and checked at compile time. Unlike purely interpreted languages, Java is compiled to bytecode which runs on the JVM, providing platform independence.
What is the main() method in Java?
The main() method is the entry point for Java applications. When you run a Java program, the JVM looks for this method with the exact signature: public static void main(String[] args). The method must be public (accessible to JVM), static (can be called without creating an object), void (doesn't return anything), and accept a String array as parameter (for command-line arguments).
What is the difference between JDK, JRE, and JVM?
JVM (Java Virtual Machine) is the engine that executes Java bytecode. JRE (Java Runtime Environment) includes the JVM plus libraries and other components needed to run Java applications. JDK (Java Development Kit) includes the JRE plus development tools like compiler (javac), debugger, and documentation generator. To develop Java programs, you need JDK. To run them, you only need JRE.
What is the purpose of the 'static' keyword in Java?
The 'static' keyword in Java indicates that a variable or method belongs to the class itself rather than to instances (objects) of the class. Static members can be accessed without creating an object of the class. Static variables are shared among all instances, and static methods can only access static variables and call other static methods directly.
What is method overloading in Java?
Method overloading is a feature that allows a class to have multiple methods with the same name but different parameters (different type, number, or order of parameters). The compiler determines which method to call based on the arguments passed. Overloading is an example of compile-time polymorphism.
What is the difference between == and .equals() in Java?
The == operator compares object references (memory addresses) to check if two references point to the same object. The .equals() method is meant to compare the contents or values of objects. For String objects, == checks if they are the same object in memory, while .equals() checks if they have the same sequence of characters.
What is inheritance in Java?
Inheritance is an OOP concept where a class (subclass/child class) inherits fields and methods from another class (superclass/parent class). This promotes code reusability and establishes an "is-a" relationship between classes. In Java, inheritance is achieved using the 'extends' keyword, and Java supports single inheritance for classes (a class can extend only one parent class).
What is polymorphism in Java?
Polymorphism means "many forms" and refers to the ability of an object to take on different forms. In Java, this is achieved through method overloading (compile-time polymorphism) and method overriding (runtime polymorphism). With polymorphism, a parent class reference can refer to child class objects, and the JVM determines which method to call at runtime based on the actual object type.
What are access modifiers in Java?
Java has four access modifiers: public (accessible from anywhere), protected (accessible within package and by subclasses), default (accessible only within package), and private (accessible only within the class). These modifiers help in implementing encapsulation by controlling what parts of your code are accessible to other parts of the program.
What is the 'final' keyword used for in Java?
The 'final' keyword has different meanings depending on where it's used: final variables cannot be reassigned, final methods cannot be overridden by subclasses, and final classes cannot be extended (inherited). Final variables must be initialized either at declaration or in constructor, and are often used for constants.
What are constructors in Java?
Constructors are special methods that are called when an object is created using the 'new' keyword. They have the same name as the class and no return type. Constructors initialize the object's state by setting initial values for instance variables. Java provides a default constructor if no constructor is defined, and constructors can be overloaded to provide different ways to initialize objects.
What is method overriding in Java?
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its parent class. The overridden method must have the same name, return type, and parameters as the parent method. Overriding is used for runtime polymorphism, and the @Override annotation is recommended to ensure proper overriding.
What is the 'this' keyword in Java?
The 'this' keyword refers to the current object instance. It's commonly used to: distinguish instance variables from local variables when they have the same name, call one constructor from another constructor of the same class (constructor chaining), pass the current object as a parameter to other methods, or return the current object from a method.