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)
-
import com.sun.scjp.Geodetics;
public class TerraCarta{
public double halfWay(){
return Geodetics.DIAMETER/2.0;
}
}
-
import static com.sun.scjp.Geodetics;
public class TerraCarta{
public double halfWay(){
return DIAMETER/2.0;
}
}
-
import static com.sun.scjp.Geodetics.*;
public class TerraCarta{
public double halfWay(){
return DIAMETER/2.0;
}
}
-
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)
-
public class Session implements Runnable,Clonable {
public void run();
public Object clone();
}
-
public class Session extends Runnable,Clonable {
public void run() {
/* do something */
}
public Object clone(){
/* make a copy */
}
}
-
public class Session implements Runnable, Clonable {
public void run() {
/* do something */
}
public Object clone(){
/* make a copy */
}
}
-
public abstract class Session implements Runnable, Clonable {
public void run() {
/* do something */
}
public Object clone(){
/* make a copy */
}
}
-
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 ?
- process(bytes);
- BitUtils.process(bytes);
- app.BitUtils.process(bytes);
- util.BitUtils.process(bytes);
- import util.BitUtils.*; process(bytes);
- 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 ?
- Inconceivable !
- Dizzying intellect !
- The code runs with no output
- An exception is thrown at runtime
- 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 ?
- Compilation of class A fails
- Line 4 prints the value 3 to System.out
- Line 4 prints the value 1 to System.out
- A runtime error occurs when line 1 executes
- 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 ?
- Compilation fails
- An exception is thrown at runtime
- The variable first is set to null
- 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 ?
- s 14
- s 16
- s 10
- Compilation fails
- 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 ?
- 42
- 420
- 462
- 42042
- Compilation fails
- 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 ?
- It is true that j==i
- It is false that j==i
- An exception is thrown at runtime
- 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 ?
- 5,6
- 5,5
- 6,5
- 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 ?
- public interface B extends A
- public interface B implements A
- public interface B instanceOf A
- 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 ?
- Mr. John Doe
- An exception is thrown at runtime.
- Compilation fails because of an error in line 12
- Compilation fails because of an error in line 15
- 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 ?
- any class
- only the Target class
- any class in the test package
- 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 ?
- for(Color c:Color.values())
- for(Color c=RED;c<=BLUE;c++)
- for(Color c;c.hasNext();c.next())
- for(Color c=Color[0];c<=Color[2];c++)
- 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 :
- It must be possible to create instance of Alpha and Beta from outside the packages in which they are defined.
- When an object of type Alpha (or any potential subclass of Alpha) has been created, the instance variable alpha may never be changed.
- 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 ?
- Line 4 of class Target can be changed to return i++;
- Line 2 of class Target can be changed to private int i=1;
- Line 3 of class Target can be changed to private int addOne(){
- 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 ?
- collie
- harrier
- Compilation fails.
- collie harrier
- 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;
}
}
- Compilation fails.
- ClassC is displayed
- The code runs with no output
- 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)
- import sun.scjp.Color.*;
- import static sun.scjp.Color.*;
- import sun.scjp.Color;
import static sun.scjp.Color.*;
-
import sun.scjp.*;
import static sun.scjp.Color.*;
-
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)
- Color skyColor=BLUE;
- Color treeColor=Color.GREEN;
- Color purple=new Color(0xff00ff)));
- if(RED.getRGB()<BLUE.getRGB()){}
- Color purple=Color.BLUE+Color.RED;
- 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 ?
- MyColor skyColor=BLUE;
- MyColor treeColor=MyColor.GREEN;
- if(RED.getRGB()<BLUE.getRGB()){}
- Compilation fails due to other error(s) in the code.
- MyColor purple=new MyColor(0xff00ff);
- 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 ?
- Canada
- null Canada
- Canada null
- Canada Canada
- Compilation fails due to an error on line 15.
- 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.)
-
interface Base2 implements Base{}
-
abstract class Class2 extends Base{
public boolean m1(){return true;}}
-
abstract class Class2 implements Base{}
-
abstract class Class2 implements Base{
public boolean m1(){return(true);}}
-
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)
-
public abstract class Canine {
public Bark speak();
}
-
public abstract class Canine {
public Bark speak(){ }
}
-
public class Canine {
public abstract Bark speak();
}
-
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.)
- "X extends Y" is correct if and only if X is a class and Y is an interface.
- "X extends Y" is correct if and only if X is an interface and Y is a class.
- "X extends Y" is correct if X and Y are either both classes or both interfaces.
- "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 ?
- woof burble
- multiple compilation errors
- Compilation fails due to an error on line 12
- Compilation fails due to an error on line 13
- Compilation fails due to an error on line 14
- 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)
- The code compiles
- if only line 11 is removed the code compiles.
- if only line 13 is removed the code compiles.
- if only line 15 is removed the code compiles.
- if lines 11 and 13 is removed the code compiles.
- 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.
|