1. Declarations & Access Control Flashcards

1
Q

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

A

No

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

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

A

Yes

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

Does an interface method have to be implemented?

A

Yes

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

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

A

Yes

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

Is this a legal identifier:

:b

A

No, identifiers cannot start with a colon

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

Is this a legal identifier:

-d

A

No, identifiers cannot begin with a -

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

Can identifiers begin with a number?

A

No

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

Can # be part of an identifier?

A

No

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

Where can a public method be accessed from?

A

Anywhere

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

Where can a private method be accessed from?

A

Only within that class in which it is declared.

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

How many public classes per source file can there be?

A

1

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

What must come first, package statements or import statements?

A

Package statements

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

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

A

Any number

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

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

A

import static…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

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

What are the 3 access modifiers?

A

Public
Private
Protected

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

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

A

strictfp
final
abstract

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

What is the default access modifier that is not typed?

A

default

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

Can you have private classes?

A

No

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

Can you have protected classes?

A

No

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

What is the visibility of a class with default access?

A

Within same package only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

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

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

A

No

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

What is strictfp

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What does making a class final mean?
It can never be subclassed ie String class
26
What is the purpose of an abstract class?
To be extended (hence why it can't be both final and abstract)
27
What is the correct syntax for abstract methods located within an abstract class? public abstract void goFast(); public abstract void goFast() {};
public abstract void goFast(); nb abstract methods end in a semi-colon
28
If a method is marked abstract, does the class also need to be marked abstract?
Yes
29
Can abstract classes have non-abstract methods?
Yes, these usually have implementations that shouldn't change
30
Can a class be marked as both abstract and final?
No
31
What has to happen to classes which implement an interface?
They must implement the methods from the interface.
32
What are all interface methods implicitly?
public and abstract
33
Do you have to declare interface methods as public and abstract?
No, they are implicitly so.
34
What must all class methods that are implemented from interfaces be declared as?
public
35
Can interface methods be static?
No
36
Does an interface extend or implement other interfaces?
Extend
37
Can an interface extend a class?
No
38
Is this legal? | public abstract interface Rollable { }
Yes, but typing abstract is redundant
39
``` Is this legal in an interface? void bounce (); ```
Yes
40
Is this legal in an interface? | final void bounce ();
No, final and abstract cannot be used together, and abstract is implicit
41
Can interface methods be protected or private?
No, always public implicitly
42
Is this legal in an interface? | static void bounce ();
No, interfaces define instance methods not static methods
43
Can you put constants in an interface?
Yes
44
What must constants be (implicitly) if declared in an interface?
public static final
45
interface Woo { int BAR = 55; } Is this BAR public, static and final?
Yes, it is implicitly done so because it is in an interface.
46
Can the values from constants from an interface be changed?
No.
47
What are methods and instance variables collectively known as?
Members
48
What can you modify memebers with - access or non-access modifiers?
Both
49
How many access modifiers can you use on members?
All four
50
Zoo z = new Zoo(); How can one get access to a member from Zoo?
z.memberName();
51
class Moo extends Zoo {... How can one get access to a member from Zoo?
memberName();
52
What does the public access modifier mean is applied to members?
Can be access by all other classes regardless of package in which they belong
53
private String doRooThings(){} Where can this method be access from?
Only from within the class in which it was declared
54
Can a subclass inherit this method from its superclass? private int giveMeNumbers();
No, it is invisible to the subclass.
55
What happens if both a superclass method and the subclass method are the same, but the superclass method is private?
The subclass method is not overriding the superclass method because it is invisible to the superclass
56
What is the difference between these two methods? ``` void testIt(){} protected void testingIt(){} ```
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
protected int x = 9; Can this protected variable be accessed through the instantiation of a new class and using p.x?
No, it can only be accessed through inheritance by extending class.
58
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?
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
Can an access modifier be applied to a local variable ie private int x = 7;
No
60
What is the only modifier that can be applied to a local variable?
final final int x = 7;
61
How can a private member be accessed?
Only within it's own class
62
Where can a public member be access from?
From absolutely anywhere
63
Can final methods be overridden by the subclass?
No
64
public CD getListing(int trackNumber, final int lenght) {} Is final int lenght valid here?
Yes, it just can't be directly edited in the method.
65
What must a method be to be abstract? (2 options)
- declared as abtract | - have no functional code
66
Will this compile? ``` public class abstractClassNot{ public abstract void wee(); } ```
No, abstract methods can only be declared in classes where class is also declared abstract.
67
Will this compile? ``` public abstract class abstractClass{ public void wee() { //code galore } } ```
Yes, abstract classes can contain non-abstract methods with full implementations.
68
What must the first concrete subclass of an abstract class do?
Must implement all abstract methods of the superclass and the superclasses above it.
69
Will this compile? ``` public abstract class Animal { abstract void foo(); } class Horse extends Animal { void foo(int i) { } } ```
No, Horse has not implemented abstract method foo, it has attempted to overload it.
70
Is this legal? abstract final void doIt();
No, methods cannot be marked as both abstract and final.
71
Is this legal? abstract final void doIt();
No, methods cannot be marked as both abstract and final.
72
Is this legal? abstract static void printMe();
No, methods cannot be declared as both static and abstract.
73
Which of the four access modifiers can synchonized methods be modified with?
public private default protected
74
What 3 things need to be known for the native modifier?
- can only be applied to methods - reserve word like abstract methods, method ends in (;) and implementation is omitted.
75
What can strictfp be used on?
Class and methods only, never on a variable.
76
Can a constructor ever have a return type?
No.
77
What's the different? ``` protected Foo() {} protected void Foo() {} ```
The first is a constructor, and the second is a perfectly legal though hard to read method.
78
Can constructors use access modifiers?
Yes
79
Can constructors be marked static?
No
80
Can constructors be marked final or abstract?
No
81
What are the eight types of primitive data types?
- char - boolean - int - float - long - double - short - byte
82
What does a reference variable refer to?
An object.
83
Can the 6 primitive number types be negative values as well as positive values?
Yes, they can be both.
84
How many bits are in a byte?
8
85
How many bits are in a short?
16
86
How many bits are in an int?
32
87
How many bits are in a long?
64
88
How many bits are in a double?
64
89
How many bits are in a float?
32
90
What does a char contain?
a single, 16-bit Unicode character
91
Where are instance variables declared?
In the class, but outside of any method
92
What access modifiers can be used on instance variables?
All four
93
Can instance variables be marked final?
Yes
94
Can instance variables be marked static?
No, because then they would not be instance variables.
95
Are local variables on the stack or on the heap?
They are on the stack
96
What happens if the value of a local variable is passed to another method?
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
If a local variable is an object reference, will the object be created on the stack or on the heap?
The object will be created on the heap, no such thing as a stack object, only a stack variable.
98
What is the main difference in use between instance variables and local variables?
Local variables must be initialized before they are used as they don't get default values.
99
What is shadowing?
When a local variable is declared with the same name as an instance variable.
100
What are the two things that arrays store?
- multiple variables of the same type | - multiple variables that are all subclasses of the same type
101
Can arrays expand dynamically?
No
102
Are these legal? int [] key; int key []; Thread[] threads; Thread threads[];
Yes
103
Is this legal for a multidimensional array? String [] managerName [];
Yes
104
Is this legal? int [5] names;
No, sizes cannot be declared in the array declaration.
105
What does final mean for primitives?
The value can never be changed
106
What does final mean for references?
The reference to an object can never be changed, but the actual object could be changed.
107
What variables can volatile and transient be applied to?
Instance variables
108
Can initialization blocks be marked as static?
Yes
109
Can local variables be marked as static?
No
110
Is this legal? int $money = 34;
Yes, variables can begin with currency signs
111
Is this legal? int m0ney = 55;
Yes, variable names can contain numeric digits as long as they are not the first digit.
112
What is the length restriction on variable identifiers?
There is no length restriction.
113
Can main be overloaded?
Yes
114
What is the import syntax for static imports?
import static
115
How many public classes can a source file have?
1
116
Does the public class have to match the name of the source file?
Yes
117
What is the order of class layout?
package statement import statements class ( 1 public, many non-public)
118
What are the two access restrictions classes can have?
public | default
119
What are interface methods implicitly?
public and abstract
120
Can interfaces have constants?
Yes
121
What are interface constants implicitly?
public, static and final
122
Can a class implementing an interface itself be abstract?
Yes
123
Does an abstract implementing class have to implement the interface methods?
No, but the first concrete class does.
124
Do interfaces extend or implement other interfaces
Extend
125
What are methods and instance variables known as?
Members
126
Can a member be inherited from the superclass?
Yes
127
If members are accessed without the "." operator, what must be true?
They must belong to the same class.
128
What does this refer to?
The executing object
129
Are private members visible to subclasses?
No
130
Where can default members be accessed from?
Only within the same package?
131
Where can protected members be accessed from?
Same package plus subclasses (classes that have been extended)
132
Can local members have access modifiers
No
133
What is the only modifier a local members can have?
final
134
What must happen before a local variable is used?
It must be initialised
135
How do abstract methods end?
semicolon
136
What are the three ways of determining a non-abstract method?
- not marked abstract - has curly braces - MIGHT have code between curly braces
137
Why can abstract methods not be marked as final or private?
Because they must be implemented by the subclass
138
Can a local and instance variable have the same name?
Yes, this is called shadowing
139
Is there such a thing as a final object?
No, the reference to the object may be final and always point to that object, but the object itself can change always
140
Can an animal array accept horse objects?
Yes, if Horse extends Animal and hence horse is an animal and passes the IS-A test