Question

In: Computer Science

Given the below Java code snippet, assuming the code is correct, and the names are meaningful,...

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

Solutions

Expert Solution

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.


Related Solutions

Write a java code snippet to prompt the user for the number of names they’d like...
Write a java code snippet to prompt the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
[50%] Code Snippet Given snippet code below that you are required to complete. You are not...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not allowed to make a new function or change any given code. Please complete each section that are marked with the notation “INSERT YOUR CODE HERE”. Once you complete the snippet below, your output should have the same result with the given output below. Descriptions: [15%] isValid() This function is for checking that there is no duplicated employee data in the linked list. This function...
Given the code snippet below, complete this program by: Displaying the data (temperatures) Displaying the minimum...
Given the code snippet below, complete this program by: Displaying the data (temperatures) Displaying the minimum temperature value Displaying the maximum temperature value Displaying the mean temperature SUBMIT THE PYTHON FILE (.PY) NOT SCREENSHOT, WORD DOCUMENT ETC. IF YOUR SUBMISSION IS NOT A .PY FILE, AUTOMATIC ZERO. #Code snippet below. Copy this into your favorite IDE and complete the tasks import numpy as np import pandas as pd temps = np.random.randint(60, 101, 6) temperatures = pd.Series(temps) 2. Write a python...
The code below is giving an arithmetic overflow. Correct the below given the below code, so...
The code below is giving an arithmetic overflow. Correct the below given the below code, so that the value of s0 is printed before calling the function fun1, inside the function Fun1 and after returning from Fun1 in main. Upload the corrected .asm or .s file in the drop box. .text main: addi $s0,$zero,2 jal Disp jal Fun1 jal Disp j Exit Fun1: addi $sp,$sp,-4 sw $s0,0($sp) addi $s0,$s0,15 jal Disp lw $s0,0($sp) addi $sp,$sp,4 jr $ra Disp: li $v0,1...
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that...
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 (3 3) Use the following pseudocode: inRun = false for each valid index i in the array If inRun...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q =...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); cout << *p; return 0; } Question 31 options: 10 20 compiler error Runtime error Question 32 A union’s members are exactly like the members of a Structure. Question 32 options: True False Question 33 Given the code below, what are the errors. #include <iostream> using namespace...
a) Find a bug in the code snippet below. Assume that a, b, c and d...
a) Find a bug in the code snippet below. Assume that a, b, c and d are correctly declared and initialized integer variables. a = b+c if (a=d) then print *,”A equals to D” else print *,”A does not equal D” end if b) Find a bug in the code snippet below. Assume that a,b,and c are correctly declared and initialized integer variables. if (a+b) > c) then print *,”Sum of A+B is greater than C” end if
Write a java code snippet that allows a teacher to track her students’ grades. Use a...
Write a java code snippet that allows a teacher to track her students’ grades. Use a loop to prompt the user for each student’s name and the grade they received. Add these values to two separate parallel ArrayLists. Use a sentinel to stop. Output a table listing each of the student’s names in one column and their associated grades in the second column.
Using JAVA 2. A run is a sequence of adjacent repeated values. Write a code snippet...
Using JAVA 2. A run is a sequence of adjacent repeated values. Write a code snippet that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 (3 3) Use the following pseudocode: inRun = false for each valid index i in the array If...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT