Java MCQ Series- 2

Here are some Java Mcq divided into different sections with explaination

Easy (1-5):

  1. What does the “public” keyword mean in a Java class declaration?
    • A) It means the class can only be accessed within its own package.
    • B) It means the class can be accessed from anywhere.
    • C) It means the class cannot have any methods.
    • D) It means the class cannot have any instance variables.
    Answer: B) It means the class can be accessed from anywhere.
    • Explanation: The “public” keyword in a Java class declaration means that the class is accessible from any other class.
  2. Which keyword is used to declare a constant in Java?
    • A) final
    • B) static
    • C) constant
    • D) const
    Answer: A) final
    • Explanation: The “final” keyword is used to declare constants in Java, and it indicates that the value of the variable cannot be changed.
  3. In Java, what is the purpose of the “this” keyword?
    • A) It refers to the current instance of the class.
    • B) It refers to the parent class.
    • C) It is used to declare a new variable.
    • D) It is used to create a new object.
    Answer: A) It refers to the current instance of the class.
    • Explanation: The “this” keyword in Java is used to refer to the current instance of the class, primarily to distinguish between instance variables and method parameters with the same name.
  4. Which of the following data types is used to store decimal numbers with double precision?
    • A) int
    • B) float
    • C) double
    • D) decimal
    Answer: C) double
    • Explanation: The “double” data type in Java is used to store decimal numbers with double precision, allowing for a larger range of values and greater precision compared to “float.”
  5. What is the result of the expression “5 + 3 * 2” in Java?
    • A) 10
    • B) 16
    • C) 11
    • D) 13
    Answer: C) 11
    • Explanation: In Java, multiplication has higher precedence than addition, so the expression “5 + 3 * 2” is evaluated as “5 + (3 * 2),” resulting in 11.

Medium (6-10):

  1. What is a constructor in Java?
    • A) A method used to destroy objects.
    • B) A method used to create objects.
    • C) A variable that cannot be modified.
    • D) A data type.
    Answer: B) A method used to create objects.
    • Explanation: A constructor in Java is a special method that is used to initialize and create objects of a class.
  2. Which Java access modifier restricts access the most?
    • A) public
    • B) protected
    • C) private
    • D) package-private (default)
    Answer: C) private
    • Explanation: The “private” access modifier in Java restricts access to within the same class, making the member (variable or method) inaccessible from other classes.
  3. What is the purpose of the “super” keyword in Java?
    • A) It refers to the superclass of a class.
    • B) It creates a new instance of a class.
    • C) It is used to call a static method.
    • D) It is a reserved keyword and has no purpose.
    Answer: A) It refers to the superclass of a class.
    • Explanation: The “super” keyword is used in Java to refer to the superclass (parent class) of the current class.
  4. What does the “static” keyword mean when applied to a method in Java?
    • A) The method cannot be overridden.
    • B) The method can only be called on an object.
    • C) The method is shared among all instances of the class.
    • D) The method is called automatically when an object is created.
    Answer: C) The method is shared among all instances of the class.
    • Explanation: When a method is declared as “static” in Java, it belongs to the class rather than to instances of the class, and it can be called using the class name.
  5. In Java, what is the purpose of the “try-catch” block?
    • A) To handle exceptions that may occur during program execution.
    • B) To create loops.
    • C) To define a new class.
    • D) To print debugging information.
    Answer: A) To handle exceptions that may occur during program execution.
    • Explanation: The “try-catch” block in Java is used to handle exceptions (runtime errors) that may occur during the execution of a program.

Hard (11-15):

  1. What is the difference between composition and inheritance in Java?
    • A) Composition is a way to reuse code by creating new classes, while inheritance is a way to create objects of existing classes.
    • B) Composition is a way to create objects of existing classes, while inheritance is a way to reuse code by creating new classes.
    • C) Composition is a way to achieve multiple inheritance, while inheritance is a way to create new classes.
    • D) Composition and inheritance are the same thing in Java.
    Answer: B) Composition is a way to create objects of existing classes, while inheritance is a way to reuse code by creating new classes.
    • Explanation: Composition involves creating objects of other classes within a class, while inheritance involves creating new classes by inheriting attributes and methods from existing classes.
  2. What is a lambda expression in Java?
    • A) A type of exception.
    • B) A way to define anonymous methods.
    • C) A reserved keyword for data types.
    • D) A type of loop.
    Answer: B) A way to define anonymous methods.
    • Explanation: A lambda expression in Java is a concise way to define anonymous methods (functions) that can be used as arguments to functions or assigned to variables.
  3. What is the purpose of the “volatile” keyword in Java?
    • A) It indicates that a variable can never be changed.
    • B) It ensures that a variable is visible to all threads and prevents caching.
    • C) It specifies that a variable is constant.
    • D) It is used to declare a method as thread-safe.
    Answer: B) It ensures that a variable is visible to all threads and prevents caching.
    • Explanation: The “volatile” keyword in Java is used to declare variables that are shared among multiple threads, ensuring that changes made to the variable are immediately visible to all threads and preventing thread-local caching.
  4. What is a checked exception in Java?
    • A) An exception that is never caught by the program.
    • B) An exception that is caught at compile-time.
    • C) An exception that is caught at runtime.
    • D) An exception that is caught by the garbage collector.
    Answer: B) An exception that is caught at compile-time.
    • Explanation: Checked exceptions in Java are exceptions that must be declared in a method’s throws clause or caught using a try-catch block at compile-time.
  5. In Java, what is the purpose of the “transient” keyword when applied to a variable?
    • A) It specifies that the variable should be ignored during serialization.
    • B) It indicates that the variable is constant.
    • C) It makes the variable public.
    • D) It declares the variable as volatile.
    Answer: A) It specifies that the variable should be ignored during serialization.
    • Explanation: The “transient” keyword is used in Java to indicate that a variable should be excluded (ignored) during the process of serialization, which is used to convert objects into a format that can be stored or transmitted.

Very Hard (16-20):

  1. What is a Java annotation, and what is its purpose?
    • A) Annotations are used to define classes in Java.
    • B) Annotations are used for documentation and have no impact on program behavior.
    • C) Annotations are used to add metadata to classes, methods, and fields to affect program behavior.
    • D) Annotations are a form of exception handling in Java.
    Answer: C) Annotations are used to add metadata to classes, methods, and fields to affect program behavior.
    • Explanation: Java annotations are used to add metadata and provide information about the program’s structure, and they can affect the behavior of the code, such as enabling or disabling certain features or optimizations.
  2. Explain the concept of “garbage collection” in Java.
    • A) Garbage collection is a process of cleaning up memory by deleting unused objects to prevent memory leaks.
    • B) Garbage collection is a process of creating new objects in memory.
    • C) Garbage collection is a way to optimize database connections.
    • D) Garbage collection is a form of error handling.
    Answer: A) Garbage collection is a process of cleaning up memory by deleting unused objects to prevent memory leaks.
    • Explanation: Garbage collection in Java is the automatic process of identifying and reclaiming memory occupied by objects that are no longer in use, thereby preventing memory leaks and improving memory management.
  3. What is the difference between “==” and “.equals()” when comparing objects in Java?
    • A) They are the same and can be used interchangeably.
    • B) “==” compares object references, while “.equals()” compares object contents.
    • C) “==” compares object contents, while “.equals()” compares object references.
    • D) Both “==” and “.equals()” compare object references.
    Answer: B) “==” compares object references, while “.equals()” compares object contents.
    • Explanation: In Java, “==” compares object references, checking if two references point to the same object in memory, while “.equals()” is used to compare the contents or values of objects, and it can be overridden in classes to provide custom comparison logic.
  4. What is the purpose of the “finally” block in a try-catch-finally statement?
    • A) It is executed if an exception is caught in the try block.
    • B) It is executed if an exception is not caught in the catch block.
    • C) It is always executed, regardless of whether an exception is thrown or not.
    • D) It is used to specify the type of exception to catch.
    Answer: C) It is always executed, regardless of whether an exception is thrown or not.
    • Explanation: The “finally” block in a try-catch-finally statement is executed no matter whether an exception is thrown or not, making it suitable for cleanup and resource release tasks.
  5. What is the Java Native Interface (JNI), and when is it used?
    • A) JNI is a way to interface with Java’s networking libraries.
    • B) JNI is a way to execute Java code from within a native (non-Java) application.
    • C) JNI is a built-in Java package for working with JSON data.
    • D) JNI is a type of exception handling in Java.
    Answer: B) JNI is a way to execute Java code from within a native (non-Java) application.
    • Explanation: The Java Native Interface (JNI) is a programming framework that allows Java code to interact with code written in other programming languages, such as C and C++. JNI is used when you need to integrate Java with native code or libraries.

Leave a Comment

error: Content is protected !!