Wednesday 30 November 2011

Drag & Drop Questions for SCJP


1) Place code into the class so that it compiles & generates the out put answer=42. Note:
Code options may be used more than once.
     Class
      public class Place here        {
           
              private Place here object;
              public Place here(Place here object){
                         this.object = object;
             }
           
               public Place here getObject() {
                       return object;
                }
      }
               public static void main(String[] args) {
               
                   Gen<String> str = new Gen<String>(“answer”);
                   Gen<Integer> intg = new Gen<Integer>(42);
                   System.out.println(str.getObject() + “=” + intg.getObject());
               }
      }
Code Options
Gen<T>
Gen<?>
Gen
?
T


Solution:-
      public class Gen<T>       {
           
              private T object;
              public Gen(T object){
                         this.object = object;
             }
           
               public T getObject() {
                       return object;
                }
      }
               public static void main(String[] args) {
               
                   Gen<String> str = new Gen<String>(“answer”);
                   Gen<Integer> intg = new Gen<Integer>(42);
                   System.out.println(str.getObject() + “=” + intg.getObject());
               }
      }



2) Given:
public void takeList(List<? extends String> list) {
       // insert code here
}
Place the compilation results on each code statement to indicate whether or not
that code will compile if inserted into the takeList() method.


Code Statements                                     Compilation Result
---------------------------------------------------------------------
List.add(“Foo”);                                      Compilation Fails
List = new ArrayList<String>();               Compilation Succeeds
List = new ArrayL:ist<Object>();            Compilation Fails
String s = list.get(0);                                Compilation Succeeds
Object o = list                                         Compilation Succeeds


3) Place the correct description of the compiler output on the code fragments to be
inserted at liens 4 and 5. The same compiler output may be used more than once.
import java.util.*;
public class X {
      public static void main(String[] args){
           // insert code here
           // insert code here
      }
      public static void foo(List<Object> list) {}
}
Code
a) ArrayList<String> x1 = new ArrayList<String>();
    foo(x1);
b) ArrayList<Object> x2 = new ArrayList<String>();
     foo(x2);
c) ArrayList<Object> x3 = new ArrayList<Object>();
    foo(x3);
d) ArrayList  x4 = new ArrayList ();
     foo(x4);
Compiler Output
1) Compilation succeeds
2) Compilation fails due to an error in the first statement
3) Compilation of the first statement succeeds, but compilation fails due to error
in the second statement.
Solution:-
a = 3
b = 2
c = 1
d = 1



4) Place the output option in the actual output sequence to indicate the output
from this code.
class Alpha {
      public void foo(String… args)
         { System.out.println(“Alpha:foo”);}
      public void bar(String a)
         { System.out.println(“Alpha:bar”);}
}
public class Beta extends Alpha {
     public void foo(String a)
         { System.out.println(“Beta:foo”);}
      public void bar(String a)
         { System.out.println(“Beta:bar”);}
      public static void main(String[] argv){
            Alpha a = new Beta();
            Beta b = (Beta)a;
           
            a.foo(“test”);    b.foo(“test”);
            a.bar(“test”);     b.bar(“test”);
      }
}
Actual output sequence
1)              2)             3)              4)
Output options
Alpha:foo                Alpha:bar            Beta:foo            Beta:bar
Solution:-
1) Alpha:foo   2) Beta:foo  3) Beta:bar    4) Beta:ba


5) Place the lines in the correct order to complete the enum.
enum Element {
1st
2nd
3rd
4th
5th
}
Lines
public string info() {return “element”;}
};
FIRE { public string info() {return “Hot”;}
EARTH, WIND,
}
Solution
EARTH, WIND,
FIRE { public string info() {return “Hot”;}
};
public string info() {return “element”;}
}


6) Place the code elements in order so that the resulting java sources file will
compile correctly, resulting in a class called com.sun.cert.AddressBook.
Source file
1st
2nd
3rd
ArrayList entries;
}
Code Element
package com.sun,cert;
package com.sun,cert.*;
import java.util.*;
import java.*;
public class AddressBook {
public static class AddressBook{
Solution: -
package com.sun,cert;
import java.util.*;
public class AddressBook {
ArrayList entries;
}



7) Place the code fragment in position to complete the displayable interface.
interface Reloadable {
public void reload();
}
interface diplayable place here place here {
place here
}
Code Fragments:
extends public void display(); Reloadable
implements public void display() {/*Display*/} Edit
Solution:-
interface Reloadable {
public void reload();
}
interface diplayable extends Reloadable {
public void display();
}
8) Place the correct code in the code sample to achieve the expected results:
Expected Results:-
Output: 1 2 4 8 16 32
Code Sample:-
int [] y = { 1, 2, 4, 8, 16, 32};
System.out.print (“Output: ”);
Place here
System.out.prinr (x);
System.out.print(“ “);
}

Code:-
1) for(int x : y) {
2) for(int x = y[ ]){
3) foreach(y as x) {
4) foreach(int x : y) {
5) for (int x=1; x=y[ ]; x++){
Solution:-
int [] y = { 1, 2, 4, 8, 16, 32};
System.out.print (“Output: ”);
for(int x : y){
System.out.prinr (x);
System.out.print(“ “);
}
9) Chain these constructors to create objects to read from a file named “in” and to
write to a file named “out”.
reader = place here place here”in” ));
writer= place here place here place here”out” )));
Constructor
new FileReader( new PrintReader( new BufferedReader(
new BufferedReader( new FileWrite( new PrintWriter(
Solution:-
reader = new BufferedReader(new FileReader(“in”));
writer = new PrintWriter(new BufferedWriter(new FIleWriter(“out”)));

Place the code fragments into position to use a BufferedReader to read in an entire
text file:
class PrintFile {
public static void main(String[] args){
BufferedReader buffReader = null;
//More code here to initialize buffReader
try{
String temp;
while ( place here place here ) {
System.out.printin(temp);
}
} catch place here
e.printStackTrace();
}
}
}
Code Fragments
(temp = buffReader.readLine() ) && buffReader.hasNext()
(temp = buffReader.nextLine()) (IOException e) {
!= null (FileNotFoundException e){
Solution:-
class PrintFile {
public static void main(String[] args){
BufferedReader buffReader = null;
//More code here to initialize buffReader
try{
String temp;
while ((temp = buffReader.readLine() ) != null ) {
System.out.printin(temp);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

10) Given:
class A {
String name = “A”;
String getName() {
return name;
}
String greeting(){
return “class A”;
}
}
class B extends A {
String name = “B”;
String greeting(){
return “class B”;
}
}
public class Client {
public static void main(String[] args) {
A a = new A();
A b = new B();
System.out.println(a.greeting() + “ has name ” + a.getName());
System.out.println(b.greeting() + “ has name ” + b.getName());
}
}
Place the names “A” and “B” in the following output:
class place here has name place here
class place here has name place here
Names
A B
Solution:-
class A has name A
class B has name A










No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...