examsveda.in

Java MCQs series -4

Java MCQs – Medium (1-20)

1. What is the purpose of the “throw” statement in Java?

  • A) To declare a variable
  • B) To exit a loop
  • C) To throw an exception
  • D) To define a method

Answer: C) To throw an exception

  • Explanation: The “throw” statement in Java is used to explicitly throw an exception within a method or block of code.

2. Which keyword is used to create a new thread in Java?

  • A) thread
  • B) new
  • C) start
  • D) run

Answer: B) new

  • Explanation: In Java, you create a new thread by creating an instance of a class that extends the “Thread” class and then using the “new” keyword to instantiate the thread.

3. What is the purpose of the “volatile” keyword when applied to a variable in Java?

  • A) It specifies that the variable is constant.
  • B) It ensures that the variable is visible to all threads and prevents caching.
  • C) It prevents the variable from being modified.
  • D) It makes the variable a global variable.

Answer: B) It ensures that the 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 Java interface?

  • A) A class that cannot be instantiated
  • B) A blueprint for a class that defines methods but does not provide implementations
  • C) A class with only static methods
  • D) A class that extends another class.

Answer: B) A blueprint for a class that defines methods but does not provide implementations

  • Explanation: In Java, an interface is a blueprint for a class that defines methods (method signatures) without providing implementations. Classes that implement an interface must provide implementations for all the methods declared in the interface.

5. What is the purpose of the “synchronized” keyword in Java?

  • A) It specifies that a method should be executed in a background thread.
  • B) It ensures that only one thread can execute a synchronized block of code at a time.
  • C) It prevents a method from being overridden in a subclass.
  • D) It specifies that a method cannot throw exceptions.

Answer: B) It ensures that only one thread can execute a synchronized block of code at a time.

  • Explanation: The “synchronized” keyword in Java is used to create synchronized blocks of code that can be accessed by only one thread at a time, preventing concurrent access.

6. In Java, what is the purpose of the “try-with-resources” statement?

  • A) To declare variables
  • B) To create loops
  • C) To handle exceptions and automatically close resources
  • D) To define inner classes.

Answer: C) To handle exceptions and automatically close resources

  • Explanation: The “try-with-resources” statement in Java is used for handling exceptions and automatically closing resources, such as files, streams, or database connections, when they are no longer needed.

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

8. What is the purpose of the “continue” statement in a loop?

  • A) It exits the loop prematurely.
  • B) It skips the current iteration and continues to the next iteration.
  • C) It restarts the loop from the beginning.
  • D) It creates a nested loop.

Answer: B) It skips the current iteration and continues to the next iteration.

  • Explanation: The “continue” statement in a loop is used to skip the current iteration and continue with the next iteration of the loop.

9. What is method overloading in Java?

  • A) A method with the same name but different return types.
  • B) A method with the same name and the same parameter types in the same class.
  • C) A method with a different name but the same parameter types in the same class.
  • D) A method with the same name but in different classes.

Answer: B) A method with the same name and the same parameter types in the same class

  • Explanation: Method overloading in Java refers to defining multiple methods in the same class with the same name but different parameter lists.

10. What is the purpose of the “instanceof” operator in Java?

  • A) To create a new instance of a class.
  • B) To compare two objects for equality.
  • C) To check if an object is an instance of a particular class or interface.
  • D) To perform mathematical operations.

Answer: C) To check if an object is an instance of a particular class or interface.

  • Explanation: The “instanceof” operator in Java is used to check if an object is an instance of a specific class or interface.

11. Which of the following is not a valid access modifier in Java? – A) public – B) private – C) protected – D) internal

Answer: D) internal

  • Explanation: “internal” is not a valid access modifier in Java. The correct access modifiers are “public,” “private,” and “protected.”

12. What is the purpose of the “finally” block in a try-catch-finally statement in Java? – A) To specify the catch clause – B) To specify the try clause – C) To specify the cleanup code that is always executed, regardless of whether an exception is thrown or not – D) To specify the exception type to be caught

Answer: C) To specify the cleanup code that is always executed, regardless of whether an exception is thrown or not

  • Explanation: The “finally” block in a try-catch-finally statement in Java is used to specify cleanup code that is always executed, regardless of whether an exception is thrown or not.

13. In Java, what is the purpose of the “super” keyword when used in a constructor? – A) It calls the constructor of the current class. – B) It calls a method in the superclass. – C) It initializes instance variables. – D) It calls the constructor of the superclass.

Answer: D) It calls the constructor of the superclass.

  • Explanation: In a constructor, the “super” keyword is used to call the constructor of the superclass.

14. What is the purpose of the “this” keyword in Java? – A) It creates a new object. – B) It refers to the superclass. – C) It refers to the current instance of the class. – D) It initializes static variables.

Answer: C) It refers to the current instance of the class.

  • Explanation: In Java, the “this” keyword is used to refer to the current instance of the class, allowing you to distinguish between instance variables and method parameters with the same name.

15. What is the Java keyword used for creating and instantiating an anonymous inner class? – A) class – B) new – C) instance – D) anonymous

Answer: B) new

  • Explanation: To create and instantiate an anonymous inner class in Java, you use the “new” keyword followed by the class or interface you want to implement.

16. In Java, which of the following is true about constructors? – A) Constructors can have a return type. – B) Constructors cannot be overloaded. – C) Constructors are called explicitly using the “call” keyword. – D) Constructors have the same name as the class.

Answer: D) Constructors have the same name as the class.

  • Explanation: In Java, constructors have the same name as the class and are called automatically when an object of the class is created.

17. What is the purpose of the “break” statement in a switch statement in Java? – A) To exit the switch statement prematurely. – B) To continue to the next case. – C) To restart the switch statement. – D) To skip the current case.

Answer: A) To exit the switch statement prematurely.

  • Explanation: The “break” statement in a switch statement is used to exit the switch statement prematurely and prevent fall-through to subsequent cases.

18. Which Java keyword is used to define a constant? – A) static – B) final – C) constant – D) const

Answer: B) final

  • Explanation: In Java, the “final” keyword is used to define constants.

19. In Java, what is a checked exception? – A) An exception that is checked at compile time – B) An exception that is checked at runtime – C) An exception that is never checked – D) An exception that cannot be caught

Answer: A) An exception that is checked at compile time

  • Explanation: A checked exception in Java is an exception that is checked at compile time, and the code must either handle it with a try-catch block or declare it using the “throws” keyword.

20. What is the purpose of the “default” case in a switch statement in Java? – A) To specify the first case – B) To specify the last case – C) To provide a default action when none of the other cases match – D) To indicate that the switch statement has no cases

Answer: C) To provide a default action when none of the other cases match

  • Explanation: The “default” case in a switch statement in Java is used to provide a default action to be executed when none of the other cases match the value being switched.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!