 Indiana University Bloomington Evaluation Services and Testing
QuizSite  Students
Quizzes · Tests · Evaluations · Surveys
 Activity: Practice_Nine Student: dgerman (Adrian German) Instructor: dgerman Course: J201 Section: 4285 Question 1.
Fill in the blank space for the following program that should print the sum of the elements in array `a`.

```class One <
public static void main(String[] args) <
double a[] = <1, 2, 3, 4};
int sum;
for (int i = _____ = 0; i < a.length; i++) <
sum += a[i];
}
System.out.println(sum);
}
} ```
Do it by hand, then check with the computer. Question 2.
Fill in the blank space for the following program that should print the number of times the element 1 (one) appears in array `a`.

```class One <
public static void main(String[] args) <
int a[] = <1, 2, 1, 2, 1, 2, 3, 1, 1};
int _____ = 0;
for (int i = 0; i < a.length; i++)
if (a[i] == 1) <
x = x + 1;
}
System.out.println(x);
}
} ```
Do it by hand, then check with the computer. Question 3.
Fill in the blank space for the following program that should print the largest value that appears in the array `a`.

```class One <
public static void main(String[] args) <
int a[] = <1, 2, 1, 2, 1, 2, 3, 1, 1};
int max = a;
for (int i = 1; i < a.length; i++)
if (max _____ a[i]) <
max = a[i];
}
System.out.println(max);
}
} ```

`<`
`<=`
`>`
`>=`
none of the above Question 4.
Fill in the blank space for the following program that should sort array `a` in ascending order (smallest number first).

```class One <
public static void main(String[] args) <
int a[] = <3, 2, 1, 6, 5, 4};
for (int i = 0; i < a.length - 1; i++)
for (int j = i; j < a.length; j++)
if (a[i] _____ a[j]) <
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
} ```

`<`
`<=`
`>`
`>=`
none of the above Question 5.
Fill in the blank space for the following program that should print the number of elements passed to it on the command line.

```class One <
public static void main(String[] vals) <
System.out.println(_____ + " arguments.");
}
} ```
Do it by hand, then check with the computer. Question 6.
Fill in the blank space for the following applet that should monitor the mouse movement.
```import java.applet.*;
import java.awt.event.*;
public class One extends Applet implements MouseMotionListener <
public void init() <
_____(this);
}
public void mouseMoved(MouseEvent e) <
System.out.println("The mouse is being moved.");
}
public void mouseDragged(MouseEvent e) < }
} ```
Do it by hand, then check with the computer. Question 7.
Fill in the blank space for the following applet that should monitor the mouse movement.
```import java.applet.*;
import java.awt.event.*;
public class One extends Applet <
public void init() <
}
}
class Two implements MouseMotionListener <
public void mouseMoved(MouseEvent e) <
System.out.println("The mouse is being moved.");
}
public void mouseDragged(MouseEvent e) < }
}```
Do it by hand, then check with the computer. Question 8.
Fill in the blank space for the following applet that should monitor the mouse movement.
```import java.applet.*;
import java.awt.event.*;
public class One extends Applet <
public void init() <
_____ a = new Two();
}
}
class Two implements MouseMotionListener <
public void mouseMoved(MouseEvent e) <
System.out.println("The mouse is being moved.");
}
public void mouseDragged(MouseEvent e) < }
}```
Do it by hand, then check with the computer. Question 9.
Fill in the blank space for the following program to compile.

```class Player < }
class Defender extends Player < }
class Forward extends Player < }
class Sweeper extends Defender < }
class Match <
public static void main(String[] args) <
_____ team[] = new _____;
team = new Sweeper();
team = new Forward();
}
} ```
Note that the answer applies to both blank spaces. Question 10.
Choose the best answer to fill the blank in this program that sorts its command line arguments in ascending order (as `String`s).
```class One <
public static void main(String[] args) <
boolean done = true;
for (int i = 0; i < args.length - 1; i++) <
if (args[i].compareTo(args[i + 1]) > 0) <
String temp = args[i];
args[i] = args[i + 1];
args[i + 1] = temp;
done = false;
}
}
if (! done) <
_____;
} else <
for (int i = 0; i < args.length; i++)
System.out.print(args[i] + " ");
System.out.println();
}
}
} ```

This program can't be saved, no matter what.
`i++`
`args(main)`
`main(args)`
It can only be done by adding a do-while loop. Question 11.
You compile and run this program. What is the output?
```class A <
void fun() < System.out.println("A"); }
}
class B extends A <
void fun() < System.out.println("B"); }
}
class C extends B <
void fun() < System.out.println("C"); }
}
class Test <
public static void main(String[] args) <
A a = new B();
A b = new C();
a.fun();
b.fun();
}
} ``` Question 12.
You compile and run this program. What is the output?
```class Example <
public static void main(String[] args) <
Example e = new Example(2);
Example f = new Example();
System.out.println(e.getBalance() + f.getBalance());
}
Example() <
this(20);
}
Example (int n) <
balance = n;
}
int balance;
int getBalance() <
return balance;
}
} ``` Question 13.
You compile and run this program. What is the output?
```class Example <
public static void main(String[] args) <
Example e = new Example();
Example f = new Example();
System.out.println(e.fun() + e.fun(1));
}
int fun() <
return 3 + fun(3);
}
int fun(int n) <
return n * 2;
}
} ``` Question 14.
You compile and run this program. What is the output?
```class A <
public static void main(String[] args) <
A a = new A();
B b = new B();
A c = new B();
System.out.println(a.fun() + "" +
b.fun() + "" +
c.fun());
}
int fun() <
return 3;
}
}
class B extends A <
int fun() <
return 6;
}
} ```
Please type NONE if the program does not compile. Question 15.
You compile and run this program. What is the output?
```class A <
public static void main(String[] args) <
System.out.println(k(g(2) + h(2)));
}
public static double f(double x)
<
return g(x) + Math.sqrt(h(x));
}
public static double g(double x) < return 4 * h(x); }
public static double h(double x) < return x * x + k(x) - 1; }
public static double k(double x) < return 2 * (x + 1); }
} ```
Do it by hand, then check with the computer. Question 16.
Which of the following code fragments is logically equivalent to this one?
```int i = 2;
while (i < 10) <
System.out.println(i);
i = i + 2;
}```
Check all that apply.

```for (int i = 2; i < 10; i = i + 2) <
System.out.println(i);
}```

```for (int i = 1; i < 9; i = i + 2) <
System.out.println(i + 1);
}```

```for (int i = 2; i <= 8; i += 2) <
System.out.println(i);
}```

```for (int i = 1; i < 5; ++i) <
System.out.println(2 * i);
}```

```for (int i = 2; i < 10; ++i) <
if (i % 2 == 0)
System.out.println(i);
}```

```for (int i = 2; i < 10; ++i) <
System.out.println(i);
System.out.println(i + 2);
}``` Question 17.
Which of the following statements about arrays is true?

All of the elements in an array must be of the same type.
You may not dimension an array to be larger than 100.
Arrays automatically stretch to fit the number of inputs given.
Two arrays can be joined together with the `+` operator.
All of the above are true.