设为首页 加入收藏

TOP

What is the result when you compile and run the following code?(一)
2014-11-16 18:20:03 来源: 作者: 【 】 浏览:39
Tags:What the result when you compile and run following code

public class ThrowsDemo


{


static void throwMethod()


{


System.out.println(“Inside throwMethod.”);


throw new IllegalAccessException(“demo”);


}


public static void main(String args[])


{


try


{


throwMethod();


}


catch (IllegalAccessException e)


{


System.out.println(“Caught ” + e);


}


}


}


Choices:


a. Compilation error


b. Runtime error


c. Compile successfully, nothing is printed.


d. Inside throwMethod. followed by caught:


java.lang.IllegalAccessExcption: demo


Answer:______


**———————————————————————————————–**


2.What will happen when you attempt to run the following code from command line using command


– java AssertTest (Assume that the code is compiled with assertions enabled)


1. public class AssertTest


2. {


3. static int i = 10;


4. public static void main(String args[])


5. {


6. i = i*2;


7. try


8. {


9. assert isValid() : i = i/4;


10. } catch(AssertionError ignore){}


11.


12. System.out.println(“i = ” +i);


13. }


14.


15. public static boolean isValid()


16. {


17. i = i * 2;


18. return false;


19. }


20. }


A. It will print – i = 20


B. It will print – i = 10


C. It will print – i = 40


D. It will print – i = 5


E. None of these


Answer:______


**———————————————————————————————–**


3.What will happen when you attempt to compile and run the following code


public class Static


{


static


{


int x = 5;


}


static int x,y;


public static void main(String args[])


{


x–;


myMethod();


System.out.println(x + y + ++x);


}


public static void myMethod()


{


y = x++ + ++x;


}


}


Choices:


a. Compiletime error


b. prints : 1


c. prints : 2


d. prints : 3


e. prints : 7


f. prints : 8


Answer:______


**———————————————————————————————–**


4.You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability


A. java.util.Map


B. java.util.Set


C. java.util.List


D. java.util.StoredSet


E. java.util.StoredMap


F. java.util.Collection


Answer:______


**———————————————————————————————–**


5.What should be inserted at line 7 so that the code compiles and guarantees to print – Apple Cricket Banana in the same order.


1. import java.util.*;


2. public class SetTest4. {


5. public static void main(String args[]{


// what should be inserted here


set.add(“Apple”);10.


set.add(“Cricket”);


set.add(“Banana”);


Iterator iter = set.iterator();


while(iter.hasNext()){


System.out.print(iter.next() + ” “); }


}


}


A. Set set = new LinkedHashSet();


B. Set set = new HashSet();


C. Set set = new TreeSet();


D. TreeSet maintains the Set elements sorted according to their natural order; however other sets do not guarantee iteration order of the set. In particular, they do not guarantee that the order will remain constant over time.


Answer:______


**———————————————————————————————–**


6.Which Java collection class can be used to maintain the entries in the order in which they were last accessed


A. java.util.HashSet


B. java.util.LinkedHashMap


C. java.util.Hashtable


D. java.util.VectorE. None of these


Answer:______


**———————————————————————————————–**


7. import java.util.*;


public class Pippin {


public static void main(String argv[]){


TreeMap tm = new TreeMap();


tm.put(“one”, new Integer(1));


tm.put(“two”,new Integer(3));


tm.put(“three”,new Integer(2));


Iterator it = tm.keySet().iterator();


while(it.hasNext()){


Integer iw = tm.get(it.next());


System.out.print(iw);


}


}


}


A Compile time error at line 10.


B Compilation and output of 123


C Compilation and output of the digits “1″, “2″ and “3″, but the order can’t be determined.


D Compilation and output of onetwothree

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇请问在Asp.net中xml文件与xsl文件.. 下一篇常见hibernate面试题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: