1 Flashcards

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
Q

What is an inner class?

A

A member of the outer class.

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

What access does an inner class have?

A

Direct access to the outer class’s members, including private ones.

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

Why create inner classes?

A

To logically group classes that are only used in one place.

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

What are the two special kinds of inner classes?

A

Local Class and Anonymous Class.

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

What is a local class?

A

An inner class declared inside a block.

30
Q

What is an anonymous class?

A

A class with no name that is declared and instantiated in one statement.

31
Q

What are the differences between local and anonymous classes?

A
  • Local has a name
  • Local can have multiple instances
  • Local can have a constructor
  • Local can implement multiple interfaces
  • Anonymous cannot.
32
Q

What is meant by effectively final?

A

A variable not declared as final, but its value does not change anywhere else in the code.

33
Q

What are modifiers in Java?

A

Modifiers are keywords in Java that define the properties of classes, methods, and variables.

34
Q

True or False: The ‘private’ modifier allows access to a class member only within its own class.

35
Q

What does the ‘public’ modifier indicate?

A

The ‘public’ modifier indicates that a class member is accessible from any other class.

36
Q

Fill in the blank: The ______ modifier restricts access to the package and subclasses.

37
Q

What is an abstract class?

A

An abstract class is a class that cannot be instantiated and may contain abstract methods that must be implemented by subclasses.

38
Q

How do you declare an abstract method in Java?

A

By using the ‘abstract’ keyword in a method declaration within an abstract class.

39
Q

True or False: An interface can contain method implementations.

40
Q

What is the main purpose of an interface in Java?

A

An interface defines a contract that classes can implement, specifying methods that must be provided.

41
Q

Multiple Choice: Which keyword is used to implement an interface in a class? A) extends B) implements C) inherits

A

B) implements

42
Q

What is the difference between an interface and an abstract class?

A

An interface cannot contain any implementation, whereas an abstract class can have both abstract and concrete methods.

43
Q

What is an inner class?

A

An inner class is a class defined within another class and has access to the outer class’s members.

44
Q

True or False: Anonymous classes can be used to instantiate a class without giving it a name.

45
Q

What is the syntax to create an anonymous class?

A

By using the ‘new’ keyword followed by the class or interface name and overriding its methods.

46
Q

Fill in the blank: An ______ class is declared inside another class and can access the outer class’s members.

47
Q

What keyword is used to define an abstract class?

48
Q

Multiple Choice: Which of the following can be declared as abstract? A) Class B) Method C) Both A and B

A

C) Both A and B

49
Q

What happens if a class extends an abstract class?

A

It must implement all abstract methods of the parent abstract class unless it is also declared abstract.

50
Q

True or False: An interface can extend another interface.

51
Q

What is a static nested class?

A

A static nested class is a nested class that does not have access to instance variables and methods of the outer class.

52
Q

Fill in the blank: The keyword ______ is used to declare a class that cannot be subclassed.

53
Q

What is the default access modifier for class members in Java?

A

Package-private (no modifier specified)

54
Q

Multiple Choice: Which modifier makes a class member accessible only within its own package? A) private B) protected C) default

A

C) default

55
Q

What type of class cannot be instantiated directly?

A

Abstract class

56
Q

True or False: You can create an instance of an abstract class.

57
Q

What is the use of the ‘synchronized’ modifier?

A

The ‘synchronized’ modifier is used to control access to a method or block by multiple threads.

58
Q

Fill in the blank: Inner classes can access the ______ class’s members directly.

59
Q

What does it mean if a class is marked as ‘final’?

A

It cannot be subclassed.

60
Q

Multiple Choice: Which of the following is NOT a valid modifier in Java? A) public B) private C) global

61
Q

What is the benefit of using interfaces?

A

Interfaces allow for multiple inheritance of type and define a contract for classes to implement.

62
Q

True or False: A class can implement multiple interfaces.

63
Q

What is the result of declaring a method as ‘abstract’?

A

The method must be implemented in any concrete subclass.

64
Q

Fill in the blank: A class that contains at least one abstract method is considered ______.

65
Q

What is the main characteristic of an anonymous class?

A

It does not have a name and is defined at the point of instantiation.

66
Q

What is the purpose of the ‘volatile’ modifier?

A

The ‘volatile’ modifier indicates that a variable’s value will be modified by different threads.

67
Q

True or False: An abstract class can implement an interface.

68
Q

What is the result of a class implementing an interface?

A

The class must provide implementations for all methods declared in the interface.

69
Q

Fill in the blank: Methods in an interface are implicitly ______.

70
Q

What is the main purpose of using inner classes?

A

To logically group classes that are only used in one place and to increase encapsulation.