Title: Test01- Requires Respondus LockDown Browser
Course: Java Programming Intermediate (APT TEMPLATE) > Section: Test01

Test for Generics competency.

Q: True or False: The code shown below will compile and run successfully producing the following output:

Able Baker Charley aBle

/*File Q01 Copyright 2013 R.G.Baldwin
--------------------------------------------------------*/
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;

public class Q01{
public static void main(String args[]){

Collection <String> collection =
new Q01Runner().getCollection();

collection.add("Able");
collection.add("aBle");
collection.add("Baker");
collection.add("Charley");
collection.add("Baker");

Iterator <String> iter = collection.iterator();
while(iter.hasNext()){
System.out.print(iter.next() + " ");
}//end while loop
}//end main()
}//end class Q01
//======================================================//

class Q01Runner{
Collection <String> getCollection(){
Collection <String> collection =
new TreeSet<String>();
return collection;
}//end getCollection
}//end class Q01Runner
- True
- False
A: True

Q: True or False: The code shown below will compile and run successfully producing the following output:

Cat Dog Dog Emu cAt

/*File Q02 Copyright 2013 R.G.Baldwin
--------------------------------------------------------*/
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;

public class Q02{
public static void main(String args[]){

Collection <String> collection =
new Q02Runner().getCollection();

collection.add("Cat");
collection.add("cAt");
collection.add("Dog");
collection.add("Emu");
collection.add("Dog");

Iterator <String> iter = collection.iterator();
while(iter.hasNext()){
System.out.print(iter.next() + " ");
}//end while loop
}//end main()
}//end class Q02
//======================================================//

class Q02Runner{
Collection <String> getCollection(){
Collection <String> collection =
new TreeSet<String>();
return collection;
}//end getCollection
}//end class Q02Runner
//======================================================//
- True
- False
A: False

Q: True or False: The code shown below will compile and run successfully producing the following output:

Joe,500
500,Tom

/*File Q03.java
************************************************/
public class Q03{

public static void main(String[] args){
Q03Runner <String,Integer> varA =
new Q03Runner <>(
"Joe",new Integer(500));
System.out.print(varA.get1());
System.out.println("," + varA.get2());

Q03Runner <Integer,String> varB =
new Q03Runner <>(
new Integer(500),"Tom");
System.out.print(varB.get1());
System.out.println("," + varB.get2());
}//end main
}//end class Q03
//=============================================//

class Q03Runner<T1,T2>{
private T1 t1;;
private T2 t2;

public Q03Runner(T1 t1,T2 t2){
this.t1 = t1;
this.t2 = t2;
}//end constructor

public T1 get1(){
return t1;
}//end get1

public T2 get2(){
return t2;
}//end get2
}//end Q03Runner
//=============================================//
- True
- False
A: True

Q: True or False: The code shown below will compile and run successfully producing the following output:

900,Bill
Pat,100

/*File Q04.java
************************************************/
public class Q04{

public static void main(String[] args){
Q04Runner <String,Integer> aVar =
new Q04Runner <>(
"Pat",new Integer(100));
System.out.print(aVar.get1());
System.out.println("," + aVar.get2());

Q04Runner <Integer,String> bVar =
new Q04Runner <>(
new Integer(900),"Bill");
System.out.print(bVar.get1());
System.out.println("," + bVar.get2());
}//end main
}//end class Q04
//=============================================//

class Q04Runner<T1,T2>{
private T1 t1;;
private T2 t2;

public Q04Runner(T1 t1,T2 t2){
this.t1 = t1;
this.t2 = t2;
}//end constructor

public T1 get1(){
return t1;
}//end get1

public T2 get2(){
return t2;
}//end get2
}//end Q04Runner
//=============================================//
- True
- False
A: False

Q: True or False: The code shown below will compile and run successfully producing the following output:

class java.awt.Frame
class javax.swing.JFrame

/*File Q05.java
************************************************/
import java.awt.Component;
import java.awt.Frame;
import javax.swing.JFrame;

public class Q05{
/*Note the following inheritance hierarchy
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame
*/
public static void main(String[] args){
Q05Runner <Frame> var1 =
new Q05Runner <>(new Frame("frame0"));
var1.runIt();

Q05Runner <JFrame> var2 =
new Q05Runner <>(new JFrame("jFrame0"));
var2.runIt();
}//end main
}//end class Q05
//=============================================//
class Q05Runner<T extends Component>{
T t;

Q05Runner(T t){
this.t = t;
}//end constructor

public void runIt(){
System.out.println(t.getClass());
}//end runIt
}//end Q05Runner
//=============================================//
- True
- False
A: True

Q: True or False: The code shown below will compile and run successfully producing the following output:

class java.awt.Frame
class javax.swing.JFrame

/*File Q06.java
************************************************/
import java.awt.Component;
import java.awt.Frame;
import javax.swing.JFrame;

public class Q06{
/*Note the following inheritance hierarchy
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame
*/
public static void main(String[] args){
Q05Runner <Frame> var1 =
new Q05Runner <>(new Frame("frame0"));
var1.runIt();

Q05Runner <JFrame> var2 =
new Q05Runner <>(new JFrame("jFrame0"));
var2.runIt();
}//end main
}//end class Q06
//=============================================//
class Q05Runner<T extends JFrame>{
T t;

Q05Runner(T t){
this.t = t;
}//end constructor

public void runIt(){
System.out.println(t.getClass());
}//end runIt
}//end Q05Runner
//=============================================//
- True
- False
A: False

Q: True or False: The code shown below will compile and run successfully producing the following output:

class java.awt.Frame
class java.awt.Window
class javax.swing.JFrame

/*File Q07.java
************************************************/
import java.util.ArrayList;
import java.awt.Window;
import java.awt.Frame;
import javax.swing.JFrame;
/*Note the following inheritance hierarchy
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame
*/
public class Q07{
public static void main(String[] args){

Frame frame0 = new Frame("Frame0");

ArrayList <Frame> listA = new ArrayList<>();
listA.add(frame0);
new Q07Runner().display(listA);

ArrayList<Window> listB = new ArrayList<>();
listB.add(new Window(frame0));
new Q07Runner().display(listB);

ArrayList<JFrame> listC = new ArrayList<>();
listC.add(new JFrame("JFrame0"));
new Q07Runner().display(listC);

}//end main
}//end class Q07
//=============================================//
class Q07Runner{
void display(ArrayList<? super JFrame> list){
for(Object n : list){
System.out.println(n.getClass());
}//end for loop
}//end display
}//end Q07Runner
//=============================================//
- True
- False
A: True

Q: True or False: The code shown below will compile and run successfully producing the following output:

class java.awt.Frame
class java.awt.Window
class javax.swing.JFrame

/*File Q08.java
************************************************/
import java.util.ArrayList;
import java.awt.Window;
import java.awt.Frame;
import javax.swing.JFrame;
/*Note the following inheritance hierarchy
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame
*/
public class Q08{
public static void main(String[] args){

Frame frame0 = new Frame("Frame0");

ArrayList <Frame> listA = new ArrayList<>();
listA.add(frame0);
new Q08Runner().display(listA);

ArrayList<Window> listB = new ArrayList<>();
listB.add(new Window(frame0));
new Q08Runner().display(listB);

ArrayList<JFrame> listC = new ArrayList<>();
listC.add(new JFrame("JFrame0"));
new Q08Runner().display(listC);

}//end main
}//end class Q08
//=============================================//
class Q08Runner{
void display(ArrayList<? super Frame> list){
for(Object n : list){
System.out.println(n.getClass());
}//end for loop
}//end display
}//end Q08Runner
//=============================================//
- True
- False
A: False

Q: True or False (read very carefully for meaning):

According to "The Java Tutorials" from Oracle,

"In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs. The difference is that the inputs to formal parameters are types, while the inputs to type parameters are values."
- True
- False
A: False

Q: True or False (read very carefully for meaning):

According to "The Java Tutorials" from Oracle, one of the benefits of using generics is:

"Stronger type checks at compile time. A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find."
- True
- False
A: True