In: Computer Science
Given the below Java code snippet, assuming the code is correct, and the names are meaningful, select the best answer for the below questions:
public static void main(String[] args) {
int numStations = 10;
int enter = 0;
int exit = 0;
Train train = new Train(numStations);
for (int station = 0; station < numStations; station++)
{
enter = enter + train.enterPassengers(station);
exit = exit + train.exitPassengers(station);
}
System.out.println("Number of passengers entered the train: " + enter);
System.out.println("Number of passengers exited the train: " + exit);
System.out.println("Number of passengers remaining on the train: " + (enter-exit));
}
The for-loop in the code is:
Counting the number of stations in the train
Is looping 9 times to determine enter and exit of the train
Sums up the number of passengers entering and leaving the train
Exercise #2
Review the below Java method and assume the comments are correct.
// determines if positive number is odd or even
// returns true if even
public Boolean isEven(int num)
{
if (num <= 0)
{
return false;
}
if (num%2 = 0)
return true;
return false;
}
Select the best answer what is wrong with the code:
It is setting variable to uninitialized value
It is using undeclared variable
It is using single equal sign as equality
Exercise #3
Review the below Java method and assume the comments are correct.
// Initialize array values to 0
public void initialize()
{
int[] values = new int[10];
for (int i = 1; i <= 10; i++)
values[i] = 0;
}
Select the best answer what is wrong with the code:
It is using an uninitialized array
It is overstepping array boundary
It is looping too many times
None of the above
Exercise #4
Given the below Java code snippet, assuming the code is correct, and the names are meaningful, select the best answer for the below question:
fax.setNumber("8761234567");
fax.load(paper);
String status = fax.run();
System.out.println("The fax result status : " + status);
The code snippet :
Makes multiple paper copies
Starts the fax machine and displays status
Loads paper and sends a fax
Exercise #1:
Answer : Sums up the number of passengers entering and leaving the train.
Explanation : It is written in the question that the names are assumed to be meaningful. The class Train seems to be a train's status and the total number of stations it visits. The for loop then iterates over all the stations and increments the variables enter and exit for every station. After the for loop's iterations the print statements further support the theory that the variables enter and exit are the number of passengers entering and exiting the train. Hence, we can say that the for loop sums up the number of passengers entering and exiting the train.
Exercise #2:
Answer : It is using single equal sign as equality.
Explanation : The equality operator (==) is used to evaluate whether a given condition is true or false. In this case, the condition to check whether a number is even or odd can be checked by verifying whether number%2 is equal to 0 or not. So, in Java, it has to be written as:
if(num%2==0)
Instead, it is written as:
if(num%2=0)
Which uses assignment operator. Hence, it will lead to an error.
Exercise #3:
Answer : It is overstepping array boundary.
Explanation : Java uses 0 based indexing, that implies, if an array of size 10 is used, the index numbers will be in the range 0 to 9 (both numbers inclusive). In the given code, the size of array is 10. In the for loop, the starting index i has value 1 and will go upto i=10 which will lead to an ArrayIndexOutOfBoundsException.
Exercise #4:
Answer : Starts the fax machine and displays status
Explanation : According to the given code, the fax object first sets up the number and then loads up the paper. The the fax.run() method is called whoch doesn't necessarily mean that the fax is sent, however, it returns the status of fax machine. This supports the option that, fax machine is started and its status is displayed.