1. Declarations and Access Control Flashcards

1
Q

How identifiers can begin, what chars can have after that and what could be their length ?

A

Identifiers can begin with a letter, an underscore, or a currency character. After the first character, identifiers can also include digits. Identifiers can be of any length.

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

How you can compile and execute Java programs without IDE ?

A

You can compile and execute Java programs using the command-line programs javac and java, respectively.

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

What is the special signature of main() method? Could it be overloaded ? Write three different legal declarations of main() method.

A

The only versions of main() methods with special powers are those versions with method signatures equivalent to public static void main(String[] args). main() can be overloaded.

static public void main(String[] args)
public static void main(String… x)
static public void main(String bang_a_gong[])

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

Static imports syntax ?

A

the syntax is import static…

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

How many public and nonpublic classes can have a source code file ?

A

A source code file can have only one public class and more than one nonpublic class.

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

How many packages and imports can have a source file ?

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

Order of import, class declaration and package ?

A
The package statement (if any) must be the first (noncomment) line in a source file. 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
8
Q

How many access modifiers, general/class access levels you know?

A

There are three access modifiers: public, protected, and private. There are four access levels: public, protected, default, and private. Classes can have only public or default access.

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

What is the difference between default and public class access ?

A

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

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

What nonaccess modifiers can have a class ?

A

Classes can also be modified with final, abstract, or strictfp.

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

is the following class definition correct and why:

public final abstract class Tortoise { }

A

A class cannot be both final and abstract. By definition, an abstract class is one that must be extended by another class to be instantiated, whereas a final class can’t be extended by another class. By marking an abstract class as final, you’re saying the class can never be instantiated, so the compiler refuses to process the code.

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

Can an abstract class be instantiated ?

When a class is required to be marked as an abstract one?

What types of method can have an abstract class ?

When the abstract methods in an abstract class should be implemented ?

A

An abstract class cannot be instantiated.

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

An abstract class can have both abstract and nonabstract methods.

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
13
Q

What are the differences and similarities between interfaces and abstract classes ? Can an abstract class implement an interface ?

A

An interface is like a 100% abstract class and is implicitly abstract whether you type the abstract modifier in the declaration or not.

An interface can have only abstract methods, no concrete methods allowed.

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

A class implementing an interface can itself be abstract. An abstract implementing class does not have to implement the interface methods (but the first concrete subclass must).

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

What is called class member ?

A

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

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

What access modifiers can class members have ?

A

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

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

How class visibility is related to its members visibility ?

A

If a class cannot be accessed, its members cannot be accessed. Determine class visibility before determining member visibility.

17
Q

Difference between public and private member access modifier ?

A
public members can be accessed by all other classes, even in other packages. If a superclass member is public, the subclass inherits it—regardless of
package. private members can be accessed only by code in the same class. private members are not visible to subclasses, so private members cannot be inherited.
18
Q

What “this.” means ?

A

this. always refers to the currently executing object. this.aMethod() is the same as just invoking aMethod().

19
Q

Difference between default and protected member access modifier ?

A

Default and protected members differ only when subclasses are involved:

  • Default members can be accessed only by classes in the same package;
  • Protected members can be accessed by other classes in the same package, plus subclasses regardless of package;
  • protected = package + kids (kids meaning subclasses);
  • For subclasses outside the package, the protected member can be accessed only through inheritance; a subclass outside the package cannot access a protected member by using a reference to a superclass instance. (In other words, inheritance is the only mechanism for a subclass outside the package to access a protected member of its superclass.);
- 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;
20
Q

Can local variables have non-access or access modifiers ?

A

Local (method, automatic, or stack) variable declarations cannot have access
modifiers. final is the only modifier available to local variables.

21
Q

Do local variables get default values ?

A

Local variables don’t get default values, so they must be initialized before use.

22
Q

What other non access member modifiers have ?

A
  • final methods cannot be overridden in a subclass;
  • abstract methods are declared with a signature, a return type, and an optional throws clause, but they are not implemented. The first nonabstract (concrete) class to extend an abstract class must implement all of the abstract class’ abstract methods. abstract methods end in a semicolon—no curly braces and they could not be final or private;
  • the synchronized modifier applies only to methods and code blocks. synchronized methods can have any access control and can also be marked final;
  • The native modifier applies only to methods - indicate that the method is implemented in native code using JNI(Java Native Interface);
  • The strictfp modifier applies only to classes and methods - ensures that you get exactly the same results from your floating point calculations on every platform;
23
Q

What is var-arg method ?

A

Var-arg method is methods that can declare a parameter that accepts from zero to many arguments. A var-arg parameter is declared with the syntax type… name; for instance: doStuff(int… x) { }. A var-arg method can have only one var-arg parameter. In methods with normal parameters and a var-arg, the var-arg must come last.

24
Q

Can we declare a local and instance variable with the same name ?

A

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

25
Q

Can we mark an object with final ?

A

Yes, we can create: final Object obj = new Object(); , but there is no such thing as a final object. An object reference marked final does NOT mean the object itself can’t change.

26
Q

What is 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. An enum can be declared outside or inside a class, but NOT in a method. An enum declared outside a class must NOT be marked static, final, abstract, protected, or private.

27
Q

What the following commads do:

javac -help
javac -version Foo.java Bar.java

A

The first invocation doesn’t compile any files, but prints a summary of valid
options. The second invocation passes the compiler an option (-version, which
prints the version of the compiler you’re using), and passes the compiler two .java
files to compile (Foo.java and Bar.java). Whenever you specify multiple options and/or files, they should be separated by spaces.

Structural overview: javac [options] [source files]

28
Q

What the following commad does: java -version MyClass x 1 ?

A
This command can be interpreted as "Show me the version of the JVM being
used, and then launch the file named MyClass.class and send it two String
arguments whose values are x and 1."

The java command is used to invoke the Java Virtual Machine (JVM): “java [options] class [args]”. The [options] and [args] parts of the java command are optional, and they can both have multiple values.You must specify exactly one class file to execute, and the java command assumes you’re talking about a .class file, so you don’t specify the .class extension on the command line.