Q-001
public class Circle {
double radius;
public double area;
public Circle(double r){
radius=r;
}
public double getRadius(){
return radius;
}
public void setRadius(double r){
radius=r;
}
public double getArea(){
return /* ??? */;
}
public static void main(String[] args) {
Circle c1=new Circle(17.4);
c1.area=Math.PI * c1.getRadius() * c1.getRadius();
}
}
This class is poorly encapsulated. You need to change the circle class to compute and return the area instead.
Which two modifications are nessary to ensure that the class is being properly encapsulated?
A) Remove the area field.
B) Change the getArea() method as follows:
public double getArea(){return Math.PI*radius*radius;}
C) Add the following method:
public void updatgeArea(){ area = Math.PI*radius*radius;}
D) Change the access modifier to the setRadius() method to be protected.
Answer : A, B
Q-002
Given the code fragment:
public class Test {
public static List data=new ArrayList();
//insert code here
{
for(String x:strs){
data.add(x);
}
return data;
}
public static void main(String[] args) {
String[] d={"a", "b", "c"};
update(d);
for (String s:d){
System.out.println(s+" ");
}
}
}
Which code fragment, when insertd at //insert code here, enables the code to compile and print a b c ?
A) List update[String[] strs)
B) static ArrayList update(String[] strs)
C) static List update(String[] strs)
D) static void update(String[] strs)
E) ArrayList static update(String[] strs)
Answer : C
Q-003
Given
public class Test1 {
public static void main(String[] args) {
StringBuilder message=new StringBuilder("hello java");
int pos=0;
try{
for(pos=0;pos<12;pos++){
switch(message.charAt(pos)){
case 'a':
case 'e':
case 'o':
String uc=Character.toString(message.charAt(pos)).toUpperCase();
message.replace(pos, pos+1, uc);
}
}
}
catch(Exception e){
}
System.out.println(message);
}
}
What is the result?
A) hEllO jAvA!
B) hello java!
C) Out of limits hEllo jAvA!
D)out of limits
Answer : A
Q-004
Given:
class Star{
public void doStuff(){
System.out.println("Twinking Star");
}
}
interface Universe{
public void doStuff();
}
class Sun extends Star implements Universe{
public void doStuff(){
System.out.println("Shining Sun");
}
}
public class Test1 {
public static void main(String[] args) {
Sun obj2=new Sun();
Star obj3=obj2;
((Sun)obj3).doStuff();
((Star)obj2).doStuff();
((Universe)obj2).doStuff();
}
}
What is the result?
A)
Shining Sun
Shining Sun
Shining Sun
B)
Shining Sun
Twinking Star
Shining Sun
C)Compilation fails.
D) A classCastException is thrown at runtime.
Answer : A
Sun如果沒有實作doStuff(), 則會以Star的doStuff()取代,
Sun如果實作了doStuff(), 則Star的doStuff()就被覆醘不見了
Q-005
Which code fragment causes a compilation error?
A) float flt=100F;
B) float flt=(float)1_11.00;
C) float flt=flt=100;
D) double y1=203.22;
float flt=y1;
E) int y2=100;
float flt=(float)y2;
Answer : D
Q-006
Given :
class Alpha{
public String[]main=new String[2];
Alpha(String[] main){
for (int ii=0; ii < main.length; ii++){
this.main[ii]=main[ii]+5;
}
}
public void main(){
System.out.println(main[0]+main[1]);
}
}
public class Test {
public static void main(String[] args) {
Alpha main=new Alpha(args);
main.main();
}
}
And the commands:
javac Test.java
java Test 1 2
what is the result?
A) 1525
B) 13
C) An compilation fails.
D) An exception is thrown at runtime.
E) The program fails to execute a runtime error.
Answer : A
Q-007
Given:
abstract class X{
public abstract void methodX();
}
interface Y{
public void methodY();
}
Which two code fragments are valid?
A) class Z extends X implements y{
public void methodZ();
}
B) abstract class Z extends X implements Y{
public void methodZ();
}
C) class Z extends X implements Y{
public void methodX(){}
}
D) abstract class Z extends X implements Y{
}
E) class Z extends X implements Y{
public void methodY(){}
}
Answer : B, D
Q-008
Given :
class A{}
class B{}
interface X{}
interface Y{}
Which two definitions of class C are valid?
A) class C extends A implements X{}
B) class C implements Y extends B{}
C) class C extends A,B{}
D) class C implements X, Y extends B{}
E) class C extends B implements X, Y{}
Answer : A, E
Q-009
Give :
package handy.dandy;
public class Keystroke{
public void tyepExclamation(){
System.out.println("1");
}
}
And
1. package handy;
2. public class Greet{
3. public static void main(String[] args){
4. String greeting="Hello";
5. System.out.print(greeting);
6. Keystroke stroke=new Keystroke();
7. stroke.typeExclamation();
8. }
9. }
What three modifications, make independently, enable the code to compile and run?
A) line 6 replace with handy.dandy.Keystroke stroke=new Keystroke();
B) line 6 replaced with handy.*.Keystroke stroke=new Keystroke();
C) line 6 replace with handy.dandy.Keystroke stroke=new handy.dandy.Keystroke();
D) import handy.*; added before line1
E) import handy.*; added after line1
F) import handy.dandy.Keystroke;added after line1
G) import handy.dandy.Keystroke.typeExclamation(); added before line 1
Answer : C, E, F
Q-010
Given the code fragment
String color="tea";
switch(color){
case "Red" :
System.out.println("Found Red");
case "Blue" :
System.out.println("Found Blue");
break;
case "Tea" :
System.out.println("Found Tea");
break;
default:
System.out.println("Found Default");
}
What is the result?
A) Found Red
Found Default
B) Found Tea
C) Found Tea
Found Default
D) Found Default
Answer : D
Q-011
Given
public class App{
public static void main(String[] args){
int i=10;
int j=20;
int k=j+=i/5;
System.out.print(i+":"+j+":"+k);
}
}
What is the result?
A) 10:22:20
B) 10:22:22
C) 10:22:6
D) 10:30:6
Answer : B
Q-012
Given the code fragment
if(aVar++<10){
System.out.println(aVar+"Hello world!");
}
else{
System.out.println(aVar+"Hello Universe!");
}
What is the result if the integer aVar is 9?
A) 10 Hello world!
B) 10 Hello Universe!
C) 9 Hello world!
D) Compilation fails.
Answer : A
Q-013
Given the fragment
13. String[][] arra=new String[3][];
14. arra[0]=new String[]{"rera", "lity"};
15. arra[1]=new String[]{"apple","berry","cherry", grapes"};
16. arra[2]=new String[]{"beans","carrot","potato")};
17.
18. //insert code fragment here
Which code fragment when inserted at line 18, enable the code to successfully change arra elements to uppercase?
A) for (int i=0;i< arra.length;i++){
for (int j=0;j< arra[i].length;j++){
arra[i][j]=arra[i][j].toUpperCase();
}
}
B) for (int i=0;i< 3;i++){
for (int j=0;j< 4;j++){
arra[i][j]=arra[i][j].toUpperCase();
}
}
C) for (int i:arra.length){
for (String x:arra){
arra[i]=x.toUpperCase();
}
}
Answer : A
Q-014
Given :
public class Test {
public static void main(String[] args) {
Test ts=new Test();
System.out.print(isAvailable+" ");
isAvailable=ts.doStuff();
System.out.println(isAvailable);
}
public static boolean doStuff(){
return !isAvailable;
}
static boolean isAvailable=false;
}
What is the result?
A) true true
B) true false
C) false true
D) false false
E) Compilation fail.
Answer : C
Q-015
Given the code fragment
int[] lst={1,2,3,4,5,4,3,2,1};
int sum=0;
for (int frnt=0, rear=lst.length-1;frnt=5;frnt++, rear--){
sum=sum+lst[frnt]+lst[rear];
}
System.out.print(sum);
What is the result?
A) 20
B) 25
C) 29
D) Compilation fails.
E) An ArrayIndexOutOfBoundException is thrown at runtime.
Answer : A
Q-016
Given
public class Test {
int sum=0;
public void doCheck(int number){
if(number%2==0){
break;
}
else{
for(int i=0;i<number;i++){
sum+=i;
}
}
}
public static void main(String[] args) {
Test obj=new Test();
System.out.println("Red "+obj.sum);
obj.doCheck(2);
System.out.println("Orange "+obj.sum);
obj.doCheck(3);
System.out.println("Green "+obj.sum);
}
}
What is the result?
A) Red 0
Orage 0
Green 3
B) Red 0
Orage 0
Green 6
C) Red 0
Orage 1
Green 4
D) Compilation fails.
Answer : D
若將break;改為return; 則答案為 A
Q-017
Given
public class MyForloop {
public static void main(String[] args) {
int[]x={6,7,8};
for (int i:x){
System.out.print(i+" ");
i++;
}
}
}
What is the result?
A) 6 7 8
B) 7 8 9
C) 0 1 2
D) 6 8 10
E) Compliation fails.
Answer : A
Q-018
Given
public class Jump {
static String args[]={"lazy", "lion", "is", "always"};
public static void main(String[] args) {
System.out.println(args[1]+" "+args[2]+" "+args[3]+" jumping");
}
}
And the commands:
javac Jump.java
java Jump crazy elephant is always
What is the result?
A) lazy lion is jumping
B) Lion is always jumping
C) crazy elephant is jumping
D) elephant is always jumping
E) Compilation fails.
Answer : D
Q-019
Given
public class Test2 {
public static void doChange(int[] arr){
for(int pos=0;pos<arr.length;pos++){
arr[pos]=arr[pos]+1;
}
}
public static void main(String[] args) {
int[] arr={10,20,30};
doChange(arr);
for(int x:arr){
System.out.print(x+",");
}
doChange(arr[0], arr[1],arr[2]);
System.out.print(arr[0]+","+arr[1]+","+arr[2]);
}
}
What is the result?
A) 11,21,31,11,21,31
B) 11,21,31,12,22,32
C) 12,22,32,12,22,32
D) 10,20,30,10,20,30
E) Compilation fails.
Answer : E
若將doChange(arr[0], arr[1], arr[2])改為doChange(arr), 則答案為B
Q-020
Given
class Caller{
public static void call(){
System.out.println("Called");
}
}
public class TestCall {
public static void main(String[] args) {
//insert code here
}
}
Which two snippets inserted independently at //insert code here, enable the code to compile and run?
A) new Caller().call();
B) call();
C) Caller.call();
D) Caller c;
c.call();
E) Caller().call();
Answer : A, C