Here a easy set of MCQ Test your knowledge
Java MCQs – Easy (1-20)
- What is Java primarily known for?
- A) High performance
- B) Platform independence
- C) Low-level programming
- D) Dynamic typing
- Explanation: Java is primarily known for its platform independence, which allows Java programs to run on any platform with a Java Virtual Machine (JVM).
- Which of the following is not a valid Java data type?
- A) int
- B) char
- C) boolean
- D) string
- Explanation: The correct data type for representing text in Java is “String,” not “string.”
- 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.
- Explanation: In Java, the
public static void main(String[] args)
method is the entry point of a program.
- Which operator is used to compare two values for equality in Java?
- A) ==
- B) =
- C) !=
- D) >
- Explanation: The
==
operator is used to compare two values for equality in Java.
- What is the result of the expression
5 / 2
in Java?- A) 2.5
- B) 2
- C) 2.0
- D) Error
- Explanation: In Java, integer division truncates the decimal part, so
5 / 2
results in 2.
- Which keyword is used to declare a variable constant in Java?
- A) final
- B) const
- C) static
- D) constant
- Explanation: The
final
keyword is used to declare a variable as constant in Java.
- 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
- Explanation: In Java, single-line comments are denoted by
//
.
- 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
- Explanation: The
for
loop is used for iterating a block of code a specific number of times in Java.
- What is the Java keyword used to exit a loop prematurely?
- A) break
- B) continue
- C) return
- D) exit
- Explanation: The
break
keyword is used to exit a loop prematurely in Java.
- Which statement is used to define a method in Java?
- A) declare
- B) method
- C) define
- D) public
- Explanation: In Java, methods are defined using the
public
keyword followed by the return type, method name, and parameter list.
- 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
- Explanation: OOP stands for Object-Oriented Programming, which is a key paradigm in Java.
- Which keyword is used to create an instance of a class in Java?
- A) new
- B) class
- C) instance
- D) create
- Explanation: The
new
keyword is used to create an instance (object) of a class in Java.
- 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)
- Explanation: The default access modifier (package-private) allows access within the same package.
- Which Java primitive data type is used to store single characters?
- A) char
- B) character
- C) string
- D) int
- Explanation: The
char
data type is used to store single characters in Java.
- 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.
- 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.
- 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.
- Explanation: The Java compiler translates Java source code into bytecode, which is executed by the Java Virtual Machine (JVM).
- 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
- Explanation: In Java, a method is a named block of code that performs a specific task or function.
- Which operator is used to concatenate strings in Java?
- A) +
- B) &
- C) *
- D) /
- Explanation: The
+
operator is used for string concatenation in Java.
- 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.
- Explanation: The
public
access modifier in Java allows access to a method or variable from any class.
- 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;
- Explanation: The correct way to create an object in Java is by using the
new
keyword followed by the class name and parentheses.