Monday, October 19, 2009

Midterm Questions and Answers

Here are the answers to my 10 questions:

1. Define the term 'security through obscurity' and describe why it is not a good practice.

Security through obscurity refers to the use of secrecy as a mean to develop a secure application or system. It is usually not a good practice because it can still contain vulnerabilities that could have otherwise been detected if it were publicly disclosed.

2. Explain why writing "Please send your reply to...." after posting your question on a forum is bad.

Because most posters would like to earn recognition through answering your questions, and by asking them to reply privately would not help them achieve that goal.

3. What does the command 'ant -f emma.build.xml' do?

Emma is a coverage tool that generates an html report indicating how much lines of your code is actually being executed. The command emma.build.xml executes Emma.

Consider the following program written in Java for questions #4 - 10.

/**
* Simple program that fills an array with integers.
* @author Daniel Colton
*/
PUBLIC class MyList {
PUBLIC Integer TOP = 0; // Tracks the TOP INDEX
PUBLIC
Integer[] intArray = new Integer[10]; // Array OF integers
/**
* Create an instance of MyList and insert 10 integers
* into it.
*/
PUBLIC static void main(String[] args) {
MyList list
= NULL;
MyList newList = new MyList();
FOR (INT i = 0; i < 10; i++){
list.
ADD(i);
}
}
/**
* Adds an integer into the array.
*/
PUBLIC void ADD(Integer i) {
intArray[top]
= i;
TOP++;
}
/**
* Prints the ith elemnt of the array.
*/
PUBLIC void PRINT(Integer i) {
String
MESSAGE = new String("List";
System.out.println(MESSAGE+"["i+"] = "intArray[i]);
}
}


4. List at least two lines that can be considered a CheckStyle error.

- Missing '@param' statement above lines 'public void add(Integer i)' and 'public void print(Integer i)'.
- Missing javadoc coments above data variable declarations at lines 'public Integer top = 0;' and 'public Integer[] intArray = new Integer[10];'.
- Use of ambiguous naming convention for variables such as 'i' at lines 'public void add(Integer i)' and 'public void print(Integer i)'

5. List at least two lines that can be considered a PMD error.

- Unused variable 'newList' at line 'MyList newList = new MyList();'.
- String variable 'MESSAGE' at line 'String MESSAGE = new String("List");' should be declared as a string literal (String MESSAGE = "List").

6. List at least one line that can be considered a FindBug error.

- A null pointer dereferace for the 'List' variable at line 'MyList list = null;'.

7. Rewrite the program so that it passes all three of the above mentioned tests.

I also added the two functions, getValue and onProgramEnded, which are useful when writing JUnit tests for Questions #8, 9, and 10.

/**
* Simple program that fills an array with integers.
* @author Daniel Colton
*/
PUBLIC class MyList {
/** Tracks the top index. */
PUBLIC Integer TOP = 0;
/** Array of integers */
PUBLIC Integer[] intArray = new Integer[10];
/**
* Create an instance of MyList and insert 10 integers
* into it.
*/
PUBLIC static void main(String[] args) {
MyList list
= new MyList();
FOR (INT i = 0; i < 10; i++){
list.
ADD(i);
}
list.onProgramEnded
();
}
/**
* Adds an integer into the array.
* @param index refers to the index position in the intArray.
*/
PUBLIC void ADD(Integer INDEX) {
intArray[top]
= INDEX;
TOP++;
}
/**
* Prints the ith elemnt of the array.
* @param index refers to the index position in the intArray.
*/
PUBLIC void PRINT(Integer INDEX) {
String
MESSAGE = "List"
System.out.println
(MESSAGE+"["INDEX+"] = "intArray[index]);
}

/**
* Returns the ith element of the intArray.
* @param index refers to the index position in the intArray.
*/
PUBLIC INT getValue(Integer INDEX){
RETURN intArray[index];
}
/**
* This method is called after the program executes.
*/
PUBLIC void onProgramEnded(){
}
}


8. Write an assertion test for the program that you have re-written in question #7.

Following assertion test checks to see if all values are properly inserted into the integer array:

import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
* This is an acceptance test that tests if an integer value
* is inserted into the array.
*/
PUBLIC class TestMyList {
@Test
PUBLIC void testIfInserted() {
MyList list
= new MyList();
FOR (INT i = 0; i < 10; i++){
list.
ADD(i);
}
boolean isInserted
;
FOR (INT i = 0; i < 10; i++){
IF (intArray.getValue(i) != NULL) {
isInserted
= true;
}
ELSE {
isInserted
= false;
}
assertTrue
("Check if inserted" isInserted);
}
}
}


9. Write a behavioural test for the program that you have re-written in question #7.

The following program checks if the values are inserted in the correct order (from 0 to 9):

import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
* This is a behavioural test that tests if the integer values
* are inserted in the correct order.
*/
PUBLIC class TestMyList2 {
@Test
PUBLIC void testValuesInserted() {
MyList list
= new MyList();
FOR (INT i = 0; i < 10; i++){
list.
ADD(i);
}
FOR (INT i = 0; i < 10; i++){
assertEquals
("Check insert order" list.get(i), i);
}
}
}


10. Write a unit test for the program that you have re-written in question #7.

The following program tests if the getValue method returns the correct value from the integer array:

import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
* This is a Unit test that tests if the getValue method returns
* the correct value from the list.
*/
PUBLIC class TestMyList3 {
@Test
PUBLIC void testGetValue() {
MyList list
= new MyList();
FOR (INT i = 0; i < 10; i++){
list.
ADD(i);
}
assertEquals
("Test getValue" list.get(0), 0);
assertEquals("Test getValue" list.get(9), 9);
assertEquals("Test getValue" list.get(4), 4);
assertEquals("Test getValue" list.get(7), 7);
}
}

No comments:

Post a Comment