This page is optimized for mobile devices, if you would prefer the desktop version just click here

8.6 Java4260r: review of type inference  (Page 2/3)

Figure 2 - Question 2.
Object instantiated.

Answer 2

Question 3

True or False: The code shown in Listing 3 will compile and run successfully producing the output shown in Figure 3 .

Listing 3 - Question 3.
/*File Q03.java ************************************************/import java.awt.*; /*Note the following inheritance hierarchyjava.lang.Object java.awt.Componentjava.awt.Container java.awt.Windowjava.awt.Frame */public class Q03{ public static void main(String[]args){ Foo<>var = new Foo<Window>(); var.runIt();}//end main }//end class Q03//=============================================// class Foo<T extends Component>{ public void runIt(){System.out.println("Object instantiated."); }//end runIt}//end Foo
Figure 3 - Question 3.
Object instantiated.

Answer 3

Question 4

True or False: The code shown in Listing 4 will compile and run successfully producing the output shown in Figure 4 .

Listing 4 - Question 4.
/*File Q04.java ************************************************/public class Q04{ public static void main(String[]args){ Foo<String,Integer>varA = new Foo<String,Integer>("abcde",500); System.out.print(varA.get1());System.out.println("," + varA.get2()); Foo<Integer,String>varB = new Foo<Integer,String>(900,"fghijkl"); System.out.print(varB.get1());System.out.println("," + varB.get2()); }//end main}//end class Q04 //=============================================//class Foo<T1,T2>{ private T1 t1;private T2 t2; public Foo(T1 t1,T2 t2){this.t1 = t1; this.t2 = t2;}//end constructor public T1 get1(){return t1; }//end get1public T2 get2(){ return t2;}//end get2 }//end Foo
Figure 4 - Question 4.
abcde,500 900,fghijkl

Answer 4

Question 5

True or False: The code shown in Listing 5 will compile and run successfully producing the output shown in I mage 5 .

Listing 5 - Question 5.
/*File Q05.java ************************************************/public class Q05{ public static void main(String[]args){ Foo<String,Integer>varA = new Foo<>("abcde",500); System.out.print(varA.get1());System.out.println("," + varA.get2()); Foo<Integer,String>varB = new Foo<>(900,"fghijkl"); System.out.print(varB.get1());System.out.println("," + varB.get2()); }//end main}//end class Q05 //=============================================//class Foo<T1,T2>{ private T1 t1;private T2 t2; public Foo(T1 t1,T2 t2){this.t1 = t1; this.t2 = t2;}//end constructor public T1 get1(){return t1; }//end get1public T2 get2(){ return t2;}//end get2 }//end Foo
Figure 5 - Question 5.
abcde,500 900,fghijkl

Answer 5

Question 6

True or False: The code shown in Listing 6 will compile and run successfully producing the output shown in Figure 6 .

Listing 6 - Question 6.
/*File Q06.java ************************************************/public class Q06{ public static void main(String[]args){ Foo<>varA = new Foo<String,Integer>("abcde",500); System.out.print(varA.get1());System.out.println("," + varA.get2()); Foo<>varB = new Foo<Integer,String>(900,"fghijkl"); System.out.print(varB.get1());System.out.println("," + varB.get2()); }//end main}//end class Q06 //=============================================//class Foo<T1,T2>{ private T1 t1;private T2 t2; public Foo(T1 t1,T2 t2){this.t1 = t1; this.t2 = t2;}//end constructor public T1 get1(){return t1; }//end get1public T2 get2(){ return t2;}//end get2 }//end Foo
<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.