1. What is the primary purpose of a class in Java?
a) To create a new data type
b) To serve as a blueprint for objects
c) To execute a program
d) To store variables
Answer:
b) To serve as a blueprint for objects
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?
a) Using the 'new' keyword
b) Using the 'object' keyword
c) Using the 'class' keyword
d) Using the 'create' keyword
Answer:
c) Using the 'class' keyword
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?
a) A type of variable
b) An instance of a class
c) A function
d) A data structure
Answer:
b) An instance of a class
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?
a) By defining a class
b) By using the 'new' keyword
c) By declaring a variable
d) By calling a method
Answer:
b) By using the 'new' keyword
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?
a) new class MyClass {}
b) class MyClass {}
c) MyClass class {}
d) create MyClass {}
Answer:
b) class MyClass {}
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;
}
a) Creates a class named 'Main' with a method 'x'
b) Creates an object 'x' in the class 'Main'
c) Declares a variable 'x' inside the class 'Main'
d) Executes a method 'x' in the class 'Main'
Answer:
c) Declares a variable 'x' inside the class 'Main'
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?
a) To create a new variable
b) To create a new class
c) To create a new method
d) To create a new object
Answer:
d) To create a new object
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?
a) Methods within a class
b) Variables within a class
c) Parameters of a method
d) Return values of methods
Answer:
b) Variables within a class
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?
a) Methods operate independently of objects
b) Methods define the behavior of objects
c) Methods are used to create objects
d) Methods store data for objects
Answer:
b) Methods define the behavior of objects
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?
a) Creating a class named 'Main'
b) Declaring a variable 'myObj'
c) Creating an object 'myObj' of the class 'Main'
d) Initializing a method 'myObj'
Answer:
c) Creating an object 'myObj' of the class 'Main'
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?
a) A class is a special type of object
b) An object is a specific instance of a class
c) A class and an object are the same
d) An object is a blueprint for a class
Answer:
b) An object is a specific instance of a class
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);
}
}
a) 0
b) 5
c) Error
d) null
Answer:
b) 5
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?
a) Start with a lowercase letter and use camel case
b) Start with an uppercase letter and use camel case
c) All uppercase letters
d) All lowercase letters
Answer:
b) Start with an uppercase letter and use camel case
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?
a) Yes, it's called a nested class
b) No, classes cannot contain other classes
c) Yes, but only if it's abstract
d) No, classes can only contain methods and variables
Answer:
a) Yes, it's called a nested class
Explanation:
In Java, a class can contain another class, known as a nested class.
15. What is an instance variable in Java?
a) A variable defined within a method
b) A static variable defined at the class level
c) A variable defined at the class level
d) A final variable in a method
Answer:
c) A variable defined at the class level
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?
a) The method that initializes variables
b) The first method that runs in any Java program
c) The final method in a class
d) Any public method in a class
Answer:
b) The first method that runs in any Java program
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?
a) Yes
b) No
c) Only if it's a primitive type
d) Only if it's a static object
Answer:
b) No
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?
a) They are still considered the same
b) They represent different states of the same object
c) They represent two distinct objects
d) An error occurs
Answer:
c) They represent two distinct objects
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?
a) To construct methods in the class
b) To initialize the object's state when it is created
c) To declare variables in the class
d) To perform calculations
Answer:
b) To initialize the object's state when it is created
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?
a) It specifies the exact values of objects
b) It provides a structure that can be used to create objects
c) It compiles the Java program
d) It runs the Java application
Answer:
b) It provides a structure that can be used to create objects
Explanation:
A class in Java acts like a template, providing the structure and behavior that objects created from the class will possess​``【oaicite:1】``​.
Comments
Post a Comment