1 Flashcards

(70 cards)

1
Q

What do access level modifiers determine?

A

They determine whether other classes can use a particular field or invoke a particular method.

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

What does the public access modifier do?

A

Members are accessible by everyone.

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

What does the private access modifier do?

A

Members are only accessible from within the class.

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

What does the protected access modifier do?

A

Accessible from within the class, within its subclasses, and within classes from the same package.

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

What is the default access level if no modifier is stated?

A

Package-private level access.

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

What does the final keyword do when declaring a variable?

A

Prevents its value from being changed.

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

What happens to an object’s reference when declared as final?

A

The variable cannot be made to refer to a different object after the declaration.

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

What happens to the state of an object declared as final?

A

The state of the object can change.

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

What is the effect of a final method?

A

It cannot be overridden.

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

What is the effect of a final class?

A

It cannot be extended.

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

What is a static field?

A

A field that belongs to the class instead of each instance having its own.

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

How do you access a static field or method?

A

Use the class name.

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

What do static and final combined create?

A

A constant.

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

What can interfaces specify?

A

A list of required methods to be implemented.

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

What does decoupling mean in the context of interfaces?

A

Separation of the interface from the implementation.

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

Why use interfaces?

A

Enable and enforce strict sharing of methods among classes and make code more reusable.

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

What can an interface contain?

A

Method signatures, default methods, static methods, and nested types.

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

What is the difference between class inheritance and interface inheritance?

A

Class inheritance uses ‘extends’ while interface inheritance uses ‘implements’.

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

What is one of the main characteristics of an interface?

A

It cannot have a constructor or any instance fields.

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

What is an abstract class?

A

A cross between a regular class and an interface.

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

What must any abstract method use?

A

The abstract keyword.

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

Can an abstract class be instantiated?

A

No, it cannot be instantiated even if it has a constructor.

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

When should you consider using abstract classes?

A

When code needs to be shared among closely related classes.

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

When should you consider using interfaces?

A

When unrelated classes would implement the interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is an inner class?
A member of the outer class.
26
What access does an inner class have?
Direct access to the outer class's members, including private ones.
27
Why create inner classes?
To logically group classes that are only used in one place.
28
What are the two special kinds of inner classes?
Local Class and Anonymous Class.
29
What is a local class?
An inner class declared inside a block.
30
What is an anonymous class?
A class with no name that is declared and instantiated in one statement.
31
What are the differences between local and anonymous classes?
* Local has a name * Local can have multiple instances * Local can have a constructor * Local can implement multiple interfaces * Anonymous cannot.
32
What is meant by effectively final?
A variable not declared as final, but its value does not change anywhere else in the code.
33
What are modifiers in Java?
Modifiers are keywords in Java that define the properties of classes, methods, and variables.
34
True or False: The 'private' modifier allows access to a class member only within its own class.
True
35
What does the 'public' modifier indicate?
The 'public' modifier indicates that a class member is accessible from any other class.
36
Fill in the blank: The ______ modifier restricts access to the package and subclasses.
protected
37
What is an abstract class?
An abstract class is a class that cannot be instantiated and may contain abstract methods that must be implemented by subclasses.
38
How do you declare an abstract method in Java?
By using the 'abstract' keyword in a method declaration within an abstract class.
39
True or False: An interface can contain method implementations.
False
40
What is the main purpose of an interface in Java?
An interface defines a contract that classes can implement, specifying methods that must be provided.
41
Multiple Choice: Which keyword is used to implement an interface in a class? A) extends B) implements C) inherits
B) implements
42
What is the difference between an interface and an abstract class?
An interface cannot contain any implementation, whereas an abstract class can have both abstract and concrete methods.
43
What is an inner class?
An inner class is a class defined within another class and has access to the outer class's members.
44
True or False: Anonymous classes can be used to instantiate a class without giving it a name.
True
45
What is the syntax to create an anonymous class?
By using the 'new' keyword followed by the class or interface name and overriding its methods.
46
Fill in the blank: An ______ class is declared inside another class and can access the outer class's members.
inner
47
What keyword is used to define an abstract class?
abstract
48
Multiple Choice: Which of the following can be declared as abstract? A) Class B) Method C) Both A and B
C) Both A and B
49
What happens if a class extends an abstract class?
It must implement all abstract methods of the parent abstract class unless it is also declared abstract.
50
True or False: An interface can extend another interface.
True
51
What is a static nested class?
A static nested class is a nested class that does not have access to instance variables and methods of the outer class.
52
Fill in the blank: The keyword ______ is used to declare a class that cannot be subclassed.
final
53
What is the default access modifier for class members in Java?
Package-private (no modifier specified)
54
Multiple Choice: Which modifier makes a class member accessible only within its own package? A) private B) protected C) default
C) default
55
What type of class cannot be instantiated directly?
Abstract class
56
True or False: You can create an instance of an abstract class.
False
57
What is the use of the 'synchronized' modifier?
The 'synchronized' modifier is used to control access to a method or block by multiple threads.
58
Fill in the blank: Inner classes can access the ______ class's members directly.
outer
59
What does it mean if a class is marked as 'final'?
It cannot be subclassed.
60
Multiple Choice: Which of the following is NOT a valid modifier in Java? A) public B) private C) global
C) global
61
What is the benefit of using interfaces?
Interfaces allow for multiple inheritance of type and define a contract for classes to implement.
62
True or False: A class can implement multiple interfaces.
True
63
What is the result of declaring a method as 'abstract'?
The method must be implemented in any concrete subclass.
64
Fill in the blank: A class that contains at least one abstract method is considered ______.
abstract
65
What is the main characteristic of an anonymous class?
It does not have a name and is defined at the point of instantiation.
66
What is the purpose of the 'volatile' modifier?
The 'volatile' modifier indicates that a variable's value will be modified by different threads.
67
True or False: An abstract class can implement an interface.
True
68
What is the result of a class implementing an interface?
The class must provide implementations for all methods declared in the interface.
69
Fill in the blank: Methods in an interface are implicitly ______.
public
70
What is the main purpose of using inner classes?
To logically group classes that are only used in one place and to increase encapsulation.