• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/40

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

40 Cards in this Set

  • Front
  • Back
What is the difference between an abstract class and an interface?
Interfaces don't define non-default methods. Also, classes cannot inherit from more than one class, abstract or not, while a class may implement many interfaces. Both can have constant declarations but in an interface they are implicitly public static and final.
How do you derive a new class from a class?
Where A is a subclass of B,
public class A extends B {
// class contents
}
Given an interface, how would you make your class behave like the interface.
//Where Class A uses interface B
public class A implements B {
// Must define methods declared in B
}
What is the common superclass from which all Java classes derive directly or indirectly?
Object.
What are some advantages of all classes deriving from a common class like Object?
Every class inherits all the methods defined in Object. These include methods to get the class name, getClass() and various methods related to synchronization like, wait() or notify() and methods useful for storing an object in a collection like hashCode() or equals().
What are some methods defined in the Object class and how are they useful?
getClass() returns the class object.
hashCode() and equals() are useful for comparison and in collections.
notify(), notifyAll() and wait() are used in thread synchronization.
toString() is used to get a string representation of the object.
clone() is used to get a deep copy of the object.
finalize() is useful for clean up just before garbage collection.
What does the default toString() method implementation do?
Returns the class name and hash code. If this is not the desired representation of the object, the developer should override it.
What are primitive wrapper classes?
Wrapper classes are used to wrap a primitive type in an Object, mainly to make it easier to use them in a collection and for using reflection. The action of converting from a primitive type to a wrapper type is called boxing. The other way around ( wrapper to primitive ) is called unboxing. Some examples of wrapper classes are Integer class, Boolean class, Double class.
What is the difference between constructors and other methods?
A constructor has no return type and has the same name as the class.
What is a static method?
A static method is associated with a class rather than an object of that class. It can be invoked without using an instance of that class. For example:

public class A {
static public doStuff(){
}
}

//Usage: A.doStuff();
What is an initializer block?
It's a block defined outside of a method that gets appended to the end of each constructor. Variables declared final may be initialized in these blocks. You can define any number of these and they will be appended in the order in which they occur in your code.
What is a static initializer block?
A block of code defined outside of a method and declared static. It will be executed when the class is loaded rather than when the object is instantiated. Many of these may be defined.
Explain the access modifiers public, private protected and default.
public members and classes may be accessed from anywhere.
private members may be accessed only by methods of the class in which they are declared.
protected members may be accessed from within the class or a subclass.
default (a.k.a package private) members and classes may be accessed from anywhere in the same package. There is no actual modifier for default.
Explain how the final keyword is used in Java.
A method declared final may not be overridden. A class declared final may not be extended. A variable declared final may be assigned only once. A static final variable may be used like a constant. Method arguments declared final can not be changed by the called method.
Why would you declare a class final?
Making a class final adds some security and consistency to the class behaviour. E.g. by making the String class final, you cannot override its length method. The compiler may optimize the code and make it run faster when a class is declared final.
What are the four types of nested classes?
1. static nested class
2. inner (non-static) nested class
3. local class
4. anonymous class

A static nested class is just like a normal Java class. If it is public, it can be accessed or used to create objects just like any other class.
A non-static inner class requires an object of the containing class.
A local class is declared within and scoped to a block.
An anonymous class is defined in place after the new operator without assigning any name to it. It's a way to pass functions as parameters.
What are the primitive types in Java?
Mnemonic:
I EIGHT a BYTE but was SHORTed 16 bits INT-to 32 of those LONG 64's that FLOATed 32 bits while DOUBLE-ing 64 of them.
They just didn't have the CHAR-acter of the 16's.

byte ( 8 bits )
short (16 bits )
int ( 32 bits )
long ( 64 bits )
float ( 32 bits )
double ( 64 bits )
boolean ( unspecified )
char ( 16 bits )
How are primitive types different from classes in Java?
Primitive types are created when ever a variable of these types is declared and are not classes.
Primitive types are stored on the JVM stack rather than the heap. If you need to treat them as objects, they need to be boxed first.
What is an abstract class?
An abstract class is a class declared with the abstract keyword. Although references to such a class may be used, the class may not be directly instantiated. Abstract classes may define methods or just declare them and may contain variables.
Where would you use the keyword extends and implements?
extends is the keyword used to declare that this class is a subclass of the class that follows the extends keyword. Implements is a keyword that designates this class as an implementer of the specified interface.
How do you call a constructor of a super class? Where would you call it?
A call to the default super class constructor is implicitly the first line of each constructor unless a different constructor is explicitly provided by the developer. So for the first line of a constructor that calls a non-default super class constructor, one might write: super("some string");
It's a compilation error if it is not the first line.
How do you call a method in a super class from a subclass?
Use the super keyword like so:
super.someMethodCall("some string parameter" );
Note that in most cases the super keyword may be omitted.
How are parameters passed in Java, by value or by reference?
By value but in the case of objects, the value is a reference.
What if you wanted to pass a primitive type ( say an int ) so that the called method can modify it and the caller sees the modified value?
Wrap it in a wrapper class or make it a member of a class object and pass the object to the method.
Why don't we call the new keyword for primitive types like int, float and double?
Primitive types are not objects so they are not created with the new operator which allocates memory on the heap. They are created by simply declaring them and assigning a value.
How are Arrays defined in Java?
An array is a container object that holds a fixed number of values of a single type. The member called length is initialized when the array is created and tells how many elements are in the array.
How do you clone an array?
Use the clone() method of the array object.
When we say that String's are immutable in Java, what does that mean?
It means that String objects cannot be changed once created. Consequently, each time a String is altered, a new String must be created to get a proper result. This has a performance impact so that if a program performs lots of String manipulation, the helper classes StringBuilder and/or StringBuffer should be used.
Describe some do's and don't's for manipulating Strings in Java.
Because Strings are immutable, any little operation you do with them creates one or more new strings which impacts performance. One can use a StringBuilder or StringBuffer to manipulate the string instead and then convert it when the alterations / additions are complete.
What's the difference between StringBuffer and StringBuilder?
StringBuffer is thread safe while StringBuilder is not.
What is the conditional or ternary operator in Java?
It's shorthand for an if/else block.
Example:

(boolean condition) ? // if true : // else ;

So it might look like:

X.equals(Y) ? X = "if true" : Y ="else block" ;
Is String a primitive type in Java?
No but it is one of the most frequently used objects, consequently it has some special features. Enclosing a String of characters in double quotes creates a new String object.
Which package is always imported by default?
java.lang
Which operator checks the type of an object?
instanceof
What is the difference between the == operator and the equals() method?
== compares two operands and in the case of primitives, returns true if the values are equal. In the case of Objects, it checks to see if the references are the same. The default implementation of the equals() method does the same thing but the equals method is override-able and many pre-defined classes have such overrides.
What is a marker interface?
A Marker interface is an empty interface used to indicate that a given class implements a certain functionality. Serializable for example, indicates that a classes non-transient data can be written to an OutputStream. RandomAccess indicates that that a collection supports fast ( almost constant time ) random access.
What are annotations and how are they useful?
Annotations are pieces of metadata that contain information used by the compiler, associated with an object at run time or compile time. For example, @Deprecated, @Override and @SuppressWarnings.
What is a static import?
Declaring an import static allows unqualified access to static members. For example:

import static java.lang.Math.PI;

The above would allow you to use PI without any further qualification.
When should static imports be used?
They should be used sparingly and only on frequently used classes. This will save typing the extra boiler plate code while preserving the quality of the namespace.
When should a data field be declared final?
Objects with fields declared final can be passed from one thread to another without synchronization. It contributes to robust software design and is slightly faster. Unless there's a good reason why not, all fields should be declared final by default.