Basics Flashcards

1
Q

When is a prefix operator evaluated?

A

Before its FIRST use in the expression

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

When is a postfix operate evaluated?

A

After it is FIRST used in the expression

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

Which packages are automatically inherited into every Java source file?

A

java.lang. This does not mean that you can’t import it explicitly, its just not required.

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

How many public classes can appear at the top level of a source file?

A

Only 1. There can be public nested classes, but not more than 1 top-level public class. You can have more than 1 class at the top level so long as only 1 is public

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

Does an abstract class need abstract methods to be abstract?

A

No. An abstract class simply means it is not a concrete class and cannot be instantiated.

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

Can you import a class statically?

A

No, only static methods can be imported. “import static a.b.C” where C is a class is not permitted

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

Can you import a method statically?

A

Yes. To import all static methods of a class use the .* notation or just reference the method name to import only that method

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

Does importing a class give you direct access to its members?

A

No, you must explicitely import them.

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

Is it valid to use () in front of a method name when importing that static method

A

No, this is not allowed.

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

What is the order for declaration of class/interface definitions, import statements and package statements?

A

(1) Package statement (2) import statements (3) class/interface declarations

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

How many package statements can occur in a source file?

A

1 and only 1. If there is no package statement then the classes/interfaces defined in the class can NEVER be referenced by a class in another package

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

What java statements can be annotated with the abstract keyword and what does it mean in each case?

A

(1) abstract class - it cannot be instantiated (2) abstract method - needs to be implemented by child classes and the class must be declared abstract. You CANNOT have abstract variables.

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

Where can the final keyword appear and what does it mean?

A

(1) final class - you cannot extend the class (2) final variable - you cannot change the variable once its value is set either in the declaraiton or the constructor (3) final method - you cannot override the method

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

Where can the keyword static appear and what does it mean?

A

(1) nested classes NOT top-level classes (2) variables (3) methods - in each case, only 1 entity appears per class in the JVM

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

Define what class cohesiveness means?

A

It is ensuring that each of your classes only does what it is intended for with clear boundaries and responsibilities

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

Descrbe each of the rules used by the Java compiler to determine if an identifier is legal?

A

(1) The identifier can start with loweror upper case alphabet, (2) can’t start with numbers, (3) can start with underscore or connecting symbols, (4) can start with any currency-based symbol. (5) Can be any length in theory. (6) cannot be a java keyword, and is case-sensitive (7) the characters used can be any letter, currency characters, connecting characters and numbers

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

Are interfaces objects?

A

No.

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

What are the 8 rules of source file declarations?

A

(1) There can only be 1 public class in a source code file (2) Comments can appear anywhere
(3) Package statements, if included must be the first line of the file
(4) If there is a public class, it must match the name of the source code file
(5) If there are import statements they must appear between the package statement and class/interface declarations
(6) import and package statementa apply to all class in the file - you cannot declare classes in the same source code file in different packages or with different import statements
(7) A file can have more than one nonpublic class
(8) Files with no public class do not have to have a class whose name matches the source code file

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

What are the 5 requirements for a method to be executable in a class?

A

(1) call main
(2) return type is void
(3) accepts an array of strings
(4) be static
(5) be public

Note this means the main method can be final

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

Can the main() method be overloaded?

A

Yes, but any other main method will not be executable. It will just be treated as a normal main method.

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

Can a main method accept a varargs?

A

Yes. The following is perfectly legitimate -

public static void main (String .. args)

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

What is a classes fully qualified name? Give an example

A

The fully qualified name of a class is its class name plus the package path to get to it e.g. java.util.ArrayList and not just ArrayList.

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

Can you use a static import statement to import non-static entities?

A

No - you can only use static imports to import static members of a class

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

What is the order of the static and import keywords in a static import statement?

A

always, always “import static”. You cannot have static import instead

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

Describe import statements

A

Import statements allow you to save typing by importing classes that are not part of this classes package. In order to use a class you must explicitly import it or it is included as part of a wildcard.

e.g. import java.util.*; or 
import java.util.ArrayList will both give you access to ArrayList. You can also import all static methods of a class by doing static import java.lang.System.*;

Bear in mind that if you are importing two classes with the same name, the compiler will complain and you will have to explcitely use their fully qualified names or rename the classes

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

What will java.*; do?

A

This will import all non-static classes and interfaces in the java package if there are any. It will compile even if the package contains no classes or interfaces.

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

What are the two other names typically associated with inner classes?

A

Nested classes and anonymous classes

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

What is a top-level class?

A

It is a class that appears at the top level of a Java source code file

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

What are the two types of modifiers and what modifiers fall into each type?

A

Access and non-access. Access modifiers - public, protected, private; Non-access - final, static, abstract

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

What are the two access control modifiers that can be used on a top-level class?

A

public and none. (package private).

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

Why can’t you have a protected top-level class in a source code file?

A

This is semantically equivalent to using the public access modifier. It makes sense that the class is only visible in the package but not for sub-classing, as it would need to be made public to be visible.

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

Can you combine final and abstract? Why?

A

Final prevents the class from being subclasses, but asbtract means the class has to be subclassed because it cannot be instantiated, hence they cannot be used together.

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

What Java entities can strictfp be used on?

A

A class and method, but never a variable. OCP to explain why

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

Why would you use final on a class?

A

Final means that you cannot subclass that class and is only used when you want to guarentee the implementation of that class where none of your methods are overriden in the entire class. If it is not the entire class, then use final on the method which is more common. Ask - If this class is subclassed, then could this change the expected behaviour in a bad way? Use it carefully, as you are effectively saying that this class cannot be improved upon or substituted with a subclass if a problem is found with it. Using final stops your code from being extensible which is bad OO design.

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

What is an abstract class and why would you use it?

A

Abstract classes cannot be instantiated. They don’t require abstract methods. They exist soley to be subclassed and provide a way to create general/abstract behaviour to all subclasses.

NOTE : You can compile and even execute an abstract class - you just can’t instantiate it.

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

Can abstract apply to methods and variables?

A

Only classes and methods. It doesn’t make sense to have an abstract variable. If a class has abstract methods, then it must also be declared as abstract.

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

When marking a method as abstract what else must you do?

A

Not provide a method body defined by the curly brackets. You do, however, provide any parameters for the signature including () if that is necessary.

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

Are interfaces abstract?

A

Yes, but it is redundant to mark an interface as abstract as it is implied in the nature of the entity. All interfaces are abstract! Likewise, you don’t have to mark a method as being abstract in an interface, even though an interface contains only abstract entities (methods).

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

What is an interface?

A

An interface is a contract that can be implemented by any class. It is a 100% abstract class.

NOTE : The compiler adds “public abstract” to all method declarations in an interface and the class is implicitely public.

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

What are the 9 rules of an interface?

A

(1) All interface methods are implictely public and abstract
(2) Any variables declared must be public, static and final
(3) Interface methods cannot be static
(4) Methods cannot be marked as final because they are inheritently abstract
(5) An interface can extend one or more other interfaces
(6) An interface is not an object and cannot extend an object
(7) An interface cannot implement another interface
(8) Declared with the keyword interface rather than class
(9) Interface types can be used polymorphically

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

Can you explicitely declare an interface as abstract?

A

Yes, but it is redundant.

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

what access can modifiers can be used on an interface?

A

Either public or default access. Protected and private are not allowed for similar reasons that a protected and private top-level class are not allowed.

43
Q

How many top-level classes/interfaces can youhave?

A

As many as you like, but you can only have one public class/interface and its name must be th same as the source code file.

44
Q

Can an interface have static methods?

A

No, an interface only defines instance methods.

45
Q

Can interface methods have access modifiers other than public?

A

Declaring an interface method as private or protected will not compile. They don’t make sense in the context of an interface.

46
Q

Describe interface constants

A

In the same way that you don’t define a method as public and abstract but it is, an varibles you declare will also be public, static and final and treated as such. You cannot have a variable declared as being private or protected. they are not allowed in an interface.

47
Q

What are the only 2 access modifiers than can be used for a class? (Note - not a nested class that is treated as a different entity in Java)

A

public and default access

48
Q

What are the two different relationship types between classes?

A

Is-a and has-a. If a class has access to another class, then it is either through inheritance or via the package structure. Access across packages is provided by the dot operator and access of super classes is provided through inheritance

49
Q

What does it mean for code in one class to have access to code in another class?

A

It means it is visible.

50
Q

When a superclass declares a private method whose signature is mimicked in a subclass is that subclass overriding that private super method?

A

No - if it is private, then it is invisible to any subclasses.

51
Q

What are the 3 ways for accessing a method?

A

(1) using a method in the same class (2) via the dot operator (3) via inheritance

52
Q

If a method is declared protected, can a subclass access that method through an object reference?

A

No, only through inheritance. For this to work, the member needs to be declared public. i.e. publicly visible outside the package.

53
Q

Can access modifiers be applied to local variables?

A

No

54
Q

What is the one access modifier that can be applied to local variables?

A

Final. Once initialised, the local variable cannot be changed.

55
Q

What are the two different types of members of a class?

A

variables and methods.

56
Q

What are the 7 different non-access modifiers?

A

final, abstract, static, synchronised, native, strictfp, transient.

57
Q

What are the 4 different access modifiers?

A

public, protected, private and default access

58
Q

What does final mean in the context of a method and when would you use it?

A

A final method cannot be overridden. It is used when you want to guarantee the functionality of the method. It is often done for safety and security reasons. The reasons for not doing it are it goes against being extensible and is not maintainable.

59
Q

Which other Java entity behaves similarly to local variables?

A

Method arguments. They behave exactly as if they were local variables.

60
Q

Describe what final means in the context of a method argument?

A

A final method argument cannot have its value changed during the execution of that method. If it is an object type, it can still have its member variables changed and methods invoked, but its object reference cannot be changed.

61
Q

What does abstract mean in the context of a method?

A

The method will have no body and subclasses are forced to implement it who wish to be concrete classes. It is ended with a semicolon and forces the enclosing class to also be declared abstract. The reverse, however is not true - an abstract class does not need to have abstract methods.

62
Q

What does concrete mean?

A

Non-abstract

63
Q

Is it possible to overload methods from a super class?

A

Yes. If you match the method name but have a different parameter list, then you are overloading the method, not overriding it.

64
Q

What is the definition of a method’s signature?

A

It is the combination of a method name and its parameter list. If it is the same from a super class, then you are overriding that method.

65
Q

Why can’t a method or class be declared as both abstract and final?

A

Because they are opposite each other. One says it must be subclassed or overridden to be of any use and the other says that you cannot subclass or override.

66
Q

Can private methods be declared as abstract? Why?

A

No - because they are private to that class, and any subclass will not be able to see that private method. It won’t compile.

67
Q

Can methods and classes be both abstract and static?

A

No. top level classes and methods cannot be both static and abstract. static methods are shadowed, not overridden, and static classes are not allowed.

68
Q

Describe use of the synchronized keyword.

A

Only applicable to methods and not to classes or variables, it means only one thread can be in that method at any one time.

It can be used in conjunction with all the other access modifiers for a method.

69
Q

Describe use of the word native

A

Native, like synchronized can only be used on methods - not classes or variables and means that the implementation of the method is platform specific often being written in C. Like an abstract method, the method signature ends with a semicolon and no body.

70
Q

Describe use of the word strictfp

A

strictfp is a non-access modifier for use on classes and methods. It enforces the entity to use floating point types and operations that strictly adhere to IEEE 754. It enforces common behaviour across different JVM. A variable can never be declared as strictfp.

71
Q

What is the definition of arguments?

A

Arguments are the variables or values used as part of a method call.

72
Q

What is the definition of parameters?

A

Parameters are the variables or values used as part of a method signature

73
Q

What are the 5 rules for using varargs?

A

(1) You must declare the type of the varargs which can be primitive or reference type
(2) You follow the type with the ellipsis, a space and then the name of the array that will hold the parameters
(3) The method signature can contain other parameters
(4) the varargs must be the last parameter in the method signature if it is present at all.
(5) You can only have 1 varargs in a method signature.

74
Q

Can you invoke a method with no arguments whose signature contains a varargs?

A

Yes, the array will not be null, but it will be of size 0.

75
Q

At what point in the code does a constructor get called?

A

When the object is created using the new operator.

76
Q

Describe constructors

A

(1) They are the same name as the class and are used to construct objects of that class type. (2) They cannot have a return type; (3) they can take multiple parameters.(4) They cannot be static (5) nor can they be overriden. (6) They cannot be final or abstract (cannot be overriden)

77
Q

Describe the two different variable types in Java

A

Primitive and reference variables. primitives can be 1 of 8 types - byte, short, int, long, float, double, char, boolean. Once declared its type can never be changed.

Once declared, a reference type can not be changed; it refers to the memory address that contains the object. Both are passed by value - the value of the variable and the value of the memory reference.

78
Q

Describe what a signed integral or numeric type means?

A

A signed number means that it can either be positive or negative. The left most bit is used to represent the sign of the number. 1 means negative and 0 means positive. e.g. a byte has 7 bits for its value meaning a range of -128 to 127 which is 256 values (including the 0).

79
Q

List the 6 numeric types and their bit sizes

A

byte 8
short 16
int 32
long 64

float 32
double 64

80
Q

Compare all modifiers that can be used for class v local variables v variables v methods for both access/non-access

A

local variables - final
variables - private, public, protected, final, static, transient, volatile
methods - private, public, protected, abstract, final, static, synchronized, strictfp, native
class - public, final, abstract, strictfp

81
Q

What does the keyword transient mean?

A

A transient variable is one in which you will not be serializing to a stream. Transient only applies to isntance variables (not class variables) and never to methods or classes.

82
Q

What does the keyword volatile mean?

A

It, like transient, can only be applied to instance variables

83
Q

when must the initial assignment of a final variable be completed by?

A

The end of the constructor. i.e. in the variable, static/instance initializers, static methods, and then the constructor.

84
Q

When implementing an interface method or overriding a method, can you introduce checked exceptions in the method?

A

No, these exceptions must be declared in the interface method. Same for overriding. It can throw or use exceptions which are subtypes of the exception in the abstract method it is overriding/implementing however.

Also, you do not have to include the checked exceptions from the interface method.

85
Q

When implementing an interface method or overriding a method, can you introduce runtime exceptions in the method?

A

Yes. These are not requried to be declared in the abstract method.

86
Q

Can interfaces implement other interfaces?

A

No, they can extend multiple interfaces though, but not extend an object.

87
Q

Can interfaces extend mutiple interfaces?

A

Yes.

88
Q

What are the four basic variable scopes?

A

static, instance, local and block. Static is the longest and exists for as long as the class is loaded into the JVM; instance for as long as their is an instance of the object, local for as long as the method is on the call stack and block - only within the block.

89
Q

Define shadowing

A

Shadowing is similar to overriding. Rather than replace the variable, it shadows it for a time taking its place for that particular scope. e.g. local variable and an instance variable.

90
Q

What are the default values for all primitive types and object references?

A
int, byte, small, long - 0
object reference - null
float, double - 0.0
boolean - false
char '\u000'
91
Q

Explain the difference between an unitialized object and a null reference

A

The compiler will complain if a local object is not initialized (even with null) before use including checking for null.

92
Q

If you have two object references of the same type and copy one into the other, how many objects do you have?

A

1, but 2 object references to that object.

93
Q

What is the heap?

A

It is that area of memory that is used by the JVM for storing and retrieving objects. It is the only part of memory involved in garbage collection (no stack).

94
Q

What is the heap?

A

It is that area of memory that is used by the JVM for storing and retrieving objects. It is the only part of memory involved in garbage collection (no stack).

For the purpose of garbage collection, the collector runs and looks for objects on the heap which are no longer reachable, reachable being the key word.

95
Q

What is the heap?

A

It is that area of memory that is used by the JVM for storing and retrieving objects. It is the only part of memory involved in garbage collection (no stack).

96
Q

Describe Garbage Collection

A

For the purpose of garbage collection, the collector runs and looks for objects on the heap which are no longer reachable, reachable being the key word.

It is possible to call the JVM to ask it to run the GC but there are not gaurantees.

97
Q

When is an object eligible for GC>

A

When there are no live threads which have access to that object. We are really talking about having a reference variable that points to that object on the heap.

GC does not ensure that you will not run out of memory - but only that the memory you have will be managed as efficiently as possible.

98
Q

How do you make an object eligble for garbage collection?

A

By assigning all references to that object to null.

99
Q

How do you make an object eligble for garbage collection?

A

By assigning all references to that object to null, or assign the reference to another object.

100
Q

How do you make an object eligble for garbage collection?

A

By assigning all references to that object to null, or assign the reference to another object.

Local variables that are not returned or assigned to variables with bigger scope are eligible for GC.

101
Q

When is an object eligible for GC>

A

When there are no live threads which have access to that object. We are really talking about having a reference variable that points to that object on the heap.

GC does not ensure that you will not run out of memory - but only that the memory you have will be managed as efficiently as possible.

Just before throwing an OutofMemory error, the JVM will call the garbage collector. This is gauranteed, but it obviously does not gaurantee that you will always have enough memroy.

102
Q

What are islands of isolation?

A

When objects refer to each other in a cycilc manner, and noboby refers to anybody within their “inner circle”, thus making an island of objects that nbody has reference to and is eligible for GC. The GC can normally detect this.

103
Q

Why is it recommended that you don’t override finalize at all?

A

Because there are no gaurantees as to when the GC will run. It has non-deterministic behaviour, and so the finalize method on an object which is gauranteed to be called when you GC may or may not be called.

104
Q

Why is it recommended that you don’t override finalize at all?

A

Because there are no gaurantees as to when the GC will run. It has non-deterministic behaviour, and so the finalize method on an object which is gauranteed to be called when you GC may or may not be called.

The finalize method is only called once and could itself make the object ineligible for GC depending on the code you put into the finalize methd. It will only run ONCE though, and the GC will remember this and not run it more than once.