Data Structures and Java Class Library Flashcards

1
Q

What is an annotation and what rules should be followed when using them?

A

Code annotations are metadata about code that can be evaluated by the compiler and later at runtime. They must start with an @ sign and a capital letter.

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

What are code conventions and what is their use?

A

Code conventions are a set of style rules (such as variable naming or source code formatting) that helps programmers write code that is easily understandable by other programmers, or by themselves in the future. These rules do not affect the functionality of the software and they are not checked by the compiler.

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

What rules must be followed when naming each of the following: packages, classes, interfaces and methods?

A
  • A package’s name must be in lowercase letters.
  • Class and interface names must be nouns and start with a capital letter, and if they consist of more than word, the subsequent words must be capitalized too (this is a variant of camel case).
  • Methods must start with a lowercase letter and they should consist of verbs. Words after the first one should start with a capital letter (camel case).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Should required variables be initialized when declaring them? Where must required variables go within a code block?

A

True. They must go at the beginning of the block.

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

What is the function of blank lines in source code formatting?

A

They are used to separate groups of semantically related code statements.

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

What are bracketed comments?

A

They are multi-line comments and can be used by prepending the symbols /* to a block of code, and appending a */ at the end. The block of code within those two pairs of symbols is considered a comment and will not be executed.

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

What is Javadoc?

A

Javadoc is a documentation generator that relies on a special bracketed comments called Javadoc comments, put before classes and methods. They are like regular comments but they start with the symbols /**, end with **/, and have certain tags to mark important details about the documented class or method, for example: returns, throws.

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

In which order are the elements of a class typically arranged?

A

Static variables and methods, instance variables, constructors, getters and setters, instance methods.

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

Why is overriding the method for string representation of objects useful?

A

It is useful to centralize the textual representation of a class.

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

What’s the output of toString() if it’s not overridden?

A

It outputs the name of the class, followed by the hash code of the object (e.g., Customer@1c8697ce).

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

When should == and .equals() be used?

A

== is for comparing primitive data types, while .equals() is for comparing complex types.

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

What are some of the steps taken in a standard implementation of .equals()?

A
  • Check if the comparison using == already returns true (in that case, the object references are the same)-
  • Check if the given object is of the same type as this class, if it is, compare the relevant attributes, if it’s not, call the .equals() method of the superclass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the properties of a correctly implemented equals()?

A
  • Reflexive: a.equals(a) always returns true
  • Symmetric: a.equals(b) returns the same value as b.equals(a)
  • Transitive: if a.equals(b) and a.equals(c) is true, then b.equals(c) must also be true
  • Consistent: it must return the same value as long as relevant attributes remain unchanged
  • Existent: a.equals(null) must return false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What requirements should a self-implemented hashCode() meet?

A

STABILITY-EQUALS-WIDE-IDENTITY

  • Stability/consistency: two objects with identical content should generate the same hashCode.
  • The calculation must use the same attributes as equals().
  • The hash code should be as widely spread as possible. Only a few different assignments should generate the same hash code.
  • Only identity-related attributes should be included.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the Comparable interface and what is its main method for?

A

Any class can implement the Comparable interface and its method compareTo() in order to define order between instances of the class.

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

How can objects be copied?

A

Objects can be copied in mainly two ways: by implementing the Cloneable interface and its clone() method, or by using a copy constructor (a constructor that takes an instance of the class as parameter, and assigns its attributes to a new instance).

17
Q

What are shallow and deep copies?

A

A shallow copy only copies the reference of an object, while a deep copy is a different instance of the object with the same attributes. The clone() method implemented by the Object class only does shallow copies, so any deep copy will have to be implemented by overriding the clone() method oneself.

18
Q

How is the Java Class Library structured and what are some of the main packages it offers?

A

The Java Class Library is divided hierarchically in sub-packages, and some of the most important ones are: java.nio (for accessing filesystems, virtual or not), java.security (for certificates and cryptography), java.util (data structure classes), javax.swing (a GUI framework that provides lightweight UI components but is not thread-safe).

19
Q

How does the compiler load libraries?

A

Libraries must be made known to the compiler by adding them to the build path.

20
Q

What are arrays?

A

An array is a data structure that allows storing data of the same type in a sequential way, with a fixed capacity set at initialization.

21
Q

How is an array stored and accessed in memory?

A

An array’s elements are stored sequentially in the main memory. The memory for an array is not reserved until the size is specified, so a declaration without initialization or capacity indication would reserve no memory.

Accessing array elements at specific indexes is fast and independent of the size of the array (O(1) complexity).

22
Q

What values do array elements hold, if any, before being set?

A

Array elements are set to the default value corresponding to their data type at initialization. An int array would consist of all zeroes at initialization, a boolean one would have false values and complex data types would be null.

23
Q

What is required for importing an external package or program library?

A

The compiled external library is required.

24
Q

What does the Java Class Library not contain a sub-package for?

A

Programming of websites.

25
Q

Between an Array List and a Linked List, which one should be chosen to implement a shopping cart?

A

A Linked List should be chosen because a shopping cart requires many additions and modifications, operations that are expensive in arrays (Array List creates arrays internally); in the case of a Linked List, these are more efficient because only the “backwards” and “forward” references need to be changed when adding elements.

26
Q

What is a map and what are its main variants?

A

A Map is a data structure consisting of a key and a corresponding value. The Map interface in Java defines some methods that allow iterating over the set of keys or values and this interface has many implementations such as:

  • HashMap (a fast-access map that uses a hash function to determine the location where the element is stored).
  • TreeMap (a map that keeps the keys sorted by their natural order, and saves the data in a tree structure).
  • LinkedHashMap (a map that uses a doubly-chained list and preserves the order in which the elements were inserted) and
  • WeakHashMap (a map that is similar to HashMap except that it might delete elements when memory is scarce).
27
Q

What Java data structure could be used to implement a stack?

A

The Java language developers currently recommend ArrayDeque to implement a stack, although java.util.Stack is also available.

28
Q

How can primitive values such as booleans and integers be transformed into Strings?

A

It can be done by concatenating them with an empty string (implicit conversion) or by using the static method String.valueOf().

29
Q

How are numeric or boolean values in Strings converted back to their real values (original types)?

A

The wrapper classes (Boolean, Integer, Double, Float, Short, etc) provide static methods for this purpose, such as Integer’s parseInt(). In the case of numerical values, the NumberFormatException exception could be thrown if the string representation of the number is not valid.

30
Q

Are Strings immutable in Java?

A

Yes, all methods that modify Strings return a new String instance.

31
Q

How can one concatenate many Strings in Java in an efficient way?

A

Java provides the StringBuffer class, which allows appending any number of Strings without creating additional objects unnecessarily. This is because StringBuffer uses a temporary buffer and the resulting string is only assembled once a call to toString() is made.

32
Q

How can dates be formatted in Java?

A

Java provides several classes for date formatting, including SimpleDateFormat, which allows the developer to specify how a date is represented as a String by using a format pattern (a sequence of attributes and separators between attributes). ParseException could occur if the input String does not comply with the specified pattern. Certain patterns are already predefined so one can use the corresponding constants without writing the pattern manually.

33
Q

What is the purpose of the java.util.Calendar class?

A

It is an abstract class made for the representation of dates in any calendar system. GregorianCalendar, which represents the most common calendar in the Western world, is an example of a class that extends this one, and has interesting methods such as roll(), which allows one to advance a date by a number of days, months or the preferred unit. A regular Date object can be obtained from a GregorianCalendar (for example, to use a SimpleDateFormat on it) by using the getTime() method.

34
Q

What is the File class in Java?

A

It is a class that represents both files and directories and allows interacting with the filesystem, often requiring to specify file paths, which are especially formatted Strings that indicate a hierarchy of elements separated by a character (/ on Linux, \ on Windows).

In Java >1.8, the java.nio.File is provided for working with the filesystem in a more intuitive way, including a library that handles path specifications (java.nio.file.Paths).

35
Q

What are data streams, data sources and data sinks?

A

Working with files in Java is abstracted by the concept of data streams, a general interface for reading and writing arbitrary data via data sources (input data) and data sinks (output data). An input stream transfers data from a source to our program, while an output stream goes the opposite way, transferring data from our program to a data sink.

Data streams are meant to encapsulate the functionality of reading and writing data, allowing the developer to focus on the program’s logic and not so much on the technical details of data transmission.

36
Q

What are the two main types of data streams?

A

Java distinguishes between two types of data streams: the character-oriented one (which are well suited for transmission of text), and byte-oriented ones (which are used to transfer binary data such as video or audio).

Some input streams for binary data are: FileInputStream, AudioInputStream and ObjectInputStream.

Among the available output streams for binary data, there’s the FileOutputStream, FilterOutputStream, ObjectOutputStream, etc.

Input streams for text data extend the class Reader, instead of the InputStream one. 
Examples of text-oriented input streams are InputStreamReader, BufferedReader (offers the readLine method to read line per line) and StringReader; these cannot read from files directly so a data source must be created first.
37
Q

What is the most common exception thrown when using FileReader?

A

A FileNotFound exception can be thrown if the file does not exist.

38
Q

What is the most common exception thrown when using BufferedReader?

A

IOException can be thrown in the readLine() method, and/or when the file streams are closed, which indicates an error in processing the input stream.

39
Q

Why is the concept of data streams important?

A

Data streams are a powerful tool because their abstraction of file handling allows the programmer to focus on the program’s logic and not so much on the underlying details of data transmission. Each data stream has a concrete task and communicates with other data streams via a defined interface, which allows them to be combined to treat data (for encryption, decryption, compression, etc).

Since data streams implement a general interface, they can be assembled into a pipeline, where each data stream does not need to know what the next stream does with the data.