Java MCQ

Introduction to Java, Basic Syntax and Structure

1

What are the key features of Java?

Correct Answer: B) Platform independence through bytecode

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.

2

What are Java's primitive data types?

Correct Answer: B) int, float, double, boolean, char, byte, short, long

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.

3

How is Java different from other programming languages?

Correct Answer: B) It uses braces for code blocks and is strongly typed

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.

4

What is the main() method in Java?

Correct Answer: B) The entry point for Java applications

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).

5

What is the difference between JDK, JRE, and JVM?

Correct Answer: B) JVM executes bytecode, JRE provides runtime environment, JDK includes development tools

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.

6

What is the purpose of the 'static' keyword in Java?

Correct Answer: B) To indicate that a member belongs to the class rather than instances

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.

7

What is method overloading in Java?

Correct Answer: B) Creating multiple methods with same name but different parameters

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.

8

What is the difference between == and .equals() in Java?

Correct Answer: B) == compares references, .equals() compares content for objects

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.

9

What is inheritance in Java?

Correct Answer: B) A mechanism where a class acquires properties of another class

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).

10

What is polymorphism in Java?

Correct Answer: B) The ability of an object to take many forms

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.

11

What are access modifiers in Java?

Correct Answer: B) Keywords that control the visibility and accessibility of classes, methods, and variables

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.

12

What is the 'final' keyword used for in Java?

Correct Answer: B) To make variables, methods, or classes unmodifiable

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.

13

What are constructors in Java?

Correct Answer: B) Special methods used to initialize objects

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.

14

What is method overriding in Java?

Correct Answer: B) Providing a specific implementation of a method in subclass that exists in parent class

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.

15

What is the 'this' keyword in Java?

Correct Answer: B) Reference to the current object instance

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.

Java Multiple Choice Questions Next