JAVA MCQ Series -3

Here a easy set of MCQ Test your knowledge

Java MCQs – Easy (1-20)

  1. What is Java primarily known for?
    • A) High performance
    • B) Platform independence
    • C) Low-level programming
    • D) Dynamic typing
    Answer: B) Platform independence
    • Explanation: Java is primarily known for its platform independence, which allows Java programs to run on any platform with a Java Virtual Machine (JVM).
  2. Which of the following is not a valid Java data type?
    • A) int
    • B) char
    • C) boolean
    • D) string
    Answer: D) string
    • Explanation: The correct data type for representing text in Java is “String,” not “string.”
  3. What does the “public static void main(String[] args)” method indicate in a Java program?
    • A) It is the main method where program execution starts.
    • B) It is a method for declaring variables.
    • C) It is used for mathematical calculations.
    • D) It is a constructor.
    Answer: A) It is the main method where program execution starts.
    • Explanation: In Java, the public static void main(String[] args) method is the entry point of a program.
  4. Which operator is used to compare two values for equality in Java?
    • A) ==
    • B) =
    • C) !=
    • D) >
    Answer: A) ==
    • Explanation: The == operator is used to compare two values for equality in Java.
  5. What is the result of the expression 5 / 2 in Java?
    • A) 2.5
    • B) 2
    • C) 2.0
    • D) Error
    Answer: B) 2
    • Explanation: In Java, integer division truncates the decimal part, so 5 / 2 results in 2.
  6. Which keyword is used to declare a variable constant in Java?
    • A) final
    • B) const
    • C) static
    • D) constant
    Answer: A) final
    • Explanation: The final keyword is used to declare a variable as constant in Java.
  7. What is the correct syntax for a single-line comment in Java?
    • A) // This is a comment
    • B) /* This is a comment */
    • C) # This is a comment
    • D) ‘ This is a comment
    Answer: A) // This is a comment
    • Explanation: In Java, single-line comments are denoted by //.
  8. Which loop is used for iterating a block of code a specific number of times in Java?
    • A) for loop
    • B) while loop
    • C) if statement
    • D) switch statement
    Answer: A) for loop
    • Explanation: The for loop is used for iterating a block of code a specific number of times in Java.
  9. What is the Java keyword used to exit a loop prematurely?
    • A) break
    • B) continue
    • C) return
    • D) exit
    Answer: A) break
    • Explanation: The break keyword is used to exit a loop prematurely in Java.
  10. Which statement is used to define a method in Java?
    • A) declare
    • B) method
    • C) define
    • D) public
    Answer: D) public
    • Explanation: In Java, methods are defined using the public keyword followed by the return type, method name, and parameter list.
  11. What does OOP stand for in the context of Java programming?
    • A) Object-Oriented Programming
    • B) Object-Oriented Protocol
    • C) Overloaded Operator Precedence
    • D) Object-Oriented Process
    Answer: A) Object-Oriented Programming
    • Explanation: OOP stands for Object-Oriented Programming, which is a key paradigm in Java.
  12. Which keyword is used to create an instance of a class in Java?
    • A) new
    • B) class
    • C) instance
    • D) create
    Answer: A) new
    • Explanation: The new keyword is used to create an instance (object) of a class in Java.
  13. Which access modifier allows a variable or method to be accessed within the same package?
    • A) public
    • B) private
    • C) protected
    • D) package-private (default)
    Answer: D) package-private (default)
    • Explanation: The default access modifier (package-private) allows access within the same package.
  14. Which Java primitive data type is used to store single characters?
    • A) char
    • B) character
    • C) string
    • D) int
    Answer: A) char
    • Explanation: The char data type is used to store single characters in Java.
  15. What does the “static” keyword indicate in Java?
    • A) It makes a method thread-safe.
    • B) It allows access to instance variables.
    • C) It indicates that a method or variable belongs to the class rather than an instance.
    • D) It prevents method overriding.
    Answer: C) It indicates that a method or variable belongs to the class rather than an instance.
    • Explanation: The static keyword in Java indicates that a method or variable belongs to the class itself and not to individual instances of the class.
  16. What is the purpose of the Java compiler?
    • A) It converts Java source code into machine code.
    • B) It converts Java source code into bytecode.
    • C) It runs Java programs.
    • D) It checks for syntax errors.
    Answer: B) It converts Java source code into bytecode.
    • Explanation: The Java compiler translates Java source code into bytecode, which is executed by the Java Virtual Machine (JVM).
  17. What is the term used for a named block of code in Java that performs a specific task?
    • A) Class
    • B) Method
    • C) Variable
    • D) Object
    Answer: B) Method
    • Explanation: In Java, a method is a named block of code that performs a specific task or function.
  18. Which operator is used to concatenate strings in Java?
    • A) +
    • B) &
    • C) *
    • D) /
    Answer: A) +
    • Explanation: The + operator is used for string concatenation in Java.
  19. What is the purpose of the “public” access modifier in Java?
    • A) It restricts access to within the same class.
    • B) It allows access from any class.
    • C) It makes a method private.
    • D) It hides a method from other classes.
    Answer: B) It allows access from any class.
    • Explanation: The public access modifier in Java allows access to a method or variable from any class.
  20. Which of the following is not a valid way to create an object in Java?
    • A) new ClassName()
    • B) ClassName.new()
    • C) ClassName instance = new ClassName();
    • D) ClassName instance = new ClassName;
    Answer: B) ClassName.new()
    • Explanation: The correct way to create an object in Java is by using the new keyword followed by the class name and parentheses.

Leave a Comment

error: Content is protected !!