jobs4timesLogo jobs4timesLogo

Declarations and Access Control

Question : 1

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

Question : 2

Given :
package com.sun.scjp;

public class Geodetics{
public static final double DIAMETER=12756.32; //kilometers
}
Which two correctly access the DIAMETER member of the Geodetics ? (Choose two)
  1. import com.sun.scjp.Geodetics;
    
    public class TerraCarta{
     public double halfWay(){
      return Geodetics.DIAMETER/2.0;
     }
    }
    
  2. import static com.sun.scjp.Geodetics;
    
    public class TerraCarta{
     public double halfWay(){
      return DIAMETER/2.0;
     }
    }
    
  3. import static com.sun.scjp.Geodetics.*;
    
    public class TerraCarta{
     public double halfWay(){
      return DIAMETER/2.0;
     }
    }
    
  4. import static com.sun.scjp;
    
    public class TerraCarta{
     public double halfWay(){
      return DIAMETER/2.0;
     }
    }
    

Answer : A, C

Question : 3

Place the code elements in order so that the resulting Java source file will compile correctly, resulting in a class called com.sun.cert.AddressBook.
Source File :

 1 st 
 2 nd
 3 rd
 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;
}

Question : 4



Which two classes correctly implement both the java.lang.Runnable and the java.lang.Clonable interfaces ? (Choose two)
  1. public class Session implements Runnable,Clonable {	
    public void run();
    public Object clone();
    }
    
  2. public class Session extends Runnable,Clonable {	
    public void run() {
     /* do something */
    }
    public Object clone(){
      /* make a copy */  
    }
    
    }
    
  3. public class Session implements Runnable, Clonable {	
    public void run() {
     /* do something */
    }
    public Object clone(){
      /* make a copy */
    }
    
    }
    
  4. public abstract class Session implements Runnable, Clonable {	
    public void run() {
     /* do something */
    }
    public Object clone(){
      /* make a copy */
    }
    
    }
    
  5. public class Session implements Runnable,implements Clonable {	
    public void run() {
     /* do something */
    }
    public Object clone(){
      /* make a copy */ 
    }
    
    }
    

Answer : C, D

Question : 5

Given classes defined in two different filters :
package util;

public class BitUtils {	
private static void process(byte[] bytes){
	//..........
}
	
}


package app;

public class SomeApp {	
public static void main(String[] args){
 byte[] bytes=new byte[256];
   
  // insert code here
}
}
What is required at insert code here in class SomeApp to use the process method of BitUtils ?
  1. process(bytes);
  2. BitUtils.process(bytes);
  3. app.BitUtils.process(bytes);
  4. util.BitUtils.process(bytes);
  5. import util.BitUtils.*; process(bytes);
  6. SomeApp cannot use the process methods in BitUtils.

Answer : F

Question : 6

Given :
class Cup { }
class PoisionCup extends Cup{ }

..................

public void takeCup(Cup c){
 if(c instanceof PoisionCup){
  System.out.println("Inconceivable !");	 
 }
 else if(c instanceof Cup){
  System.out.println("Dizzying intellect !");
 }
 else {
  System.exit(0);
 }
	
}


And the execution of the statements :

Cup cup=new PoisionCup();
takeCup(cup);
What is the output ?
  1. Inconceivable !
  2. Dizzying intellect !
  3. The code runs with no output
  4. An exception is thrown at runtime
  5. Compilation fails

Answer : A

Question : 7

Click the Exhibit button
public class A{
private int counter=0;
public static int getInstanceCount(){
	return counter;
}
public A(){
	counter++;
}

}

Given this code from Class B :
1 A a1=new A();
2 A a2=new A();
3 A a3=new A();
4 System.out.println(A.getInstanceCount());
What is the result ?
  1. Compilation of class A fails
  2. Line 4 prints the value 3 to System.out
  3. Line 4 prints the value 1 to System.out
  4. A runtime error occurs when line 1 executes
  5. Compilation fails because of an error on Line 4

Answer : A

Question : 8

Given :
String[] elements={"for","tea","too"};
String first=(elements.length > 0)?elements[0]:null;
What is the result ?
  1. Compilation fails
  2. An exception is thrown at runtime
  3. The variable first is set to null
  4. The variable first is set to elements[0]

Answer : D

Question : 9

Given :
interface DeclareStuff{
public static final int EASY=3;
void doStuff(int t);	
}

public class TestDeclare implements DeclareStuff {	
public static void main(String []args){
int x=5;
new TestDeclare().doStuff(++x);	
}

void doStuff(int s) {
s +=EASY + ++s;
System.out.println("s "+s);
}

}
What is the result ?
  1. s 14
  2. s 16
  3. s 10
  4. Compilation fails
  5. An exception is thrown at runtime

Answer : D

Question : 10

Given :
public class TestString {	
public static void main(String []args){
String str="420";
str+=42;
System.out.println(str);
}

}
What is the Output ?
  1. 42
  2. 420
  3. 462
  4. 42042
  5. Compilation fails
  6. An exception is thrown at runtime

Answer : D

Question : 11

10 public class Converter {	
11 public static void main(String[] args){
12 Integer i=args[0];
13 int j=12;
14 System.out.println("It is "+(j==i)+" that j==i");
15 }
16 }
What is the result when the programmer attempts to compile the code and run it with the command
java Converter 12 ?
  1. It is true that j==i
  2. It is false that j==i
  3. An exception is thrown at runtime
  4. Compilation fails because of an error in Line 12

Answer : D

Question : 12

Given :
10 int x=0;
11 int y=10;
12 do{
13  y--;
14  ++x; 
15 }while(x<5);
16 System.out.println(x+","+y);
What is the result ?
  1. 5,6
  2. 5,5
  3. 6,5
  4. 6,6

Answer : B

Question : 13

Given :
public interface A{
 String DEFAULT_GREETING="Hello World";
 public void method();
}
A programmer wants to create an interface called B that has A as its parent. Which interface declaration is correct ?
  1. public interface B extends A
  2. public interface B implements A
  3. public interface B instanceOf A
  4. public interface B inheritsFrom A

Answer : A

Question : 14

Given :
11 public enum Title {
12 MR("Mr."),MRS("Mrs."),MS("Ms.");
13 private final String title;
14 private Title(String t){ title=t; }
15 public String format(String last,String first){
16  return title+" "+first+" "+last;
17 }
18 }
19 public static void main(String[] args){
20 System.out.println(Title.MR.format("Doe", "John"));
21 }
What is the result ?
  1. Mr. John Doe
  2. An exception is thrown at runtime.
  3. Compilation fails because of an error in line 12
  4. Compilation fails because of an error in line 15
  5. Compilation fails because of an error in line 20

Answer : A

Question : 15

Given :
package test;

class Target{
public String name="hello";
}
What can directly access and change the value of the variable name ?
  1. any class
  2. only the Target class
  3. any class in the test package
  4. any class that extends Target

Answer : C

Question : 16

1 public class Ball {
2 public enum Color{RED,GREEN,BLUE};	
3 public void foo(){
4 //insert code here
5 { System.out.println(c); }
6 }
7 }
Which code inserted at Line 4 causes the foo method to print RED, GREEN, and BLUE ?
  1. for(Color c:Color.values())
  2. for(Color c=RED;c<=BLUE;c++)
  3. for(Color c;c.hasNext();c.next())
  4. for(Color c=Color[0];c<=Color[2];c++)
  5. for(Color c=Color.RED;c<=Color.BLUE;c++)

Answer : A

Question : 17 click the task button

Insert six modifiers into the code such that it meets all of these requirements :
  1. It must be possible to create instance of Alpha and Beta from outside the packages in which they are defined.
  2. When an object of type Alpha (or any potential subclass of Alpha) has been created, the instance variable alpha may never be changed.
  3. The value of the instance variable alpha must always be "A" for objects of the Alpha.
Code :

package alpha;
xxxxxx class Alpha {
xxxxxx String alpha;
xxxxxx Alpha(){ this("A"); }
xxxxxx Alpha(String a){ alpha=a; }
}

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


Modifiers :
private
protected
public
Solution :
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{
private Beta(String a){ super(a); }
}

Question : 18

Given :
1 public class Target{
2 private int i=0;
3 public int addOne(){
4  return ++i;
5 }
6 }

And:

1 public class Client{
2 public static void main(String[] args){
3 System.out.println(new Target().addOne());
4 }
5 }
Which change can you make to Target without affecting Client ?
  1. Line 4 of class Target can be changed to return i++;
  2. Line 2 of class Target can be changed to private int i=1;
  3. Line 3 of class Target can be changed to private int addOne(){
  4. Line 2 of class Target can be changed to private Integer i=0;

Answer : D

Question : 19 Click the Task button

Replace two of the Modifiers that appear in the Single class to make the code compile.
Note : Three modifiers will not be used and four modifiers in the code will return unchanged.
Code :

public class Single{
private static Single instance;
public static Single getInstance(){
 if(instance==null) instance=create();
 return instance;
}
private Single() { }
protected Single create(){ return new Single();}
}

class SingleSub extends Single{
}


Modifiers :
final
protected
private
abstract
static
Solution :
public class Single{
private static Single instance;
public static Single getInstance(){
 if(instance==null) instance=create();
 return instance;
}
protected Single() { }
static Single create(){ return new Single();}
}

class SingleSub extends Single{
}

Question : 20

Given :
public class Test{
public enum Dogs{collie,harrier};
public static void main(String[] args){
 Dogs myDogs=Dogs.collie;
 switch(myDogs){
 case collie:
	System.out.println("collie");
 case harrier:
	System.out.println("harrier");
 }
}
}
What is the result ?
  1. collie
  2. harrier
  3. Compilation fails.
  4. collie harrier
  5. An exception is thrown at runtime.

Answer : D

Question : 21 Click the Exhibit button.

Given :
ClassA a=new ClassA();
a.methodA();
What is the result ?
public class ClassA {
 public void methodA(){
  ClassB classB=new ClassB();
  classB.getValue();
 }
}

And :

class ClassB{
public ClassC classC;
 public String getValue() {
  return classC.getValue();
 }
}

And :

class ClassC{
public String value;
 public String getValue(){
  value="ClassB";
  return value;
 }
}
  1. Compilation fails.
  2. ClassC is displayed
  3. The code runs with no output
  4. An exception is thrown at runtime.

Answer : D

Question : 22 Click the button.

Please code fragments into position so the output is :

The quantity is 420

place here update(int quantity,int adjust){
 place here
}
public void callUpdate(){
 int quant=100;
 place here
 System.out.println("The quantity is "+quant);
}

Code Fragments :

1 public int

2 public void

3 quantity=quantity+adjust;

4 quant=update(quant,320);

5 update(quant,320);

6 quantity=quantity+adjust;
  return quantity;
Solution :
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);
}

Question : 23

Given :
1 package sun.scjp;
2 public enum Color { RED,GREEN,BLUE }

1 package sun.beta;
2 //insert code here
3 public class Beta{
4 Color g=GREEN;
5 public static void main(String[] args){
6  System.out.println(GREEN);
7 }
8 }
Which two code fragments, inserted individually at Line 2 of the Beta declaration, will allow this code to compile ? (Choose two)
  1. import sun.scjp.Color.*;
  2. import static sun.scjp.Color.*;
  3. import sun.scjp.Color; import static sun.scjp.Color.*;
  4. import sun.scjp.*; import static sun.scjp.Color.*;
  5. import sun.scjp.Color; import static sun.scjp.Color.GREEN;

Answer : C, E

Question : 24

Given :
11 public class Fabric{
12 public enum Color{
13 RED(0xff0000),GREEN(0x00ff00),BLUE(0x0000ff);
14 private final int rgb;
15 Color(int rgb){this.rgb=rgb;}
16 public int getRGB(){return rgb;}
17 };
18 public static void main(String[] args){
19 //insert code here
20 }
21 }
Which two code fragments,inserted independently at line 19, allow the Fabric class to compile ? (Choose two)
  1. Color skyColor=BLUE;
  2. Color treeColor=Color.GREEN;
  3. Color purple=new Color(0xff00ff)));
  4. if(RED.getRGB()<BLUE.getRGB()){}
  5. Color purple=Color.BLUE+Color.RED;
  6. if(Color.RED.ordinal()<Color.BLUE.ordinal()){}

Answer : B, F

Question : 25

Given :
10 public class RainBow{
11 public enum MyColor{
12 RED(0xff0000),GREEN(0x00ff00),BLUE(0xff0000);
13 private final int rgb;
14 MyColor(int rgb){this.rgb=rgb;}
15 public int getRGB(){return rgb;}
16 };
17 public static void main(String[] args){
18 //insert code here
19 }
20 }
Which code fragment, inserted at line 18, allows the Rainbow class to compile ?
  1. MyColor skyColor=BLUE;
  2. MyColor treeColor=MyColor.GREEN;
  3. if(RED.getRGB()<BLUE.getRGB()){}
  4. Compilation fails due to other error(s) in the code.
  5. MyColor purple=new MyColor(0xff00ff);
  6. MyColor purple=MyColor.BLUE+MyColor.RED;

Answer : B

Question : 26

Given :
10 class Money{
11 private String country="Canada";
12 public String getC(){return country;}
13 }
14 class Yen extends Money{
15 public String getC(){return super.country;}
16 }
17 public class Euro extends Money{
18 public String getC(int x){return super.getC();}
19 public static void main(String[] args){
20 System.out.println(new Yen().getC()+" "+new Euro().getC());
21 }
22 }
What is the result ?
  1. Canada
  2. null Canada
  3. Canada null
  4. Canada Canada
  5. Compilation fails due to an error on line 15.
  6. Compilation fails due to an error on line 18.

Answer : E

Question : 27

Given the following.
interface Base{
boolean m1();
byte m2(short s);
}
Which code fragments will compile ? (Choose all that apply.)
  1. interface Base2 implements Base{}
    
  2. abstract class Class2 extends Base{
    public boolean m1(){return true;}}
    
  3. abstract class Class2 implements Base{}
  4. abstract class Class2 implements Base{
    public boolean m1(){return(true);}}
    
  5. class Class2 implements Base{
    boolean m1(){return false;}
    byte m2(short s){return 42;} }
    

Answer : C, D

  • A is incorrect because interfaces don't implement any thing.
  • B is incorrect because classes don't extend interfaces.
  • C is correct because an abstract class doesn't have to implement any or all of its interface's methods.
  • D is correct because the method is correctly implemented.
  • E is incorrect because interface methods are implicitly public, so the methods being implemented must be public.

Question : 28


Which declare a compilable abstract class ? (Choose all that apply)
  1. public abstract class Canine { 
    	public Bark speak();
    }
    
  2. public abstract class Canine { 
    	public Bark speak(){ } 
    }
    
  3. public class Canine { 
     public abstract Bark speak(); 
    }
    
  4. public class Canine abstract { 
     public abstract Bark speak(); 
    }
    

Answer : B

  • A is incorrect because abstract methods must be marked as such.
  • B is correct. abstract classes don't have to have any abstract methods.
  • C is incorrect because you can't have an abstract method unlesss the class is abstract.
  • D is incorrect because the keyword abstract must come before the class name.

Question : 29


What is true ? (Choose all that apply.)
  1. "X extends Y" is correct if and only if X is a class and Y is an interface.
  2. "X extends Y" is correct if and only if X is an interface and Y is a class.
  3. "X extends Y" is correct if X and Y are either both classes or both interfaces.
  4. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces.

Answer : C

  • A is incorrect because classes implement interfaces, they don't extend them.
  • B is incorrect because interfaces only "inherit from" other interfaces.
  • C is correct.
  • D is incorrect based on the preceding rules.

Question : 30

Given :
11 enum Animals{
12 DOG("woof"),CAT("meow"),FISH("burble");
13 String sound;
14 Animals(String s){sound=s;}
15 }
16 public class TestEnum{
17 static Animals a;
18 public static void main(String[] args){
19 System.out.println(a.DOG.sound+" "+a.FISH.sound);
20}
21}
what is the result ?
  1. woof burble
  2. multiple compilation errors
  3. Compilation fails due to an error on line 12
  4. Compilation fails due to an error on line 13
  5. Compilation fails due to an error on line 14
  6. Compilation fails due to an error on line 19

Answer : A

  • A is correct; enums can have constructors and variables.
  • B,C,D,E and F are incorrect; these lines all use correct syntax.

Question : 31

Given :
11 enum A { A }
12 class E2{
13 enum B { B }
14 void C(){
15 enum D { D }
16 }
17 }
Which statements are true ? (Choose all that apply)
  1. The code compiles
  2. if only line 11 is removed the code compiles.
  3. if only line 13 is removed the code compiles.
  4. if only line 15 is removed the code compiles.
  5. if lines 11 and 13 is removed the code compiles.
  6. if lines 11,13 and 15 are removed the code compiles.

Answer : D, F

  • D and F are correct. Line 15 is the only line that will not compile, because enums cannot be local to a method.
  • A,B,C and E are incorrect based on the above.
BACK