Unit_5-MCQ1
Unit_5-MCQ1
Unit 5 MCQ 1
1. A rectangular box fits inside another rectangular box if and only if the height, width, and depth of the smaller box
are each less than the corresponding values of the larger box. Consider the following three interface declarations
that are intended to represent information necessary for rectangular boxes.
Which of the interfaces, if correctly implemented by a Box class, would be sufficient functionality for a user of the
Box class to determine if one Box can fit inside another?
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III
Unit 5 MCQ 1
Which of the following replacements for /* missing code */ will correctly implement the Circle
constructor?
Unit 5 MCQ 1
(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III
Unit 5 MCQ 1
3. Consider the following class that stores information about temperature readings on various dates.
/* missing code */
// There may be instance variables, constructors, and methods that are not shown.
Consider the following code segments that are potential replacements for /* missing code */.
return d1.compareTo(d2);
return -1;
Unit 5 MCQ 1
return 0;
else
return 1;
Which of the code segments could be used to replace /* missing code */ so that compareTo can be used to order
TemperatureReading objects by increasing temperature value?
(A) II only
(B) I and II only
(C) I and III only
(D) II and III only
(E) I, II, and III
Unit 5 MCQ 1
4. Consider the following two methods, which appear within a single class.
Unit 5 MCQ 1
Which of the following best explains the reason why the class will not compile?
(A) The variable numGallons is not declared in the getNumGallons method.
(B) The variable saltWater is not declared in the isSaltWater method.
(C) The isSaltWater method does not return the value of an instance variable.
(D) The value returned by the getNumGallons method is not compatible with the return type of the method.
(E) The value returned by the isSaltWater method is not compatible with the return type of the method.
Unit 5 MCQ 1
6. Consider the following class definition. The class does not compile.
The accessor method getScore is intended to return the score of a Player object. Which of the following
best explains why the class does not compile?
(A) The getScore method should be declared as private.
(B) The getScore method requires a parameter.
(C) The return type of the getScore method needs to be defined as double.
(D) The return type of the getScore method needs to be defined as String.
(E) The return type of the getScore method needs to be defined as void.
Unit 5 MCQ 1
What is printed as a result of executing the code segment if the code segment is the first use of a SomeClass
object?
(A) 0
(B) 1
(C) 11
(D) 14
(E) 30
Unit 5 MCQ 1
8. Consider the following while loop. Assume that the int variable k has been properly declared and
initialized.
while (k < 0)
{
System.out.print("*");
k++;
}
Which of the following ranges of initial values for k will guarantee that at least one "*" character is printed?
I. k < 0
II. k = 0
III. k > 0
(A) I only
(B) III only
(C) I and II only
(D) II and III only
(E) I, II, and III
Unit 5 MCQ 1
return myStoredValue;
I. System.out.println(m.getValue());
II. System.out.println(m.myStoredValue);
III. System.out.println(m);
(A) I only
(B) II only
(C) III only
(D) I and II
(E) I and III
Unit 5 MCQ 1
Consider the following code segment, which appears in a class other than Item or ItemCollection.
Unit 5 MCQ 1
Unit 5 MCQ 1
public Point ()
public Circle()
Unit 5 MCQ 1
In a client program which of the following correctly declares and initializes Circle circ with center at (29.5, 33.0)
and radius 10.0 ?
(A) Circle circ = new Circle(29.5, 33.0, 10.0);
(B) Circle circ = new Circle((29.5, 33.0), 10.0);
(C) Circle circ = new Circle(new Point (29.5, 33.0), 10.0);
Circle circ = new Circle();
(D) circ.myCenter = new Point(29.5, 33.0);
circ.myRadius = 10.0;
Circle circ = new Circle();
circ.myCenter = new Point();
(E) circ.myCenter.myX = 29.5;
circ.myCenter.myY = 33.0;
cire.myRadius = 10.0;
Unit 5 MCQ 1
public Point ()
public Circle()
Unit 5 MCQ 1
Which of the following would be the best specification for a Circle method isInside that determines whether a
Point lies inside this Circle?
(A) public boolean isInside()
(B) public void isInside(boolean found)
(C) public boolean isInside(Point p)
(D) public void isInside(Point p, boolean found)
(E) public boolean isInside(Point p, Point center, double radius)
13. The following method is intended to return a string containing the character at position n in the string str. For
example, getChar("ABCDE", 2) should return "C".
/* missing precondition */
public String getChar(String str, int n)
{
return str.substring(n, n + 1);
}
Which of the following is the most appropriate precondition for the method so that it does not throw an exception?
(A) /* Precondition: 0 < n < str.length() - 1 */
(B) /* Precondition: 0 <= n <= str.length() - 1 */
(C) /* Precondition: 0 <= n <= str.length() */
(D) /* Precondition: n > str.length() */
(E) /* Precondition: n >= str.length() */
14. In the Toy class below, the raisePrice method is intended to increase the value of the instance variable
price by the value of the parameter surcharge. The method does not work as intended.
Which of the following changes should be made so that the class definition compiles without error and the method
raisePrice works as intended?
Unit 5 MCQ 1
Directions: Select the choice that best fits each statement. The following question(s) refer to the following information.
15. Which of the following changes to SomeClass will allow other classes to access but not modify the value of myC ?
(A) Make myC public.
(B)
(C)
(D)
(E)
Unit 5 MCQ 1
The following code segment appears in a method in a class other than Something.
Which of the following best describes the behavior of the code segment?
The code segment does not compile because the increment method should be called on an object of the
(A)
class Something, not on the class itself.
The code segment creates a Something object s. The class Something’s static variable count is
(B)
initially 0, then increased by 1.
The code segment creates a Something object s. The class Something’s static variable count is
(C)
initially 0, then increased by 5, then increased by 1.
The code segment creates a Something object s. After executing the code segment, the object s has a
(D)
count value of 1.
The code segment creates a Something object s. After executing the code segment, the object s has a
(E)
count value of 5.
Unit 5 MCQ 1
The following statement appears in a method in a class other than Tester. It is intended to create a new
Tester object t with its attributes set to 10 and 20.
Which of the following can be used to replace /* missing constructor */ so that the object t is correctly
created?
public Tester(int first, int second)
{
(A) num1 = first;
num2 = second;
}
public Tester(int first, int second)
{
(B) num1 = 1;
num2 = 2;
}
public Tester(int first, int second)
{
(C) first = 1;
second = 2;
}
public Tester(int first, int second)
{
(D) first = 10;
second = 20;
}
public Tester(int first, int second)
{
(E) first = num1;
second = num2;
}
Unit 5 MCQ 1
The following code segment appears in a class other than MenuItem or Combo.
Unit 5 MCQ 1
The following code segment appears in a method in a class other than Class1 or Class2.
Unit 5 MCQ 1
(A) 2
(B) 1
(C) 0
(D) -2
(E) Nothing is printed because the code segment does not compile.
Unit 5 MCQ 1
The class contains the withdraw method, which is intended to update the instance variable balance under
certain conditions and return a value indicating whether the withdrawal was successful. If subtracting
withdrawAmount from balance would lead to a negative balance, balance is unchanged and the
withdrawal is considered unsuccessful. Otherwise, balance is decreased by withdrawAmount and the
withdrawal is considered successful.
Which of the following code segments can replace /* missing code */ to ensure that the withdraw method
works as intended?
I.
if (withdrawAmount > balance)
{
return "Overdraft";
}
else
{
balance -= withdrawAmount;
return true;
}
II.
if (withdrawAmount > balance)
{
return false;
}
else
{
balance -= withdrawAmount;
return balance;
}
Unit 5 MCQ 1
III.
if (withdrawAmount > balance)
{
return false;
}
else
{
balance -= withdrawAmount;
return true;
}
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III