cs Flashcards
Output?
public static void main(String [] args){
int number = 6;
int secondNumber = changeNumber (number);
System.out.print(number + “ “ + secondNumber);
}
public static int changeNumber(int number){ number = 12; return number; }
6 12
Which Line will Line A produce?
public class Unknown{
private int x;
public Unknown(){ x = 17; method1(); method2(5); method3(); System.out.println(x); // Line D } public void method1(){ --x; int x = this.x; x++; System.out.println(this.x); // Line A } public void method2(int x){ x++; System.out.println(x); // Line B } public void method3(){ --x; int x = 2; x++; System.out.println(x); // Line C } }
16
Output?
public void foo () {
int x = 42;
int y = 24;
mystery (x, y);
System.out.println (x + “ “ + y);
}
public void mystery (int var1, int var2) { int temp = var1; var1 = var2; var2 = temp; }
42 24
What should be done to correct the following code?
public class exam {
float mark;
public static void main(String[]arg){
float aCopyofMark;
exam e = new exam();
System.out.println( e.mark + aCopyofMark);
}
}
Initalize aCopyofMark
How many objects are created in the following declaration?
String name;
0
Output?
int income = 30;
boolean condition1 = true, condition2 = true;
if(income < 100)
if(income > 10)
if(condition1){
System.out.print(“A”);
if(income < 20)
System.out.print(“B”);
}
else
System.out.print(“C”);
if(!condition2){
if(income > 50)
System.out.print(“D”);
}
else{
System.out.print(“E”);
}
AE
The scope of a variable in Java is:
The part of the program in which it can be used
Which of the following JUnit tests will fail?
assertNull(50-25-25);
Imagine a class named Club, to be a member a person must be aged 15 or older, but less than 50. What values would you use to boundary test a method designed to check if a person was a suitable age to join?
14 15 49 50
Which set of values is just enough to provide code coverage for the following code: if (cost > 10 && tip > 4)
cost: 20 and tip: 2, cost: 20 and tip: 6, cost: 9 and tip: 1, cost: 9 and tip: 6
cost: 20 and tip: 4, cost: 5 and tip: 1
cost: 20 and tip: 2, cost: 9 and tip: 5, cost: 15 and tip: 6
cost: 20 and tip: 2, cost: 9 and tip: 5, cost: 15 and tip: 6
Which set of values is just enough to provide code coverage for the following code: if (cost < 10 || cost >20)
cost: 5, 10, 15, 20
cost: 5, 25
cost: 5, 25, 15
cost: 5, 25, 15
Which line of code is needed at the beginning of the follow block of code used to test whether remove throws the correct exception:
try { collection.remove(item); } catch (Exception exception) { thrown = exception; } assertNotNull(thrown); assertTrue(thrown instanceof NoSuchElementException); assertEquals(thrown.getMessage(), "That item is not in the collection.");
Exception thrown;
Exception thrown = null;
Exception thrown = NoSuchElementException;
Exception thrown = That item is not in the collection.”;
Exception thrown = null;
Which of the following lines of code would properly test the equality of two arrays. For example:
Object[] expectedArray = {"H","I","J","K"}; Object[] actualArray = {"A","B","C","D"};
assertEquals(expectedArray, actualArray);
assertEquals(actualArray, expectedArray);
if (actualArray.length == expectedArray.length) { for (int i =0; i < expectedArray.length; i++) { assertEquals(expectedArray[i],actualArray[i]); } } for (int i =0; i < expectedArray.length; i++) { assertEquals(expectedArray[i],actualArray[i]); } for (int i =0; i < actualArray.length; i++) { assertEquals(expectedArray[i],actualArray[i]); }
assertTrue( expectedArray.equals( actualArray) );
if (actualArray.length == expectedArray.length) {
for (int i =0; i < expectedArray.length; i++)
{
assertEquals(expectedArray[i],actualArray[i]);
}
}
Suppose you are writing a method in a new class. You are also writing unit test cases to demonstrate that this method works correctly. You know you have written enough test cases to demonstrate the method works as desired when?
You have written at least one test case for every input value that can be given to the method.
You have written at least one test case for every output value that can be produced by the method.
You have written at least one test case that uses the method.
You have written at least one test case for every input/output value combination that can be given to/produced by the method.
You have written separate test cases for each identifiable “group” of input values and/or output values where the behavior is expected to be similar.
You have written separate test cases for each identifiable “group” of input values and/or output values where the behavior is expected to be similar.
Which method is run before each test method?
All of these answers are correct
public void setUp(){}
public void SetUp(){}
public Hokie(){}
public void setup(){}
public void SetUp(){}, public void setup(){}, and public Hokie(){} are all correct
public void setUp(){}
Which of the following is false according to the course style guidelines?
constants use a reverse camel case convention
package names should be all lowercase
methods and variable follow the same naming convention
classes and interfaces both follow the same naming convention
constants use a reverse camel case convention
Reasons for following style and documentation guidelines are:
code is easier to debug
code is more readable and easier to understand
all of these
code is easier to maintain
all of these
Which of the following is not a commonly used javadoc tag?
@return
@version
@param
@args
@author
@args
Which of the following do not need to be avoided to adhere to the course style guidelines
unused fields/variables
sufficient javadocs
lines that are too long
debug statements left within the code
hardcoded values
sufficient javadocs
When an exception occurs within a method, the method creates an Exception object then hands it off to the Java runtime system to take further action, this is referred to as…
instantiating a semaphore
exception passing
runtime passing
throwing an exception
throwing an exception