Chapter 6 Practice Questions Flashcards

1
Q

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.

A

c
Explanation: Objects are instances of classes that can hold data (fields) and perform actions (methods).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.

A

b
Explanation: A class is a blueprint that defines what fields and methods objects of that class will have.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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.

A

c
Explanation: An instance is a concrete object created from the template provided by a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

TRUE OR FALSE: You can create only one object from a class.

A

False
Explanation: A program can create as many objects from a class as needed, each being a separate instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which of the following is an example of an object you’ve already used in Java?
a. Rectangle
b. int
c. Scanner
d. File

A

c
Explanation: Scanner is an object provided by the Java API for reading input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

b
Explanation: Access specifiers control how fields and methods in a class can be accessed by other classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A

b
Explanation: The code properly uses encapsulation by providing set and get methods for accessing the private value field.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which of these access specifiers restricts access to within the same class?
a. public
b. private
c. protected
d. default

A

b
Explanation: The private specifier limits access to the field or method within the same class only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

TRUE OR FALSE: A private field can be accessed directly by code in another class.

A

Answer: False
Explanation: Private fields are accessible only within the class they are declared in. Other classes must use methods to access them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

a
Explanation: The length and width fields are public, so they can be directly accessed and manipulated. The program prints 10 * 5 = 50.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

TRUE OR FALSE: Access specifiers only apply to methods.

A

False
Explanation: Access specifiers can apply to fields, methods, and constructors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

a
Explanation: Objects are stored in memory and can use the fields and methods defined by their class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

TRUE OR FALSE: Every instance of a class shares the same fields and methods.

A

Answer: False
Explanation: Each object (instance) of a class has its own copy of the fields, but all objects share the same methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the term for the process of bundling fields and methods into a single unit?
a. Polymorphism
b. Inheritance
c. Encapsulation
d. Abstraction

A

c
Explanation: Encapsulation bundles fields and methods together and restricts access to the data through access specifiers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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.

A

b
Explanation: Data hiding ensures that fields in a class are private and accessed via accessors (getters) and mutators (setters).

17
Q

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.

A

b
Explanation: Accessor methods (getters) retrieve the value of a private field.

18
Q

TRUE OR FALSE: A method with the same name but a different parameter list is an example of method overloading.

A

TRUE
Explanation: Method overloading occurs when multiple methods in the same class have the same name but different parameter lists.

19
Q

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.

A

b
Explanation: The box variable is not initialized and does not reference any object, causing a compilation error.

20
Q

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.

A

c
Explanation: Java provides a default constructor with no arguments if no other constructor is defined in the class.

21
Q

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

a
Explanation: The code creates a new String object with the value “John Doe” and assigns it to the name reference variable.

22
Q

TRUE OR FALSE: Wildcard imports allow importing all classes in a package.

A

TRUE
Explanation: A wildcard import (e.g., import java.util.*) imports all classes in a package.

23
Q

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.

A

b
Explanation: Shadowing occurs when a local variable has the same name as an instance field, hiding the instance field within the method.

24
Q

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

a
Explanation: Java passes objects by reference, meaning the method receives the memory address of the object.

25
Q

TRUE OR FALSE: Method signatures include the return type.

A

FALSE
Explanation: Method signatures include the method name and parameter types, but not the return type.

26
Q

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.

A

Answer: b
Explanation: The wid parameter is passed but not assigned to the width field.

27
Q

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.

A

b
Explanation: If no constructor is written, Java provides a default constructor that initializes the Rectangle object.

28
Q

TRUE OR FALSE: An object’s fields must be accessed using public methods when they are private.

A

TRUE
Explanation: Private fields can only be accessed via public accessor and mutator methods.

29
Q

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

a
Explanation: The method calculates and returns the area dynamically based on the current values of length and width.

30
Q

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.

A

d
Explanation: Constructors are typically public to allow objects to be instantiated.