Java Object Concepts Questions
1. **What is an object in Java?**
- A) A collection of variables
- B) An instance of a class
- C) A method
- D) A data type
2. **What is a class in Java?**
- A) A blueprint for creating objects
- B) A type of variable
- C) A method of an object
- D) None of the above
3. **What does encapsulation mean in Java?**
- A) Hiding the internal state of an object
- B) Creating multiple instances of a class
- C) Merging two classes
- D) None of the above
4. **What is inheritance in Java?**
- A) A way to define new classes based on existing ones
- B) A method for creating objects
- C) A technique for data hiding
- D) None of the above
5. **What is a constructor in Java?**
- A) A method that initializes an object
- B) A type of variable
- C) A way to create classes
- D) None of the above
6. **What is method overloading?**
- A) Defining multiple methods with the same name but different parameters
- B) Creating a method inside another method
- C) Overriding a method in a subclass
- D) None of the above
7. **What is method overriding?**
- A) Defining a method in a subclass that has the same name and parameters as a method in
the parent class
- B) Calling a method multiple times
- C) Creating a method with a different return type
- D) None of the above
8. **What is the purpose of the `super` keyword in Java?**
- A) To refer to the parent class
- B) To create a new object
- C) To call a method from the current class
- D) None of the above
9. **What is polymorphism in Java?**
- A) The ability to present the same interface for different underlying data types
- B) The process of creating multiple classes
- C) A way to hide data
- D) None of the above
10. **What is an interface in Java?**
- A) A contract that classes can implement
- B) A type of class
- C) A method of data hiding
- D) None of the above
11. **What is an abstract class in Java?**
- A) A class that cannot be instantiated and may contain abstract methods
- B) A class that can be instantiated
- C) A class with only static methods
- D) None of the above
12. **What does the `this` keyword refer to in Java?**
- A) The current object instance
- B) The parent class
- C) The main method
- D) None of the above
13. **What is a static method in Java?**
- A) A method that belongs to the class rather than any instance
- B) A method that cannot be overridden
- C) A method that can only be called once
- D) None of the above
14. **What is the difference between `==` and `.equals()` in Java?**
- A) `==` checks for reference equality, while `.equals()` checks for value equality
- B) Both do the same thing
- C) `.equals()` is used for primitive types only
- D) None of the above
15. **What is a package in Java?**
- A) A namespace that organizes a set of related classes and interfaces
- B) A way to hide data
- C) A method for creating objects
- D) None of the above
16. What will be printed by this code?
public class Main {
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Cherry"};
for (String fruit : fruits) {
System.out.print(fruit + " ");
A) Apple Banana Cherry
B) Apple, Banana, Cherry
C) "Apple" "Banana" "Cherry"
D) None of the above
17. What is the output of the following code?
public class Main {
public static void main(String[] args) {
int num = 7;
System.out.println(num % 2 == 0 ? "Even" : "Odd");
A) Even
B) Odd
C) 7
D) None of the above
18. What will be printed by the following code?
public class Main {
public static void main(String[] args) {
String str = "Java";
str = str.toUpperCase();
System.out.println(str);
A) Java
B) JAVA
C) "Java"
D) None of the above
19. What is the output of this program?
public class Main {
public static void main(String[] args) {
int x = 5;
x += 3;
System.out.println(x);
A) 5
B) 3
C) 8
D) None of the above
20. What will this code output?
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a > b ? a : b);
A) 10
B) 20
C) a
D) None of the above