Click the Start button when you are ready to begin the exam, but only then as you can only take the exam once. Click on the Next button to go to the next question. Click on the Previous 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.
Integer int1 = new Integer(3);
Integer int2 = new Integer(3);
Integer int3 = int2;
I. (int3.equals(int2))
II. (int1.equals(int2))
III. (int3 == int2)
IV. (int1 == int2)
V. (int2 == int3)
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.
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.
When the search value is the first element, sequential will always be faster, as it will only need to check one element whereas a binary search would start in the middle of the array and have to keep looking.
There is a method called checkString that determines whether a string is the same forwards and backwards. The following data sets can be used for testing the method. Which is a best set of test cases?
There is a better answer. While it is good to test with strings that contain both upper and lower case characters there is another reason why data set 2 is better.
Data set 2 since it contains strings which should return true and should return false.
The add method with one parameters will add that value to end of the list. The method set changes the value at that index in the list. The method add with an index will move anything at the index or above one to the right and then set the value of that index.
Consider the following declaration for a class that will be used to represent points in the xy-coordinate plane. Which of these constructors would be legal for the NamedPoint class?
public class Point
{
private int myX; // coordinates
private int myY;
public Point( )
{
myX = 0;
myY = 0;
}
public Point(int a, int b)
{
myX = a;
myY = b;
}
// ... other methods not shown
}
// The following incomplete class declaration is intended to extend the
// above class so that two-dimensional points can be named.
public class NamedPoint extends Point
{
private String myName;
// constructors go here
// ... other methods not shown
}
Proposed Constructors:
I. public NamedPoint()
{
myName = "";
}
II. public NamedPoint(int d1, int d2, String name)
{
myX = d1;
myY = d2;
myName = name;
}
III. public NamedPoint(int d1, int d2, String name)
{
super(d1, d2);
myName = name;
}
NamedPoint will inherit from Point all fields but the fields are private and they can not be directly accessed in NamedPoint. You can use super as the first line in a constructor to initialize inherited fields. You can also set your own fields in a constructor. If you donβt use super as the first line in a constructor one will be put there by the compiler that will call the parentβs no argument constructor.
II is invalid. Children inherit all of the fields from a parent but do not have direct access to private fields. You can use super in a constructor to initialize inherited fields by calling the parentβs constructor with the same parameter list.
I is okay but II is invalid. Children inherit all of the fields from a parent but do not have direct access to private fields. You can use super in a constructor to initialize inherited fields by calling the parentβs constructor with the same parameter list.
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
}
// Consider the following declaration that appears in a client program:
TimeRecord[] timeCards = new TimeRecord[100];
// Assume that timeCards has been initialized with TimeRecord
// objects. Consider the following code segment that is intended to compute
// the total of all the times stored in timeCards.
TimeRecord total = new TimeRecord(0,0);
for (int k = 0; k < timeCards.length; k++)
{
/* missing expression */
}
Which of the following can be used to replace /* missing expression */ so that the code segment will work as intended?
I.
total.advance(timeCards[k].getHours(), timeCards[k].getMinutes());
II.
timeCards[k].advance();
III.
total += timeCards[k].advance();
IV.
total.advance(timeCards[k].hours, timeCards[k].minutes);
V.
timeCards[k].advance(timeCards[k].getHours(), timeCards[k].getMinutes());
You can not access private inherited fields directly. You can either use public method to get and set their values or invoke the parentβs constructor using super(paramList) as the first line of code in a constructor.
You can not access private inherited fields directly. You can either use public method to get and set their values or invoke the parentβs constructor using super(paramList) as the first line of code in a constructor.
You can not access private inherited fields directly. You can either use public method to get and set their values or invoke the parentβs constructor using super(paramList) as the first line of code in a constructor.
To initialize inherited private fields you can use the parentβs constructor. Use super followed by the parameter list as the first line of code in the constructor.
A sequential search loops through the elements of an array starting with the first and ending with the last and returns from the loop as soon as it finds the passed value. It has to check every value in the array when the value it is looking for is not in the array. This would take 10 executions of the loop.
I. Methods from a superclass can be used in a subclass without rewriting
or copying code.
II. Objects from subclasses can be passed as arguments to a method designed
for the superclass
III. Objects from subclasses can be stored in the same array
IV. All of the above
V. None of the above
In fact, all of the reasons listed are valid. Subclasses can reuse methods written for superclasses without code replication, subclasses can be stored in the same array, and passed as arguments to methods meant for the superclass. All of which make writing code more streamlined.
II is also valid. In some cases a single method is applicable for a number of subclasses, and inheritance allows you to pass objects of the subclasses to the same method instead of writing individual methods for each subclass.
II and III are also valid, in some cases a single method is applicable for a number of subclasses, and inheritance allows you to pass all the subclasses to the same method instead of writing individual methods for each subclass and you might want to store subclasses together in a single array, and inheritance allows for this.
This canβt be true because a[1]--; means the same as a[1] = a[1] - 1; So the 3 will become a 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.
The variable i loops from 1 to 6 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 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.
int[][] matrix = { {1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};
int sum = 0;
int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++)
{
sum = sum + matrix[row][col];
}
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] = 3;
else if (row == col)
mat[row][col] = 2;
else
mat[row][col] = 1;
}
}
This will fill mat with 3 if the row index is less than the column index, 2 if the row index is equal to the column index, and a 1 if the row index is greater than the column index.
Assume that temp is an int variable initialized to be greater than zero and that a is an array of type int. Also, consider the following code segment. What of the following will cause an infinite loop?
When a contains a value that is less than or equal to zero, then multiplying that value by 2 will never make the result larger than the temp value (which was set to some value > 0), so an infinite loop will occur.
The first test is false since num2 is less than 0 and for a complex conditional joined with And (&&) to be true both expressions must be true. Next, else if ((num2<0) || (num1<0)) is executed and this will be true since num2 is less than 0 and for a complex conditional joined with Or (||) only one of the expressions must be true for it to execute.