`

java面试题目

 
阅读更多
最近在网上看到的一哥们总结的面试题,感觉对以后面试有点帮助,摘过来,方面以后找工作时候看下
面试java架构师的比较变态的题目
1.变态指数 4
     int x=4;
     System.out.println("value is " +((x>4)?99.9:9));
     答案: 9.0
问号表达式的后面两个条件有要求,因为前面的是float,所以后面转为float.
     估计出题者才通过SCJP的考试。
    
2.变态指数 5
     public class Test {
         public static void main(String[] args) {
    int x = 4;
   java.util.Date date = (x > 4) ? new A() : new B();
   }
     }
    
     class A extends java.util.Date {}
     class B extends java.util.Date {}
    
答案: 编译不通过,
     不知道出题人的意图
    
3.变态指数 6
     String s=new String("abc");
     创建了几个String对象?
     答案: 2个
     这样的公司最好不要去
    
     4.变态指数 7
     const是不是java的关键字?
     答案: const是java的关键字,但是java没有实现它
     一般人绝对用不到它
    
     5.变态指数 8
     short s1 = 1; s1 = s1 + 1;与 short s1 = 1; s1 += 1;那个错?
     答案: 1错2对
1因为向上转型了,最后导致类型不匹配错误 , 因为s1的+=是一个操作符,能够自动转型,
     short s1 = 1;
     s1 = s1+1;这句话在c++里面可以的
     不知道出题人的意图

6.变态指数 9
     上海贝尔的面试题:你认为效率最高的方法,实现从1加到100.
     答案 1-100的累加相当于加50次101,这样循环次数从100次降为50次:
     int sun = 0
     for(int i = 1,j = 100 ; i <= 50 ; i++,j--){
     sun = sun + i + j;
     }
     出题人脑子有问题,直接(1+100)*50不是最快...其实类似这样的优化应该不是程序员考虑的范畴吧
    
7.变态指数 10
     System.out.println(5.0942*1000);
     System.out.println(5.0943*1000);
     System.out.println(5.0944*1000);的结果
     答案 :5094.2 5094.299999999999 5094.400000000001
     原理和浮点数的计算机表示方式有关 ,你不用上机,就答对了,你最好去微软,接替安德尔森.



最近一次去华为面试的题目
一、jvm的在什么情况下效率最高
1,java堆。2,堆栈。3,java对象池
二、写一个静态方法,要求入参一个数组,出来的是数组的两倍。
三、几个类型的默认值,byte,boolean,float



java华为面试题

JAVA方面
1 面向对象的特征有哪些方面  
1.抽象:
抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。抽象包括两个方面,一是过程抽象,二是数据抽象。
2.继承:
继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承。新类继承了原始类的特性,新类称为原始类的派生类(子类),而原始类称为新类的基类(父类)。派生类可以从它的基类那里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。
3.封装:
封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。
4. 多态性:
多态性是指允许不同类的对象对同一消息作出响应。多态性包括参数化多态性和包含多态性。多态性语言具有灵活、抽象、行为共享、代码共享的优势,很好的解决了应用程序函数同名问题。



2 String是最基本的数据类型吗?
基本数据类型包括byte、int、char、long、float、double、boolean和short。
java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应该用StringBuffer类

3 int 和 Integer 有什么区别
Java 提供两种不同的类型:引用类型和原始类型(或内置类型)。Int是java的原始数据类型,Integer是java为int提供的封装类。Java为每个原始类型提供了封装类。
原始类型封装类
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
引用类型和原始类型的行为完全不同,并且它们具有不同的语义。引用类型和原始类型具有不同的特征和用法,它们包括:大小和速度问题,这种类型以哪种类型的数据结构存储,当引用类型和原始类型用作某个类的实例数据时所指定的缺省值。对象引用实例变量的缺省值为 null,而原始类型实例变量的缺省值与它们的类型有关。


4 String 和StringBuffer的区别
String 对象不能改变,StringBuffer对象可以改变。O:
String对象称为immutable string,而StringBuffer为mutable string。


5 运行时异常与一般异常有何异同?
异常表示程序运行过程中可能出现的非正常状态,运行时异常表示虚拟机的通常操作中可能遇到的异常,是一种常见运行错误。java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常。

6 说出一些常用的类,包,接口,请各举5个

7 说出ArrayList,Vector, LinkedList的存储性能和特性
ArrayList 和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插入数据慢,Vector由于使用了synchronized方法(线程安全),通常性能上较ArrayList差,而LinkedList使用双向链表实现存储,按序号索引数据需要进行前向或后向遍历,但是插入数据时只需要记录本项的前后项即可,所以插入速度较快。
8设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。
以下程序使用内部类实现线程,对j增减的时候没有考虑顺序问题。
public class ThreadTest1{
    private int j;
    public static void main(String args[]){
        ThreadTest1 tt=new ThreadTest1();
        Inc inc=tt.new Inc();
        Dec dec=tt.new Dec();
        for(int i=0;i<2;i++){
            Thread t=new Thread(inc);
            t.start();
            t=new Thread(dec);
            t.start();
        }
    }
    private synchronized void inc(){
        j++;
        System.out.println(Thread.currentThread().getName()+"-inc:"+j);
    }
    private synchronized void dec(){
        j--;
        System.out.println(Thread.currentThread().getName()+"-dec:"+j);
    }
    
    class Inc implements Runnable{
        public void run(){
            for(int i=0;i<100;i++){
                inc();
            }
        }
    }
    class Dec implements Runnable{
        public void run(){
            for(int i=0;i<100;i++){
                dec();
            }
        }
    }
}

9.JSP的内置对象及方法。
request request表示HttpServletRequest对象。它包含了有关浏览器请求的信息,并且提供了几个用于获取cookie, header, 和session数据的有用的方法。 

response response表示HttpServletResponse对象,并提供了几个用于设置送回 浏览器的响应的方法(如cookies,头信息等) 

out out 对象是javax.jsp.JspWriter的一个实例,并提供了几个方法使你能用于向浏览器回送输出结果。 

pageContext pageContext表示一个javax.servlet.jsp.PageContext对象。它是用于方便存取各种范围的名字空间、servlet相关的对象的API,并且包装了通用的servlet相关功能的方法。 

session session表示一个请求的javax.servlet.http.HttpSession对象。Session可以存贮用户的状态信息 

application applicaton 表示一个javax.servle.ServletContext对象。这有助于查找有关servlet引擎和servlet环境的信息 

config config表示一个javax.servlet.ServletConfig对象。该对象用于存取servlet实例的初始化参数。 

page page表示从该页面产生的一个servlet实例
10.用socket通讯写出客户端和服务器端的通讯,要求客户发送数据后能够回显相同的数据。
参见课程中socket通讯例子。

11说出Servlet的生命周期,并说出Servlet和CGI的区别。
Servlet被服务器实例化后,容器运行其init方法,请求到达时运行其service方法,service方法自动派遣运行与请求对应的doXXX方法(doGet,doPost)等,当服务器决定将实例销毁的时候调用其destroy方法。
与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service方法,一个实例可以服务于多个请求,并且其实例一般不会销毁,而CGI对每个请求都产生新的进程,服务完成后就销毁,所以效率上低于servlet。
12.EJB是基于哪些技术实现的?并说出SessionBean和EntityBean的区别,StatefulBean和StatelessBean的区别。

13.EJB包括(SessionBean,EntityBean)说出他们的生命周期,及如何管理事务的?

14.说出数据连接池的工作机制是什么?

15.同步和异步有和异同,在什么情况下分别使用他们?举例说明。

16.应用服务器有那些?

17你所知道的集合类都有哪些?主要方法?

18给你一个:驱动程序A,数据源名称为B,用户名称为C,密码为D,数据库表为T,请用JDBC检索出表T的所有数据。

19.说出在JSP页面里是怎么分页的?
页面需要保存以下参数:
总行数:根据sql语句得到总行数
每页显示行数:设定值
当前页数:请求参数
页面根据当前页数和每页行数计算出当前页第一行行数,定位结果集到此行,对结果集取出每页显示行数的行即可。


数据库方面:

1.存储过程和函数的区别
存储过程是用户定义的一系列sql语句的集合,涉及特定表或其它对象的任务,用户可以调用存储过程,而函数通常是数据库已定义的方法,它接收参数并返回某种类型的值并且不涉及特定用户表。
2.事务是什么?
事务是作为一个逻辑单元执行的一系列操作,一个逻辑工作单元必须有四个属性,称为 ACID(原子性、一致性、隔离性和持久性)属性,只有这样才能成为一个事务:
原子性
事务必须是原子工作单元;对于其数据修改,要么全都执行,要么全都不执行。
一致性
事务在完成时,必须使所有的数据都保持一致状态。在相关数据库中,所有规则都必须应用于事务的修改,以保持所有数据的完整性。事务结束时,所有的内部数据结构(如 B 树索引或双向链表)都必须是正确的。
隔离性
由并发事务所作的修改必须与任何其它并发事务所作的修改隔离。事务查看数据时数据所处的状态,要么是另一并发事务修改它之前的状态,要么是另一事务修改它之后的状态,事务不会查看中间状态的数据。这称为可串行性,因为它能够重新装载起始数据,并且重播一系列事务,以使数据结束时的状态与原始事务执行的状态相同。
持久性
事务完成之后,它对于系统的影响是永久性的。该修改即使出现系统故障也将一直保持。

3.游标的作用?如何知道游标已经到了最后?
游标用于定位结果集的行,通过判断全局变量@@FETCH_STATUS可以判断是否到了最后,通常此变量不等于0表示出错或到了最后。
4.触发器分为事前触发和事后触发,这两种触发有和区别。语句级触发和行级触发有何区别。
事前触发器运行于触发事件发生之前,而事后触发器运行于触发事件发生之后。通常事前触发器可以获取事件之前和新的字段值。
语句级触发器可以在语句执行前或后执行,而行级触发在触发器所影响的每一行触发一次。

常见面试题

1.    Which statement about the garbage collection mechanism are true? 
A. Garbage collection require additional programe code in cases where multiple threads are running. 
B. The programmer can indicate that a reference through a local variable is no longer of interest. 
C. The programmer has a mechanism that explicity and immediately frees the memory used by Java objects. 
D. The garbage collection mechanism can free the memory used by Java Object at explection time. 
E. The garbage collection system never reclaims memory from objects while are still accessible to running user threads. 

2. Give the following method: 
1) public void method( ){ 
2) String a,b; 
3) a=new String(“hello world”); 
4) b=new String(“game over”); 
5) System.out.println(a+b+”ok”); 
6) a=null; 
7) a=b; 
8) System.out.println(a); 
9) } 
In the absence of compiler optimization, which is the earliest point the object a refered is definitely elibile to be garbage collection. 
A. before line 3 
B.before line 5 
C. before line 6 
D.before line 7 
E. Before line 9 

3. In the class java.awt.AWTEvent,which is the parent class upon which jdk1.1 awt events are based there is a method called getID which phrase accurately describes the return value of this method? 
A. It is a reference to the object directly affected by the cause of the event. 
B. It is an indication of the nature of the cause of the event. 
C. It is an indication of the position of the mouse when it caused the event. 
D. In the case of a mouse click, it is an indication of the text under the mouse at the time of the event. 
E. It tells the state of certain keys on the keybord at the time of the event. 
F. It is an indication of the time at which the event occurred. 

4. Which statement about listener is true? 
A. Most component allow multiple listeners to be added. 
B. If multiple listener be add to a single component, the event only affected one listener. 
C. Component don?t allow multiple listeners to be add. 
D. The listener mechanism allows you to call an addXxxxListener method as many times as is needed, specifying as many different listeners as your design require. 

5.Give the following code: 
public class Example{ 
public static void main(String args[] ){ 
int l=0; 
do{ 
System.out.println(“Doing it for l is:”+l); 
}while(--l>0) 
System.out.println(“Finish”); 


Which well be output: 
A. Doing it for l is 3 
B. Doing it for l is 1 
C. Doing it for l is 2 
D. Doing it for l is 0 
E. Doing it for l is ?C1 
F. Finish 

6. Give the code fragment: 
1) switch(x){ 
2) case 1:System.out.println(“Test 1”);break; 
3) case 2: 
4) case 3:System.out.println(“Test 2”);break; 
5) default:System.out.println(“end”); 
6) } 
which value of x would cause “Test 2” to the output: 
A. 1 
B. 2 
C. 3 
D. default 

7. Give incompleted method: 
1) 
2) { if(unsafe()){//do something…} 
3) else if(safe()){//do the other…} 
4) } 
The method unsafe() well throe an IOException, which completes the method of declaration when added at line one? 
A. public IOException methodName() 
B. public void methodName() 
C. public void methodName() throw IOException 
D. public void methodName() throws IOException 
E. public void methodName() throws Exception 

8. Give the code fragment: 
if(x>4){ 
System.out.println(“Test 1”);} 
else if (x>9){ 
System.out.println(“Test 2”);} 
else { 
System.out.println(“Test 3”);} 
Which range of value x would produce of output “Test 2”? 
A. x<4 
B. x>4 
C. x>9 
D. None 

9. Give the following method: 
public void example(){ 
try{ 
unsafe(); 
System.out.println(“Test1”); 
}catch(SafeException e){System.out.println(“Test 2”); 
}finally{System.out.println(“Test 3”);} 
System.out.println(“Test 4”); 
Which will display if method unsafe () run normally? 
A. Test 1 
B. Test 2 
C. Test 3 
D. Test 4 

10. Which method you define as the starting point of new thread in a class from which new the thread can be excution? 
A. public void start() 
B. public void run() 
C. public void int() 
D. public static void main(String args[]) 
E. public void runnable() 

11.Given the following class definition: 
class A{ 
protected int i; 
A(int i){ 
this.i=i; 


which of the following would be a valid inner class for this class? 
Select all valid answers: 
A. class B{ 

B. class B extends A{ 

C. class B extends A{ 
B(){System.out.println(“i=”+i);} 

D. class B{ 
class A{} 

E. class A{} 

12. Which modifier should be applied to a method for the lock of object this to be obtained prior to excution any of the method body? 
A. synchronized 
B. abstract 
C. final 
D. static 
E. public 

13. 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){ 
8) str=s;}} 
9) public class Example{ 
10) public void method(){ 
11) SubClass s=new SubClass(“hello”); 
12) BaseClass b=new BaseClass(“world”); 
13) } 
14) } 

Which line would be cause the error? 
A. 9 B. 10 C. 11 D.12 

14. Which statement is correctly declare a variable a which is suitable for refering to an array of 50 string empty object? 
A. String [] a 
B. String a[] 
C. char a[][] 
D. String a[50] 
F. Object a[50] 

15. Give the following java source fragement: 
//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…} 

16. 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 x 
B. change private int x to static int x 
C. Change private int x to protected int x 
D. change private int x to final int x 

17. the piece of preliminary analsis work describes a class that will be used frequently in many unrelated parts of a project 
“The polygon object is a drawable, A polygon has vertex information stored in a vector, a color, length and width.” 
Which Data type would be used? 
A. Vector 
B. int 
C. String 
D. Color 
E. Date 

18. A class design requires that a member variable should be accessible only by same package, which modifer word should be used? 
A. protected 
B. public 
C. no modifer 
D. private 

19.Which declares for native method in a java class corrected? 
A. public native void method(){} 
B. public native void method(); 
C. public native method(); 
D. public void method(){native;} 
E. public void native method(); 

20. Which modifer 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? 

21. Which is the main() method return of a application? 
A. String 
B. byte 
C. char 
D. void 

22. Which is corrected argument of main() method of application? 
A. String args 
B. String ar[] 
C. Char args[][] 
D. StringBuffer arg[] 

23. “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. 

24. Give the following class defination 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(){} 

25. 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==t 
B. s.equals(t) 
C. s==u 
D. t.equals(u) 

26. 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; 

27. A socket object has been created and connected to a standard internet service on a remote network server. Which construction give the most suitable means for reading ASCII data online at a time from the socket? 
A. InputStream in=s.getInputStream(); 
B. DataInputStream in=new DataInputstream(s.getInputStream()); 
C. ByteArrayInputStream in=new ByteArrayInputStream(s.getInputStream()); 
D. BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream())); 
E. BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()),”8859-1”); 

28. 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; 

29. 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 compliles 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 

30. 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 BorderLayout 
E. East or West of BorderLayout 

31. 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 BorderLayout 
E. East or West of BorderLayout 

32. An AWT GUI under exposure condition, which one or more method well be invoke when it redraw? 
A. paint(); 
B. update(); 
C. repaint(); 
D. drawing(); 

33. Select valid identifier of Java: 
A. userName 
B. %passwd 
C. 3d_game 
D. $charge E. this 

34. Which are Java keyword? 
A. goto 
B. null 
C. FALSE 
D. native 
E. const 

35. Run a corrected class: java ?Ccs AClass a b c <enter> 
Which statement is true? 
A. args[0]=”-cs”; 
B. args[1]=”a b c”; 
C. args[0]=”java”; 
D. args[0]=”a”; E. args[1]=?b? 

36. 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. 

37. 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. 

38. Short answer: 
The decimal value of i is 12, the octal i value is: 

39. Short answer: 
The decimal value of i is 7, the hexadecimal i value is: 

40. Which is the range of char? 
A. 27~27-1 
B. 0~216-1 
C. 0~216 
D. 0~28 

41. Which is the range of int type? 
A. -216~216-1 
B.- 231~231-1 
C. -232~232-1 
D. -264~264-1 

42. Give the following class: 
public class Example{ 
String str=new String(“good”); 
char ch[]={ 
public static void main(String args[]){ 
Example ex=new Example(); 
ex.change(ex.str,ex.ch); 
System.out.println(ex.str+”and”+ex.ch); 

public void change(String str,char ch[]){ 
str=”test ok”;ch[0]=?g? 


Which is the output: 
A. good and abc 
B. good and gbc 
C. test ok and abc 
D. test ok and gbc 

43. 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++; 

44. FilterOutputStream is the parent class for BufferedOutputStream, DataOutputStream and PrintStream. Which classes are valid argument for the constructor of a FilterOutputStream? 
A. InputStream 
B. OutputStream 
C. File 
D. RandomAccessFile 
E. StreamTokenizer 

45. Given a TextArea using a proportional pitch font and constructed like this: 
TextArea t=new TextArea(“12345”,5,5); 
Which statement is true? 
A. The displayed width shows exactly five characters one each line unless otherwise constrained 
B. The displayed height is five lines unless otherwise constrained 
C. The maximum number of characters in a line will be five 
D. The user will be able to edit the character string 
E. The displayed string can use multiple fonts 

46. Given a List using a proportional pitch font and constructed like this: 
List l=new List(5,true); 
Which statement is true? 
A. The displayed item exactly five lines unless otherwise constrained 
B. The displayed item is five lines init, but can displayed more than five Item by scroll 
C. The maximum number of item in a list will be five. 
D. The list is multiple mode 

47. 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: 

48. 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]; 

49. 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; 



50. 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 accessor method provided 

51. Which correctly create an array of five empty Strings? 
A. String a[] = new String[5]; 
for (int i=0;i<5;a[i++]=””); 
B. String a []={“”,””,””,””,””}; 
C. String a[5]; 
D. String [5] a; 
E. String [] a = new String[5]; 
for (int i = 0 ;i<5;a[i++] = null); 

52. Which cannot be added to a Container? 
A. an Applet 
B. a Component 
C. a Container 
D. a MenuComponent 
E. a panel 

53. Which is the return value of Event listener?s method? 
A. String B. AWTEvent C. void D. int 

54. If we implements MouseListener, which is corrected argument of its method? (short answer) 

55. Use the operator “>>” and “>>>”. Which statement is true? 
A. 1010 0000 0000 0000 0000 0000 0000 0000 >> 4 give 
0000 1010 0000 0000 0000 0000 0000 0000 
B. 1010 0000 0000 0000 0000 0000 0000 0000 >> 4 give 
1111 1010 0000 0000 0000 0000 0000 0000 
C. 1010 0000 0000 0000 0000 0000 0000 0000 >>> 4 give 
0000 1010 0000 0000 0000 0000 0000 0000 
D. 1010 0000 0000 0000 0000 0000 0000 0000 >>> 4 give 
1111 1010 0000 0000 0000 0000 0000 0000 

56. Give following fragment. 
Outer: for(int i=0; i<3; i++) 
inner:for(int j=0;j<3;j++){ 
If(j>1)break outer; 
System.out.println(j+”and”+i); 

Which will be output? 
A. 0 and 0 B. 0 and 1 C. 0 and 2 D. 0 and 3 
E. 1 and 0 F. 1 and 1 G. 1 and 2 H. 1 and 3 
I. 2 and 0 J. 2 and 1 K. 2 and 2 L. 2 and 3 

57. Examine the following code which includes an inner class: 
public final class Test4 implements A{ 
class Inner{ 
void test(){ 
if (Test4.this.flag);{ 
sample(); 


private boolean flag=false; 

public void sample(){ 
System.out.println(“Sample”); 

public Test4(){ 
(new Inner()).test(); 

public static void main(String args[]){ 
new Test4(); 


What is the result: 
A.Print out “Sample” 
B.Program produces no output but termiantes correctly. 
C. Program does not terminate. 
D.The program will not compile 

58. What is written to the standard output given the following statement: 
System.out.println(4|7); 
Select the right answer: 
A.4 
B.5 
C.6 
D.7 
E.0 

59. Look the inheritance relation: 
person 

---------------- 
| | 
man woman 
In a source of java have the following line: 
person p=new man(); 
What statement are corrected? 
A. The expression is illegal. 
B. Compile corrected but running wrong. 
C. The expression is legal. 
D. Will construct a person?s object. 

60. Look the inheritance relation: 
person 

---------------- 
| | 
man woman 
In a source of java have the following line: 
woman w=new man(): 

What statement are corrected? 
A. The expression is illegal. 
B. Compile corrected but running wrong. 
C. The expression is legal. 
D. Will construct a woman object. 

61. Which can NOT be used in declaring or declaring and initializing an automatic (method local) variable? 
A. final 
B. static 
C. expressions 
D. Constants of non-primitive type 
E. initialized arrays (such as “ {“Hello”,”Good bye”}”). 

62. Given the following incomplete method: 
1) public void method(){ 
2) 
3) if (someTestFails()){ 
4) 
5) } 
6) 
7) } 
You want to make this method throw an IOException if,and only if,the method someTestFails() returns a value of true. 
Which changes achieve this? 
A. Add at line 2:IOException e; 
B. Add at line 4:throw e; 
C. Add at line 4:throw new IOException(); 
D. Add at line 6:throw new IOException(); 
E. Modify the method declaration to indicate that an object of type Exception might be thrown. 

63. Given the following definition: 
String s = null; 
Which code fragments cause an object of type NullPointerException to be thrown? 
A. if((s!=null)&(s.length()>0)) 
B. if((s!=null)&&(s.length()>0)) 
C. if((s==null)|(s.length()==0)) 
D. if((s==null)||(s.length()==0)) 

64. The following is a program 
1) class Exsuper{ 
2) String name; 
3) String nick_name; 
4) 
5) public ExSuper(String s,String t){ 
6) name = s; 
7) nick_name = t; 
8) } 
9) 
10) public string toString(){ 
11) return name; 
12) } 
13) } 
14) 
15) public class Example extends ExSuper{ 
16) 
17) public Example(String s,String t){ 
18) super(s,t); 
19) } 
20) 
21) public String to String(){ 
22) return name +”a.k.a”+nick_name; 
23) } 
24) 
25) public static void main(String args[]){ 
26) ExSuper a = new ExSuper(“First”,”1st”); 
27) ExSuper b = new Example(“Second”,”2nd”); 
28) 
29) System.out.println(“a is”+a.toString()); 
30) System.out.println(“b is”+b.toString()); 
31) } 
32) } 
What happens when the user attempts to compile and run this program? 
` A. A Compiler error occurs at line 21 
B. An object of type ClassCastException is thrown at line 27 
C.The following output: 
a is First 
b is second 
D. The following output: 
a is First 
b is Secong a.k.a 2nd 
F. The following output: 
a is First a.k.a 1st 
b is Second a.k.a 2nd 

65. Which statements are true about Listeners? 
A. At most one listener can be added to any simple Component. 
B. The return value from a listener is used to control the invocation of other listener 
C. If multiple listeners are added to a single component, they must all be made friends to each other 
D. If multiple listeners are added to single component, the order of invocation of the listener is not specified. 
E. In the AWT, listener methods generally take an argument, which is an instance of some subclass of java.awt.AWTEvent class. 

66. Given the following class outline: 
class Example{ 
private int x; 
// rest of class body 
public static void main(String args[]){ 
//implementation of main mehtod} 

Which statement is true? 
A. x=2 is a valid assignment in the main() method of class Example. 
B. Changing private int x to int x would make x=2 a valid assignment in the main() method of class Example. 
C. Changing private int x to public int x would make x=2 a valid assignment in the main() method of class Example. 
D. Changing private int x to static int x would make x=2 a valid assignment in the main() method of class Example. 
E. Changing class Example to public class Example would make x=2 a valid assignment in the main() method of class Example. 

67. Which statement is true about an inner class? 
A. It must be anonymous 
B. It can not implement an interface 
C. It is only accessible in the enclosing class 
D. It can access any final variables in any enclosing scope. 

68. Which statement is true about the grid bag layout manager? 
A. The number of rows and columns is fixed when the container is created. 
B. The number of rows and columns is fixed when the GridBagLayout object is created. 
C. If a component has a fill value of BOTH, then as the container change size, the component is resized. 
D. Every component must carry a non-zero weightx and weighty value when it is added to the container 
E. If a row has a weighty value that is non-zero, then as the container changes height, the row changes height. 

69. Which statement are true about writing a class that is to handle the events issued by a user interface component. 
A. Subclassing an adapter is inappropriate in this case. 
B. The class should implement some listener interface 
C. A class can implement multiple listener interfaces if desired. 
D. A subclass of an AWT component cannot listen to its own events. 
E. When implements listener interface, a class need only provide those handler methods that it chooses. 

70.The argument for a class?s main() method is called args, and the class is invoked as follows. 
java Example cat 
What would be the effect of trying to access args[0] in the main method? 
A. The value produced is cat 
B. The value produced is java 
C. The value produced is Example 
D. An object of type NullPointerException is thrown. 
E. An object of type ArrayIndexOutofBoundsException is thrown. 

71. Which best describes the requirements of a fully encapsulated class? 
A. Mehtods must not be private. 
B. Variables must not be public. 
C. The class must be marked final 
D. Public methods are all marked final. 
E. Modification of the objects state is only possible using method calls. 

72.Which contains objects without ordering, duplication, or any particular lookup/retrieval mechanism? 
A. Map 
B. Set 
C. List 
D. Collection 
E. Enumeration 

73.What might cause the current thread to stop executing? 
A. An interrupted exception is thrown. 
B. The thread execute a sleep() call. 
C. The thread constructs a new thread 
D. A thread of higher priority becomes ready 
E. The thread executes a read() call on InputStream 

74.Which statements are true about threads? 
A. Threads created from the same class all finish together. 
B. A thread can be created only by subclassing java.lang.Thread. 
C. Invoking the suspend() method stops a thread so that it cannot be restar
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics