Chapter 8 Practice Questions Flashcards

1
Q

What does each instance of a class in Java have?
a) Its own copy of static variables.
b) A shared reference to static methods.
c) Its own copy of instance variables.
d) Shared access to instance fields.

A

c) Its own copy of instance variables.
Explanation: Each instance of a class has its own independent copy of instance fields, which can have unique values for each object.

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

Which of the following is true about static methods?
a) They require an instance of the class to be called.
b) They cannot access static fields.
c) They belong to the class rather than any instance.
d) They can only modify instance variables.

A

c) They belong to the class rather than any instance.
Explanation: Static methods belong to the class and can be called using the class name. They do not operate on instance fields but can access static fields.

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

(True/False)
Static fields are initialized to 0 if no explicit initialization is provided.

A

True
Explanation: Primitive static fields are initialized to 0 by default if no value is explicitly assigned during declaration.

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

What happens when an object is passed as an argument to a method?
a) A copy of the object is passed.
b) The address of the object is passed.
c) The object’s fields are directly copied.
d) Nothing is passed.

A

b) The address of the object is passed.
Explanation: Java passes arguments by value. For objects, the value is the reference (memory address), allowing the method to modify the object’s contents.

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

What will the following code print?

java
Copy code
Stock xyzCompany = new Stock(“XYZ”, 9.62);
System.out.println(xyzCompany);
a) The object’s memory address.
b) A string representation of the object defined by toString().
c) A runtime error.
d) “XYZ, 9.62” without needing toString.

A

b) A string representation of the object defined by toString().
Explanation: The toString() method is called implicitly when an object is passed to System.out.println. If overridden, it will print the string representation defined in the method.

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

Which of the following creates a deep copy of an object?
a) Assigning one object to another.
b) Using a copy constructor to initialize a new object.
c) Setting the reference of one object to another.
d) None of the above.

A

b) Using a copy constructor to initialize a new object.
Explanation: A deep copy involves creating a new instance and copying all values from the original object. This is typically done using a copy constructor.

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

True/False)
The == operator compares the memory addresses of two objects, not their contents.

A

True
Explanation: The == operator checks if two reference variables point to the same memory location. To compare contents, the equals() method should be used.

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

What is the purpose of the this reference in Java?
a) To refer to the superclass.
b) To refer to the current object instance.
c) To create static fields.
d) To avoid using null references.

A

b) To refer to the current object instance.
Explanation: The this keyword refers to the current instance of the class and can resolve shadowing or chain constructors.

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

What will the following code output?

java
Copy code
enum Day { MONDAY, TUESDAY, WEDNESDAY }
System.out.println(Day.WEDNESDAY.ordinal());
a) 1
b) 2
c) 3
d) Runtime error

A

b) 2
Explanation: The ordinal() method returns the zero-based position of the constant. For Day.WEDNESDAY, the position is 2.

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

What happens when an object’s reference is set to null?
a) The object is immediately destroyed.
b) It cannot be garbage collected.
c) It becomes eligible for garbage collection.
d) The program crashes.

A

c) It becomes eligible for garbage collection.
Explanation: Setting an object’s reference to null means no variable points to it, making it eligible for garbage collection by the JVM.

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

(True/False)
Java allows you to use an enum constant in a switch statement.

A

True
Explanation: Java supports using enum constants in switch statements, simplifying code by avoiding hard-coded integer values.

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

What will the following code do?

java
Copy code
BankAccount account1 = new BankAccount(500.0);
BankAccount account2 = account1;
account1.deposit(100.0);
System.out.println(account2.getBalance());
a) Print 500.0.
b) Print 600.0.
c) Throw a runtime error.
d) Compile but not run.

A

b) Print 600.0.
Explanation: Both account1 and account2 reference the same object. Changes made through one reference are reflected in the other.

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

What does the finalize() method do?
a) Immediately frees up memory.
b) Runs before the Garbage Collector reclaims the object.
c) Closes file streams automatically.
d) Ensures an object is deep copied.

A

b) Runs before the Garbage Collector reclaims the object.
Explanation: The finalize() method is invoked just before an object is garbage collected but doesn’t guarantee when it will execute.

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