Examine the following UML diagram:
Person
-name: String
+setName(String name): void
+getName(): String
^
I
Student
-studentID: long
+Student(String sname, long sid)
+getID(): long
If you define a subclass of Person, like Student, and add a single constructor with this signature Student(String name, long id), that contains no code whatsoever, your program will only compile if the superclass (Person) contains:
1. an explicit constructor of whatever type
2. a setName() method to initialize the name field
3. an implicit constructor along with a working constructor
4. a default or no-arg constructor
Examine the following UML diagram:
Person
-name: String
+setName(String name): void
+getName(): String
^
I
Student
-studentID: long
+Student(String sname, long sid)
+getID(): long
If you define a subclass of Person, like Student, and add a single constructor with this signature Student(String name, long id), that contains no code whatsoever, your program will only compile if the superclass (Person) contains:
1. an explicit constructor of whatever type
2. a setName() method to initialize the name field
3. an implicit constructor along with a working constructor
4. a default or no-arg constructor
Front
a default or no-arg constructor
Back
A ____________ relationship exists between two classes when one class contains fields that are instances of the other class.
Front
has-A
Back
When you override a method defined in a superclass, you must do all of these except:
1. Use exactly the same number and type of parameters as the original method
2. Use an access specifier that is at least as permissive as the superclass method
3. Change either the number, type, or order of parameters in the subclass.
4. Return exactly the same type as the original method.
Front
3. Change either the number, type, or order of parameters in the subclass.
Back
Assume that two arrays are parallel. Assume the first array contains a person's id number and the second contains his or her age. In which index of the second array would you find the age of the person in the third index of the first array?
Front
3
Back
Constructors have the same name as the ____.
1. data members
2. package
3. class
4. member methods
Front
class
Back
What does the following loop print?
int[] a = {6, 1, 9, 5, 12, 3};
int len = a.length;
int x = a[0];
for (int i = 1; i < len; i++)
if (a[i] > x) x = a[i];
System.out.println(x);
Front
12
Back
int[] num = new int[100];
for (int i = 0; i < 50; i++)
num[i] = i;
num[5] = 10;
num[55] = 100;
What is the value at index 10 of the array above?
Front
10
Back
Clock
----------------------------------------
-hr: int
-min: int
-sec: int
---------------------------------------
+Clock()
+Clock(int, int, int)
+setTime(int, int, int) : void
+getHours() : int
+getMinutes() : int
+getSeconds() : int
+printTime() : void
+incrementSeconds() : int
+incrementMinutes() : int
+incrementHours() : int
+equals(Clock) : boolean
+makeCopy(Clock) : void
+getCopy() : Clock
----------------------------------------
According to the UML class diagram above, which of the following is a data member?
1. printTime()
2. min
3. Clock()
4. Clock
Front
2. min
Back
Given the following method heading
public void mystery(int list[])
and the declaration
int[] alpha = new int[50];
Which of the following is a valid call to the method mystery?
Front
mystery(alpha);
Back
What does a contain after the following loop?
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, pos = 0;
for (int i = pos; i < size - 1; i++)
a[i] = a[i + 1];
size--;
Front
{3, 7, 7, 0, 0, 0}
Back
Assume that you have an ArrayList variable named a containing 4 elements, and an object named element that is the correct type to be stored in the ArrayList. Which of these statements adds the object to the end of the collection
1. a[3] = element;
2. a.add(element, 4);
3. a[4] = element;
4. a.add(element);
Front
4. a.add(element);
Back
The ____________ relationship occurs when members of one class form a subset of another class, like the Animal, Mammal, Rodent example we discussed in the online lessons.
1. encapsulation
2. composition
3. is-A
4. has-A
Front
is-A
Back
Consider the following UML diagram.
Cadillac
+playMusic()
+accelerate()
+brake()
+turn()
^
GiPod
+playMusic()
This illustrates __________________.
Front
Implementation inheritance
Back
Mutator methods typically begin with the word "____".
Front
set
Back
A classification hierarchy represents an organization based on _____________ and _____________.
Front
generalization and specialization
Back
What does the following loop do?
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, pos = 0;
a[pos] = a[size - 1];
size--;
Front
Removes the 1 from the array, replacing it with the 7. Array now unordered.
Back
ArrayList objects can hold any object type. To store and retrieve int values in the collection, the ArrayList employs a technique called:
Front
boxing
Back
Person
-name: String
+setName(String name): void
+getName(): String
^
Student
-studentID: long
+Student(String sname, long sid)
+setID(): long
Which of these fields or methods are inherited (and accessible) by the Student class?
1. getName(), setName(), name
2. name, getName(), setName(), getID()
3. studentID, name, getName(), setName(), getID()
4. getName(), setName(), toString()
5. None of them
6. getName(), setName(), studentID, getID()
Front
4. getName(), setName(), toString()
Back
Person
-name: String
+setName(String name): void
+getName(): String
^
Student
-studentID: long
+Student(String sname, long sid)
+setID(): long
To access the name instance variable from inside a method defined in the Student class, name must use the _____________ modifier. (We'll ignore the default package-private access if no modifier is used.)
1. Any of them
2. public
3. protected or public
4. protected
5. private
6. private or protected
Front
3. protected or public
Back
Given the method heading
public void strange(int a, int b)
and the declaration
int[] alpha = new int[10];
int[] beta = new int[10];
Which of the following is a valid call to the method strange?
Front
strange(alpha[0], alpha[1]);
Back
What does a contain after the following loop?
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, pos = 0;
for (int i = pos; i < size - 1; i++)
a[i + 1] = a[i];
size--;
Front
{1, 1, 1, 0, 0, 0}
Back
int[] num = new int[100];
for (int i = 0; i < 50; i++)
num[i] = i;
num[5] = 10;
num[55] = 100;
What is the data type of the array above?
1. num
2. int
3. char
4. list
Front
int
Back
When you overload a method defined in a superclass, you must do which one of these?
1. Change either the number, type, or order of parameters in the subclass.
2. Use an access specifier that is at least as permissive as the superclass method
3. Return exactly the same type as the original method.
4. Use exactly the same number and type of parameters as the original method.
Front
Change either the number, type, or order of parameters in the subclass.
Back
To use an ArrayList in your program, you must import:
1. the java.collections package
2. the java.awt package
3. Nothing; the class is automatically available, just like regular arrays.
4. the java.util package
5. the java.lang package
Front
the java.util package
Back
If you do not specify a superclass when using inheritance, the class you are defining implicitly extends the __________________ class.
Front
Object
Back
Examine the following UML diagram:
Person
-name: String
+setName(String name): void
+getName(): String
^
Student
-studentID:long
+Student(String sname, long sid)
+getId():long
To access the name instance variable from inside a method defined in the Person class, name must use the _____________ modifier. (We'll ignore the default package-private access if no modifier is used.)
1. protected or public
2. private
3. protected or public
4. private or protected
5. public
6. Any of them
Front
Any of them
Back
What does the following loop print?
int[] a = {6, 1, 9, 5, 12, 7};
int len = a.length;
int x = 0;
for (int i = 0; i < len; i++)
if (a[i] % 2 == 0) x += a[i];
System.out.println(x);
Front
18
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 3};
int x = 0;
for (int e : a) x += e;
Front
Sums all the elements in a.
Back
Clock
-hr: int
-min: int
-sec: int
+Clock()
+Clock(int, int, int)
+setTime(int, int, int): void
+getHours(): int
+getMinutes(): int
+getSeconds(): int
+printTime(): void
+incrementSeconds(): int
+incrementMinutes(): int
+incrementHours(): boolean
+equals(Clock): boolean
+makeCopy(Clock): void
+getCopy(): Clock
According to the UML class diagram above, which method is public and doesn't return anything?
1. getCopy()
2. setTime(int, int, int)
3. incrementHours()
4. equals(Clock)
Front
setTime(int, int, int)
Back
A method that has the signature void m(int[] ar) :
A. can change the values in the array ar
B. cannot change the values in the array ar
C. can only change copies of each of the elements in the array ar
D. None of these
Front
can change the values in the array ar
Back
When you define a subclass, like Student, and don't supply a constructor, then its superclass must:
1. have no constructors defined
2. have no constructors defined or have an explicit default (no-arg) constructor defined.
3. have an explicit copy constructor defined
4. have an explicit default (no-arg) constructor defined
5. have an explicit working constructor defined
Front
have no constructors defined or have an explicit default (no-arg) constructor defined.
Back
Consider the following class definition.
public class Rectangle
{
private double length;
private double width;
private double area;
private double perimeter;
public Rectangle()
{
length = 0;
width = 0;
}
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public void set(double l, double w)
{
length = l;
width = w;
}
public void print()
{
System.out.println(length + " " + width);
}
public double area()
{
return length * width;
}
public double perimeter()
{
return 2 length + 2 width;
}
}
Suppose that you have the following declaration.
Rectangle bigRect = new Rectangle(10, 4);
Which of the following sets of statements are valid (both syntactically and semantically) in Java?
(i)
bigRect.area();
bigRect.perimeter();
bigRect.print();
(ii)
bigRect.area = bigRect.area();
bigRect.perimeter = bigRect.perimeter();
bigRect.print();
1. Both (i) and (ii)
2. Only (ii)
3. Only (i)
4. None of these
Front
Only (i)
Back
Inheritance gives your programs the ability to express _______________ between classes.
1. encapsulation
2. relationships
3. composition
4. dependencies
Front
relationships
Back
You are ____ required to write a constructor method for a class.
Front
never
Back
Examine the following UML diagram:
Person
-name: String
+setName(String name): void
+getName(): String
^
I
Student
-studentID: long
+Student(String sname, long sid)
+getID(): long
1. toString()
2. None of them
3. getName(), setName(), name
4. name, getName(), setName(), getID()
5. getName(), setName(), studentID, getID()
6. studentID, name, getName(), setName(), getID()
Front
toString()
Back
When you write an explicit ____ for a class, you no longer receive the automatically written version.
Front
constructor
Back
To print the last element in the array named ar, you can write :
A. System.out.println(ar.length);
B. System.out.println(ar[length]);
C. System.out.println(ar[ar.length]);
D. None of these
Front
None of these
Back
You cannot declare the same ____ name more than once within a block, even if a block contains other blocks.
Front
variable
Back
double[][] vals = {{1.1, 1.3, 1.5},
{3.1, 3.3, 3.5},
{5.1, 5.3, 5.5},
{7.1, 7.3, 7.5}}
What is the value of vals.length in the array above?
1. 16
2. 4
3. 0
4. 3
Front
4
Back
Examine the following UML diagram:
Person
-name: String
+setName(String name): void
+getName(): String
^
I
Student
-studentID: long
+Student(String sname, long sid)
+getID(): long
Assume that pete is a Person object variable that refers to a Person object. Which method is used to print this output: pete is Person@addbf1
1. getClass()
2. getAddress()
3. getName(), getID()
4. getID()
5. getName()
6. None of them
7. toString()
Front
toString()
Back
How many constructors can a class have?
1. Any number
2. 0
3. 1
4. 2
Front
Any number
Back
Clock
-hr: int
-min: int
-sec: int
+Clock()
+Clock(int, int, int)
+setTime(int, int, int): void
+getHours(): int
+getMinutes(): int
+getSeconds(): int
+printTime(): void
+incrementSeconds(): int
+incrementMinutes(): int
+incrementHours(): boolean
+equals(Clock): boolean
+makeCopy(Clock): void
+getCopy(): Clock
Which of the following would be a default constructor for the class Clock shown in the figure above?
1. public Clock(0, 0, 0) { setTime(); }
2. private Clock(10){ setTime(); }
3. public Clock(0) { setTime(0); }
4. public Clock(){ setTime(0, 0, 0); }
Front
public Clock(){ setTime(0, 0, 0); }
Back
To print the last element in the array named ar, you can write :
A. System.out.println(ar[length-1]);
B. System.out.println(ar[length()-1]);
C. System.out.println(ar[ar.length-1]);
D. System.out.println(ar[ar.length]);
Front
System.out.println(ar[ar.length-1]);
Back
char[][] array1 = new char[15][10];
How many rows are in the array above?
Front
15
Back
Consider the following two method calls. Which of the method headers would work to handle both calls without a syntax error?
String ans = alphabetize("Bob", "Frank", "Alice");
String ans = alphabetize("Bob", "Frank", "Alice", "Sam", "Judy");
1. String alphabetize(String...names){ }
2. String alphabetize(String[] names){ }
3. String alphabetize(String names){ }
4. String alphabetize(String a, String b, String c, String d, String e) { }
Front
1. String alphabetize(String...names){ }
Back
Consider the following class definition.
public class Rectangle
{
private double length;
private double width;
public Rectangle()
{
length = 0;
width = 0;
}
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public void set(double l, double w)
{
length = l;
width = w;
}
public void print()
{
System.out.println(length + " " + width);
}
public double area()
{
return length * width;
}
public double perimeter()
{
return 2 length + 2 width;
}
}
Which of the following statements correctly instantiate the Rectangle object myRectangle?
(i) myRectangle Rectangle = new Rectangle(10, 12);
(ii) class Rectangle myRectangle = new Rectangle(10, 12);
(iii) Rectangle myRectangle = new Rectangle(10, 12);
1. Only (ii)
2. Only (iii)
3. Only (i)
4. Both (ii) and (iii)
Front
Only (iii)
Back
Which of these loops will initialize each element in the array ar to hold the value 4 ?
A. for (int i = 1; i < ar.length; i++ ) ar[i] = 4;
B. for (int i = 0; i <= ar.length; i++ ) ar[i] = 4;
C. for (int i = 0; i < ar.length - 1; i++ ) ar[i] = 4; 0%
D. for (int i = 0; i < ar.length; i++) ar[i] = 4;
Front
for (int i = 0; i < ar.length; i++) ar[i] = 4;
Back
Consider the following UML diagram.
Cadillac
+playMusic()
+accelerate()
+brake()
+turn()
^
I
GiPod
+playMusic()
This illustrates __________________________:
Front
implementation inheritance
Back
Accessor instance method names typically begin with the word "____" or the word "____" depending on the return type of the method.
1. set, is
2. get, can
3. set, get 0%
4. get, is
Front
get, is
Back
Consider the following class definition.
public class Rectangle
{
private double length;
private double width;
public Rectangle()
{
length = 0;
width = 0;
}
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public void set(double l, double w)
{
length = l;
width = w;
}
public void print()
{
System.out.println(length + " " + width);
}
public double area()
{
return length * width;
}
public void perimeter()
{
return 2 length + 2 width;
}
}
Suppose that you have the following declaration.
Rectangle bigRect = new Rectangle();
Which of the following sets of statements are valid in Java?
(i) bigRect.set(10, 5);
(ii) bigRect.length = 10;
bigRect.width = 5;
Front
Only (i)
Back
Section 2
(50 cards)
Consider the partially-filled array named a. What does the following loop do? (cin is a Scanner object)
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6;
int value = cin.nextInt();
while (size < capacity && value > 0)
{
a[size] = value;
}
1. Reads up to 3 values and places them in the array in the unused space.
2. Reads one value and places it in the remaining first unused space endlessly.
3. Reads up to 3 values and inserts them in the array in the correct position.
4. Crashes at runtime because it tries to write beyond the array.
Front
Reads one value and places it in the remaining first unused space endlessly.
Back
The reference to an object that is passed to any object's nonstatic class method is called the ____.
1. this reference
2. magic number
3. literal constant
4. reference
Front
1. This
Back
char[][] array1 = new char[15][10];
What is the value of array1.length?
1. 0
2. 10
3. 2
4. 15
Front
4. 15
Back
What does the following loop print?
int[] a = {6, 1, 9, 5, 12, 7};
int len = a.length;
int x = 0;
for (int i = 0; i < len; i++)
if (a[i] % 2 == 0) x++;
System.out.println(x);
1. 4
2. 22
3. 18
4. 2
Front
2
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 3};
int x = 0;
for (int e : a) x++;
1. Sums all the elements in a.
2. Will not compile (syntax error)
3. Counts each element in a.
4. Finds the largest value in e.
Front
3. Counts each element in a.
Back
double[][] vals = {{1.1, 1.3, 1.5},
{3.1, 3.3, 3.5},
{5.1, 5.3, 5.5},
{7.1, 7.3, 7.5}}
What is in vals[2][1]?
1. 1.3
2. 3.3
3. 3.5
4. 5.3
Front
5.3
Back
int[] num = new int[100];
for (int i = 0; i < 50; i++)
num[i] = i;
num[5] = 10;
num[55] = 100;
What is the value of num.length in the array above?
1. 0
2. 100
3. 99
4. 101
Front
100
Back
What is the value of alpha[4] after the following code executes?
int[] alpha = new int[5];
int j;
alpha[0] = 2;
for (j = 1; j < 5; j++)
alpha[j] = alpha[j - 1] + 3;
1. 11
2. 14
3. 5
4. 8
Front
2. 14
Back
What is stored in alpha after the following code executes?
int[] alpha = new int[5];
int j;
for (j = 0; j < 5; j++)
{
alpha[j] = 2 * j;
if (j % 2 == 1)
alpha[j - 1] = alpha[j] + j;
}
1. alpha = {0, 2, 4, 6, 8}
2. alpha = {0, 2, 9, 6, 8}
3. alpha = {3, 2, 9, 6, 8}
4. alpha = {0, 3, 4, 7, 8}
Front
alpha = {3, 2, 9, 6, 8}
Back
What is the value of alpha[3] after the following code executes?
int[] alpha = new int[5];
int j;
alpha[0] = 5;
for (j = 1; j < 5; j++)
{
if (j % 2 == 0)
alpha[j] = alpha[j - 1] + 2;
else
alpha[j] = alpha[j - 1] + 3;
}
1. None of these
2. 13
3. 15
4. 10
Front
13
Back
An object is a(n) ____ of a class.
1. instance
2. method
3. field
4. constant
Front
1. Instance
Back
double[][] vals = {{1.1, 1.3, 1.5},
{3.1, 3.3, 3.5},
{5.1, 5.3, 5.5},
{7.1, 7.3, 7.5}}
What is the value of vals[4][1] in the array above?
1. 7.1
2. There is no such value.
3. 7.3
4. 1.1
Front
There is no such value.
Back
Consider the partially-filled array named a. What does the following loop do? (cin is a Scanner object)
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6;
int value = cin.nextInt();
while (value > 0)
{
a[size] = value;
size++;
value = cin.nextInt();
}
1. May crashe at runtime because it can input more elements than the array can hold
2. Reads up to 3 values and inserts them in the array in the correct position.
3. Reads one value and places it in the remaining first unused space endlessly.
4. Reads up to 3 values and places them in the array in the unused space.
Front
1. May crashe at runtime because it can input more elements than the array can hold
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 3};
int len = a.length;
int x = 0;
for (int i = 1; i < len; i++)
if (a[i] > a[x]) x = i;
System.out.println(x);
1. Finds the position of the smallest value in a.
2. Sums the elements in a.
3. Counts the elements in a.
4. Finds the position of the largest value in a.
Front
4. Finds the position of the largest value in a.
Back
Consider the partially-filled array named a. What does the following loop do? (cin is a Scanner object)
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6;
int value = cin.nextInt();
while (size < capacity && value > 0)
{
a[size] = value;
size++;
value = cin.nextInt();
}
1. Reads one value and places it in the remaining first unused space endlessly.
2. Crashes at runtime because it tries to write beyond the array.
3. Reads up to 3 values and places them in the array in the unused space.
4. Reads up to 3 values and inserts them in the array in the correct position.
Front
Reads up to 3 values and places them in the array in the unused space.
Back
What does a contain after the following loop runs?
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, value = 5;
int pos = 0;
for (pos = 0; pos < size; pos++)
if (a[pos] < value) break;
for (int j = size; j > pos; j--)
a[j] = a[j - 1];
a[pos] = value;
size++;
1. { 1, 3, 5, 0, 0, 0 }
2. { 5, 1, 3, 7, 0, 0 }
3. { 1, 3, 5, 7, 0, 0 }
4. { 1, 3, 7, 5, 0, 0 }
Front
2. { 5, 1, 3, 7, 0, 0 }
Back
To prevent subclasses from overriding an inherited method, the superclass can mark the method as:
Front
final
Back
Assume that you have an ArrayList variable named a containing 4 elements, and an object named element that is the correct type to be stored in the ArrayList. Which of these statements replaces the last object in the collection with element?
Front
a.set(3, element);
Back
Which of the following statements creates alpha, a two-dimensional array of 10 rows and 5 columns, wherein each component is of the type int?
1. Both (i) and (ii)
2. Only (ii)
3. None of these
4. Only (i)
Front
Only (ii)
Back
Which of the following is used to allocate memory for the instance variables of an object of a class?
1. the reserved word public
2. the operator new
3. the operator +
4. the reserved word static
Front
2. The operator new
Back
When you override a method defined in a superclass, you must do all of these except:
Front
Change either the number, type, or order of parameters in the subclass.
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 3};
int len = a.length;
int x = 0;
for (int i = 0; i < len; i++)
if (a[i] % 2 == 0) x++;
System.out.println(x);
1. Sums the even elements in a.
2. Finds the largest value in a.
3. Counts the even elements in a.
4. Finds the smallest value in a.
Front
Counts the even elements in a.
Back
What does the following loop print?
int[] a = {6, 1, 9, 5, 12, 7};
int len = a.length;
int x = 0;
for (int i = 0; i < len; i++)
if (a[i] % 2 == 1) x++;
System.out.println(x);
1. 18
2. 2
3. 22
4. 4
Front
4
Back
Assume that you have an ArrayList variable named a containing 4 elements, and an object named element that is the correct type to be stored in the ArrayList. Which of these statements assigns the first object in the collection to the variable element?
Front
element = a.get(0);
Back
Methods that require you to use an object to call them are called ____ methods.
1. accessor
2. instance
3. internal
4. static
Front
2. Instance
Back
Assume that you have an ArrayList variable named a containing many elements. You can rearrange the elements in random order by writing:
Front
Collections.shuffle(a);
Back
If a class's only constructor requires an argument, you must provide an argument for every ____ of the class that you create.
1. object
2. type
3. parameter
4. method
Front
1. Object
Back
Consider the following statements.
public class Rectangle
{
private double length;
private double width;
public Rectangle()
{
length = 0;
width = 0;
}
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public void set(double l, double w)
{
length = l;
width = w;
}
public void print()
{
System.out.println("Length = " + length
+ "; Width = " + width + " " +
+ " Area = " + area()
+ "; Perimeter = " + perimeter());
}
public double area()
{
return length * width;
}
public void perimeter()
{
return 2 length + 2 width;
}
}
What is the output of the following statements?
Assume that two arrays are parallel. Assume the first array contains a person's id number and the second contains his or her age. In which index of the second array would you find the age of the person in the third index of the first array?
1. 2
2. 3
3. 1
4. It cannot be determined from the information given.
Front
2. 3
Back
What does a contain after the following loop runs?
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, value = 5;
int pos = 0;
for (pos = 0; pos < size; pos++)
if (a[pos] > value) break;
for (int j = size; j > pos; j--)
a[j] = a[j - 1];
a[pos] = value;
size++;
1. { 1, 3, 5, 7, 0, 0 }
2. { 1, 3, 5, 0, 0, 0 }
3. { 1, 3, 7, 5, 0, 0 }
4. { 5, 1, 3, 7, 0, 0 }
Front
{ 1, 3, 5, 7, 0, 0 }
Back
Which of the following class definitions is correct in Java?
(i)
public class Student
{
private String name;
private double gpa;
private int id;
public void Student()
{
name = "";
gpa = 0;
id = 0;
}
public void Student(String s, double g, int i)
{
set(s, g, i);
}
public void set(String s, double g, int i)
{
name = s;
gpa = g;
id = i;
}
public void print()
{
System.out.println(name + " " + id + " " + gpa);
}
}
(ii)
public class Student
{
private String name;
private double gpa;
private int id;
public Student()
{
name = "";
gpa = 0;
id = 0;
}
public Student(String s, double g, int i)
{
set(s, g, i);
}
public void set(String s, double g, int i)
{
name = s;
gpa = g;
id = i;
}
public void print()
{
System.out.println(name + " " + id + " " + gpa);
}
}
1. Both (i) and (ii)
2. None of these
3. Only (ii)
4. Only (i)
Front
3. Only ii
Back
public class Illustrate
{
private int x;
private int y;
public Illustrate()
{
x = 1;
y = 2;
}
public Illustrate(int a)
{
x = a;
}
public void print()
{
System.out.println("x = " + x + ", y = " + y);
}
public void incrementY()
{
y++;
}
}
What does the default constructor do in the class definition above?
1. Sets the value of x to 0
2. There is no default constructor.
3. Sets the value of x to 1
4. Sets the value of x to a
Front
3. Sets the value of x to 1.
Back
Suppose that sales is a two-dimensional array of 10 rows and 7 columns wherein each component is of the type int , and sum and j are int variables. Which of the following correctly finds the sum of the elements of the fifth row of sales?
1.
sum = 0;
for(j = 0; j < 10; j++)
sum = sum + sales[5][j];
2.
sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sales[4][j];
3.
sum = 0;
for(j = 0; j < 10; j++)
sum = sum + sales[4][j];
4. sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sales[5][j];
Front
2.
sum = 0;
for(j = 0; j < 7; j++)
sum = sum + sales[4][j];
Back
What is the value of alpha[3] after the following code executes?
int[] alpha = new int[5];
int j;
for (j = 4; j >= 0; j--)
{
alpha[j] = j + 5;
if (j <= 2)
alpha[j + 1] = alpha[j] + 3;
}
1. 8
2. 10
3. 9
4. 5
Front
2. 10
Back
What is stored in alpha after the following code executes?
int[] alpha = new int[5];
int j;
for (j = 0; j < 5; j++)
{
alpha[j] = j + 1;
if (j > 2)
alpha[j - 1] = alpha[j] + 2;
}
1. alpha = {1, 2, 3, 4, 5}
2. alpha = {4, 5, 6, 7, 9}
3. alpha = {1, 5, 6, 7, 5}
4. None of these
Front
None of these
Back
Which of the following lines of code implicitly calls the toString() method, assuming that pete is an initialized Student object variable?
Front
String s = "President: " + pete;
Back
The arguments in a method call are often referred to as ____.
1. concept parameters
2. constants
3. actual parameters
4. argument lists
Front
3. Actual parameters
Back
char[][] array1 = new char[15][10];
What is the value of array1.length?
1. 15
2. 0
3. 2
4. 10
Front
15
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 3};
int len = a.length;
int x = 0;
for (int i = 0; i < len; i++)
if (a[i] % 2 == 0) x++;
System.out.println(x);
1. Sums the even elements in a.
2. Counts the even elements in a.
3. Finds the smallest value in a.
4. Finds the largest value in a.
Front
Counts the even elements in a.
Back
Suppose we want to store a tic-tac-toe board in a program; it's a
three-by-three array. It will store "O", "X" or a space at each location
(that is, every location is used), and you use a two-dimensional array
called board to store the individual tiles.
Suppose you pass board to a method that accepts a
two-dimensional array as a parameter. If the method was designed
to process two-dimensional arrays of various sizes. What information
about the size of the array would have to be passed to the method
so it could properly process the array?
A. the number of columns in the array
B. the number of rows in the array
C. the number of cells in the array
D. Both A and B
E. No additional information is needed, as the array carries its size information with it.
Front
E. No additional information is needed, as the array carries its size information with it.
Back
What does the following loop print?
int[] a = {6, 1, 9, 5, 12, 3};
int len = a.length;
int x = a[0];
for (int i = 1; i < len; i++)
if (a[i] < x) x = a[i];
System.out.println(x);
1. 1
2. 12
3. 6
4. 36
Front
1. 1
Back
What does the following loop print?
int[] a = {6, 1, 9, 5, 12, 3};
int len = a.length;
int x = a[0];
for (int i = 1; i < len; i++)
if (a[i] < x) x = a[i];
System.out.println(x);
1. 36
2. 12
3. 1
4. 6
Front
1
Back
double[] as = new double[7];
double[] bs;
bs = as;
How many objects are present after the code fragment above is executed?
1. 2
2. 14
3. 1
4. 7
Front
1
Back
To determine the number of valid elements in an ArrayList, use:
Front
the size() method
Back
Which of the following creates an array of 25 components of the type int?
(i) int[] alpha = new[25];
(ii) int[] alpha = new int[25];
1. Only (ii)
2. None of these
3. Only (i)
4. Both (i) and (ii)
Front
Only (ii)
Back
What does the following loop print?
int[] a = {6, 1, 9, 5, 12, 3};
int len = a.length;
int x = a[0];
for (int i = 1; i < len; i++)
if (a[i] > x) x = a[i];
System.out.println(x);
1. 36
2. 6
3. 12
4. 1
Front
3. 12
Back
char[][] array1 = new char[15][10];
What is the value of array1[3].length?
1. 15
2. 10
3. 2
4. 0
Front
2. 10
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 3};
int len = a.length;
int x = 0;
for (int i = 1; i < len; i++)
if (a[i] > a[x]) x = i;
System.out.println(x);
1. Finds the position of the largest value in a.
2. Sums the elements in a.
3. Finds the position of the smallest value in a.
4. Counts the elements in a.
Front
Finds the position of the largest value in a.
Back
Suppose you have the following declaration.
char[] nameList = new char[100];
Which of the following range is valid for the index of the array nameList.
(i) 1 through 100
(ii) 0 through 100
1. Both are invalid
2. Only (i)
3. None of these
4. Only (ii)
Front
1. Both are invalid
Back
What does a contain after the following loop?
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, pos = 0;
for (int i = size; i > pos; i--)
a[i - 1] = a[i];
size--;
1. {3, 7, 0, 0, 0, 0}
2. {0, 0, 0, 0, 0, 0}
3. {3, 7, 7, 0, 0, 0}
4. {1, 1, 1, 0, 0, 0}
Front
{0, 0, 0, 0, 0, 0}
Back
Section 3
(50 cards)
Methods are commonly used to
Front
Break a problem down into small manageable pieces
Back
Given the method heading
public void strange(int a, int b)
and the declaration
int[] alpha = new int[10];
int[] beta = new int[10];
Front
strange(alpha[0], alpha[1]);
Back
Which of the following about Java arrays is true?
(i) Array components must be of the type double.
(ii) The array index must evaluate to an integer.
Front
Only (ii)
Back
public static int minimum(int x, int y)
{
int smaller;
if (x < y)
smaller = x;
else
smaller = y;
return smaller;
}
Which of the following is a valid call to the method above?
1. minimum(int x, int y);
2. minimum(int 5, int 4);
3. minimum(5, 4);
4. public static int minimum(5, 4);
Front
minimum(5, 4);
Back
Which of the following statements creates alpha, an array of 5 components of the type int, and initializes each component to 10?
(i) int[] alpha = {10, 10, 10, 10, 10};
(ii) int[5] alpha = {10, 10, 10, 10, 10}
Front
Only (i)
Back
In a @return tag statement the description
A. Describes the return value
B. Must be longer than one line
C. Cannot be longer than one line
D. Describes the parameter values
Front
Describes the return value
Back
To print the last element in the array named ar, you can write :
A. System.out.println(ar.length);
B. System.out.println(ar[length]);
C. System.out.println(ar[ar.length]); D. None of these
Front
D
Back
You can create an array of three integers by writing :
Front
int ar[] = new int[3];
Back
What is stored in alpha after the following code executes?
int[] alpha = new int[5];
int j;
for (j = 0; j < 5; j++)
{
alpha[j] = j + 1;
if (j > 2)
alpha[j - 1] = alpha[j] + 2;
}
1. None of these
2. alpha = {1, 2, 3, 4, 5}
3. alpha = {1, 5, 6, 7, 5}
4. alpha = {4, 5, 6, 7, 9}
Front
1. None of these
Back
public static int minimum(int x, int y)
{
int smaller;
if (x < y)
smaller = x;
else
smaller = y;
return smaller;
}
Based on the above code, what would be the output of the statement
int s = minimum(5, minimum(3, 7));
1. 5
2. There would be no output; this is not a valid statement.
3. 7
4. 3
Front
3
Back
public class ScopeRules //Line 1
{
static final double rate = 10.50;
static int z;
static double t;
public static void main(String[]args) //Line 7
{
int num;
double x, z;
char ch;
// main block...
}
public static void one(int f, char g) //Line 15
{
// block one...
}
public static int w; //Line 20
public static void two(int one, int i) //Line 22
{
char ch;
int a; //Line 25
//block three
{
int x = 12; //Line 29
//...
}//end block three
// block two... //Line 32
}
}
Which of the following identifiers is visible on the line marked // main block?
1. All identifiers are visible in main.
2. local variables of method two
3. w (before method two)
4. z (before main)
Front
w (before method two)
Back
Local variables
A. Lose the values stored in them between calls to the method in which the variable is declared
B. May have the same name as local variables in other methods
C. Are hidden from other methods
D. All of the above
Front
All of the above
Back
Which of the following statements is NOT true?
1. Reference variables contain the address of the memory space where the data is stored.
2. The operator new must always be used to allocate space of a specific type, regardless of the type.
3. Reference variables do not directly contain the data.
4. A String variable is actually a reference variable of the type String.
Front
2. The operator new must always be used to allocate space of a specific type, regardless of the type.
Back
This type of method method performs a task and sends a value back to the code that called it.
A. Local
B. Void
C. Complex
D. Value-returning
Front
Value-returning
Back
Consider the following declaration.
double[] sales = new double[50];
int j;
Which of the following correctly initializes all the components of the array sales to 10.
(i)
for (j = 0; j < 49; j++)
sales[j] = 10;
(ii)
for (j = 1; j <= 50; j++)
sales[j] = 10;
Front
None of these
Back
Suppose you have the following declaration.
int[] beta = new int[50];
Which of the following is a valid element of beta.
(i) beta[0]
(ii) beta[50]
Front
Only (i)
Back
Consider the partially-filled array named a. What does the following loop do? (cin is a Scanner object)
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6;
int value = cin.nextInt();
while (size < capacity && value > 0)
{
a[size] = value;
size++;
1. Reads one value and places it in the remaining three unused spaces in a.
2. Reads up to 3 values and places them in the array in the unused space.
3. Reads up to 3 values and inserts them in the array in the correct position.
4. Crashes at runtime because it tries to write beyond the array.
Front
1. Reads one value and places it in the remaining three unused spaces in a.
Back
What is the output of the following Java code?
int[] list = {0, 5, 10, 15, 20};
int j;
for (j = 1; j <= 5; j++)
System.out.print(list[j] + " ");
System.out.println();
1. 5 10 15 20 0
2. 5 10 15 20 20
3. 0 5 10 15 20
4. Code contains index out-of-bound
Front
Code contains index out-of-bound
Back
The first element in the array created by the statement int[] ar = { 1, 2, 3 }; is :
Front
ar[0]
Back
methods are commonly used to
Front
Break a problem down into small manageable pieces.
Back
If you pass the first element of the array ar to the method x() like this, x(ar[0]); the element ar[0] :
Front
cannot be changed by the method x()
Back
The header of a value-returning method must specify this.
A. The method's local variable name
B. The data type of the return value
C. The name of the variable in the calling program that will receive the returned value
D. All of the above
Front
The data type of the return value
Back
public static int minimum(int x, int y)
{
int smaller;
if (x < y)
smaller = x;
else
smaller = y;
return smaller;
}
What is the return type of the method above?
Front
int
Back
public static String exampleMethod(int n, char ch)
Based on the method heading above, what is the return type of the value returned?
1. char
2. String
3. int
4. Nothing will be returned
Front
String
Back
When an object, such as a String, is passed as an argument, it is
A. Passed by value like any other parameter value
B. Actually a reference to the object that is passed
C. Encrypted
D. Necessary to know exactly how long the string is when writing the program
Front
Actually a reference to the object that is passed
Back
Consider the following declaration.
int[] alpha = new int[3];
Which of the following input statements correctly input values into alpha?
(i)
alpha = cin.nextInt();
alpha = cin.nextInt();
alpha = cin.nextInt();
(ii)
alpha[0] = cin.nextInt();
alpha[1] = cin.nextInt();
alpha[2] = cin.nextInt();
1. Only (i)
2. Only (ii)
3. None of these
4. Both (i) and (ii)
Front
Only (ii)
Back
What is the output of the following Java code?
int[] alpha = {2, 4, 6, 8, 10};
int j;
for (j = 4; j >= 0; j--)
System.out.print(alpha[j] + " ");
System.out.println();
Front
10 8 6 4 2
Back
Which of the following is not part of a method call?
A. Return type
B. Parentheses
C. Method name
D. All of the above are part of a method call
Front
Return type
Back
Which of the following about Java arrays is true?
(i) All components must be of the same type.
(ii) The array index and array element must be of the same type.
Front
Only (i)
Back
public static int minimum(int x, int y)
{
int smaller;
if (x < y)
smaller = x;
else
smaller = y;
return smaller;
}
Which of the following is a valid call to the method above?
Front
minimum(5, 4);
Back
When an object, such as a String, is passed as an argument, it is
Front
Actually a reference to the object that is passed
Back
Functional decomposition is
Front
The process of breaking a problem down into smaller pieces.
Back
int[] hit = new hit[5];
hit[0] = 3;
hit[1] = 5;
hit[2] = 2;
hit[3] = 6;
hit[4] = 1;
System.out.println(hit[1 + 3]);
What is the output of the code fragment above?
1. 1
2. 5
3. 6
4. 3
Front
1
Back
What is stored in alpha after the following code executes?
int[] alpha = new int[5];
int j;
for (j = 0; j < 5; j++)
{
alpha[j] = j + 5;
if (j % 2 == 1)
alpha[j - 1] = alpha[j] + 2;
}
Front
alpha = {8, 6, 10, 8, 9}
Back
public class ScopeRules //Line 1
{
static final double rate = 10.50;
static int z;
static double t;
public static void main(String[]args) //Line 7
{
int num;
double x, z;
char ch;
// main block...
}
public static void one(int f, char g) //Line 15
{
// block one...
}
public static int w; //Line 20
public static void two(int one, int i) //Line 22
{
char ch;
int a; //Line 25
//block three
{
int x = 12; //Line 29
//...
}//end block three
// block two... //Line 32
}
}
Where is identifier x (block three's local variable) visible?
1. In two and block three
2. In block three and main
3. In block three only
4. In one and block three
Front
In block three only
Back
The last element in the array created by the statement int[] ar = { 1, 2, 3 }; is :
Front
ar[2]
Back
Here is a loop that should average the contents of the array named ar :
double sum = 0.0;
int n = 0;
for (int i=0; i < ar.length; i++, n++)
sum = sum + ar[i];
if ( ?????? )
System.out.println("Average = " + ( sum / n );
What goes where the ???? appears :
A. sum > 0
B. n == 0
C. n > 0
D. None of these
Front
C. n > 0
Back
If you pass the array ar to the method m() like this, m(ar); the element ar[0] :
A. will be changed by the method m()
B. cannot be changed by the method m()
C. may be changed by the method m(), but not necessarily
D. None of these
Front
C. may be changed by the method m(), but not necessarily
Back
To print the number of elements in the array named ar, you can write :
A.System.out.println(length);
B.System.out.println(ar.length()); C.System.out.println(ar.length);
D.System.out.println(ar.length-1);
Front
C. System.out.println(ar.length);
Back
Consider the following declaration.
Scanner cin = new Scanner(System.in);
int[] beta = new int[3];
int j;
Which of the following input statements correctly input values into beta?
(i)
beta[0] = cin.nextInt();
beta[1] = cin.nextInt();
beta[2] = cin.nextInt();
(ii)
for (j = 0; j < 3; j++)
beta[j] = cin.nextInt();
Front
Both (i) and (ii)
Back
What is the value of alpha[3] after the following code executes?
int[] alpha = new int[5];
int j;
for (j = 4; j >= 0; j--)
{
alpha[j] = j + 5;
if (j <= 2)
alpha[j + 1] = alpha[j] + 3;
}
1. 9
2. 5
3. 8
4. 10
Front
4. 10
Back
public class ScopeRules //Line 1
{
static final double rate = 10.50;
static int z;
static double t;
public static void main(String[]args) //Line 7
{
int num;
double x, z;
char ch;
// main block...
}
public static void one(int f, char g) //Line 15
{
// block one...
}
public static int w; //Line 20
public static void two(int one, int i) //Line 22
{
char ch;
int a; //Line 25
//block three
{
int x = 12; //Line 29
//...
}//end block three
// block two... //Line 32
}
}
Which of the following identifiers is visible in method one?
1. x (block three's local variable)
2. local variables of method two
3. rate (before main)
4. one (method two's formal parameter)
Front
rate (before main)
Back
public static int minimum(int x, int y)
{
int smaller;
if (x < y)
smaller = x;
else
smaller = y;
return smaller;
}
Which of the following is a valid call to the method above?
Front
minimum(5, 4);
Back
Consider the following method which takes any number of parameters.
Which statement would return the first of the numbers passed when calling the method?
int first = firstNum(7, 9, 15);
public int firstNum(int..nums)
{
// what goes here?
}
1. return nums[0]
2. return nums;
3. return nums...0
4. return nums.first;
Front
3. return nums..0
Back
What is stored in alpha after the following code executes?
int[] alpha = new int[5];
int j;
for (j = 0; j < 5; j++)
{
alpha[j] = 2 * j;
if (j % 2 == 1)
alpha[j - 1] = alpha[j] + j;
}
1. alpha = {0, 2, 4, 6, 8}
2. alpha = {0, 3, 4, 7, 8}
3. alpha = {0, 2, 9, 6, 8}
4. alpha = {3, 2, 9, 6, 8}
Front
4. alpha = {3, 2, 9, 6, 8}
Back
int[] num = new int[100];
for (int i = 0; i < 50; i++)
num[i] = i;
num[5] = 10;
num[55] = 100;
What is the index number of the last component in the array above?
1. 99
2. 100
3. 0
4. 101
Front
1. 99
Back
To create the array variable named ar, you would use :
A. int ar;
B. int() ar;
C. int[] ar;
D. None of these
Front
C. int[] ar;
Back
Which of the following statements is NOT true?
Front
The operator new must always be used to allocate space of a specific type, regardless of the type.
Back
What is stored in alpha after the following code executes?
int[] alpha = new int[5];
int j;
for (j = 0; j < 5; j++)
{
alpha[j] = j + 1;
if (j > 2)
alpha[j - 1] = alpha[j] + 2;
}
Front
None of these
Back
Which of the following creates an array of 25 components of the type int?
(i) int[] alpha = new[25];
(ii) int[] alpha = new int[25];
Front
Only (ii)
Back
Section 4
(50 cards)
In the following code, System.out.println(num), is an example of ________.
double num = 5.4;
System.out.println(num);
num = 0.0;
Front
A void method
Back
When a method tests an argument and returns a true or false value, it should return
Front
A boolean value
Back
Which of the following is the array subscripting operator in Java?
1. .
2. {}
3. []
4. new
Front
3. []
Back
public class ScopeRules //Line 1
{
static final double rate = 10.50;
static int z;
static double t;
public static void main(String[]args) //Line 7
{
int num;
double x, z;
char ch;
// main block...
}
public static void one(int f, char g) //Line 15
{
// block one...
}
public static int w; //Line 20
public static void two(int one, int i) //Line 22
{
char ch;
int a; //Line 25
//block three
{
int x = 12; //Line 29
//...
}//end block three
// block two... //Line 32
}
}
Which of the following identifiers is visible in block three?
x (one's formal parameter)
t (before main)
z (before main)
local variables of main
Front
t (before main)
Back
public static int minimum(int x, int y)
{
int smaller;
if (x < y)
smaller = x;
else
smaller = y;
return smaller;
}
Based on the above code, what would be the output of the statement
int s = minimum(5, minimum(3, 7));
Front
3
Back
Consider the following declaration.
int[] list = new int[10];
int j;
int sum;
Which of the following correctly finds the sum of the elements of list?
(i)
sum = 0;
for (j = 0; j < 10; j++)
sum = sum + list[j];
(ii)
sum = list[0];
for (j = 1; j < 10; j++)
sum = sum + list[j];
1. Only (ii)
2. Only (i)
3. Both (i) and (ii)
4. None of these
Front
3. Both (i) and (ii)
Back
How can a method send a primitive value back to the caller?
Front
By using the return statement
Back
Which of the following methods returns an array ?
A. int acpy(int n) { ... }
B. int acpy(int[] n) { ... }
C. int[] acpy(int n) { ... }
D. None of these
Front
C. int[] acpy(int n) { ... }
Back
Suppose you have the following declaration.
double[] salesData = new double[1000];
Which of the following range is valid for the index of the array salesData.
(i) 0 through 999
(ii) 1 through 1000
1. Only (i)
2. None of these
3. Both are invalid
4. Only (ii)
Front
1. Only (i)
Back
What will be returned from the following method?
public static double methodA()
{
double a = 8.5 + 9.5;
return a;
}
Front
18.0
Back
Which of the following is NOT true about return statements?
1. A method can have more than one return statement.
2. return statements can be used in void methods to return values.
3. Whenever a return statement executes in a method, the remaining statements are skipped and the method exits.
4. A value-returning method returns its value via the return statement.
Front
2. return statements can be used in void methods to return values.
Back
Given the following method header, which of the method calls would cause an error?
public void displayValues(int x, int y)
Front
displayValue(a,b); // where a is a short and b is a long
Back
Which of the following is NOT an actual parameter?
1. Expressions used in a method call
2. Constant values used in a method call
3. Variables used in a method call
4. Variables defined in a method heading
Front
Variables defined in a method heading
Back
The lifetime of a method's local variable is
Front
Only while the method is executing
Back
In the following code, System.out.println(num), is an example of ________.
double num = 5.4;
System.out.println(num);
num = 0.0;
Front
A void method
Back
Which of the following is NOT a modifier?
Front
return
Back
What is wrong with the following method call?
displayValue (double x);
Front
The data type of the argument x should not be included in the method call
Back
Which of the following statements about strings is NOT true?
1. The class String contains many methods that allow you to change an existing string.
2. A String variable is actually a reference variable.
3. When a string is assigned to a String variable, the string cannot be changed.
4. When you use the assignment operator to assign a string to a String variable, the computer allocates memory space large enough to store the string.
Front
The class String contains many methods that allow you to change an existing string.
Back
The header of a value-returning method must specify this.
Front
The data type of the return value
Back
The value of the last element in the array created by the statement int[] ar = new int[3]; is :
A. 0
B. 1
C. 2
D. 3
Front
A. 0
Back
int[] x = new int[10];
x[0] = 34;
x[1] = 88;
println(x[0] + " " + x[1] + " " + x[10]);
What is the output of the code fragment above?
1. 34 88 88
2. This program throws an exception.
3. 34 88 0
4. 0 34 88
Front
2. This program throws an exception.
Back
Which modifier is used to specify that a method cannot be used outside a class?
Front
private
Back
If method A calls Method B, and method B calls method C, and method C calls method D, when method D finishes, what happens?
Front
Control is returned to method C
Back
Which of the following is not a benefit derived from using methods in programming?
A. Code reuse
B. Problems are more easily solved
C. Simplifies programs
D. All of the above are benefits
Front
D. All of the above are benefits
Back
Which of the following statements is NOT true?
1. Reference variables contain the address of the memory space where the data is stored.
2. The operator new must always be used to allocate space of a specific type, regardless of the type.
3. Reference variables do not directly contain the data.
4. A String variable is actually a reference variable of the type String.
Front
2. The operator new must always be used to allocate space of a specific type, regardless of the type.
Back
What will be returned from the following method?
public static double methodA()
{
double a = 8.5 + 9.5;
return a;
}
Front
18.0
Back
Formal parameters of primitive data types provide ____ between actual parameters and formal parameters.
Front
a one-way link
Back
public static int minimum(int x, int y)
{
int smaller;
if (x < y)
smaller = x;
else
smaller = y;
return smaller;
}
What is the name of the method above?
Front
minimum
Back
All @param tags in a method's documentation comment must
Front
Appear after the general description of the method
Back
When an object, such as a String, is passed as an argument, it is
Front
Actually a reference to the object that is passed
Back
To print the last element in the array named ar, you can write :
A. System.out.println(ar.length);
B. System.out.println(ar[length]);
C. System.out.println(ar[ar.length]);
D. None of these
Front
D. None of these
Back
Which of the following would be a valid method call for the following method?
public static void showProduct (int num1, double num2)
{
int product;
product = num1 * (int)num2;
System.out.println("The product is " + product);
}
A. showProduct(10.0, 4);
B. showProduct(33.0, 55.0);
C. showProduct(5.5, 4.0);
D. showProduct(10, 4.5);
Front
D. showProduct(10, 4.5);
Back
public static int minimum(int x, int y)
{
int smaller;
if (x < y)
smaller = x;
else
smaller = y;
return smaller;
}
Which of the following is a valid call to the method above?
Front
minimum(5, 4);
Back
In a @return tag statement the description
Front
Describes the return value
Back
If you pass the first element of the array ar to the method x() like this, x(ar[0]); the element ar[0] :
A. will be changed by the method x()
B. cannot be changed by the method x()
C. may be changed by the method x(), but not necessarily
D. None of these
Front
B. cannot be changed by the method x()
Back
public class ScopeRules //Line 1
{
static final double rate = 10.50;
static int z;
static double t;
public static void main(String[]args) //Line 7
{
int num;
double x, z;
char ch;
// main block...
}
public static void one(int f, char g) //Line 15
{
// block one...
}
public static int w; //Line 20
public static void two(int one, int i) //Line 22
{
char ch;
int a; //Line 25
//block three
{
int x = 12; //Line 29
//...
}//end block three
// block two... //Line 32
}
}
Which of the following is an example of a local identifier in the example above?
1. w (line 20)
2. t (line 5)
3. a (line 25)
4. rate (line 3)
Front
3. a (line 25)
Back
What is wrong with the following method call?
displayValue (double x);
Front
The data type of the argument x should not be included in the method call
Back
public static int minimum(int x, int y)
{
int smaller;
if (x < y)
smaller = x;
else
smaller = y;
return smaller;
}
Which of the following is NOT part of the heading of the method above?
1. static
2. int smaller;
3. minimum(int x, int y)
4. public
Front
2. int smaller;
Back
When an argument is passed to a method,
Front
Its value is copied into the method's parameter variable
Back
Here is a loop that is supposed to sum each of the values in the array named ar :
double sum = 0.0;
for (int i=0; i < ar.length; i++)
// ???
System.out.println(sum);
What goes on the line containing ???? :
A. i = sum + ar[i];
B. sum + ar[i];
C. sum = sum + i;
D. sum += ar[i];
Front
D. sum += ar[i];
Back
public static String exampleMethod(int n, char ch)
Which of the following statements about the method heading above is NOT true?
1. The method has two parameters.
2. exampleMethod is an identifier giving a name to this specific method.
3. The method cannot be used outside the class.
4. It is a value-returning method of type String.
Front
3. The method cannot be used outside the class.
Back
What is an actual parameter?
Front
The value passed into a method by a method call
Back
This type of method method performs a task and sends a value back to the code that called it.
Front
Value-returning
Back
int[] array1 = {1, 3, 5, 7};
for (int i = 0; i < array1.length; i++)
if (array1[i] > 5)
System.out.println(i + " " + array1[i]);
What is the output of the code fragment above?
1. 0 3
2. 7 3
3. 5 1
4. 3 7
Front
4. 3 7
Back
You can create an array of three doubles by writing :
A. double ar[] = 3;
B. double ar[3];
C. double ar[] = { 1.1, 2.2, 3.3 };
D. double[] ar = double(3);
Front
C. double ar[] = { 1.1, 2.2, 3.3 };
Back
public class ScopeRules //Line 1
{
static final double rate = 10.50;
static int z;
static double t;
public static void main(String[]args) //Line 7
{
int num;
double x, z;
char ch;
// main block...
}
public static void one(int f, char g) //Line 15
{
// block one...
}
public static int w; //Line 20
public static void two(int one, int i) //Line 22
{
char ch;
int a; //Line 25
//block three
{
int x = 12; //Line 29
//...
}//end block three
// block two... //Line 32
}
}
Which of the following identifiers is NOT visible in method two?
Front
x (variable in block three)
Back
This part of a method is a collection of statements that are performed when the method is executed.
Front
Method body
Back
The last element in the array created by the statement int[] ar = { 1, 2, 3 }; is :
A. ar[3]
B. ar[2]
C. ar(3)
D. ar(2)
Front
B. ar[2]
Back
public class ScopeRules //Line 1
{
static final double rate = 10.50;
static int z;
static double t;
public static void main(String[]args) //Line 7
{
int num;
double x, z;
char ch;
// main block...
}
public static void one(int f, char g) //Line 15
{
// block one...
}
public static int w; //Line 20
public static void two(int one, int i) //Line 22
{
char ch;
int a; //Line 25
//block three
{
int x = 12; //Line 29
//...
}//end block three
// block two... //Line 32
}
}
Which of the following identifiers is NOT visible in block three?
1. t (before main)
2. z (before main)
3. local variables of method two
4. main
Front
z (before main)
Back
Methods are commonly used to
Front
Break a problem down into small manageable pieces
Back
Section 5
(50 cards)
What is the value of alpha[3] after the following code executes?
int[] alpha = new int[5];
int j;
alpha[0] = 5;
for (j = 1; j < 5; j++)
{
if (j % 2 == 0)
alpha[j] = alpha[j - 1] + 2;
else
alpha[j] = alpha[j - 1] + 3;
}
1. 13
2. 15
3. None of these
4. 10
Front
1. 13
Back
Consider the following statements.
public class Circle
{
private double radius;
public Circle()
{
radius = 0.0;
}
public Circle(double r)
{
radius = r;
}
public void set(double r)
{
radius = r;
}
public void print()
{
System.out.println(radius + " " + area + " "
+ circumference());
}
public double area()
{
return 3.14 radius radius;
}
public double circumference()
{
return 2 3.14 radius;
}
}
Assume, also that you have the following two statements appearing inside a method in another class:
Circle myCircle = new Circle();
double r;
Which of the following statements are valid (both syntactically and semantically) in Java? (Assume that cin is Scanner object initialized to the standard input device.)
(i)
r = cin.nextDouble();
myCircle.area = 3.14 r r;
System.out.println(myCircle.area);
(ii)
r = cin.nextDouble();
myCircle.set(r);
System.out.println(myCircle.area());
1. Only (ii)
2. Both (i) and (ii)
3. Only (i)
4. None of these
Front
1. Only (ii)
Back
Assigning ____ to a field means that no other classes can access the field's values.
1. key access
2. user rights
3. private access
4. protected access
Front
3. private access
Back
The inside block, which is contained entirely within an outside block, is referred to as ____.
1. nested
2. out of scope
3. ambiguous
4. a reference
Front
1. nested
Back
What does a contain after the following loop?
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, pos = 0;
for (int i = size; i > pos; i--)
a[i - 1] = a[i];
size--;
1. {3, 7, 7, 0, 0, 0}
2. {1, 1, 1, 0, 0, 0}
3. {3, 7, 0, 0, 0, 0}
4. {0, 0, 0, 0, 0, 0}
Front
4. {0, 0, 0, 0, 0, 0}
Back
When you define a subclass, like Student, and add a single constructor with this signature Student(String name, long id), an implicit first line is added to your constructor. That line is:
1. No implicit lines are added to your constructor
2. this();
3. super(name, id);
4. base();
5. super();
Front
5. super();
Back
Assume that you have an ArrayList variable named a containing 4 elements, and an object named element that is the correct type to be stored in the ArrayList. Which of these statements replaces the first object in the collection with element?
1. a.set(0, element);
2. a.set(element, 0);
3. a.add(0, element);
4. a[0] = element;
Front
1. a.set(0, element);
Back
If a method in the superclass is declared protected, you may replace it in the subclass as long as the subclass method that you define:
1. has a different number, type, or order of parameters
2. has a different return type
3. is defined as protected or private
4. is defined as public or protected
5. is defined only as public
Front
4. is defined as public or protected
Back
Consider the following class definition.
public class Rectangle
{
private double length;
private double width;
private double area;
private double perimeter;
public Rectangle()
{
length = 0;
width = 0;
}
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public void set(double l, double w)
{
length = l;
width = w;
}
public void print()
{
System.out.println(length + " " + width);
}
public double area()
{
return length * width;
}
public double perimeter()
{
return 2 length + 2 width;
}
}
Suppose that you have the following declaration.
Rectangle bigRect = new Rectangle(10, 4);
Which of the following sets of statements are valid (both syntactically and semantically) in Java?
(i)
bigRect.area();
bigRect.perimeter();
bigRect.print();
(ii)
bigRect.area = bigRect.area();
bigRect.perimeter = bigRect.perimeter();
bigRect.print();
1. Only (ii)
2. None of these
3. Only (i)
4. Both (i) and (ii)
Front
Only (i)
Back
Consider the following statements.
public class PersonalInfo
{
private String name;
private int age;
private double height;
private double weight;
public void set(String s, int a, double h, double w)
{
name = s; age = a; height = h; weight = w;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public double getHeight()
{
return height;
}
public double getWeight()
{
return weight;
}
}
Assume you have the following code fragment inside a method in a different class:
PersonalInfo person1 = new PersonalInfo();
PersonalInfo person2 = new PersonalInfo();
String n;
int a;
double h, w;
Which of the following statements are valid (both syntactically and semantically) in Java?
(i)
person2 = person1;
(ii)
n = person1.getName();
a = person1.getAge();
h = person1.getHeight();
w = person1.getWeight();
person2.set(n, a, h, w);
1. None of these
2. Only (ii)
3. Only (i)
4. Both (i) and (ii)
Front
4. Both (i) and (ii)
Back
What is the value of alpha[4] after the following code executes?
int[] alpha = new int[5];
int j;
alpha[0] = 2;
for (j = 1; j < 5; j++)
alpha[j] = alpha[j - 1] + 3;
1. 5
2. 11
3. 8
4. 14
Front
4. 14
Back
Assume that you have an ArrayList variable named a containing 4 elements, and an object named element that is the correct type to be stored in the ArrayList. Which of these statements adds the object to the end of the collection?
1. a[3] = element;
2. a[4] = element;
3. a.add(element);
4. a.add(element, 4);
Front
3. a.add(element);
Back
A locally declared variable always ____ another variable with the same name elsewhere in the class.
1. uses
2. creates
3. deletes
4. masks
Front
4. masks
Back
A(n) ____ variable is known only within the boundaries of the method.
1. instance
2. double
3. local
4. method
Front
3. local
Back
How many constructors can a class have?
1. 2
2. Any number
3. 1
4. 0
Front
2. Any number
Back
Which of the following words indicates an object's reference to itself?
1. public
2. that
3. this
4. protected
Front
3. this
Back
A(n) ____ constructor is one that requires no arguments.
Student Response Value Correct Answer Feedback
1. write
2. default
3. class
4. explicit
Front
2. default
Back
int[] hits = {10, 15, 20, 25, 30};
copy(hits);
What is being passed into copy in the method call above?
1. 10
2. A reference to the array object hits
3. The value of the elements of hits
4. A copy of the array hits
Front
2. A reference to the array object hits
Back
public class Illustrate
{
private int x;
private int y;
public Illustrate()
{
x = 1;
y = 2;
}
public Illustrate(int a)
{
x = a;
}
public void print()
{
System.out.println("x = " + x + ", y = " + y);
}
public void incrementY()
{
y++;
}
}
How many constructors are present in the class definition above?
1. 3
2. 2
3. 1
4. 0
Front
2. 2
Back
What is the value of alpha[2] after the following code executes?
int[] alpha = new int[5];
int j;
for (j = 0; j < 5; j++)
alpha[j] = 2 * j + 1;
1. 4
2. 6
3. 1
4. 5
Front
4. 5
Back
Consider the following class definition.
public class Rectangle
{
private double length;
private double width;
public Rectangle()
{
length = 0;
width = 0;
}
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public void set(double l, double w)
{
length = l;
width = w;
}
public void print()
{
System.out.println(length + " " + width);
}
public double area()
{
return length * width;
}
public void perimeter()
{
return 2 length + 2 width;
}
}
Suppose that you have the following declaration.
Rectangle bigRect = new Rectangle();
Which of the following sets of statements are valid in Java?
(i) bigRect.set(10, 5);
(ii) bigRect.length = 10;
bigRect.width = 5;
1. Only (ii)
2. None of these
3. Only (i)
4. Both (i) and (ii)
Front
3. Only (i)
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 3};
for (int e : a) e = 0;
1. Changes each element in a to 0.
2. Compiles, but crashes when run.
3. Changes each element e to 0 but has no effect on the corresponding element in a.
4. Will not compile (syntax error)
Front
3. Changes each element e to 0 but has no effect on the corresponding element in a.
Back
What is stored in alpha after the following code executes?
int[] alpha = new int[5];
int j;
for (j = 0; j < 5; j++)
{
alpha[j] = j + 5;
if (j % 2 == 1)
alpha[j - 1] = alpha[j] + 2;
}
1. alpha = {5, 6, 10, 8, 9}
2. alpha = {8, 6, 7, 8, 9}
3. alpha = {5, 6, 7, 8, 9}
4. alpha = {8, 6, 10, 8, 9}
Front
4. alpha = {8, 6, 10, 8, 9}
Back
Programmers frequently use the same name for a(n) ____ and an argument to a method simply because it is the "best name" to use.
1. block
2. instance field
3. variable
4. constant
Front
2. instance field
Back
double[][] vals = {{1.1, 1.3, 1.5},
{3.1, 3.3, 3.5},
{5.1, 5.3, 5.5},
{7.1, 7.3, 7.5}}
How many rows are in the array above?
1. 4
2. 0
3. 2
4. 3
Front
1. 4
Back
Which of the following class definitions is correct in Java?
(i)
public class Student
{
private String name;
private double gpa;
private int id;
public void Student()
{
name = "";
gpa = 0;
id = 0;
}
public void Student(String s, double g, int i)
{
set(s, g, i);
}
public void set(String s, double g, int i)
{
name = s;
gpa = g;
id = i;
}
public void print()
{
System.out.println(name + " " + id + " " + gpa);
}
}
(ii)
public class Student
{
private String name;
private double gpa;
private int id;
public Student()
{
name = "";
gpa = 0;
id = 0;
}
public Student(String s, double g, int i)
{
set(s, g, i);
}
public void set(String s, double g, int i)
{
name = s;
gpa = g;
id = i;
}
public void print()
{
System.out.println(name + " " + id + " " + gpa);
}
}
1. Only (i)
2. Both (i) and (ii)
3. None of these
4. Only (ii)
Front
4. Only (ii)
Back
What is the value of alpha[2] after the following code executes?
int[] alpha = new int[5];
int j;
for (j = 0; j < 5; j++)
alpha[j] = 2 * j + 1;
1. 1
2. 4
3. 5
4. 6
Front
3. 5
Back
What does a contain after the following loop runs?
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, value = 5;
int pos = 0;
for (pos = 0; pos < size; pos++)
if (a[pos] > value) break;
for (int j = size; j > pos; j--)
a[j] = a[j - 1];
a[pos] = value;
size++;
1. { 5, 1, 3, 7, 0, 0 }
2. { 1, 3, 7, 5, 0, 0 }
3. { 1, 3, 5, 7, 0, 0 }
4. { 1, 3, 5, 0, 0, 0 }
Front
3. { 1, 3, 5, 7, 0, 0 }
Back
char[][] array1 = new char[15][10];
How many dimensions are in the array above?
1. 3
2. 2
3. 1
4. 0
Front
2. 2
Back
Suppose we define a class called NameInfo. It has several
private fields, including
String hospName;
and several public methods, including one that accesses hospName:
String getName();
Now, we define an array like this:
NameInfo[] listOfHospitals = new NameInfo[100];
Which expression below correctly accesses the name of the hospital
that's stored in position 5 (that is, the 6th element) of the listOfHospitals?
A. listOfHospitals.getName(5)
B. listOfHospitals.hospName[5]
C. listOfHospitals[5].hospName
D. listOfHospitals[getName(5)]
E. listOfHospitals[5].getName()
Front
E. listOfHospitals[5].getName()
Back
double[][] vals = {{1.1, 1.3, 1.5},
{3.1, 3.3, 3.5},
{5.1, 5.3, 5.5},
{7.1, 7.3, 7.5}}
What is in vals[2][1]?
1. 3.3
2. 3.5
3. 1.3
4. 5.3
Front
4. 5.3
Back
A(n) ____ method is a method that creates and initializes objects.
1. constructor
2. non-static
3. instance
4. accessor
Front
1. constructor
Back
You can use ____ arguments to initialize field values, but you can also use arguments for any other purpose.
1. constructor
2. field
3. data
4. object
Front
1. constructor
Back
The keyword ____ indicates that a field value is non-changable.
1. const
2. readonly
3. final
4. static
Front
3. final
Back
Which of the following lines of code explicitly calls the toString() method, assuming that pete is an initialized Student object variable?
1. println(super.toString());
2. println("" + pete);
3. println(pete);
4. println(pete.toString());
Front
4. println(pete.toString());
Back
int[] num = new int[100];
for (int i = 0; i < 50; i++)
num[i] = i;
num[5] = 10;
num[55] = 100;
How many components are in the array above?
1. 100
2. 0
3. 50
4. 99
Front
1. 100
Back
Consider the following method definition.
public int strange(int[] list, int item)
{
int count;
for (int j = 0; j < list.length; j++)
if (list[j] == item)
count++;
return count;
}
Which of the following statements best describe the behavior of this method?
1. This method returns the sum of all the values of list.
2. None of these
3. This method returns the number of times item is stored in list.
4. This method returns the number of values stored in list.
Front
3. This method returns the number of times item is stored in list.
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 3};
int x = 0;
for (int e : a) x += a;
1. Sums all the elements in a.
2. Will not compile (syntax error)
3. Finds the largest value in e.
4. Counts each element in a.
Front
2. Will not compile (syntax error)
Back
Which of these are superclass members are not inherited by a subclass?
1. a protected method
2. a protected instance variable
3. a public instance variable
4. a public constructor
5. a public method
Front
4. a public constructor
Back
What does the following loop do?
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, value = 5;
int pos = 0;
for (pos = 0; pos < size; pos++)
if (a[pos] > value) break;
for (int j = size; j > pos; j--)
a[j] = a[j - 1];
a[pos] = value;
size++;
1. Removes (deletes) value from a so that the array remains ordered.
2. Places value in the last element of a
3. Inserts value into a so that the array remains ordered.
4. Places value in the first unused space in a (where the first 0 appears)
Front
3. Inserts value into a so that the array remains ordered.
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 7};
int len = a.length;
int x = 0;
for (int i = 0; i < len; i++)
if (a[i] % 2 == 1) x += a[i];
System.out.println(x);
1. Finds the largest value in a.
2. Finds the smallest value in a.
3. Counts the odd elements in a.
4. Sums the odd elements in a.
Front
4. Sums the odd elements in a.
Back
What does a contain after the following loop runs?
int[] a = {1, 3, 7, 0, 0, 0};
int size = 3, capacity = 6, value = 5;
int pos = 0;
for (pos = 0; pos < size; pos++)
if (a[pos] < value) break;
for (int j = size; j > pos; j--)
a[j] = a[j - 1];
a[pos] = value;
size++;
1. { 1, 3, 7, 5, 0, 0 }
2. { 1, 3, 5, 0, 0, 0 }
3. { 1, 3, 5, 7, 0, 0 }
4. { 5, 1, 3, 7, 0, 0 }
Front
4. { 5, 1, 3, 7, 0, 0 }
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 3};
int len = a.length;
int x = 0;
for (int i = 1; i < len; i++)
if (a[i] < a[x]) x = i;
System.out.println(x);
1. Finds the position of the smallest value in a.
2. Sums the elements in a.
3. Finds the position of the largest value in a.
4. Counts the elements in a.
Front
1. Finds the position of the smallest value in a.
Back
Methods that retrieve values are called ____ methods.
1. accessor
2. class
3. mutator
4. static
Front
1. accessor
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 3};
int len = a.length;
int x = 0;
for (int i = 0; i < len; i++)
if (a[i] % 2 == 0) x += a[i];
System.out.println(x);
1. Sums the even elements in a.
2. Finds the largest value in a.
3. Finds the smallest value in a.
4. Counts the even elements in a.
Front
1. Sums the even elements in a.
Back
Consider the following class definition.
public class Rectangle
{
private double length;
private double width;
public Rectangle()
{
length = 0;
width = 0;
}
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public void set(double l, double w)
{
length = l;
width = w;
}
public void print()
{
System.out.println(length + " " + width);
}
public double area()
{
return length * width;
}
public double perimeter()
{
return 2 length + 2 width;
}
}
Suppose that you have the following declaration.
Rectangle bigRect = new Rectangle(10, 4);
Which of the following statements are valid in Java? (Assume that cin is Scanner object initialized to the standard input device.)
(i)
bigRect.length = cin.nextDouble();
bigRect.width = cin.nextDouble();
(ii)
double l;
double w;
l = cin.nextDouble();
w = cin.nextDouble();
bigRect.set(l, w);
1. Only (i)
2. Only (ii)
3. Both (i) and (ii)
4. None of these
Front
2. Only (ii)
Back
What does the following loop print?
int[] a = {6, 9, 1, 5, 12, 3};
int len = a.length;
int x = 0;
for (int i = 1; i < len; i++)
if (a[i] < a[x]) x = i;
System.out.println(x);
1. 6
2. 1
3. 2
4. 4
Front
3. 2
Back
To call a superclass method from a subclass method, when both methods have the same name and signature, prefix the call to the superclass method with:
1. super
2. the name of the superclass (that is, Person, if that is the name of the superclass)
3. no prefix is necessary. You cannot call an overridden superclass method from the subclass method you've overridden.
4. this
5. base
Front
1. super
Back
When you create your own new, user-defined types, there are really three different strategies you can use. Which of these is not one of those strategies?
1. defining a component from scratch, inheriting only the methods in the Object class
2. extending an existing component by adding new features
3. defining a component from scratch, inheriting no methods whatsoever
4. combining simpler components to create a new component
Front
3. defining a component from scratch, inheriting no methods whatsoever
Back
Assume that you have an ArrayList variable named a containing 4 elements, and an object named element that is the correct type to be stored in the ArrayList. Which of these statements adds the object to the beginning of the collection?
1. a[4] = element;
2. a.add(0, element);
3. a.insert(element);
4. a.add(element, 0);
5. a[0] = element;
Front
2. a.add(0, element);
Back
Section 6
(26 cards)
Examine the following UML diagram:
Person
-name: String
+setName(String name): void
+getName(): String
^
I
Student
-studentID: long
+Student(String sname, long sid)
+getID(): long
Which of these fields or methods are inherited by the Person class?
1. toString()
2. getName(), setName(), studentID, getID()
3. studentID, name, getName(), setName(), getID()
4. name, getName(), setName(), getID()
5. getName(), setName(), name
6. None of them
Front
toString()
Back
public class ScopeRules //Line 1
{
static final double rate = 10.50;
static int z;
static double t;
public static void main(String[]args) //Line 7
{
int num;
double x, z;
char ch;
// main block...
}
public void one(int f, char g) //Line 15
{
// block one...
}
public static int w; //Line 20
public void two(int one, int i) //Line 22
{
char ch;
int a; //Line 25
//block three
{
int z = 12; //Line 29
//...
}//end block three
// block two... //Line 32
}
}
Where is identifier z (block three's local variable) visible?
1. In block three and main
2. In block three only
3. In two and block three
4. In one and block three
Front
2. In block three only
Back
Constructors have the same name as the ____.
1. data members
2. member methods
3. class
4. package
Front
3. class
Back
Assume that you have an ArrayList variable named a containing 4 elements, and an object named element that is the correct type to be stored in the ArrayList. Which of these statements assigns the last object in the collection to the variable element?
1. element = a[3];
2. element = a[4];
3. element = a.get(3);
4. a.get(3, element);
5. a.get(element, e);
Front
3. element = a.get(3);
Back
A variable comes into existence, or ____, when you declare it.
1. is referenced
2. comes into scope
3. overrides scope
4. goes out of scope
Front
2. comes into scope
Back
When you define a subclass, like Student, and don't supply a constructor, then its superclass must:
1. have an explicit working constructor defined
2. have no constructors defined
3. have an explicit default (no-arg) constructor defined
4. have an explicit copy constructor defined
5. have no constructors defined or have an explicit default (no-arg) constructor defined.
Front
5. have no constructors defined or have an explicit default (no-arg) constructor defined.
Back
Examine the following UML diagram:
Person
-name: String
+setName(String name): void
+getName(): String
^
I
Student
-studentID: long
+Student(String sname, long sid)
+getID(): long
Which of these fields or methods are inherited by the Person class?
1. getName(), setName(), studentID, getID()
2. getName(), setName(), name
3. None of them
4. studentID, name, getName(), setName(), getID()
5. name, getName(), setName(), getID()
Front
None of them
Back
___________________ is one of the primary mechanisms that we use to understand the natural world around us. Starting as infants we begin to recognize the difference between categories like food, toys, pets, and people. As we mature, we learn to divide these general categories or classes into subcategories like siblings and parents, vegetables and dessert.
1. classification
2. encapsulation
3. specialization
4. generalization
Front
1. classification
Back
Assume you have a Student object and a Student variable named bill that refers to your Student object. Which of these statements would be legal?
bill.name = "Bill Gates"; // I
bill.setName("Bill Gates"); // II
println(bill.getName()); // III
bill.studentID = 123L; // IV
println(bill.getID()); // V
1. II, III, IV, VI
2. IV, V
3. All of them
4. II, III, V 1
5. None of them
Front
4. II, III, V
Back
When you create a new class using inheritance, you use the extends keyword to specify the __________________ when you define the ________________.
1. derived class, subclass
2. derived class, base class
3. superclass, base class
4. subclass, base class
5. superclass, subclass
6. subclass, superclass
Front
5. superclass, subclass
Back
Consider the following class definition.
public class Rectangle
{
private double length;
private double width;
private double area;
private double perimeter;
public Rectangle()
{
length = 0;
width = 0;
}
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public void set(double l, double w)
{
length = l;
width = w;
}
public void print()
{
System.out.println(length + " " + width);
}
public double area()
{
return length * width;
}
public double perimeter()
{
return 2 length + 2 width;
}
}
Suppose that you have the following declaration.
Rectangle bigRect = new Rectangle(10, 4);
Which of the following sets of statements are valid (both syntactically and semantically) in Java?
(i)
bigRect.area();
bigRect.perimeter();
bigRect.print();
(ii)
bigRect.area = bigRect.area();
bigRect.perimeter = bigRect.perimeter();
bigRect.print();
1. Both (i) and (ii)
2. Only (ii)
3. None of these
4. Only (i)
Front
4. Only (i)
Back
public class ScopeRules //Line 1
{
static final double rate = 10.50;
static int z;
static double t;
public static void main(String[]args) //Line 7
{
int num;
double x, z;
char ch;
// main block...
}
public void one(int f, char g) //Line 15
{
// block one...
}
public static int w; //Line 20
public void two(int one, int i) //Line 22
{
char ch;
int a; //Line 25
//block three
{
int z = 12; //Line 29
//...
}//end block three
// block two... //Line 32
}
}
Which of the following identifiers is NOT visible in method two?
1. x (variable in main block)
2. w (before method two)
3. rate (before main)
4. one (method name)
Front
x (variable in main block)
Back
_______________—the specification of attributes and behavior as a single entity—allows us to build on our understanding of the natural world as we create software.
Front
encapsulation
Back
int[] num = new int[100];
for (int i = 0; i < 50; i++)
num[i] = i;
num[5] = 10;
num[55] = 100;
What is the data type of the array above?
1. list
2. num
3. char
4. int
Front
4. int
Back
Methods that retrieve values are called ____ methods.
1. static
2. mutator
3. accessor
4. class
Front
3. accessor
Back
To access the name instance variable from inside a method defined in the Student class, name must use the _____________ modifier. (We'll ignore the default package-private access if no modifier is used.)
1. public
2. protected or public
3. private or protected
4. private
5. protected
6. Any of them
Front
2. protected or public
Back
Suppose we have these two array definitions in main():
String[] hospName = new String[100];
int[] hospCode = new int[100];
hospCode[I] stores the code for a specific hospital. hospName[J] stores
the name of the hospital that has code J. Suppose the code 24 is stored
at position 13 of hospCode. Which expression below references 24's
corresponding name?
A. hospName[13]
B. hospCode[24]
C. hospCode[hospName[13]]
D. hospName[hospCode[13]]
E. hospName[hospCode[24]]
Front
D. hospName[hospCode[13]]
General Feedback: What we want is the name for hospital with code 24. We could get that with hospName[24], but that's not one of the options. However, we know that hospCode[13] contains the value 24, so we can use that instead.
Back
public class Illustrate
{
private int x;
private int y;
public Illustrate()
{
x = 1;
y = 2;
}
public Illustrate(int a)
{
x = a;
}
public void print()
{
System.out.println("x = " + x + ", y = " + y);
}
public void incrementY()
{
y++;
}
}
Which of the following statements would you use to declare a new reference variable of the type Illustrate and instantiate the object with a value of 9 for the variable x?
1. Illustrate.illObject(9);
2. Illustrate illObject = new Illustrate(9);
3. Illustrate illObject(9);
4. illObject = Illustrate(9);
Front
2. Illustrate illObject = new Illustrate(9);
Back
Mutator methods typically have a return type of ______________.
1. boolean
2. String
3. void
4. int
Front
3. void
Back
Which of the following lines of code explicitly calls the toString() method, assuming that pete is an initialized Student object variable?
1. println(pete.toString());
2. println("" + pete);
3. println(super.toString());
4. println(pete)
Front
1. println(pete.toString());
Back
Mutator methods typically begin with the word "____".
1. read
2. get
3. call
4. set
Front
4. set.
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 3};
int len = a.length;
int x = 0;
for (int i = 1; i < len; i++)
if (a[i] > a[x]) x = i;
System.out.println(x);
1. Sums the elements in a.
2. Finds the position of the smallest value in a.
3. Finds the position of the largest value in a.
4. Counts the elements in a.
Front
3. Finds the position of the largest value in a.
Back
How many constructors can a class have?
1. Any number
2. 2
3. 1
4. 0
Front
1. Any number
Back
The toString() method is automatically inherited from the __________________ class.
1. GObject
2. Object
3. java.lang
4. Root
5. Component
Front
2. Object
Back
What does the following loop do?
int[] a = {6, 1, 9, 5, 12, 3};
int len = a.length;
int x = 0;
for (int i = 0; i < len; i++)
if (a[i] % 2 == 0) x += a[i];
System.out.println(x);
1. Counts the even elements in a.
2. Sums the even elements in a.
3. Finds the largest value in a.
4. Finds the smallest value in a.
Front
2. Sums the even elements in a.
Back
In the type-safe version of the ArrayList class, you specify the type of each element by:
1. None of these; an ArrayList can hold any object type
2. specifying a generic type parameter when defining the ArrayList variable
3. using the special type-safe version of the subscript operator
4. passing a Class parameter as the first argument to the constructor
5. using the setType() method once the ArrayList is constructed
Front
specifying a generic type parameter when defining the ArrayList variable