1. What is the primary purpose of a class in Java?
Answer:
Explanation:
A class in Java acts as a blueprint for creating objects, defining the state and behavior that the objects of the class will have【146†source】.
2. How do you create a class in Java?
Answer:
Explanation:
A class in Java is created by using the 'class' keyword, followed by the class name【146†source】.
3. What is an object in Java?
Answer:
Explanation:
An object in Java is an instance of a class, created using the class blueprint and containing its own state and behavior【146†source】.
4. How do you create an object in Java?
Answer:
Explanation:
An object in Java is created by specifying the class name, followed by the object name, and using the 'new' keyword【146†source】.
5. Which of the following is a valid class declaration in Java?
Answer:
Explanation:
A class in Java is declared using the 'class' keyword followed by the class name, which should start with an uppercase letter【146†source】.
6. What does the following code snippet do?
public class Main {
int x = 5;
}
Answer:
Explanation:
This code snippet creates a class named 'Main' and declares an integer variable 'x' inside it, initializing 'x' to 5【146†source】.
7. What is the purpose of the 'new' keyword in Java?
Answer:
Explanation:
The 'new' keyword in Java is used to create new objects of a class【146†source】.
8. In the context of Java classes and objects, what are attributes?
Answer:
Explanation:
Attributes in Java classes refer to the variables that are declared within a class, representing the state of objects of that class【146†source】.
9. How are methods associated with objects in Java?
Answer:
Explanation:
Methods in Java classes define the behavior of the objects, allowing the objects to perform operations or exhibit behaviors【146†source】.
10. What does the following code example illustrate?
Answer:
Explanation:
This code demonstrates creating an object named 'myObj' of the class 'Main' using the 'new' keyword【146†source】.
11. In Java, what is the relationship between a class and an object?
Answer:
Explanation:
An object is a specific instance of a class, embodying the structure and behavior defined by the class.
12. What is the output of the following Java code?
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Answer:
Explanation:
The code creates an object of the class 'Main', and then it prints the value of the variable 'x' of that object, which is 5​“【oaicite:2】“​.
13. What is the standard naming convention for classes in Java?
Answer:
Explanation:
The standard naming convention for classes in Java is to start with an uppercase letter and follow camel case notation.
14. Can a class in Java contain another class?
Answer:
Explanation:
In Java, a class can contain another class, known as a nested class.
15. What is an instance variable in Java?
Answer:
Explanation:
An instance variable in Java is a variable that is declared inside a class but outside any method and is not declared as static.
16. What is the main method in a Java class?
Answer:
Explanation:
The main method in Java is the entry point of any Java application and is the first method that gets executed.
17. Can an object in Java exist without a class?
Answer:
Explanation:
Objects in Java are instances of classes, so they cannot exist without a class.
18. What happens if two objects of the same class in Java have different values in their instance variables?
Answer:
Explanation:
Objects of the same class can have different states (values in their instance variables), representing distinct objects.
19. What is the purpose of a constructor in a class in Java?
Answer:
Explanation:
A constructor in Java is used to initialize the state of an object when it is created.
20. How is a class in Java similar to a template?
Answer:
Explanation:
A class in Java acts like a template, providing the structure and behavior that objects created from the class will possess​“【oaicite:1】“​.