Basic Flashcards
Order the following by when they are initialized:
- All non static constants, variables and blocks.
- Constructor.
- All static constants, variables and blocks.
2, 3, 1
- Among themselves the order is the order in which they appear in the code.
Which method declarations will enable a class to be run as a standalone program:
- static void main(String args[ ])
- public void static main(String args[ ])
- public static main(String[ ] argv)
- final public static void main(String [ ] array)
- public static void main(String args[ ])
4, 5
- the accessibility of the main method must be public.
- return type and method name are never separated
True/false:
- An object can be made eligible for garbage collection by making sure there are no references pointing to that object.
- You cannot directly invoke the garbage collector.
True
- You can however suggest the JVM to perform garbage collection by calling System.gc();
Which of these statements concerning the use of modifiers are true:
- By default the member is only accessible to classes in the same package and subclasses of the class.
- You cannot specify visibility of local variables.
- Local variable always have default accessibility.
- Local variables can be declared as private.
- Local variables can only be declared as public.
2
True/false:
You cannot apply any modifier except final
True.
- i.e. you cannot make them transient, volatile, static, public, and private.
True/false:
A method that has a body can be abstract.
False.
- an abstract method cannot have a body.
The following is a valid member variable declaration: private static final transient int i = 20;
True.
- You can apply all the modifiers to member variables except abstract, native and synchronized.
True/false: The following import statement is valid:
static import java.lang.System.*;
False
- The order of static and import is invalid. It should have been “import static”.
True/false: It is not required to import java.io.* or import java.io.IOException because java.io package is imported automatically.
False.
- Only java.lang package is imported automatically.
What does the zeroth element of the string array passed to the standard main method contain:
- The name of the class.
- The string “java”.
- The number of arguments.
- The first argument of the argument list, if present.
- None of the above.
4.
- If no argument is passed the args parameter is NOT null but a valid non-null String array of length zero.
An instance member …
- can be a variable, a constant or a method.
- is a variable or a constant.
- belongs to the class.
- belongs to an instance of the class.
- is same as a local variable.
1, 4 - An instance member belongs to a single instance, not the class as a whole. An instance member is a member variable or a member method that belongs to a specific object instance. All non-static members are instance members.
True/false:
Syntax for importing static fields is: import static ..*;
or import static ..;
True.
How can you declare a method someMethod() such that an instance of the class is not needed to access it and all the members of the same package have access to it:
- public static void someMethod()
- static void someMethod()
- protected static void someMethod()
- void someMethod()
- protected void someMethod()
- public abstract static void someMethod()
1, 2, 3
- must be static