0% found this document useful (0 votes)
1K views10 pages

Answer SCJP Drag and Drop

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1/ 10

Ques: 186

public int update(int quantity, int adjust){


quantity = quantity + adjust;
return quantity;
}
public void callUpdate() {
int quant = 100;
quant = update(quant, 320);
System.out.println(“The quantity is: ”+ quant);
}

Ques : 281,258

public class Flags2 {


private boolean isReady = false;
public synchronized void produce() {
isReady = true;
notifyAll();
}
public synchronized void consume() {
while(!isReady) {
try {
wait();
} catch(Exception e) {}
}
isReady = false;
}
}

Ques: 259

started
ran
interrupting
ended
(no more output)

Ques: 260

class Alpha {
public void bar( int… x ) {}
public void bar( int x ) {}
}
public class Beta extends Alpha {
public void bar(int x) {}
public int bar(String x) {return 1;}
public void bar(int x, int y) {}
}

Ques: 261

interface Reloadable
{
public void reload();
}

class Edit
{
public void Edit(){}
}

interface Displayable extends Reloadable


{
public void display();
}

Ques: 262

package alpha;
public class Alpha{
private String alpha;
public Alpha(){this("A");}
protected Alpha(String a){alpha = a;}
}

package beta;
public class Beta extends alpha.Alpha{
public Beta(String a){super(a);}
}

Ques: 263

1. class A{List<B> b} - car is vechile and car is a collectible


2.class A{} - car has a steering wheel
3.class A{B b;} - car has wheels
4.class A extends B,C{} - Mini is a car
5.class A{B b;C c;} - car is an object
6.class A implements B,C{}
7.class A extends B{}
class A{ List<B> b; }
Now here A has multiple Bs, so we can match it to - 3. car has wheels - because car has multiple
wheels and here A can have multiple Bs. So here Car symbolizes A and wheels symbolizes B.

class A extends B,C{}


This Implementation structure won't be used as it depicts multiple inheritance and this is not
supported by java

class A {}
Well here A doesn't extend any class so by default it will extend Object class, so option 5 suits it
- 5.car is an object
class A{ B c; C c; }
Here A has an object of B and C. There is no option matching this one

class A{ B b;}
Here A has one object of B. So option 2 suits it - 2.car has a steering wheel as a car can have
only one steering wheel

class A implements B,C{}


Here A is-a B and A is-a C. So option 1 suits this - 1.Car is a vehicle and car is a collectable
class A Extends B{}
Here A is-a B. So option 4 suits it - 4.Mini is a car.

Ques: 264

Pi approximately 3.141593
and E is approximately true

Ques: 265
1).ArrayList<String> x1 = new ArrayList<String>();
foo(x1); ---- Compilation of the first statement succeeds, but
compilation fails due to error in the second statement.

2). ArrayList<Object> x2 = new ArrayList<String>();


foo(x2); ---- Compilation fails due to an error in the first statement

3). ArrayList<Object> x3 = new ArrayList<Object>();


foo(x3); -- Compilation succeeds

4). ArrayList x4 = new ArrayList ();


foo(x4); -- Compilation succeeds

Ques: 266

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());
}
}

Ques: 267

public void takeList(ArrayList list){} - Compilation Succeeds


public void takeList(ArrayList<Animal> list){} - Compilation Fails
public void takeList(ArrayList<? extends Animal> list){} - Compilation Succeeds
public void takeList(ArrayList<?> list){} - Compilation Succeeds
public void tkeList(ArrayList<Object> list){} - Compilation Fails
Ques: 268

public class myint implements comparable


public string toString()
public int compareTo(object o)
{
myint i2=(myint)o;
return i-i2.i;
}

Ques: 269

java.util.Map --------> defines the method : V get(Object key)

java.util.Set --------> contains no pair of elements e1 and e2, such that e1.equals(e2)

java.util.List --------> allows access to elements by their integer index

java.util.Queue --------> is designed for holding elements prior to processing

Ques: 270

class A has name A

class B has name A

Ques: 271

public class Single{


private static Single instance;
public static Single getInstance() {
if(instance==null)instance=create();
return instance;
}
// next 2 line alone changes... ( instead of private protected and instead of protected static )
protected Single() { }
static Single cerate() { return new Single(); }
}
class SingleSub extends Single {
}

Ques: 272

public class ReadFile{


public static void main(String argv[]){
try{
File x1 = new File("MyText.txt");
FileReader x2 = new FileReader(x1);
BufferedReader x4 = new BufferedReader(x2);
String x3 = null;
while((x3 = x4.readLine()) != null){
System.out.println(x3);
}x4.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}

Ques: 273

public class Doubler {


public static int doubleMe(int h)
{
return h*2;

public class Holder {

int amount=10;
public void doubleAmount(){amount=Doubler.doubleMe(amount);}

public int getAmount(){return amount;}

Ques: 274

t.join();
t.run();
t.doIt();
Ques: 275

m1(listA); Compiles and runs without error.


m1(listB); Compiles and runs without error.
m1(listO); Does not Compile.

m2(listA); Compiles and runs without error.


m2(listB); Does not Compile.
m2(listO); Does not Compile.

Ques: 276

public class NumberNames{


private HashMap<String,Integer> map= new HashMap<String,Integer>();
public void put(String name,int value){
map.put(name,value);
}
public Set<String> getNames() {
return map.keySet();
}
}
Ques: 277

java.util.SortedSet->>>>>defines base methods for an ordered set..

java.util.Arrays->>>>>provide array manipulation utilities..

java.util.Iterator-->>>>>>defines methods for leniar access to a collection...

java.util.TreeSet->>>>> provide concrete implementaion of an ordered set..

java.util.Collection->>>>>defines base methods for all collection objects...

Ques: 278

BufferedReader reader = new BufferedReader(new FileReader("in"));

PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("out")));

Ques: 279

enum Element
{
EARTH,WIND,FIRE{
public String info(){
return "Hot";
}
};
public String info()
{
return "element";
}
}

Ques: 280

run() java.lang.Thread
wait() java.lang.Object
notify() java.lang.Object
sleep() java.lang.Thread
start() java.lang.Thread
join() java.lang.Thread
Ques: 281

public class Test {


private Boolean isReady = false;
public synchronized void produce() {
isReady = true;
notifyAll();
}
public synchronized void consume() {
while ( ! isReady ) {
try {
wait();
}catch(Exception ex) {}
}
isReady = false;
}
}

Ques: 282

public boolean doesFileExist(String []dirs,String filename)


{
String path="";

for(String dir:dirs)
{

path=path+File.separator+dir;

}
System.out.println(path);
File file = new File(path,filename);
return file.exists();
}
}

Ques: 283

(temp=buffReader.readLine())!=null

(IOException e){

Ques: 284

while(scanner.hasnext)
{
if(scanner.hasnextboolean)
{
s.o.p(scanner.nextboolean);
}
else
scanner.next;
}

Ques: 285

Dog is a Animal

Forest has a Tree

Rectangle has a side

Java Book is a programming Book

Ques: 286

Refractor this class to use generics without changing the cods’s behavior.
import java.util.*;
public class TestGenericConversion {
public static void main(String[] args) {
List<String> list = new LinkedList<String>();
list.add(“one”);
list.add(“two”);
System.out.print(list.get(0).length());
}

Ques: 287

public class GenericB< T extends Pet> {


public T foo;
public void setFoo ( T foo) {
this.foo=foo;
}
public T getFoo() {
return foo;
}
public static void main(String[] args) {
GenericB<Cat> bar = new GenericB< Cat> ();
bar.setFoo(new Cat());
Cat c= bar.getFoo();
}
}
interface Pet{ }

class Cat implements Pet { }

You might also like