Chapter 5: Methods Flashcards

1
Q

A final instance variable in Java 17 can be assigned multiple times. True or False? If false, why?

A

False – A final instance variable can only be assigned once: in the declaration, an instance initializer, or a constructor.

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

Both instance and static initializers can access static variables in Java 17. True or False?

A

True

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

A static initializer in Java 17 can access instance variables. True or False? If false, why?

A

False – A static initializer cannot access instance variables because it runs before any instance is created.

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

An instance initializer in Java 17 can assign a final instance variable. True or False? If false, why?

A

True

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

A final instance variable in Java 17 must always be initialized at the time of declaration. True or False? If false, why?

A

False – It can also be initialized in an instance initializer or a constructor.

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

A method signature in Java 17 includes the method name and parameter list. True or False? If false, why?

A

True

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

The return type is part of the method signature in Java 17. True or False? If false, why?

A

False – The method signature includes only the method name and parameter list, not the return type.

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

The access modifier of a method is required in Java 17. True or False? If false, why?

A

False – The access modifier is optional; a method can have default access.

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

A method in Java 17 must always have an exception list. True or False? If false, why?

A

False – The exception list is optional; methods are not required to declare exceptions.

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

A method in Java 17 can have an empty parameter list. True or False? If false, why?

A

True

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

Abstract methods in Java 17 must have a method body. True or False? If false, why?

A

False – Abstract methods cannot have a body; they are meant to be implemented by subclasses.

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

The method body is always required for all methods in Java 17. True or False? If false, why?

A

False – Abstract methods do not require a method body.

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

Optional specifiers like final and static are required in Java 17 method declarations. True or False? If false, why?

A

False – Optional specifiers are not required; methods can be declared without them.

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

A method declaration in Java 17 includes all necessary details to call the method. True or False? If false, why?

A

True

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

In Java 17, an access modifier controls where the method can be referenced. True or False? If false, why?

A

True

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

What happens if both void print(int x) and void print(Integer x) exist, and you call print(5)?

A

The print(int x) method is called — Java prefers the exact primitive match.

17
Q

Which is preferred in method overload resolution: widening or boxing?

A

Widening is preferred over boxing.

18
Q

Will Java allow a method call that requires both widening and boxing (e.g., int → Long)?

A

No — Java does not combine widening and boxing in overload resolution.

19
Q

What happens if only void print(Integer x) is defined, and you call print(5)?

A

The int will be boxed to Integer, and the method will be called.

20
Q

What is widening in Java type conversion?

A

Widening is converting a smaller primitive type to a larger one, like int to long or float to double.

21
Q

What is boxing in Java type conversion?

A

Boxing is converting a primitive type to its corresponding wrapper class, like int to Integer.

22
Q

In method overloading, which is preferred: widening or boxing?

A

Widening is preferred over boxing during method resolution.