Java经典模拟题(onlyfortrainning)(2)
2012-12-26来源/作者:卫凯点击次数:936
1 1. which modifier should be applied to a method for the lock of object this to be obtained prior to execution any of the method body?
a. synchronizedb. abstractc. finald. statice. public
12. the following code is entire contents of a file called example.java,causes precisely one error during compilation:
1)class subclass extends baseclass{
2) }
3)class baseclass(){
4) string str;
5) public baseclass(){
6) system.out.println(“ok”);}
7) public baseclass(string s){
str=s;}
9)}
10)public class example{
11) public void method(){
12) subclass s=new subclass(“hello”);
13) baseclass b=new baseclass(“world”);
14) }
15) }
which line would be cause the error?
a. 9b. 10c. 11d.12
13. which statement is correctly declare a variable a which is suitable for refering to an array of 50 string empty object?
a. string [] ab. string a[]c. char a[][]d. string a[50]f. object a[50]
14. give the following java source fragment:
//point x
public class interesting{
//do something
}
which statement is correctly java syntax at point x?
a. import java.awt.*;b.package mypackage;c. static int pi=3.14
d. public class myclass{//do other thing…}e. class myclass{//do something…}
15. give this class outline:
class example{
private int x;
//rest of class body…
}
assuming that x invoked by the code java example, which statement can made x be directly accessible in main() method of example.java?
a. change private int x to public int xb. change private int x to static int x
c. change private int x to protected int xd. change private int x to final int x
16. a class design requires that a member variable should be accessible only by same package, which modifier word should be used?
a. protectedb. publicc. no modifierd. private
17. which modifier should be applied to a declaration of a class member variable for the value of variable to remain constant after the creation of the object?
18. which is corrected argument of main() method of application?
a. string argsb. string ar[]c. char args[][]d. stringbuffer arg[]
19. “the employee object is a person, an employee has appointment store in a vector, a hire date and a number of dependent”
short answer: use shortest statement declare a class of employee.
20. give the following class definition inseparate source files:
public class example{
public example(){//do something}
protected example(int i){//do something}
protected void method(){//do something}
}
public class hello extends example{//member method and member variable}
which methods are corrected added to the class hello?
a. public void example(){}b. public void method(){}
c. protected void method(){}d. private void method(){}
21. float s=new float(0.9f);
float t=new float(0.9f);
double u=new double(0.9);
which expression’s result is true?
a. s= =tb. s.equals(t)c. s= =ud. t.equals(u)
22. give following class:
class aclass{
private long val;
public aclass(long v){val=v;}
public static void main(string args[]){
aclass x=new aclass(10l);
aclass y=new aclass(10l);
aclass z=y;
long a=10l;
int b=10;
}
}
which expression result is true?
a. a= =b;b. a= =x;c. y= =z;d. x= =y;
e. a= =10.0;
23. string s=”example string”;
which operation is legal?
a. s>;>;>;=3;b. int i=s.length();c. s[3]=“x”;
d. string short_s=s.trim();e. string t=“root”+s;
24. what happens when you try to compile and run the following program?
class mystery{
string s;
public static void main(string[] args){
mystery m=new mystery();
m.go();
}
void mystery(){
s=”constructor”;
}
void go(){
system.out.println(s);
}
}
a.this code will not compile
b.this code compile but throws an exception at runtime
c.this code runs but nothing appears in the standard output
d.this code runs and “constructor” in the standard output
e.this code runs and writes ”null” in the standard output
25. what use to position a button in a frame ,only width of button is affected by the frame size, which layout button well be set ?
a. flowlayout;b. gridlayout;c. north of borderlayout
d. south of borderlayoute. east or west of borderlayout
26. what use to position a button in a frame, size of button is not affected by the frame size, which layout button will be set?
a. flowlayout;b. gridlayout;c. north of borderlayout
d. south of borderlayoute. east or west of borderlayout
27. select valid identifier of java:
a. usernameb. %passwdc. 3d_gamed. $chargee. this
28. which are java keyword?
a. gotob. nullc. falsed. nativee. const
29. give the following java class:
public class example{
static int x[]=new int[15];
public static void main(string args[]){
system.out.println(x[5]);
}
}
which statement is corrected?
a.when compile, some error will occur.
b.when run, some error will occur.
c.output is zero.
d.output is null.
30. give the following java class:
public class example{
public static void main(string args[]){
static int x[] = new int[15];
system.out.println(x[5]);
}
}
which statement is corrected?
a.when compile, some error will occur.
b.when run, some error will occur.
c.output is zero.
d.output is null.
31. short answer:
the decimal value of i is 12, the octal i value is:
32. short answer:
the decimal value of i is 7, the hexadecimal i value is:
33. give the following class:
public class example{
string str=new string(“good”);
char ch[]={‘a’,’b’,’c’};
public static void main(string args[]){
example ex=new example();
ex.change(ex.str,ex.ch);
system.out.println(ex.str+”and”+ex.ch[0] +ex.ch[1] +ex.ch[2]);
}
public void change(string str,char ch[]){
str=”test ok”;ch[0]=’g’;
}
}
which is the output:
a. good and abcb. good and gbcc. test ok and abcd. test ok and gbc
34. which code fragments would correctly identify the number of arguments passed via command line to a java application, exclude the name of the class that is being invoke.
a. int count = args.length;b. int count = args.length-1;
c. int count=0; while(args[count]!=null)
count++;
d. int count=0;while
(!(args[count].equals(“”))) count++;
35. filteroutputstream is the parent class for bufferedoutputstream, dataoutputstream and printstream. which classes are valid argument for the constructor of a filteroutputstream?
a. inputstreamb. outputstreamc. file
d. randomaccessfile e. streamtokenizer
36. given this skeleton of a class currently under construction:
public class example{
int x,y,z;
public example (int a, int b) {
//lots of complex computation
x=a; y=b;
}
public example(int a, int b, int c){
// do everything the same as single argument
// version of constructor
// including assignment x=a, y=b, z=c
z=c;
}
}
what is the most concise way to code the “do everything…” part of the constructor taking two arguments?
short answer:
37. which correctly create a two dimensional array of integers?
a.int a[][] = new int[][];
b.int a[10][10] = new int[][];
c.int a[][] = new int[10][10];
d.int [][]a = new int[10][10];
e.int []a[] = new int[10][10];
38. which are correct class declarations? assume in each case that the text constitutes the entire contents of a file called fred.java?
a.public class fred{
public int x = 0;
public fred (int x){
this.x=x;
}
}
b.public class fred{
public int x = 0;
public fred (int x){
this.x=x;
}
}
c.public class fred extends mybaseclass, myotherbaseclass{
public int x = 0;
public fred(int xval){
x=xval;
}
}
d.protected class fred{
private int x = 0;
private fred (int xval){
x=xval;
}
}
e.import java.awt.*;
public class fred extends object{
int x;
private fred(int xval){
x = xval;
}
}
39. a class design requires that a particular member variable must be accessible for direct access by any subclasses of this class. but otherwise not by classes which are not members of the same package. what should be done to achieve this?
a.the variable should be marked public
b.the variable should be marked private
c.the variable should be marked protected
d.the variable should have no special access modifier
e.the variable should be marked private and an accessible method provided
a. synchronizedb. abstractc. finald. statice. public
12. the following code is entire contents of a file called example.java,causes precisely one error during compilation:
1)class subclass extends baseclass{
2) }
3)class baseclass(){
4) string str;
5) public baseclass(){
6) system.out.println(“ok”);}
7) public baseclass(string s){
str=s;}
9)}
10)public class example{
11) public void method(){
12) subclass s=new subclass(“hello”);
13) baseclass b=new baseclass(“world”);
14) }
15) }
which line would be cause the error?
a. 9b. 10c. 11d.12
13. which statement is correctly declare a variable a which is suitable for refering to an array of 50 string empty object?
a. string [] ab. string a[]c. char a[][]d. string a[50]f. object a[50]
14. give the following java source fragment:
//point x
public class interesting{
//do something
}
which statement is correctly java syntax at point x?
a. import java.awt.*;b.package mypackage;c. static int pi=3.14
d. public class myclass{//do other thing…}e. class myclass{//do something…}
15. give this class outline:
class example{
private int x;
//rest of class body…
}
assuming that x invoked by the code java example, which statement can made x be directly accessible in main() method of example.java?
a. change private int x to public int xb. change private int x to static int x
c. change private int x to protected int xd. change private int x to final int x
16. a class design requires that a member variable should be accessible only by same package, which modifier word should be used?
a. protectedb. publicc. no modifierd. private
17. which modifier should be applied to a declaration of a class member variable for the value of variable to remain constant after the creation of the object?
18. which is corrected argument of main() method of application?
a. string argsb. string ar[]c. char args[][]d. stringbuffer arg[]
19. “the employee object is a person, an employee has appointment store in a vector, a hire date and a number of dependent”
short answer: use shortest statement declare a class of employee.
20. give the following class definition inseparate source files:
public class example{
public example(){//do something}
protected example(int i){//do something}
protected void method(){//do something}
}
public class hello extends example{//member method and member variable}
which methods are corrected added to the class hello?
a. public void example(){}b. public void method(){}
c. protected void method(){}d. private void method(){}
21. float s=new float(0.9f);
float t=new float(0.9f);
double u=new double(0.9);
which expression’s result is true?
a. s= =tb. s.equals(t)c. s= =ud. t.equals(u)
22. give following class:
class aclass{
private long val;
public aclass(long v){val=v;}
public static void main(string args[]){
aclass x=new aclass(10l);
aclass y=new aclass(10l);
aclass z=y;
long a=10l;
int b=10;
}
}
which expression result is true?
a. a= =b;b. a= =x;c. y= =z;d. x= =y;
e. a= =10.0;
23. string s=”example string”;
which operation is legal?
a. s>;>;>;=3;b. int i=s.length();c. s[3]=“x”;
d. string short_s=s.trim();e. string t=“root”+s;
24. what happens when you try to compile and run the following program?
class mystery{
string s;
public static void main(string[] args){
mystery m=new mystery();
m.go();
}
void mystery(){
s=”constructor”;
}
void go(){
system.out.println(s);
}
}
a.this code will not compile
b.this code compile but throws an exception at runtime
c.this code runs but nothing appears in the standard output
d.this code runs and “constructor” in the standard output
e.this code runs and writes ”null” in the standard output
25. what use to position a button in a frame ,only width of button is affected by the frame size, which layout button well be set ?
a. flowlayout;b. gridlayout;c. north of borderlayout
d. south of borderlayoute. east or west of borderlayout
26. what use to position a button in a frame, size of button is not affected by the frame size, which layout button will be set?
a. flowlayout;b. gridlayout;c. north of borderlayout
d. south of borderlayoute. east or west of borderlayout
27. select valid identifier of java:
a. usernameb. %passwdc. 3d_gamed. $chargee. this
28. which are java keyword?
a. gotob. nullc. falsed. nativee. const
29. give the following java class:
public class example{
static int x[]=new int[15];
public static void main(string args[]){
system.out.println(x[5]);
}
}
which statement is corrected?
a.when compile, some error will occur.
b.when run, some error will occur.
c.output is zero.
d.output is null.
30. give the following java class:
public class example{
public static void main(string args[]){
static int x[] = new int[15];
system.out.println(x[5]);
}
}
which statement is corrected?
a.when compile, some error will occur.
b.when run, some error will occur.
c.output is zero.
d.output is null.
31. short answer:
the decimal value of i is 12, the octal i value is:
32. short answer:
the decimal value of i is 7, the hexadecimal i value is:
33. give the following class:
public class example{
string str=new string(“good”);
char ch[]={‘a’,’b’,’c’};
public static void main(string args[]){
example ex=new example();
ex.change(ex.str,ex.ch);
system.out.println(ex.str+”and”+ex.ch[0] +ex.ch[1] +ex.ch[2]);
}
public void change(string str,char ch[]){
str=”test ok”;ch[0]=’g’;
}
}
which is the output:
a. good and abcb. good and gbcc. test ok and abcd. test ok and gbc
34. which code fragments would correctly identify the number of arguments passed via command line to a java application, exclude the name of the class that is being invoke.
a. int count = args.length;b. int count = args.length-1;
c. int count=0; while(args[count]!=null)
count++;
d. int count=0;while
(!(args[count].equals(“”))) count++;
35. filteroutputstream is the parent class for bufferedoutputstream, dataoutputstream and printstream. which classes are valid argument for the constructor of a filteroutputstream?
a. inputstreamb. outputstreamc. file
d. randomaccessfile e. streamtokenizer
36. given this skeleton of a class currently under construction:
public class example{
int x,y,z;
public example (int a, int b) {
//lots of complex computation
x=a; y=b;
}
public example(int a, int b, int c){
// do everything the same as single argument
// version of constructor
// including assignment x=a, y=b, z=c
z=c;
}
}
what is the most concise way to code the “do everything…” part of the constructor taking two arguments?
short answer:
37. which correctly create a two dimensional array of integers?
a.int a[][] = new int[][];
b.int a[10][10] = new int[][];
c.int a[][] = new int[10][10];
d.int [][]a = new int[10][10];
e.int []a[] = new int[10][10];
38. which are correct class declarations? assume in each case that the text constitutes the entire contents of a file called fred.java?
a.public class fred{
public int x = 0;
public fred (int x){
this.x=x;
}
}
b.public class fred{
public int x = 0;
public fred (int x){
this.x=x;
}
}
c.public class fred extends mybaseclass, myotherbaseclass{
public int x = 0;
public fred(int xval){
x=xval;
}
}
d.protected class fred{
private int x = 0;
private fred (int xval){
x=xval;
}
}
e.import java.awt.*;
public class fred extends object{
int x;
private fred(int xval){
x = xval;
}
}
39. a class design requires that a particular member variable must be accessible for direct access by any subclasses of this class. but otherwise not by classes which are not members of the same package. what should be done to achieve this?
a.the variable should be marked public
b.the variable should be marked private
c.the variable should be marked protected
d.the variable should have no special access modifier
e.the variable should be marked private and an accessible method provided