Please try the following pretest. We donβt expect you to know the answers to these questions at all yet! So donβt worry about it if you donβt know the answers. If you have no idea on the answer, it is okay to skip questions or to make your best guess.
Please make sure you are logged into Runestone before beginning. Click the Start button when you are ready to begin the exam. You can only take the exam once. There are no time limits, but it will keep track of how much time you take. Click on the Next button to go to the next question. Click on the Prev button to go to the previous question. Use the number buttons to jump to a particular question. Click the Pause button to pause the exam (you will not be able to see the questions when the exam is paused). Click on the Finish button after you have answered all the questions. The number correct, number wrong, and number skipped will be displayed.
This would be true if the loop was printing each character twice and was incrementing the index by 1, but it prints two characters at a time and increments the index by 2.
This will loop through the string and print two characters at a time. The first time through the loop index = 0 and it will print "12". The second time through the loop index = 2 and it will print "34". The third time through the loop index = 4 and it will print "56". Remember that the substring method that takes two integer values will start the substring at the first value and include up to the character before the second value.
Consider the following data field and method findLongest. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended. For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10], the call findLongest(10) should return 3, the length of the longest consecutive block of 10s. Which of the following best describes the value returned by a call to findLongest?
The variable lenCount is incremented each time the current array element is the same value as the target. It is never reset so it counts the number of occurrences of the value target in nums. The method returns maxLen which is set to lenCount after the loop finishes if lenCount is greater than maxLen.
It is the length of the shortest consecutive block of the value target in nums.
The loop starts with var1=0 and var2=2. The while checks that var2 isnβt 0 (2!=0) and that var1 / var2 is greater than or equal to zero (0/2=0) so this is equal to zero and the body of the while loop will execute. The variable var1 has 1 added to it for a new value of 1. The variable var2 has 1 subtracted from it for a value of 1. At this point var1=1 and var2=1. The while condition is checked again. Since var2 isnβt 0 (1!=0) and var1/var2 (1/1=1) is >= 0 so the body of the loop will execute again. The variable var1 has 1 added to it for a new value of 2. The variable var2 has 1 subtracted from it for a value of 0. At this point var1=2 and var2=0. The while condition is checked again. Since var2 is zero the while loop stops and the value of var1 is 2 and var2 is 0.
The loop wonβt finish executing because of a division by zero.
At a certain high school students receive letter grades based on the following scale: 93 or above is an A, 84 to 92 inclusive is a B, 75 to 83 inclusive is a C, and below 75 is an F. Which of the following code segments will assign the correct string to grade for a given integer score?
Choice I uses multiple ifβs with logical ands in the conditions to check that the numbers are in range. Choice Choice II wonβt work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". Choice III uses ifs with else if to make sure that only one conditional is executed.
Choice II wonβt work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". This could have been fixed by using else if instead of just if.
Choice III is one of the correct answers. However, choice I is also correct. Choice I uses multiple ifβs with logical ands in the conditions to check that the numbers are in range. Choice III uses ifs with else if to make sure that only one conditional is executed.
Choice II wonβt work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". This could have been fixed by using else if instead of just if.
Choice II wonβt work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". This could have been fixed by using else if instead of just if.
Susan is 5 years older than Matt. Three years from now Susanβs age will be twice Mattβs age. What should be in place of condition in the code segment below to solve this problem?
This would be true if Susan was 5 years younger than Matt and three years ago she was twice his age. But, how could she be younger than him now and twice his age three years ago?
This is almost right. It has Susan as 5 years older than Matt now. But the second part is wrong. Multiplication will be done before addition so (2 * m + 3) wonβt be correct for in 3 years Susan will be twice as old as Matt. It should be (2 * (m + 3)) or (2 * m + 6).
This canβt be right because Susan is 5 years older than Matt, so the first part is wrong. It has Susan equal to Mattβs age minus 5 which would have Matt older than Susan.
The while loop will iterate 8 times. The value of num each time through the loop is: 0, 2, 4, 6, 8, 10, 12, and 14. The corresponding remainder operator of 3 is: 0, 2, 1, 0, 2, 1, 0, 2, which is print to the console.
The loop will iterate 8 times not 9. When the value of num exceeds 14, num will no longer be evaluated against the conditional statements. The remainder operator of 3 will be evaluated on the num values of 0, 2, 4, 6, 8, 10, 12 and 14.
Given the following incomplete class declaration, which of the following can be used to replace the missing code in the advance method so that it will correctly update the time?
public class TimeRecord
{
private int hours;
private int minutes; // 0<=minutes<60
public TimeRecord(int h, int m)
{
hours = h;
minutes = m;
}
// postcondition: returns the
// number of hours
public int getHours()
{
/* implementation not shown */
}
// postcondition: returns the number
// of minutes; 0 <= minutes < 60
public int getMinutes()
{
/* implementation not shown */
}
// precondition: h >= 0; m >= 0
// postcondition: adds h hours and
// m minutes to this TimeRecord
public void advance(int h, int m)
{
hours = hours + h;
minutes = minutes + m;
/* missing code */
}
// ... other methods not shown
}
This will update the hours and minutes correctly. It will add the floor of the division of minutes by 60 to hours and then set minutes to the remainder of the division of minutes by 60.
When a contains a value that is equal to zero then multiplying that value by 2 will always be 0 and will never make the result larger than the temp value (which was set to some value > 0), so an infinite loop will occur.
This canβt be true because a[1]--; means the same as a[1] = a[1] - 1; so the 3 changes to 2. Parameters are all pass by value in Java which means that a copy of the value is passed to a method. But, since an array is an object a copy of the value is a copy of the reference to the object. So changes to objects in methods are permanent.
This would only be correct if we had s1 = s2; after s2.toLowerCase(); was executed. Strings are immutable and so any change to a string returns a new string.
This would be correct if we had s1 = s3; after s3.toUpperCase(); was executed. Strings are immutable and so any change to a string returns a new string.
When the recursive call to mystery(1) occurs (the 4th call to mystery), the division of x /10 equals .01--this becomes 0 because this is integer division and the remainder is thrown away. Therefore the current call will be completed and all of the previous calls to mystery will be completed.
The first call to mystery with the integer 1234 will print 1234 % 10. The β%β means remainder. The remainder of 1234 divided by 10 is 4 so the first thing printed must be 4.
The first call to mystery with the integer 1234 will print 1234 % 10. The β%β means remainder. The remainder of 1234 divided by 10 is 4 so the first thing printed must be 4.
The first call to mystery with the integer 1234 will print 1234 % 10. The β%β means remainder. The remainder of 1234 divided by 10 is 4 so the first thing printed must be 4.
This has a recursive call which means that the method calls itself when (x / 10) is greater than or equal to zero. Each time the method is called it prints the remainder of the passed value divided by 10 and then calls the method again with the result of the integer division of the passed number by 10 (which throws away the decimal part). After the recursion stops by (x / 10) == 0 the method will print the remainder of the passed value divided by 10 again.
If the search value is not in the array, a sequential search will have to check every item in the array before failing, a binary search will be faster.
Results will differ depending on the exact location of the element, but Binary Search will still find the element faster while Sequential will have to check more elements.
The search value is the first element in the array.
Only when the search value is the first item in the array, and thus the first value encountered in sequential search, will sequential be faster than binary.
Sequential Search can never be faster than Binary Search.
The add method that takes just a value as a parameter adds that value to the end of the list. The set replaces the value at that index with the new value. The add with parameters of an index and a value puts the passed value at that index and moves any existing values by one index to the right (increments the index). So the list looks like: 1 // add 1 1 2 // add 2 1 2 3 // add 3 1 2 4 // set index 2 to 4 1 2 5 4 // add 5 to index 2 (move rest right) 1 2 5 4 6 // add 6 to end
int [][] mat = new int [3][4];
for (int row = 0; row < mat.length; row++)
{
for (int col = 0; col < mat[0].length; col++)
{
if (row < col)
mat[row][col] = 1;
else if (row == col)
mat[row][col] = 2;
else
mat[row][col] = 3;
}
}
When you create a 2-d array the first value is the number of rows and the second is the number of columns. This code will put a 1 in the array when the row index is less than the column index and a 2 in the array when the row and column index are the same, and a 3 in the array when the row index is greater than the column index.
This would be true if the first value when you create a 2-d array was the number of columns and the second was the number of rows. Also you would need to set the value to 3 when the column index was greater than the row and a 1 when the row index was greater than the column index.
This would be true if you set the value to 3 when the column index was greater than the row and a 1 when the row index was greater than the column index.
The variable i loops from 1 to 6 and each time the values are as follows: i = 1, t = 10, a = 4, b = 9, i = 2, t = 4, a = 11, b =2, i = 3, t = 11, a = 5, b = 8, i = 4, t = 5, a = 12, b = 1, i = 5, t = 12, a = 6, b = 7, i = 6, t = 6, a = 13, b = 0
This is the definition of encapsulation and this is done in Java using private (a member is directly accessible only in the class that defines it) and protected (a member is directly accessible only within code in the same package and in subclasses).
Data (fields) are directly accessible by objects in the same package and in subclasses.
Encapsulation means that only code in the defining class has direct access. The visibility modifier protected gives direct access to code in classes in the same package and subclasses.
Data (fields) are directly accessible by objects in the same package.
Encapsulation means that only code in the defining class has direct access. The default package access gives direct access to code in classes in the same package.