Multiple Choice Identify the
choice that best completes the statement or answers the question.
|
|
1.
|
Math.pow(3,3) returns
|
|
2.
|
Which of the following is not an advantage of using
methods.
a. | Using methods makes reusing code easier. | c. | Using methods makes programs easier
to read. | b. | Using methods makes programs run faster. | d. | Using methods hides detailed implementation
from the clients. |
|
|
3.
|
You should fill in the blank in the following code with
______________.
public class Test { public static void
main(String[] args) {
System.out.print("The grade is " +
getGrade(78.5)); System.out.print("\nThe grade is
" + getGrade(59.5)); }
public static _________
getGrade(double score) { if (score >=
90.0) return
'A'; else if (score >=
80.0) return
'B'; else if (score >=
70.0) return
'C'; else if (score >=
60.0) return
'D';
else return
'F'; } }
a. | char | c. | void | b. | int | d. | boolean |
|
|
4.
|
Analyze the following code:
class Test { public
static void main(String[] args) {
System.out.println(xMethod((double)5)); }
public
static int xMethod(int n) {
System.out.println("int"); return
n; }
public static long xMethod(String n)
{
System.out.println("long"); return
n; } } What is the output of this program?
a. | The program displays long followed by 5. | c. | The
program does not compile. | b. | The program displays int followed by
5. | d. | The program runs fine
but displays things other than 5. |
|
|
5.
|
Analyze the following code.
public class Test {
public static void main(String[] args)
{ System.out.println(m(2));
}
public static int m(int num)
{ return num;
} public static void m(int num)
{ System.out.println(num);
} }
a. | The program runs and prints 2 once. | c. | The program runs and prints 2
twice. | b. | The program has a compile error because the second m method is defined, but not
invoked in the main method. | d. | The program has a compile error because the two methods m have the same
signature. |
|
|
6.
|
Analyze the following code:
class Test { public
static void main(String[] args) {
System.out.println(xmethod(5)); }
public static int
xmethod(int n, long t) {
System.out.println("int"); return
n; }
public static long xmethod(long n)
{ System.out.println("long");
return n; } }
a. | The program does not compile because the compiler cannot distinguish which xmethod to
invoke. | c. | The program displays int followed by 5. | b. | The program displays
long followed by 5. | d. | The
program runs fine but displays things other than 5. |
|
|
7.
|
Variables defined in the method header are called _____.
a. | global variables | c. | arguments | b. | parameters | d. | local variables |
|
|
8.
|
Suppose your method does not return any value, which of the following keywords
can be used as a return type?
a. | public | c. | void | b. | static | d. | int |
|
|
9.
|
The following program find the lowest integer in an array of integers. You
want to save the lowest value in an integer variable called low. You can assume the
array nums hold a variety of integer values. Examine the following
code:
<low variable declaration>
for (int x =0; x < nums.length;
x++) { if (nums[x] < low) low =
nums[x]; }
Which of the following is the best way to declare the
low variable?
a. | int low = 0; | c. | int low = -1000000 | b. | int low = 1000000 | d. | int low =
Integer.MAX_VALUE |
|
|
10.
|
Which of the following code segments will reverse the direction of a GridWorld
Actor object?
a. | setDirection(getDirection + 90); | d. | setDirection(Location.HALF_CIRCLE); | b. | setDirection(getDirection -
90); | e. | None of
these | c. | setDirection(getDirection() + Location.SOUTH); |
|
|
11.
|
Which term refers to using methods of the same name, but different
parameters?
a. | Overriding | d. | Polymorphism | b. | Overloading | e. | Encapsulation | c. | Inheritance |
|
|
12.
|
Which of the five methods called by the Critter act( ) method would you override
if you wanted to change possible locations to which the critter could move?
a. | selectMoveLocation | d. | getNeighbors | b. | getMoveLocation | e. | getEmptyAdjacentLocations | c. | processActors |
|
|
13.
|
How does a ChameleonCritter decide which color to change to?
a. | Changes it color match to the color of the nearest Actor in the grid. | d. | Changes its color
randomly to the subset provided by the GridWorld case study | b. | Changes its color to
match a random neighboring actor | e. | Changes it color to match a neighboring Actor, starting North of the ChameleonCritter
and rotating clockwise | c. | Changes it color to match the adjacent
neighbor’s color in the direction it is facing |
|
|
14.
|
Which built-in ArrayList method will change an existing object in the ArrayList
without changing its index?
a. | add(index, new object) | d. | remove(index), add(new object) | b. | put(index, new
object) | e. | None of
these | c. | set(index, new object) |
|
|
15.
|
What does this statement output? String s1 =
“Mississippi”; System.out.println(s1.indexOf(“ssi”));
|
|
16.
|
Which of the following outputs the size of the String str?
a. | str.length | d. | str.size() | b. | str.length() | e. | None of these | c. | str.size |
|
|
17.
|
Which of the following is not a primitive data type?
a. | boolean | d. | Integer | b. | char | e. | All are primitive data
types | c. | double |
|
|
18.
|
Which of the following will tell you the number of columns in a 2D array called
matrix?
a. | matrix.length(); | d. | matrix.length; | b. | matrix[0].length(); | e. | matrix[0].length; | c. | matrix.size(); |
|
|
19.
|
what does the following code do, assuming nums is an array of
integers?
int x=0; for(int i=nums.length; i >= 0; i--) x
+= nums[i]; System.out.println(x);
a. | Counts number of elements in an array | d. | Outputs the contents of the
array | b. | Sums the contents of the array | e. | Assigns the value of x to all elements in the
array | c. | Finds the largest element in the array |
|
|
20.
|
public static void fill(int[][]m) { int n = m.length; for(int i =1; i < n - 1; i++) {
for(int j =
1; j < n - 1; j++) { m[i][j]=1; } } }
Which of the
following answers shows the contents of m?
a. | 0000 0000 0000 0000 | c. | 0000 0110 0110 0000 | b. | 1100 1100 0000 0000 | d. | 1111 1111 1111 1111 |
|
|
21.
|
Consider the method public String mystery(String
s) { String s1 = s.substring(0,1); String
s2 = s.substring(1, s.length() - 1); String s3 = s.substring(s.length() -
1); if (s.length() <=
3) return s3 + s2 +
s1; else return
s1 + mystery(s2) + s3; }
What is the output
of
System.out.println(mystery("DELIVER"));
a. | DELIVER | d. | RELIVED | b. | DEVILER | e. | DLEIEVR | c. | REVILED |
|
|
22.
|
public interface InterfaceA { void methodA(); }
public interface
InterfaceB extends InterfaceA { void methodB(); }
public class ClassA implements
InterfaceA { public void methodA() {}
public void methodB() {} }
public class ClassB extends ClassA implements
InterfaceB { public ClassB() {} ...
<methods not shown> }
What is the minimum number of methods that must be defined in
classB for it to compile with no errors?
a. | No methods required | d. | methodA and methodB | b. | methodA | e. | methodA, methodB and
toString | c. | methodB |
|
|
23.
|
int n = 2005; for (int i = 0; i < 50; i++) n =
(n + 3) / 2;
What is the value of n after the for loop ends?
|
|
24.
|
twist is defined as follows: public void twist(String[]
w) { String temp = w[0].substring(0,
1); w[0] = w[1].substring(0, 1) +
w[0].substring(1); w[1] = temp +
w[1].substring(1); }
What is the output of the following code
segment?
String[] words = {"HOW",
"NEAT"}; twist(words): System.out.println(words[0] + " " +
words[1]);
a. | NOW NOW | d. | HOW NEAT | b. | HOW HOW | e. | NOW HEAT | c. | NOW
HOW |
|
|
25.
|
If Crossword extends Puzzle and implements
Solvable, and:
Puzzle p = new Puzzle(); Crossword cw = new Crossword(10, 20);
are
declared, which of the following statements will cause a syntax error?
a. | p = cw; | d. | Solvable x = new Crossword(10, 20); | b. | cw = new
Puzzle(); | e. | All of the above
will compile with no errors. | c. | p = new
Crossword(10,20); |
|
|
26.
|
What is the output of the following code? String barb =
"BARBARA"; scramble(barb); System.out.println(barb);
The method scramble is
defined as follows: public String scramble(String str) { if
(str.length() >= 2)
{ int n = str.length() /
2; str = scramble(str.substring(n)) +
str.substring(0, n); } return
str; }
a. | BARBARA | d. | ARBABARB | b. | ARBABAR | e. | ARABARBARB | c. | AABAR |
|
|
27.
|
What are the values in arr after the following statements are
executed? int[] arr = {1, 1, 0, 0, 0};
for (int i = 2; i < arr.length;
i++) arr[i] = arr[i-1] + arr[i-2];
a. | 11011 | d. | 11235 | b. | 11210 | e. | 11248 | c. | 11222 |
|
|
28.
|
What is the output of the following code? List nums = new
ArrayList(3); nums.add(New Integer(1)); nums.add(New Integer(2)); nums.add(0,
nums.get(1));
Object x = nums.get(0); Object y = nums.get(2);
if (x ==
y) System.out.println(x + " is equal to " +
y); else System.out.println(x + " is NOT equal to " +
y);
a. | 1 is equal to 2 | d. | 2 is NOT equal to 2 | b. | 1 is NOT equal to 2 | e. | IndexOutOfBoundsException | c. | 2 is equal to
2 |
|
|
29.
|
Which of the following conditions must always be true after the while loop
finishes? while (hours < hours0 || (hours == hours0 && mins <
mins0)) { mins += 5; if (mins >=
60) { mins -=
60; hours++;
} }
a. | hours > hours0 && mins >= mins0 | d. | hours >= hours0 &&
(hours != hours0 && mins >= mins0) | b. | hours >= hours0 && mins >=
mins0 | e. | hours >= hours0
&& (hours != hours0 || mins >= mins0) | c. | hours < hours0 || (hours == hours0
&& mins < mins0) |
|
|
30.
|
Consider the following method: public int goFigure(int
x) { if (x <
100) x = goFigure(x +
10); return (x - 1); }
What does goFigure(60) return?
|
|
31.
|
fun is defined as follows: public int fun(int[]
v) { v[0]--; return
v[0] + 2; }
What is the value of v[0] after the following code segment
is executed? int [] v = { 3, 4, 5 }; v[0] = fun(v);
|
|
32.
|
The method xProperty is defined as follows: public boolean xProperty(int
a) { return a == 2 * (a / 10 + a % 10); }
For which of the
following values of a does xProperty(a) return true?
|
|
33.
|
Consider the method public int[] copyX(int[]
arr) { int[] result = new
int[arr.length];
for (int i = 0; i < arr.length;
i++) { if
(arr[i] <=
0)
break; if (arr[i] >
10)
break; result[i] =
arr[i]; } return
result; }
Suppose it is rewritten as follows: public int[]
copyX(int[] arr) { int[] result = new
int[arr.length]; int i = 0;
while (< condition >)
{ result[i] =
arr[i];
i++; } return result; }
Which of
the following expressions can replace < condition > so that the second version is equivalent to
the first one (i.e., for any int[] parameter arr, it returns the same result as the first
version)?
a. | i < arr.length && (arr[i] <= 0 || arr[i] >
10) | d. |
i < arr.length && !(arr[i] <= 0 || arr[i] >
10) | b. | (arr[i] <= 0 || arr[i] > 10) || i >=
arr.length | e. | i <
arr.length && arr[i] > 0 && arr[i] <=
10) | c. | (arr[i] <= 0 || arr[i] > 10) && i <
arr.length |
|
|
34.
|
Alicia is five years older than her brother Ben. Three years from now
Alicia's age will be twice Ben's age. How old are Alicia and Ben now? Ha wrote the
following program to solve this puzzle: public class AliciaAndBen {
public static void main(String[] args)
{ for (int a = 1; a <= 100;
a++) for (int
b = 1; b <= 100;
b++)
if (< condition
>)
System.out.println("Alicia is " + a + " and Ben is " +
b); } }
Which of the following expressions should replace <
condition > in Hal's program so that it displays the correct solution to the puzzle?
a. | (a == b - 5) && (a - 3 == 2 * (b - 3)) | d. | a == (b - 5) && (2 * a -
3) == (b - 3) | b. | a == b + 5 && a + 3 == 2 * b + 6 | e. | None of the above works | c. | (a == (b + 5))
&& ((a + 3) ++ (2 * b + 3)) |
|
|
35.
|
Suppose a two-dimensional array of chars m has 4 rows and 5 columns and holds
the values represented in the picture below: x.xxx xx..x .x.xx xx...
What are the
values in m after the following code segment is executed? int r, c; for (r = 1; r <
m.length; r++) for (c = 1; c < m[0].length - 1;
c++) if (m[r-1][c-1] ==
'x' && m[r-1][c+1] ==
'x')
m[r][c] = 'x';
a. |
x.xxx
xx..x
.xxxx xx... | d. |
x.xxx
xx.xx
.xxxx xxxx. | b. |
xxxxx
xx..x
.xxxx xx... | e. |
x.xxx
xx.xx
.x.xx xxx.. | c. |
x.xxx
xx.xx
xxxxx xxx.. |
|