READ the directions and answer the
followi ng questions below. Be sure to sho your work whereve necessary or
PARTIA L CREDIT. This quiz will be grade d out of 40 p oints.
True/False Questio ns (2 points each):
Decide w hether
the following st tements are True or
False.
1. In an rray called nums, the i ndex of the last element is nums.l ength
___False____
2. An array of
type double is a p imitive typ
__True______
3.
The elements of a
n array of type Student are null until the objects inside of the array
are instantia ed
_true_______
4. A method can
rec ive as well as return a o ne and two- dimensional array
__False_____
Multiple Choice Qu estions (3 p oints each):
Select the best response for the
following questions. Be sure to circle your answ er and write the letter in the
space pr ovided.
5. What is the value of i after the code above has been executed?
|
____2____ __
|
|
a) 0
|
b) 1
|
|
c) 2
|
d) 3
|
|
6. What is the value of count after the code above has been execut ed?
|
____3____ __
|
|
a) 1
|
b) 2
|
|
c) 3
|
d) 4
|
|
________ __
a) Whenever the last element is equal to val
b) Whenever the array a contains any el e ment that e quals to va c) Whenever exactly one ele ment in the rray a is equal to val d) Whenever the first element is
equal to val
ANS---A---
8. Deter mine the
output.
int[] [] numbe rs = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
System .out.pri ntln(numbers[1][4]);
a) 4
c) In dexOutOfB unds
b) 8
d) 5
---------C--------
Open-
nded Question (4 p oints):
9. Describe the FO R
changes need to hav e the code p rint every v lue in the a rray.
public class Test {
public static void
main(String[] args) {
int arr1[] =
{ 1, 3, 7, 9, 15 };
for (int index = 0;
index <
arr1.length; index++) {
System.out.println(arr1[index]);
}
}
}
FOR Q
ESTIONS 10-13, USE THE RESOUCE CL ASS ABOVE!
10. Crea e an array named dogs of type Do g with 3 dog s.
Dog dogs[]= new Dog[3];
11. In or der, add the dogs provided below into
the array.
Labra
|
|
12
|
or
|
||
Dachs
|
und
|
4
|
Poodl
|
|
9
|
class Dog
{
private static
String breed;
private static int age;
public static void
main(String[] args) {
Dog dogs[]= new
Dog[3];
dogs[0].breed="Labraor";
dogs[1].breed="Dachsund";
dogs[2].breed="Poodl";
dogs[0].age=12;
dogs[1].age=4;
dogs[2].age=9;
12. Change the breed of second dog to a Beagle.
class Dog
{
private static
String breed;
private static int age;
public static void
main(String[] args) {
Dog dogs[]= new
Dog[3];
dogs[0].breed="Labraor";
dogs[1].breed="Beagle";
dogs[2].breed="Poodl";
dogs[0].age=12;
dogs[1].age=4;
dogs[2].age=9;
13. Use n
Enhanced For Loop t o output the dogs’ breed and age.
public class Dog
{
private
String breed;
private int age;
public
Dog(String breed, int age) {
this.breed = breed;
this.age = age;
}
public void
setBreed(String breed) {
this.breed = breed;
}
public
String getBreed() {
return breed;
}
public void
setAge(int age) {
this.age = age;
}
public int
getAge() {
return age;
}
public static void
main(String[] args) {
Dog d = new Dog("German", 4);
System.out.println("Breed
: " + d.getBreed());
System.out.println("Age
: " + d.getAge());
}
}
14.
Write a method
named diag onalSum that accepts a square, 2D array filled with integer values (rows = colu ns) and ret urns the sum) and return the sum of the diagonal (start from the to p left).
class
Sum_Diagonal {
public static void
main(String args[]) throws
IOException {
int d[][]
= { { 1, 2, 6 }, { 3, 8, 5 }, { 5, 6, 7 } };
int k = 0, j = 0;
int sum1 = 0,
sum2 = 0;
for (j = 0;
j <
d.length; j++) {
for (k = 0; k < d.length; k++)
System.out.print(d[j][k] + " ");
System.out.println();
}
for (j = 0;
j <
d.length; j++) {
sum1 = sum1 + d[j][j];
}
k = d.length - 1;
for (j = 0;
j <
d.length; j++) {
if (k >= 0) {
sum2 = sum2 + d[j][k];
k--;
}
}
System.out.println("Sum
of Digonal elements are :" + sum1 + "
and "
+
sum2);
}
}
EmoticonEmoticon