Multiple Choice
Identify the
letter of the choice that best completes the statement or answers the question.
|
|
1.
|
int [ ] [ ] nums = new int [5] [5]; //assume nums is filled with
zeroes
int
z =0;
for
(int x = 1; x < nums.length; x+=2){
for (int y = 1; y < nums.length; y++){
z++;
if (z>9) z =1;
nums[y][x] =
z;
}
}
Which of the following represents what the nums array will look
like?
a. | 00000
01050
02060
03070
04080 | c. | 00000
01234
00000
05678
00000
| b. | 00000
01594
02615
03726
04837 | d. | 00000
01234
05678
09123
04567 | | | | |
|
|
2.
|
Consider the following class:
public class Clock {
private int
hours;
private int mins;
public Clock(int h, int m)
{
hours = h;
mins = m;
}
//moves this clock one minute
forward
public void
move()
{
//missing code
}
public void
set(int h, int m)
{
hours = h;
mins = m;
normalize();
}
private void
normalize()
{
while(mins
>=60)
{
mins -= 60;
hours++;
}
hours %= 12;
}
}
Which of the
following could replace <missing code> in the move method?
Option I. mins++;
mins%60;
Option II.
mins++;
normalize( );
Option III. set(hours, mins + 1); a. | All of
Above | d. | I
only | b. | II
only | e. | III
only | c. | II and
III | | | | |
|
|
3.
|
What
is the output of the following?
public class Person
{
String name;
public Person(String nm) { name = nm;}
public String getName() {return name;}
public String toString() {return getName();}
}
-----------------------------------------------------------------
public class OldLady extends Person
{
private int age;
public OldLady(String nm, int yrs)
{
super(nm);
age = yrs;
}
public String getName()
{
return "Mrs.
" + super.getName();
}
public int getAge()
{
return age;
}
}
-----------------------------------------------------------------
public class OldLadyRunner
{
public static void
main(String[] args) {
OldLady o = new
OldLady("Robinson", 92);
System.out.println(o + ", " + o.getAge());
}
}
a. | Mrs. Robinson,
92 | c. | Robinson | b. | Robinson, 92 | d. | Error: ClassCastException | | | | |
|
|
4.
|
int n = 12;
System.out.print(
calculate(n));
System.out.print(" " + n);
public static double calculate(int
n)
{
n %=7;
return (double) (12/n);
}
What is the output of this program? a. | 2.0
5 | c. | 2.4
6 | b. | 2.4
12 | d. | 2.0
12 | | | | |
|
|
5.
|
What
is the name of the law that states:
!( a &&
b) is the same as !a | | !b a. | deMorgans
Law | d. | Newtons
Law | b. | Avogadros
Law | e. | Archimedes
Law | c. | Boyles
Law | | | | |
|
|
6.
|
This
program is supposed to randomly choose a letter of the alphabet. Any letter of the alphabet can
be chosen with equal probability. Consider the following code segment with a missing
statement:
String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// MISSING STATEMENT
System.out.println(abc.substring((k,
k+1));
Which of the following can replace //MISSING STATEMENT for the code to
work properly? a. | int k =
Math.random(0,25); | d. | int k =
Math.random(25); | b. | int k = (int) (26*Math.random(
)); | e. | int k =
Math.random(26) - 1; | c. | int k =Math.random(25) + 1; | | | | |
|
|
7.
|
public class Party {
private ArrayList <String> theGuests;
public Party(){theGuests=null;}
public Party(ArrayList<String>
guests){theGuests=guests;}
public void
setGuests(ArrayList <String> guests) { theGuests=guests;}
public String toString()
{
/* code not shown */
}
public class BDayParty extends Party
{
private String theName;
public BDayParty(String name, ArrayList<String> guests)
{
/* code not shown */
}
public String getName() {return theName;}
}
public static void main(String[] args)
{
ArrayList<String> guests = new ArrayList<String>();
guests.add("Alice");
guests.add("Ben");
guests.add("Candy");
//Add code here
}
}
Which of the following declarations is NOT valid, when added to the main
method? a. | Party [ ]
celebrations = new Party(guests), new Party( )}; | b. | BdayParty[ ]
celebrations = {new BDayParty(Lee:, guests), new BDayParty(Henry,
guests>}; | c. | BDayParty [ ] celebrations = new BDayParty(Malika,
guests), new Party(guests)}; | d. | All of the above are valid. | e. | Party[ ]
celebrations = new Party[2]; | | |
|
|
8.
|
Consider the following method:
public int
locate(String str, String oneLetter)
{
int j =0;
while(j < str.length() && str.substring(j,
j+1).compareTo(oneLetter) < 0)
{
j++;
}
return j;
}
Which of the following must be true when the while loop
terminates? a. | j == str.length(
) && str.substring(J, j+1).compareTo(oneLetter) >= 0 | d. | j == str.length() | b. | j <=
str.length( ) | | str.substring(J, j+1).compareTo(oneLetter) > 0 | e. | str.substring(j, j+1) >=0 | c. | j == str.length(
) | | str.substring(J, j+1).compareTo(oneLetter) >= 0 | | | | |
|
|
9.
|
What
is the output for the following program?
int
[ ] nums = new
int [5];
for (int x = 0; x < nums.length; x++)
{
nums[x] = (int) Math.pow(2, x) %
5;
System.out.print(nums[x]);
}
a. | 12431 | c. | 22222 | b. | 14410 | d. | 24312 | | | | |
|
|
10.
|
Which
of the following expressions will be true when x and y are boolean variables with different
values?
Option I. ( x || y) &&
(!x || !y)
Option II. (x || y) && !(x &&
y)
Option III. (x && !y) || (!x && y)
a. | I
only | d. | I and II
only | b. | I, II, and
III | e. | II
only | c. | II and III
only | | | | |
|
|
11.
|
Which
of the following code segments will move Actor andy south to the adjacent grid cell, assuming the new
Location is valid and empty?
Option
I. loc = andy.getLocation( );
loc.setRow(loc.get() + 1);
Option
II. loc = andy.getLocation();
Location newLoc = new Location(loc.getRow() + 1, loc.getCol());
andy.setLocation(newLoc);
Option
III. loc = andy.getLocation();
andy.moveTo(loc.getAdjacentLocation(Location.SOUTH));
a. | I and II
only | d. | III
only | b. | I
only | e. | II and III
only | c. | II
only | | | | |
|
|
12.
|
Suppose you wanted to create an Employee class.
One of the data fields would be the boss data field, which would hold
the name of the employees boss. If everyone had the same boss, which of the follow would
be the best way to declared the boss data field? a. | final String boss; | d. | private String boss; | b. | static String
boss; | e. | String
boss; | c. | public String boss; | | | | |
|
|
13.
|
int x = 2;
int y = 3;
int z = 0;
while(z <
100){
z +=
(int) Math.pow(x,
y);
}
System.out.println(z);
What is the
output?
|
|
14.
|
In
the GridWorld design, why is the Actor class not made abstract, with the act method
abstract? a. | To avoid
duplication of the setColor method in Actors subclasses | d. | To enable polymorphism for instances of
Actor | b. | To be able to override the act method in Actors
subclasses | e. | To avoid
dupliction of code in constructors of Actors subclasses | c. | To be able to
create Actor objects in GridWorld applications and explore their attributes and
behaviors. | | | | |
|
|
15.
|
Consider the following interface and class:
public interface Student
{
double getGPA();
int getSemesterUnits();
}
public class FullTimeStudent implements Student
{
//Required methods go here
}
What is the minimum set of methods that a develop must implement in
order to successfully compile the FullTimeStudent class? a. | No methods would
need to be implemented | b. | getGPA( ), getSemesterUnits( ) | c. | getGPA( ),
getSemesterUnits( ), equals (Object s) | d. | getGPA( ), getSemesterUnits( ), equals(Object s),
toString() | | |
|
|
16.
|
public class Counter {
private int count = 0;
public Counter(){count = 0;}
public Counter(int x){count = x;}//line 1
public int getCount() {return count;}//line 2
public void setCount(int c){int count = c;}//line 3
public void increment(){count++;}//line 4
public String toString(){return "" + count;}//line 5
public static void main (String []
args)
{
Counter c= new
Counter();
c.setCount(3);
c.increment();
System.out.println(c.getCount());
}
}
This class is supposed to output 4, but has an error. What is
actually printed, and which line in the above class should be changed to get the correct output of
4? a. | 1, line
3 | d. | 3, line
4 | b. | 0, line
4 | e. | 36, line
5 | c. | 0, line
1 | | | | |
|
|
17.
|
How
does a Bug (default) act if it is located in the northeast corner of a bounded grid and is
facing north when the step button is clicked? a. | The Bug turns northwest | d. | The Bug turns west | b. | The Bug turns
northeast | e. | The Bug turns
south | c. | The Bug is removed from the grid | | | | |
|