Java Flashcards

1
Q

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?

A

We must use a wrapper, a new class which holds as memeber the ‘needs-to-be-swapped’ object.

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

How a pointer to a function is defined?

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

What are the .h files expected to hold?

What does the pre-processor do with these?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  • 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?*
A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  • 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?*
A

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;

}

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

What is the use of final keyboard with methods or even to the class itself?

A

final before a method - It ensures no sub-class override this method.

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

Describe abstract class?

A
  • 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.*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

describe the synchronized keyboard

A

It is mentioned when we want to allow only one thread to access the method at a time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  • Describe the volatile keyboard*
  • why is it needed in the attached example*
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  • What is the different between throws and throw keyboards?*
  • when finally is used?*
  • Which new alternative there is?*
A

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.

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

what’s the difference between character stream and byte stream? How would you read from a file and print to the screen?

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

How can a new exception be created?

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

Explain about Vector and Enumeration Objects

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

What is the difference between int and Integer?

A

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.

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

Polymorphism

The type of the reference variable would determine the methods that it can invoke on the object.

Explain the sentence above.

A
  • 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.

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

Describe the difference between abstract class and interface

A
  • Abstract variables can be of any kind
    • Interface variables are final and can be static or not.
  • Abstract methods can be abstract or not
    • Interface method are abstract

So basically, we use abstract class when we want to have a class which holds some methods which are shared by all its subclasses, while forces the subclasses to implement other specific methods.

We use an interface when we want the subclasses to implement all of its methods.

17
Q

How many interfaces can a class implement and how many parent classes can sub-class extend?

A

A class can implement many interfaces but extend only one class.

18
Q

What are packages for?

A
  • ​prevent naming conflicts
  • control access
  • make searching easier
  • make usaeg of classes/interfaces/enumerations and notations easier.
19
Q

How can a class be related to a package?

How can we refer to some class for another package?

A
  • At the top of the file should be mentioned: package <name> </name>
  • There are three ways:
    • <Package name>.<nameofclass></nameofclass>
    • import <package>.*</package>
    • import <package>.<nameofclass></nameofclass></package>
20
Q

Name all 8 Collection interfaces:

A
  • The Collection interface
  • The List interface
  • The Set interface
  • The SortedSet Interface
  • The Map interface
  • The Map.Entry interface
  • The SortedMap interface
  • The Enumeration interface
21
Q

Name ways to make an object eligible for Garbage Collector

A

There are generally four different ways to make an object eligible for garbage collection.

  • Nullifying the reference variable
  • Re-assigning the reference variable
  • Object created inside method
  • Island of Isolation