Q-061
public class Test{
public static void main(String[] args) {
int ar1[]={2,4,6,8};
int ar2[]={1,3,5,7,9};
ar2=ar1;
for (int e2:ar2){
System.out.print(" "+e2);
}
}
}
What is the result?
A) 2 4 6 8
B) 2 4 6 8 9
C) 1 3 5 7
D) 1 3 5 7 9
E) Compliation fails.
F) An exception is thrown at runtime.
Answer : A
Q-062
public class Vowel{
private char var;
public static void main(String[] args) {
char var1='a';
char var2=var1;
var2='e';
Vowel obj1=new Vowel();
Vowel obj2=obj1;
obj1.var='i';
obj2.var='o';
System.out.println(var1+", "+var2);
System.out.println(obj1.var+", "+obj2.var);
}
}
What is the result?
A) a, e
i, o
B) a, e
o, o
C) e, e
i, o
D) e, e
o, o
Answer : B
Q-063
class MissingInfoException extends Exception{}
class AgeOutOfRangeException extends Exception{}
class Candidate{
String name;
int age;
Candidate(String name, int age)throws Exception{
if(name==null){
throw new MissingInfoException();
}
else if (age<=10 ||age>=150){
throw new AgeOutOfRangeException();
}
else{
this.name=name;
this.age=age;
}
}
public String toString(){
return name+" age:"+age;
}
}
4. public class Test{
5. public static void main(String[] args){
6. Candidate c=new Candidate("James", 20);
7. Candidate c1=new Candidate("Williams", 32);
8. System.out.println(c);
9. System.out.println(c1);
10. }
11. }
Which change can ables the code to rint the following?
James age:20
williams age: 32
A) replacing line5 with public static void main(String[] args) throws MissingInfoException, AgeOutOfRangeException{
B) replacing line5 with public static void main(String[] args)throws Exception{
C) enclosing line 6 and line 7 with in a try block and adding:
catch(Exception e1){//code goes here}
catch(MissingInfoException e2){//code goes here}
catch(AgeOutOfRangeException e3){//code goes here}
D) enclosing line 6 and line 7 with in a try block and adding:
catch(MissingInfoException e2){//code goes here}
catch(AgeOutOfRangeException e3){//code goes here}
Answer : B
Q-064
public abstract class Shape{
private int x;
private int y;
public abstract void draw();
public void setAchor(int x, int y){
this.x=x;
this.y=y;
}
}
Which two classses use the Shape class correctly?
A) public class Circle implements Shape{
private int radius;
}
B) public abstract class Circle extends Shape{
private int radius;
}
C) public class Circle extends Shape{
private int radius;
public void draw();
}
D) public abstract class Circle implements Shape{
private int radius;
public void draw();
}
E) public class Circle extends Shape{
private int radius;
public void draw(){/*code here*/}
}
F) public abstract class Circle implements Shape{
private int radius;
public void draw(){/*code here*/}
}
Answer : B, E
Q-065
public class X{
String str="default";
X(String s){
str=s;
}
void print(){
System.out.println(str);
}
public static void main(String[] args) {
new X("hello").print();
}
}
What is the result?
A) hello
B) default
C) Compilation fails.
D) The program prints nothing.
E) An exception is thrown at runtime.
Answer : A
Q-066
public class Test{
static int speed=10;
int weight;
public static void doAnything(int x){
weight=++x;
speed++;
}
public static void main(String[] args) {
Test obj=new Test();
obj.doAnything(speed);
System.out.println(speed+" "+obj.weight);
obj.doAnything(speed);
System.out.println(speed+" "+obj.weight);
}
}
What is the result?
A) 11 11
12 12
B) 10 11
10 12
C) 10 10
10 10
D) Compilation fails.
E) An exception is thrown at runtime.
Answer : D
錯在weight=++x;
Q-067
class Alpha{
int ns;
static int s;
Alpha(int ns){
if(s < ns){
s=ns;
this.ns=ns;
}
}
void doPrint(){
System.out.println("ns="+ns+" s="+s);
}
}
public class Test{
public static void main(String[] args) {
Alpha ref1=new Alpha(50);
Alpha ref2=new Alpha(125);
Alpha ref3=new Alpha(100);
ref1.doPrint();
ref2.doPrint();
ref3.doPrint();
}
}
What is the resule?
A) ns=50 s=125
ns=125 s=125
ns=100 s=125
B) ns=50 s=125
ns=125 s=125
ns=0 s=125
C) ns=50 s=50
ns=125 s=125
ns=100 s=100
D) ns=50 s=50
ns=125 s=125
ns=0 s=125
Answer : B
Q-068
Which two statements are true for a two-dimensional array of primitive data type?
A) it can not contain elements of different types.
B) The length of each dimension must be the same.
C) At declaration time, the number of elements of the array in each dimension must be specified.
D) All methods of the class object may be invoked on the two-dimensional array.
Answer : A, D
Q-069
Q-070
Q-071
Q-072
Q-073
Q-074
Q-075
Q-076
Q-077
Q-078
Q-079
Q-080