Multiple Choice Identify the
choice that best completes the statement or answers the question.
|
|
1.
|
Consider the following class declaration. public
class Sample { private int a; private double
b;
public Sample(int x, double y)
{ a = x; b =
y; } // No other constructors
}
// The following method appears in a class other than
Sample. public static void test()
{ Sample object = new /* missing constructor call */
; }
Which of the following could be used to replace /* missing
constructor call */ so that the method will compile without error?
a. | Sample() | b. | Sample(int x = 10, double y = 6.2) | c. | Sample(int x, double y) | d. | Sample(10,
6.2) | e. | Sample(6.2, 10) |
|
|
2.
|
Consider the following class declaration
public class
SomeClass
{ private int num; public SomeClass(int n)
{ num = n; } public void increment(int more)
{ num = num + more; } public int getNum()
{ return num; } }
The following code segment appears in
another class. SomeClass one = new SomeClass(100); SomeClass two = new
SomeClass(100); SomeClass three = one; one.increment(200); System.out.println(one.getNum() +
" " + two.getNum() + " " + three.getNum()); What is printed as a result of
executing the code segment?
a. | 100 100 100 | b. | 300 100 100 | c. | 300 100
300 | d. | 300 300 100 | e. | 300 300 300 |
|
|
3.
|
Consider the definition of the Person class below. The class uses the
instance variable adult to indicate whether a person is an adult or not. public class
Person { private String name; private int age; private boolean adult; public
Person (String n, int a) { name = n; age = a; if (age >= 18) { adult =
true; } else { adult = false; } } }
Which of the following
statements will create a Person object that represents an adult person?
a. | Person p = new Person ("Homer",
"adult"); | b. | Person p = new
Person ("Homer", 23); | c. | Person p = new
Person ("Homer", "23"); | d. | Person p = new
Person ("Homer", true); | e. | Person p = new
Person ("Homer", 17); |
|
|
4.
|
public class Student {
private
String name; private String
major; private double gpa;
private int hours;
public Student(String newName, String
newMajor, double newGPA, int newHours)
{ name =
newName; major =
newMajor; gpa =
newGPA; hours =
newHours; }
public String
toString( ) {
return name + "\n" + major +
"\n" + gpa + "\n" + hours
} }
Which of the following could be used to instantiate a new Student
s1?
a. | Student s1 = new Student( ); | b. | s1 = new Student( ); | c. | Student s1 = new
Student("Jane Doe", "Computer Science", 3.333,
33); | d. | new Student s1 = ("Jane Doe", "Computer
Science", 3.333, 33); | e. | new
Student(s1); |
|
|
5.
|
Assume that another method has been defined
that will compute and return the student’s class rank (Freshman, Sophomore, etc). It is
defined as:
public String getClassRank( )
Given that s1 is a student, which of the
following would properly be used to get s1’s class rank?
a. | s1 = getClassRank( ); | b. | s1.toString( ); | c. | s1.getHours(
); | d. | s1.getClassRank( ); | e. | getClassRank(s1); |
|
|
6.
|
What does the following code
compute?
int num = 0;
for(int j = 0; j < 1000;
j++) { c.flip(
); if(c.isHeads())
num++; } double value = (double)
num / 1000;
a. | the number of Heads flipped out of 1000
flips | b. | the number of Heads flipped in a row out of 1000
flips | c. | the percentage of heads flipped out of 1000
flips | d. | the percentage of times neither Heads nor Tails were
flipped out of 1000 flips | e. | nothing at
all
|
|
|
7.
|
Use the following information to answer questions The Die class
from chapter 4 has two constructors defined as follows. Assume MIN_FACES is an int equal to
6.
public Die( )
public
Die(int faces) {
{ numFaces = 6;
if(faces
< MIN_FACES) numFaces =6; faceValue =
1;
else numFaces =
faces;
faceValue =
1; }
}
The instruction
Die d = new Die(10); results in
a. | The Die d having numFaces = 6 and faceValue =
1 | b. | The Die d having numFaces = 10 and faceValue =
1 | c. | The Die d having numFaces = 10 and faceValue =
10 | d. | The Die d having numFaces = 6 and faceValue =
10 | e. | A syntax error |
|
|
8.
|
Consider the following class declaration. public class
IntCell { private int myStoredValue; // constructor not
shown
public int
getValue() { return myStoredValue; } public
String toString () { return "" + myStoredValue;
} }
Assume that the
following declaration appears in a client class.
IntCell m = new IntCell(); Which of these statements
can be used in the client class? I.
System.out.println(m.getValue()); II.
System.out.println(m.myStoredValue); III.
System.out.println(m);
a. | I only | b. | I I only | c. | I I I
only | d. | I and II | e. | I and III |
|
|
9.
|
Consider the following method. public static int
mystery(boolean a, boolean b, boolean c) { int answer = 7; if (!a)
{ answer += 1; } if (b) { answer += 2; } if
(c) { answer += 4; } return answer; }
Which of the
following method calls will return the value 11 ?
a. | mystery(true, true, true) | b. | mystery(true, false, true) | c. | mystery(false, true,
false) | d. | mystery(false, false, true) | e. | mystery(false, false,
false) |
|
|
10.
|
what does this method do
public int foo (int num1, int
num2) { int result = num1;
if (num2 >
num1) result = num2; return
result; }
a. | accepts two integer parameters and returns the first
number | b. | accepts two integer parameters and returns the first and
second number | c. | accepts two
integer parameters and returns the second number | d. | accepts two integer parameters and returns the larger of the
two. | e. | accepts two integer parameters and returns
nothing |
|
|
11.
|
Consider the following methods, which appear in the same class. public
void printSum(int x, double y) { System.out.println(x + y); } public void
printProduct(double x, int y) { System.out.println(x * y); } Consider the following code
segment, which appears in a method in the same class as printSum and printProduct. int num1 =
5; double num2 = 10.0; printSum(num1, num2); printProduct(num1, num2); What, if anything,
is printed as a result of executing the code segment?
a. | 15 50 | b. | 15 50.0 | c. | 15.0 50 | d. | 15.0 50.0 | e. | Nothing is printed
because the code does not compile. |
|
|
12.
|
Consider the following method. public static String abMethod(String a,
String b) { int x = a.indexOf(b); while (x >= 0) { a =
a.substring(0, x) + a.substring(x + b.length()); x = a.indexOf(b); } return
a; }
What, if anything, is returned by the method call abMethod("sing the song",
"ng") ?
a. | "si" | b. | "si the so" | c. | "si the
song" | d. | "sig the
sog" | e. | Nothing is returned because a
StringIndexOutOfBoundsException is thrown. |
|
|
13.
|
Which method can be defined only once in a program?
a. | Math() | b. | main method | c. | finalize
method | d. | static method | e. | private method |
|
|
14.
|
What is the output of this program?
class box
{ int
width; int
height; int
length; int
volume; void volume(int height, int length, int width)
{ volume =
width*height*length; }
} class Prameterized_method
{ public static void main(String
args[])
{ box obj = new
box(); obj.height =
1; obj.length =
5; obj.width =
5;
obj.volume(3,2,1);
System.out.println(obj.volume);
} }
|
|
15.
|
What is the output of this program?
class equality
{ int x;
int y; boolean
isequal()
{ return(x == y);
} }
class Output
{ public static void main(String
args[])
{ equality obj = new
equality(); obj.x =
5; obj.y =
15;
System.out.println(obj.isequal()); }
}
a. | True | b. | False | c. | 0 | d. | Error | e. | 20 |
|