Chapter 6 Practice Questions Flashcards
What is an object in Java?
a. A data type used for calculations.
b. A blueprint for creating classes.
c. An entity in memory that can store data and perform operations.
d. A way to encapsulate primitive data types.
c
Explanation: Objects are instances of classes that can hold data (fields) and perform actions (methods).
Which of the following is true about a class?
a. It is an instance of an object.
b. It defines fields and methods for objects.
c. It cannot be used to create multiple objects.
d. It is the same as a method.
b
Explanation: A class is a blueprint that defines what fields and methods objects of that class will have.
What is an instance of a class?
a. A method that belongs to the class.
b. A copy of the class file.
c. A specific object created from a class.
d. A field declared inside a method.
c
Explanation: An instance is a concrete object created from the template provided by a class.
TRUE OR FALSE: You can create only one object from a class.
False
Explanation: A program can create as many objects from a class as needed, each being a separate instance.
Which of the following is an example of an object you’ve already used in Java?
a. Rectangle
b. int
c. Scanner
d. File
c
Explanation: Scanner is an object provided by the Java API for reading input.
What is the purpose of access specifiers in Java?
a. To create methods in a class.
b. To define the visibility of class members.
c. To initialize objects.
d. To restrict object creation.
b
Explanation: Access specifiers control how fields and methods in a class can be accessed by other classes.
What will the following code do?
java
Copy code
public class Example {
private int value;
public void setValue(int v) {
value = v;
}
public int getValue() {
return value;
}
}
a. Cause an error because value is private.
b. Successfully encapsulate the value field.
c. Fail because the methods are not private.
d. Compile but will not run.
b
Explanation: The code properly uses encapsulation by providing set and get methods for accessing the private value field.
Which of these access specifiers restricts access to within the same class?
a. public
b. private
c. protected
d. default
b
Explanation: The private specifier limits access to the field or method within the same class only.
TRUE OR FALSE: A private field can be accessed directly by code in another class.
Answer: False
Explanation: Private fields are accessible only within the class they are declared in. Other classes must use methods to access them.
What does the following code do?
java
Copy code
public class Rectangle {
public int length;
public int width;
}
Rectangle rect = new Rectangle();
rect.length = 10;
rect.width = 5;
System.out.println(rect.length * rect.width);
a. Prints 50.
b. Throws a compilation error because fields are not private.
c. Prints 15.
d. Causes a runtime error.
a
Explanation: The length and width fields are public, so they can be directly accessed and manipulated. The program prints 10 * 5 = 50.
TRUE OR FALSE: Access specifiers only apply to methods.
False
Explanation: Access specifiers can apply to fields, methods, and constructors.
What happens when an object is created from a class?
a. The object is stored in memory and can be used to call methods or access fields.
b. A new class file is generated for the object.
c. Only fields are available, not methods.
d. The object replaces the class definition.
a
Explanation: Objects are stored in memory and can use the fields and methods defined by their class.
What will the following code print?
java
Copy code
public class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
Circle c = new Circle(5);
System.out.println(c.getArea());
a. Prints the area of a circle with radius 5.
b. Compilation error due to the private radius field.
c. Prints 0 because the radius was not initialized.
d. Runtime error because Math.PI is undefined.
a
Explanation: The radius is initialized via the constructor, and Math.PI is a constant. The code calculates and prints the area of the circle.
TRUE OR FALSE: Every instance of a class shares the same fields and methods.
Answer: False
Explanation: Each object (instance) of a class has its own copy of the fields, but all objects share the same methods.
What is the term for the process of bundling fields and methods into a single unit?
a. Polymorphism
b. Inheritance
c. Encapsulation
d. Abstraction
c
Explanation: Encapsulation bundles fields and methods together and restricts access to the data through access specifiers.