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.
Which of the following is true about data hiding in Java?
a. Fields in a class are public.
b. Fields in a class are private and accessed via methods.
c. Fields in a class can only be accessed by constructors.
d. Fields in a class are automatically protected.
b
Explanation: Data hiding ensures that fields in a class are private and accessed via accessors (getters) and mutators (setters).
What is the purpose of an accessor method?
a. Modifies the value of a private field.
b. Retrieves the value of a private field.
c. Deletes the private field.
d. Hides the value of a field from other classes.
b
Explanation: Accessor methods (getters) retrieve the value of a private field.
TRUE OR FALSE: A method with the same name but a different parameter list is an example of method overloading.
TRUE
Explanation: Method overloading occurs when multiple methods in the same class have the same name but different parameter lists.
Consider the following code snippet. What will happen?
java
Copy code
Rectangle box;
System.out.println(box.getArea());
a. It will print the area of the rectangle.
b. It will throw a compilation error because the box variable is not initialized.
c. It will throw a runtime error because box is null.
d. It will compile but not run.
b
Explanation: The box variable is not initialized and does not reference any object, causing a compilation error.
What is the default constructor in Java?
a. A constructor provided by the programmer.
b. A constructor with arguments.
c. A no-arg constructor provided by Java if no constructor is written.
d. A constructor that initializes all fields to custom values.
c
Explanation: Java provides a default constructor with no arguments if no other constructor is defined in the class.
What will the following code do?
java
Copy code
String name = new String(“John Doe”);
a. Creates a reference variable name and points it to a String object with “John Doe”.
b. Throws an error because the constructor is incorrect.
c. Does nothing.
d. Creates a String object without initializing it.
a
Explanation: The code creates a new String object with the value “John Doe” and assigns it to the name reference variable.
TRUE OR FALSE: Wildcard imports allow importing all classes in a package.
TRUE
Explanation: A wildcard import (e.g., import java.util.*) imports all classes in a package.
What is shadowing in Java?
a. Declaring a variable with the same name in two classes.
b. Declaring a local variable with the same name as an instance field.
c. Overloading a method with the same name but different return types.
d. Using an object as a method parameter.
b
Explanation: Shadowing occurs when a local variable has the same name as an instance field, hiding the instance field within the method.
What happens when you pass an object as an argument to a method?
a. The object’s memory address is passed to the parameter.
b. A copy of the object is passed to the parameter.
c. The object cannot be passed as an argument.
d. A runtime error occurs.
a
Explanation: Java passes objects by reference, meaning the method receives the memory address of the object.
TRUE OR FALSE: Method signatures include the return type.
FALSE
Explanation: Method signatures include the method name and parameter types, but not the return type.
Identify the error in the following code:
java
Copy code
public Rectangle(double len, double wid)
{
length = len;
}
a. Missing a return type.
b. The constructor is missing the assignment for the width field.
c. Constructors cannot accept parameters.
d. There is no error.
Answer: b
Explanation: The wid parameter is passed but not assigned to the width field.
What does this code do?
java
Copy code
Rectangle room = new Rectangle();
a. Creates a reference variable room without initializing it.
b. Calls the default constructor and creates a Rectangle object.
c. Calls the no-arg constructor written by the programmer.
d. Throws a runtime error.
b
Explanation: If no constructor is written, Java provides a default constructor that initializes the Rectangle object.
TRUE OR FALSE: An object’s fields must be accessed using public methods when they are private.
TRUE
Explanation: Private fields can only be accessed via public accessor and mutator methods.
What will happen when the following code executes?
java
Copy code
public double getArea()
{
return length * width;
}
a. It will dynamically calculate the area of the rectangle.
b. It will throw an error if length or width is not initialized.
c. It will store the result in a variable called area.
d. It will return the value 0 by default.
a
Explanation: The method calculates and returns the area dynamically based on the current values of length and width.
Which of the following is NOT true about a constructor?
a. It has the same name as the class.
b. It has no return type.
c. It can be overloaded.
d. It must always be private.
d
Explanation: Constructors are typically public to allow objects to be instantiated.