1.Declarations and Access Control Flashcards

1
Q

Identifiers can begin with…

A

a letter, an underscore, or a currency character

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

After the first character…

A

identifiers can also include digits

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

Identifier length can be…

A

Identifiers can be of any length

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

You can compile and execute Java programs using the command-line programs…

A

java and javac. Both programs support a variety of

command-line options.

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

The only versions of main() methods with special powers are those
versions with method signatures equivalent to…

A

public static void main(String[] args)

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

can main be overloaded?

A

Yes

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

what does an import statement is good for?

A

only job is to save keystrokes

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

why do we use asterisk (*) in imports?

A

to search through the contents of a single

package

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

what is the syntax for static imports?

A

import static….

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

how many public classes can a source file have?

A

only one

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

If the source file contains a public class, what can be the class names?

A
the filename must match the
public class name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How many package statements can a file have?

A

Only one

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

Where is the correct place for package statement

A

the first line in a source file

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

Where is the correct place for import statement

A

after the package and before the class declaration. If there is no package statement, import statements must be the first (noncomment) statements in the source file.

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

how the package and import statements apply to classes in a file

A

to all classes in the file

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

how many nonpublic classes can a file have?

A

more than one

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

How can we name nonpublic classes?

A

no public classes have no naming restrictions

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

What are class access modifiers?

A

public, protected, and private

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

How many access levels are there?

A

public, protected, default, and private

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

Which accesses a class can have?

A

public or default access

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

A class with default access can be seen..

A

only by classes within the same

package.

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

A class with public access can be seen by…

A

all classes from all packages.

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

Class visibility revolves around whether code in one class can…

A

❑ Create an instance of another class
❑ Extend (or subclass) another class
❑ Access methods and variables of another class

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

Nonaccess modifiers for classes are…

A

final, abstract, or strictfp

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

Can a class be both final and abstract?

A

No

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

What does it mean for a class to have “final” as modifier?

A

A final class cannot be subclassed.

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

What does it mean for a class to have “abstract” as modifier?

A

An abstract class cannot be instantiated

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

Can a non abstract class have an abstract method inside?

A

No. A single abstract method in a class means the whole class must be abstract.

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

Can an abstract class have non abstract methods?

A

Yes, an abstract class can have both abstract and nonabstract methods.

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

When should the abstract methods be implemented?

A

The first concrete class to extend an abstract class must implement all of its abstract methods

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

What is an interface?

A

Interfaces are contracts for what a class can do, but they say nothing about the way in which the class must do it.

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

What is the rule for implementing an interface in means of hierarchy tree?

A

Interfaces can be implemented by any class, from any inheritance tree

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

Can an interface have non-abstract classes?

A
An interface is like a 100-percent abstract class and is implicitly abstract whether you type the abstract modifier in the declaration or not. So an interface can have only abstract methods, no concrete methods
allowed.
34
Q

What are the implicit modifiers for an interface?

A

Interface methods are by default public and abstract—explicit declaration of these modifiers is optional

35
Q

What are the implicit modifiers for interface constants?

A

public,static, and final.

36
Q

What is the rule for a non-abstract interface implementing class in means of exceptions?

A

It must not declare any new checked exceptions for an implementation method.It must not declare any checked exceptions that are broader than the exceptions declared in the interface method. It may declare runtime exceptions on any interface method
implementation regardless of the interface declaration.

37
Q

How can be the signture of methods in a class that is implementing an interface?

A

It must maintain the exact signature (allowing for covariant returns) and return type of the methods it implements (but does not have to declare the exceptions of the interface).

38
Q

Can a class implementing an interface be abstract?

A

Yes. An abstract implementing class does not have to implement the interface methods (but the first concrete subclass must).

39
Q

How many interfaces an interface can extend?

A

One or more.

40
Q

How many interfaces or classes an interface can implement?

A

Interfaces cannot extend a class or implement a class or interface.

41
Q

What are the members of a class?

A

Methods and instance (nonlocal) variables are known as “members.”

42
Q

How many access levels can members have?

A

public, protected, default, and private.

43
Q

What are the ways to access members?

A
❑ Code in one class can access a member of another class.
❑ A subclass can inherit a member of its superclass.
44
Q

What does “this.” refer to?

A

always refers to the currently executing object

45
Q

Can private members be inherited?

A

No, they are not visible to subclasses.

46
Q

What is the difference between package & default*

A

protected = package + kids (kids meaning subclasses).

47
Q

Can a subclass outside the package access a protected member using a reference to the class?

A

No. Inheritance is the only mechanism for a subclass outside the package to access a protected member of its superclass.

48
Q

Can the members of the same package with a sub class access a protected member that the subclass inherited?

A

A protected member inherited by a subclass from another package is not accessible to any other class in the subclass package, except for the subclass’ own subclasses.

49
Q

Can local variables have access modifiers?

A

No

50
Q

Which modifiers local variables can use?

A

Only final.

51
Q

Do local vriables get default values?

A

No, they must be initialized.

52
Q

What does “final” mean for a method?

A

final methods cannot be overridden in a subclass.

53
Q

What does “abstract” mean for a method?

A

abstract methods are declared with a signature, a return type, and an optional throws clause, but they are not implemented.

54
Q

Do abstract methods have curly braces?

A

No, abstract methods end in a semicolon—no curly braces.

55
Q

How should a first non-abstract class implement methods that extends an abstract class?

A

The first nonabstract (concrete) class to extend an abstract class must implement all of the abstract class’ abstract methods.

56
Q

How is synchronized applied?

A

The synchronized modifier applies only to methods and code blocks. synchronized methods can have any access control and can also be marked final.

57
Q

Can abstract methods be private?

A

No, because abstract methods must be implemented by a subclass.

58
Q

Can abstract methods be final?

A

No, because abstract methods must be implemented by a subclass.

59
Q

Who can have native modifier?

A

The native modifier applies only to methods.

60
Q

Who can have strictfp modifier?

A

The strictfp modifier applies only to classes and methods.

61
Q

How many var-arg’s can a method have?

A

A var-arg method can have only one var-arg parameter.

62
Q

Where is the location for var-arg parameter in a method?

A

In methods with normal parameters and a var-arg, the var-arg must come last.

63
Q

Which access modifiers can an instance variable have?

A

Any.

64
Q

Which of these a variable can have; final, transient, abstract, synchronized, native, or strictfp?

A

Instance variables can be marked as final or transient but can’t be abstract, synchronized, native, or strictfp.

65
Q

It is legal to declare a local variable with the same name as an instance
variable; this is called..

A

Shadowing.

66
Q

Can final variables be reassigned?

A

No, final variables cannot be reassigned once assigned a value.

67
Q

Can final variable refer to a different object?

A

final reference variables cannot refer to a different object once the object has been assigned to the final variable. But there is no such thing as a final object. An object reference marked final does NOT mean the object itself can’t change.

68
Q

When a final variable should be assigned?

A

final variables must be initialized before the constructor completes.

69
Q

The transient and volatile modifiers apply only to…

A

instance variables.

70
Q

Is array an object?

A

Yes, arrays can hold primitives or objects, but the array itself is always an object.

71
Q

An array of objects can hold any object that passes the..

A

IS-A (or instanceof) test for the declared type of the array.

72
Q

Which ones are legal array declarations?
int[] key;
int key [];
int[5] scores;

A

1 and 2. Remember, the JVM doesn’t allocate space until you actually instantiate the array object. That’s when size matters. It is never legal to include the size of an array in the declaration.

73
Q

How many copies are there for static classes/variables?

A

There is only one copy of a static variable/class and all instances share it.

74
Q

Can static methods access nonstatic members?

A

No, static methods do not have direct access to nonstatic members.

75
Q

What is an enum?

A

An enum specifies a list of constant values assigned to a type.An enum is NOT a String or an int; an enum constant’s type is the enum type. For example, SUMMER and FALL are of the enum type Season.

76
Q

Where are the places for declaring enums?

A

An enum can be declared outside or inside a class, but NOT in a method. But an enum declared outside a class must NOT be marked static, final, abstract, protected, or private.

77
Q

What can an enum contain?

A

enums can contain constructors, methods, variables, and constant-specific class bodies.

78
Q

Can enum constructor be overloaded?

A

Yes.

79
Q

Can enum constructors be directly invoked?

A

No, never. They are always called automatically when an enum is initialized.

80
Q

Which one is a legal enum declaration?
enum Foo { ONE, TWO, THREE}
enum Foo { ONE, TWO, THREE};

A

Both, semicolon at the end of an enum declaration is optional.

81
Q

What does MyEnum.values() return?

A

ıt returns an array of MyEnum’s values.