Chapter 1: Declarations and Access Control Flashcards

1
Q

What can identifiers begin with?

A

Identifiers can begin with 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

Can identifiers include digits?

A

Yes, after the first character, identifiers can also include digits.

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

Does an identifier have a length limit?

A

No, identifiers can be of any length.

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

What two commands are used to compile and execute Java programs?

A

javac and java.

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

What main() signature has special powers?

A

Method signatures equivalent to public static void main(String[] args)

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

Can the main() be overloaded?

A

Yes, main() can be overloaded.

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

What is the purpose of an Import statement?

A

An import statement’s only job is to save keystrokes.

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

What symbol is used to search through the contents of a single package?

A

asterisk (*) 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 syntax is correct: “Static Import” or “Import Static”

A

The syntax is import static….

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

What type of classes can be imported?

A

You can import API classes and/or custom classes.

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

How many public classes can a source file have?

A

A source code file can have only one public class.

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

When is a filename required to have the same name as a class?

A
If the source file contains a public class, the filename must match the
public class name.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How many package statements can a file have?

A

A file can have only one package statement, but it can have multiple
imports.

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

Where should a import statement appear in a file?

A

The import statements (if any) must come 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

What classes do package and import statements affect?

A

package and import statements apply to all classes in the file.

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

True or False: A file can only have one class.

A

False - A file can have more than one nonpublic class.

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

True or False: Files with no public classes have no naming restrictions.

A

True - Files with 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 the three access modifiers?

A

There are three access modifiers: public, protected, and private.

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

What are the four access levels?

A

There are four access levels: public, protected, default, and private.

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

True or False: Classes can have protected and private access.

A

False - Classes can have only public or default access.

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

What classes can access a class with default access?

A

A class with default access can be seen only by classes within the same package.

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

What does class visitability revolve around?

A

Class visibility revolves around whether code in one class can

  • 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
23
Q

True or False: A class can be both final and abstract.

A

False - A class cannot be both final and abstract.

24
Q

True or False: A final class cannot be subclassed.

A

True - A final class cannot be subclassed.

25
Q

True or False: An abstract class can be instantiated.

A

False - An abstract class cannot be instantiated.

26
Q

True or False: A concrete class can have an abstract method.

A
False - A single abstract method in a class means the whole class must be
abstract.
27
Q

True or False: An abstract class can have abstract and non abstract methods.

A

True - An abstract class can have both abstract and nonabstract methods.

28
Q

What must the first class that extends an abstract class do?

A

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

29
Q

What are Interfaces?

A

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

30
Q

What type of class can implement an interface?

A

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

31
Q

True or False: An interface is like a 100-percent abstract class and is implicitly abstract whether you type the abstract modifier in the declaration or not.

A

True - Except in Java 8 Statics and Default methods can have implementation.

32
Q

What two modifiers are applied to interface methods by default?

A

public and abstract—explicit declaration of these modifiers is optional.

33
Q

What three modifiers are applied to interface fields by default?

A

Interfaces can have constants, which are always implicitly public, static, and final. (optional in any combination.)

34
Q

What are the properties of a legal non-abstract implementing class?

A

It provides concrete implementations for the interface’s methods.

It must follow all legal override rules for the methods it implements.

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.

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).

35
Q

True or False: A class implementing an interface cannot be abstract.

A

False - A class implementing an interface can itself be abstract.

36
Q

True or False: An abstract implementing class must implement the interface methods.

A

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

37
Q

In terms of inheritance, what is the main difference between a class and an interface?

A

A class can extend only one class (no multiple inheritance), but it can implement many interfaces.

38
Q

True or False: An interface can extend one or more other interfaces.

A

True - Interfaces can extend multiple interfaces.

39
Q

Can interfaces extend a class or implement a class or interface?

A

No, interfaces cannot extend a class or implement a class or interface.

40
Q

What is a member of a class?

A

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

41
Q

What access levels can members have?

A

Members can use all four access levels: public, protected, default, and private.

42
Q

When can a member be accessed without the ( . ) operator?

A

Members accessed without the dot operator (.) must belong to the same class.

this.aMethod() is the same as just invoking aMethod().

43
Q

What is the proper way to declare a var-arg?

A

A var-arg parameter is declared with the syntax type… name; for instance: doStuff(int… x) { }.

44
Q

True or False: A var-arg parameter type can appear anywhere in a parameter list along side normal parameters.

A

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

45
Q

True or False: Instance variables can’t be abstract, synchronized, native, or strictfp.

A

True: However, they can have any access level and be marked final.

46
Q

What is it called when a local variable has the same name as an instance variable?

A

This is called “shadowing.”

47
Q

True or False: final variables can refer to different objects once the object has been assigned to a final variable.

A

False - final reference variables cannot refer to a different object once the object has been assigned to the final variable.

48
Q

When must a final variable be initialized?

A

Before the constructor completes.

49
Q

True or False: Java allows for final objects.

A

False - There is no such thing as a final object. An object reference marked final does NOT mean the object itself can’t change.

50
Q

True or False: An array is an object.

A

True - Arrays can hold primitives or objects, but the array itself is always an object.

51
Q

True or False: Array brackets can only appear to the left of the variable name.

A

False - When you declare an array, the brackets can be to the left or to the right of the variable name.

52
Q

When is it legal to declare the size of an array in the declaration?

A

It is never legal to include the size of an array in the declaration.

53
Q

True or False: An array of type animal cannot hold any subclasses of the animal type.

A

False - An array of objects can hold any object that passes the IS-A (or instanceof) test for the declared type of the array. For example, if Horse extends Animal, then a Horse object can go into an Animal array.

54
Q

If a variable is marked static, it is not tied to _____.

A

any particular instance of a class.

55
Q

True or False: A class instance is required to use a its static methods.

A

False - No class instances are needed in order to use static members of the class.

56
Q

How many copies of a static/class variable are available to instances of that class?

A

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

57
Q

True or False: static methods do not have direct access to nonstatic members.

A

True - Static methods are unaware of instances. However, instances are aware of static variables/methods.