Java Flashcards
All objects instances in Java are references to the objects, and the objects are saved on the heap.
How can we swap two objects than?
We must use a wrapper, a new class which holds as memeber the ‘needs-to-be-swapped’ object.
How a pointer to a function is defined?
What are the .h files expected to hold?
What does the pre-processor do with these?
- Declarations of constants
- Types
- Global variables
- functions the module exports to other modules.
The pre processor simply copies the .h file’s content to the beginning of the module.
Of course, the module must specify:
- # include “<module>.h"</module>
- How can we define a global variable to be accessed from other modules?*
- How can we deny a lot of unecessary inclusions of .h files?*
Assume: String file_name; appears in b.c
Then in b.h we should write:
- extern string file_name;
- We can deny unecessary inclusions using*
- # ifndef B_H*
- # define B_H*
- …*
- …*
- …*
- # endif*
where B_H is used when b.h is the file.
- How can we use macros in Java?*
- How can we use macros in C, and when should we?*
- How can we use macros in C++, and how it differs from C?*
In java - we can’t.
In C,
#define MAX(a,b) (a>b? a:b)
_#define POWER2(a) (a*a)_
we should always wrap the expression with parenthesis.
- we should use these when the functions are small.*
- In C++, we use inline before we define the function, the compiler handles the function so there is type checking.*
inline int add(int a, int b){
return a + b;
}
What is the use of final keyboard with methods or even to the class itself?
final before a method - It ensures no sub-class override this method.
Describe abstract class?
- A class which can never be instantiated. Thus its sole purpose is to be inherited. Note: it cannot be both abstract and final because that would make it totally useless.*
- Abstract class might have both abstract and normal methods.*
- Abstract methods don’t have a body.*
describe the synchronized keyboard
It is mentioned when we want to allow only one thread to access the method at a time
- Describe the volatile keyboard*
- why is it needed in the attached example*
The volatile keyboard states that when we access the variable, it must be synchronized with the main memory. It can be applied to instance variables only.
- What is the different between throws and throw keyboards?*
- when finally is used?*
- Which new alternative there is?*
throws - postpone the handling of an exception
throw - used to invoke an expression explicitly.
finally is occasionally used when we want to close I/O objects. we say “no matter what, don’t forget to close them”.
There is a new alternative, defining the I/O objects within the parenthesis of try. This way the program automatically closes it, without the need of finally.
what’s the difference between character stream and byte stream? How would you read from a file and print to the screen?
How can a new exception be created?
Explain about Vector and Enumeration Objects
What is the difference between int and Integer?
int is not a class. It has no method. It is not an object. It is called a primitive type.
An Integer is a wrapper for the primitive type int. It is an object, a class. As such, it has methods.
Polymorphism
The type of the reference variable would determine the methods that it can invoke on the object.
Explain the sentence above.
- Assume Child extends Parent.*
- Assume Child has the method play, Parent does not.*
Parent p = new Child();
p.play() - will not execute. The reference variable is of type Parent, which does not have this method.
But,** if it had, then the play method of the **Child** class would have been executed, and not the **Parent’s.
Checking the method exists within the parent class occurs at compilation time. The execution of the child’s method occurs in Run-time.