1. Declarations & Access Control Flashcards Preview

JavaTest > 1. Declarations & Access Control > Flashcards

Flashcards in 1. Declarations & Access Control Deck (140)
Loading flashcards...
1
Q

Does a supercalss know anything about the subclasses that inherit from it?

A

No

2
Q

Is a subclass free to override instance variables and methods define in a superclass?

A

Yes

3
Q

Does an interface method have to be implemented?

A

Yes

4
Q

Can an _ be used at the beginning of an identifier?

A

Yes

5
Q

Is this a legal identifier:

:b

A

No, identifiers cannot start with a colon

6
Q

Is this a legal identifier:

-d

A

No, identifiers cannot begin with a -

7
Q

Can identifiers begin with a number?

A

No

8
Q

Can # be part of an identifier?

A

No

9
Q

Where can a public method be accessed from?

A

Anywhere

10
Q

Where can a private method be accessed from?

A

Only within that class in which it is declared.

11
Q

How many public classes per source file can there be?

A

1

12
Q

What must come first, package statements or import statements?

A

Package statements

13
Q

How many non-public classes can a source file have?

A

Any number

14
Q

What is the syntax for static imports:
import static…
static import…

A

import static…

15
Q

Can you use an import statement to search through the java API such as ‘import java.*’

A

No, this is legal but it will NOT search across packages

16
Q

What are the 3 access modifiers?

A

Public
Private
Protected

17
Q

What are the three nonaccess modifiers you need to know for the OCA exam

A

strictfp
final
abstract

18
Q

What is the default access modifier that is not typed?

A

default

19
Q

Can you have private classes?

A

No

20
Q

Can you have protected classes?

A

No

21
Q

What is the visibility of a class with default access?

A

Within same package only

22
Q

If a class is public, yet in a different package, do you still need to have an import statement to gain access to that class.

A

Yes

23
Q

Should a class ever be marked as both final and abstract?

A

No

24
Q

What is strictfp

A

a non-access modifier that can modifiy a method or a class

25
Q

What does making a class final mean?

A

It can never be subclassed ie String class

26
Q

What is the purpose of an abstract class?

A

To be extended (hence why it can’t be both final and abstract)

27
Q

What is the correct syntax for abstract methods located within an abstract class?
public abstract void goFast();
public abstract void goFast() {};

A

public abstract void goFast();

nb abstract methods end in a semi-colon

28
Q

If a method is marked abstract, does the class also need to be marked abstract?

A

Yes

29
Q

Can abstract classes have non-abstract methods?

A

Yes, these usually have implementations that shouldn’t change

30
Q

Can a class be marked as both abstract and final?

A

No

31
Q

What has to happen to classes which implement an interface?

A

They must implement the methods from the interface.

32
Q

What are all interface methods implicitly?

A

public and abstract

33
Q

Do you have to declare interface methods as public and abstract?

A

No, they are implicitly so.

34
Q

What must all class methods that are implemented from interfaces be declared as?

A

public

35
Q

Can interface methods be static?

A

No

36
Q

Does an interface extend or implement other interfaces?

A

Extend

37
Q

Can an interface extend a class?

A

No

38
Q

Is this legal?

public abstract interface Rollable { }

A

Yes, but typing abstract is redundant

39
Q
Is this legal in an interface?
void bounce ();
A

Yes

40
Q

Is this legal in an interface?

final void bounce ();

A

No, final and abstract cannot be used together, and abstract is implicit

41
Q

Can interface methods be protected or private?

A

No, always public implicitly

42
Q

Is this legal in an interface?

static void bounce ();

A

No, interfaces define instance methods not static methods

43
Q

Can you put constants in an interface?

A

Yes

44
Q

What must constants be (implicitly) if declared in an interface?

A

public static final

45
Q

interface Woo {
int BAR = 55;
}

Is this BAR public, static and final?

A

Yes, it is implicitly done so because it is in an interface.

46
Q

Can the values from constants from an interface be changed?

A

No.

47
Q

What are methods and instance variables collectively known as?

A

Members

48
Q

What can you modify memebers with - access or non-access modifiers?

A

Both

49
Q

How many access modifiers can you use on members?

A

All four

50
Q

Zoo z = new Zoo();

How can one get access to a member from Zoo?

A

z.memberName();

51
Q

class Moo extends Zoo {…

How can one get access to a member from Zoo?

A

memberName();

52
Q

What does the public access modifier mean is applied to members?

A

Can be access by all other classes regardless of package in which they belong

53
Q

private String doRooThings(){}

Where can this method be access from?

A

Only from within the class in which it was declared

54
Q

Can a subclass inherit this method from its superclass?

private int giveMeNumbers();

A

No, it is invisible to the subclass.

55
Q

What happens if both a superclass method and the subclass method are the same, but the superclass method is private?

A

The subclass method is not overriding the superclass method because it is invisible to the superclass

56
Q

What is the difference between these two methods?

void testIt(){}
protected void testingIt(){}
A

void testIt(){} - default access, only accessible if class is in same package

protected void testingIt(){} - protected access, can be accessed by classes outside of same package only through inheritance i.e. extending a class

57
Q

protected int x = 9;

Can this protected variable be accessed through the instantiation of a new class and using p.x?

A

No, it can only be accessed through inheritance by extending class.

58
Q

If a subclass entends superclass and has access to protected members in the superclass, can another class extend child and have access to the first superclass’ members?

A

No, once they have been extend and viewed by the subclass, they are then essentially private and cannot be viewed any further by any more subclasses.

59
Q

Can an access modifier be applied to a local variable ie

private int x = 7;

A

No

60
Q

What is the only modifier that can be applied to a local variable?

A

final

final int x = 7;

61
Q

How can a private member be accessed?

A

Only within it’s own class

62
Q

Where can a public member be access from?

A

From absolutely anywhere

63
Q

Can final methods be overridden by the subclass?

A

No

64
Q

public CD getListing(int trackNumber, final int lenght) {}

Is final int lenght valid here?

A

Yes, it just can’t be directly edited in the method.

65
Q

What must a method be to be abstract? (2 options)

A
  • declared as abtract

- have no functional code

66
Q

Will this compile?

public class abstractClassNot{
public abstract void wee();
}
A

No, abstract methods can only be declared in classes where class is also declared abstract.

67
Q

Will this compile?

public abstract class abstractClass{
public void wee() {
//code galore
}
}
A

Yes, abstract classes can contain non-abstract methods with full implementations.

68
Q

What must the first concrete subclass of an abstract class do?

A

Must implement all abstract methods of the superclass and the superclasses above it.

69
Q

Will this compile?

public abstract class Animal {
abstract void foo();
}
class Horse extends Animal {
void foo(int i) { }
}
A

No, Horse has not implemented abstract method foo, it has attempted to overload it.

70
Q

Is this legal?

abstract final void doIt();

A

No, methods cannot be marked as both abstract and final.

71
Q

Is this legal?

abstract final void doIt();

A

No, methods cannot be marked as both abstract and final.

72
Q

Is this legal?

abstract static void printMe();

A

No, methods cannot be declared as both static and abstract.

73
Q

Which of the four access modifiers can synchonized methods be modified with?

A

public
private
default
protected

74
Q

What 3 things need to be known for the native modifier?

A
  • can only be applied to methods
  • reserve word
    like abstract methods, method ends in (;) and implementation is omitted.
75
Q

What can strictfp be used on?

A

Class and methods only, never on a variable.

76
Q

Can a constructor ever have a return type?

A

No.

77
Q

What’s the different?

protected Foo() {}
protected void Foo() {}
A

The first is a constructor, and the second is a perfectly legal though hard to read method.

78
Q

Can constructors use access modifiers?

A

Yes

79
Q

Can constructors be marked static?

A

No

80
Q

Can constructors be marked final or abstract?

A

No

81
Q

What are the eight types of primitive data types?

A
  • char
  • boolean
  • int
  • float
  • long
  • double
  • short
  • byte
82
Q

What does a reference variable refer to?

A

An object.

83
Q

Can the 6 primitive number types be negative values as well as positive values?

A

Yes, they can be both.

84
Q

How many bits are in a byte?

A

8

85
Q

How many bits are in a short?

A

16

86
Q

How many bits are in an int?

A

32

87
Q

How many bits are in a long?

A

64

88
Q

How many bits are in a double?

A

64

89
Q

How many bits are in a float?

A

32

90
Q

What does a char contain?

A

a single, 16-bit Unicode character

91
Q

Where are instance variables declared?

A

In the class, but outside of any method

92
Q

What access modifiers can be used on instance variables?

A

All four

93
Q

Can instance variables be marked final?

A

Yes

94
Q

Can instance variables be marked static?

A

No, because then they would not be instance variables.

95
Q

Are local variables on the stack or on the heap?

A

They are on the stack

96
Q

What happens if the value of a local variable is passed to another method?

A

The local variable itself still only lives during the lifetime of the method and will cease to exist at the end of the method.

97
Q

If a local variable is an object reference, will the object be created on the stack or on the heap?

A

The object will be created on the heap, no such thing as a stack object, only a stack variable.

98
Q

What is the main difference in use between instance variables and local variables?

A

Local variables must be initialized before they are used as they don’t get default values.

99
Q

What is shadowing?

A

When a local variable is declared with the same name as an instance variable.

100
Q

What are the two things that arrays store?

A
  • multiple variables of the same type

- multiple variables that are all subclasses of the same type

101
Q

Can arrays expand dynamically?

A

No

102
Q

Are these legal?

int [] key;
int key [];
Thread[] threads;
Thread threads[];

A

Yes

103
Q

Is this legal for a multidimensional array?

String [] managerName [];

A

Yes

104
Q

Is this legal?

int [5] names;

A

No, sizes cannot be declared in the array declaration.

105
Q

What does final mean for primitives?

A

The value can never be changed

106
Q

What does final mean for references?

A

The reference to an object can never be changed, but the actual object could be changed.

107
Q

What variables can volatile and transient be applied to?

A

Instance variables

108
Q

Can initialization blocks be marked as static?

A

Yes

109
Q

Can local variables be marked as static?

A

No

110
Q

Is this legal?

int $money = 34;

A

Yes, variables can begin with currency signs

111
Q

Is this legal?

int m0ney = 55;

A

Yes, variable names can contain numeric digits as long as they are not the first digit.

112
Q

What is the length restriction on variable identifiers?

A

There is no length restriction.

113
Q

Can main be overloaded?

A

Yes

114
Q

What is the import syntax for static imports?

A

import static

115
Q

How many public classes can a source file have?

A

1

116
Q

Does the public class have to match the name of the source file?

A

Yes

117
Q

What is the order of class layout?

A

package statement
import statements
class ( 1 public, many non-public)

118
Q

What are the two access restrictions classes can have?

A

public

default

119
Q

What are interface methods implicitly?

A

public and abstract

120
Q

Can interfaces have constants?

A

Yes

121
Q

What are interface constants implicitly?

A

public, static and final

122
Q

Can a class implementing an interface itself be abstract?

A

Yes

123
Q

Does an abstract implementing class have to implement the interface methods?

A

No, but the first concrete class does.

124
Q

Do interfaces extend or implement other interfaces

A

Extend

125
Q

What are methods and instance variables known as?

A

Members

126
Q

Can a member be inherited from the superclass?

A

Yes

127
Q

If members are accessed without the “.” operator, what must be true?

A

They must belong to the same class.

128
Q

What does this refer to?

A

The executing object

129
Q

Are private members visible to subclasses?

A

No

130
Q

Where can default members be accessed from?

A

Only within the same package?

131
Q

Where can protected members be accessed from?

A

Same package plus subclasses (classes that have been extended)

132
Q

Can local members have access modifiers

A

No

133
Q

What is the only modifier a local members can have?

A

final

134
Q

What must happen before a local variable is used?

A

It must be initialised

135
Q

How do abstract methods end?

A

semicolon

136
Q

What are the three ways of determining a non-abstract method?

A
  • not marked abstract
  • has curly braces
  • MIGHT have code between curly braces
137
Q

Why can abstract methods not be marked as final or private?

A

Because they must be implemented by the subclass

138
Q

Can a local and instance variable have the same name?

A

Yes, this is called shadowing

139
Q

Is there such a thing as a final object?

A

No, the reference to the object may be final and always point to that object, but the object itself can change always

140
Q

Can an animal array accept horse objects?

A

Yes, if Horse extends Animal and hence horse is an animal and passes the IS-A test